Commit 164e56cd8f150c96ab633eaf166cf0851077d5c6

Authored by Dancovich
1 parent 0e745c2b
Exists in master

Criados testes de validação de credenciais

impl/core/src/test/java/security/athentication/credentials/AcceptOrDenyCredentialsTest.java 0 → 100644
... ... @@ -0,0 +1,111 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package security.athentication.credentials;
  38 +
  39 +import javax.enterprise.context.RequestScoped;
  40 +import javax.inject.Inject;
  41 +
  42 +import junit.framework.Assert;
  43 +
  44 +import org.jboss.arquillian.container.test.api.Deployment;
  45 +import org.jboss.arquillian.junit.Arquillian;
  46 +import org.jboss.shrinkwrap.api.spec.JavaArchive;
  47 +import org.junit.Test;
  48 +import org.junit.runner.RunWith;
  49 +
  50 +import test.Tests;
  51 +import br.gov.frameworkdemoiselle.internal.context.ContextManager;
  52 +import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext;
  53 +import br.gov.frameworkdemoiselle.security.AuthenticationException;
  54 +import br.gov.frameworkdemoiselle.security.SecurityContext;
  55 +import br.gov.frameworkdemoiselle.util.Beans;
  56 +import configuration.resource.ConfigurationResourceTest;
  57 +
  58 +@RunWith(Arquillian.class)
  59 +public class AcceptOrDenyCredentialsTest {
  60 +
  61 + @Inject
  62 + private SecurityContext context;
  63 +
  64 + @Deployment
  65 + public static JavaArchive createDeployment() {
  66 + JavaArchive deployment = Tests.createDeployment(ConfigurationResourceTest.class);
  67 + deployment.addClass(StrictAuthenticator.class);
  68 + deployment.addClass(Credentials.class);
  69 + return deployment;
  70 + }
  71 +
  72 + @Test
  73 + public void denyWrongCredentials() {
  74 + ContextManager.activate(ThreadLocalContext.class, RequestScoped.class);
  75 +
  76 + Credentials credentials = Beans.getReference(Credentials.class);
  77 + credentials.setLogin("wronglogin");
  78 +
  79 + try{
  80 + context.login();
  81 + Assert.fail("Authenticator aceitou credenciais erradas");
  82 + }
  83 + catch(AuthenticationException ae){
  84 + //Erro esperado
  85 + }
  86 + finally{
  87 + ContextManager.deactivate(ThreadLocalContext.class, RequestScoped.class);
  88 + }
  89 +
  90 + }
  91 +
  92 + @Test
  93 + public void acceptRightCredentials() {
  94 + ContextManager.activate(ThreadLocalContext.class, RequestScoped.class);
  95 +
  96 + Credentials credentials = Beans.getReference(Credentials.class);
  97 + credentials.setLogin("demoiselle");
  98 +
  99 + try{
  100 + context.login();
  101 + }
  102 + catch(AuthenticationException ae){
  103 + Assert.fail("Authenticator negou credenciais corretas");
  104 + }
  105 + finally{
  106 + ContextManager.deactivate(ThreadLocalContext.class, RequestScoped.class);
  107 + }
  108 +
  109 + }
  110 +
  111 +}
... ...
impl/core/src/test/java/security/athentication/credentials/Credentials.java 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +package security.athentication.credentials;
  2 +
  3 +import java.io.Serializable;
  4 +
  5 +import javax.enterprise.context.RequestScoped;
  6 +
  7 +@RequestScoped
  8 +public class Credentials implements Serializable {
  9 +
  10 + private static final long serialVersionUID = 1L;
  11 + private String login;
  12 +
  13 +
  14 + public String getLogin() {
  15 + return login;
  16 + }
  17 +
  18 +
  19 + public void setLogin(String login) {
  20 + this.login = login;
  21 + }
  22 +
  23 +
  24 +
  25 +}
... ...
impl/core/src/test/java/security/athentication/credentials/StrictAuthenticator.java 0 → 100644
... ... @@ -0,0 +1,77 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package security.athentication.credentials;
  38 +
  39 +import java.security.Principal;
  40 +
  41 +import br.gov.frameworkdemoiselle.security.AuthenticationException;
  42 +import br.gov.frameworkdemoiselle.security.Authenticator;
  43 +import br.gov.frameworkdemoiselle.util.Beans;
  44 +
  45 +public class StrictAuthenticator implements Authenticator {
  46 +
  47 + private static final long serialVersionUID = 1L;
  48 +
  49 + private Principal currentUser;
  50 +
  51 + @Override
  52 + public void authenticate() throws AuthenticationException {
  53 +
  54 + Credentials c = Beans.getReference(Credentials.class);
  55 + if ("demoiselle".equals(c.getLogin())){
  56 + this.currentUser = new Principal() {
  57 +
  58 + public String getName() {
  59 + return "demoiselle";
  60 + }
  61 + };
  62 + }
  63 + else{
  64 + throw new AuthenticationException("As credenciais fornecidas não são válidas");
  65 + }
  66 + }
  67 +
  68 + @Override
  69 + public void unAuthenticate() {
  70 + this.currentUser = null;
  71 + }
  72 +
  73 + @Override
  74 + public Principal getUser() {
  75 + return this.currentUser;
  76 + }
  77 +}
... ...