From 164e56cd8f150c96ab633eaf166cf0851077d5c6 Mon Sep 17 00:00:00 2001 From: Dancovich Date: Fri, 9 Aug 2013 15:05:19 -0300 Subject: [PATCH] Criados testes de validação de credenciais --- impl/core/src/test/java/security/athentication/credentials/AcceptOrDenyCredentialsTest.java | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/security/athentication/credentials/Credentials.java | 25 +++++++++++++++++++++++++ impl/core/src/test/java/security/athentication/credentials/StrictAuthenticator.java | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 213 insertions(+), 0 deletions(-) create mode 100644 impl/core/src/test/java/security/athentication/credentials/AcceptOrDenyCredentialsTest.java create mode 100644 impl/core/src/test/java/security/athentication/credentials/Credentials.java create mode 100644 impl/core/src/test/java/security/athentication/credentials/StrictAuthenticator.java diff --git a/impl/core/src/test/java/security/athentication/credentials/AcceptOrDenyCredentialsTest.java b/impl/core/src/test/java/security/athentication/credentials/AcceptOrDenyCredentialsTest.java new file mode 100644 index 0000000..924cfec --- /dev/null +++ b/impl/core/src/test/java/security/athentication/credentials/AcceptOrDenyCredentialsTest.java @@ -0,0 +1,111 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package security.athentication.credentials; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; + +import junit.framework.Assert; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import test.Tests; +import br.gov.frameworkdemoiselle.internal.context.ContextManager; +import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; +import br.gov.frameworkdemoiselle.security.AuthenticationException; +import br.gov.frameworkdemoiselle.security.SecurityContext; +import br.gov.frameworkdemoiselle.util.Beans; +import configuration.resource.ConfigurationResourceTest; + +@RunWith(Arquillian.class) +public class AcceptOrDenyCredentialsTest { + + @Inject + private SecurityContext context; + + @Deployment + public static JavaArchive createDeployment() { + JavaArchive deployment = Tests.createDeployment(ConfigurationResourceTest.class); + deployment.addClass(StrictAuthenticator.class); + deployment.addClass(Credentials.class); + return deployment; + } + + @Test + public void denyWrongCredentials() { + ContextManager.activate(ThreadLocalContext.class, RequestScoped.class); + + Credentials credentials = Beans.getReference(Credentials.class); + credentials.setLogin("wronglogin"); + + try{ + context.login(); + Assert.fail("Authenticator aceitou credenciais erradas"); + } + catch(AuthenticationException ae){ + //Erro esperado + } + finally{ + ContextManager.deactivate(ThreadLocalContext.class, RequestScoped.class); + } + + } + + @Test + public void acceptRightCredentials() { + ContextManager.activate(ThreadLocalContext.class, RequestScoped.class); + + Credentials credentials = Beans.getReference(Credentials.class); + credentials.setLogin("demoiselle"); + + try{ + context.login(); + } + catch(AuthenticationException ae){ + Assert.fail("Authenticator negou credenciais corretas"); + } + finally{ + ContextManager.deactivate(ThreadLocalContext.class, RequestScoped.class); + } + + } + +} diff --git a/impl/core/src/test/java/security/athentication/credentials/Credentials.java b/impl/core/src/test/java/security/athentication/credentials/Credentials.java new file mode 100644 index 0000000..264cbfc --- /dev/null +++ b/impl/core/src/test/java/security/athentication/credentials/Credentials.java @@ -0,0 +1,25 @@ +package security.athentication.credentials; + +import java.io.Serializable; + +import javax.enterprise.context.RequestScoped; + +@RequestScoped +public class Credentials implements Serializable { + + private static final long serialVersionUID = 1L; + private String login; + + + public String getLogin() { + return login; + } + + + public void setLogin(String login) { + this.login = login; + } + + + +} diff --git a/impl/core/src/test/java/security/athentication/credentials/StrictAuthenticator.java b/impl/core/src/test/java/security/athentication/credentials/StrictAuthenticator.java new file mode 100644 index 0000000..d91d20f --- /dev/null +++ b/impl/core/src/test/java/security/athentication/credentials/StrictAuthenticator.java @@ -0,0 +1,77 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package security.athentication.credentials; + +import java.security.Principal; + +import br.gov.frameworkdemoiselle.security.AuthenticationException; +import br.gov.frameworkdemoiselle.security.Authenticator; +import br.gov.frameworkdemoiselle.util.Beans; + +public class StrictAuthenticator implements Authenticator { + + private static final long serialVersionUID = 1L; + + private Principal currentUser; + + @Override + public void authenticate() throws AuthenticationException { + + Credentials c = Beans.getReference(Credentials.class); + if ("demoiselle".equals(c.getLogin())){ + this.currentUser = new Principal() { + + public String getName() { + return "demoiselle"; + } + }; + } + else{ + throw new AuthenticationException("As credenciais fornecidas não são válidas"); + } + } + + @Override + public void unAuthenticate() { + this.currentUser = null; + } + + @Override + public Principal getUser() { + return this.currentUser; + } +} -- libgit2 0.21.2