From 551723da9f220d0913f15474ef46d4bf17c6c2f8 Mon Sep 17 00:00:00 2001 From: Emerson Oliveira Date: Wed, 11 Sep 2013 11:28:55 -0300 Subject: [PATCH] Adição de testes para o interceptor LoggedIn. --- impl/core/src/test/java/security/interceptor/loggedin/CustomAuthenticator.java | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/security/interceptor/loggedin/DummyProtectedClass.java | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/security/interceptor/loggedin/LoggedInInterceptorTest.java | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/resources/beans.xml | 1 + 4 files changed, 236 insertions(+), 0 deletions(-) create mode 100644 impl/core/src/test/java/security/interceptor/loggedin/CustomAuthenticator.java create mode 100644 impl/core/src/test/java/security/interceptor/loggedin/DummyProtectedClass.java create mode 100644 impl/core/src/test/java/security/interceptor/loggedin/LoggedInInterceptorTest.java diff --git a/impl/core/src/test/java/security/interceptor/loggedin/CustomAuthenticator.java b/impl/core/src/test/java/security/interceptor/loggedin/CustomAuthenticator.java new file mode 100644 index 0000000..a7ed3dc --- /dev/null +++ b/impl/core/src/test/java/security/interceptor/loggedin/CustomAuthenticator.java @@ -0,0 +1,81 @@ +/* + * 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.interceptor.loggedin; + +import javax.enterprise.context.SessionScoped; + +import br.gov.frameworkdemoiselle.security.Authenticator; +import br.gov.frameworkdemoiselle.security.User; + +@SessionScoped +public class CustomAuthenticator implements Authenticator { + + private static final long serialVersionUID = 1L; + + private User currentUser; + + @Override + public void authenticate() { + this.currentUser = new User() { + + private static final long serialVersionUID = 1L; + + public String getId() { + return "demoiselle"; + } + + @Override + public Object getAttribute(Object key) { + return null; + } + + @Override + public void setAttribute(Object key, Object value) { + } + }; + } + + @Override + public void unauthenticate() { + this.currentUser = null; + } + + @Override + public User getUser() { + return this.currentUser; + } +} diff --git a/impl/core/src/test/java/security/interceptor/loggedin/DummyProtectedClass.java b/impl/core/src/test/java/security/interceptor/loggedin/DummyProtectedClass.java new file mode 100644 index 0000000..5f341c3 --- /dev/null +++ b/impl/core/src/test/java/security/interceptor/loggedin/DummyProtectedClass.java @@ -0,0 +1,54 @@ +/* + * 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.interceptor.loggedin; + +import br.gov.frameworkdemoiselle.security.LoggedIn; + +@LoggedIn +public class DummyProtectedClass { + + private String dummyAttrib; + + public String getDummyAttrib() { + return dummyAttrib; + } + + public void setDummyAttrib(String dummyAttrib) { + this.dummyAttrib = dummyAttrib; + } + +} diff --git a/impl/core/src/test/java/security/interceptor/loggedin/LoggedInInterceptorTest.java b/impl/core/src/test/java/security/interceptor/loggedin/LoggedInInterceptorTest.java new file mode 100644 index 0000000..b84d5d6 --- /dev/null +++ b/impl/core/src/test/java/security/interceptor/loggedin/LoggedInInterceptorTest.java @@ -0,0 +1,100 @@ +/* + * 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.interceptor.loggedin; + +import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import javax.inject.Inject; + +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 br.gov.frameworkdemoiselle.context.RequestContext; +import br.gov.frameworkdemoiselle.context.SessionContext; +import br.gov.frameworkdemoiselle.security.AuthenticationException; +import br.gov.frameworkdemoiselle.security.NotLoggedInException; +import br.gov.frameworkdemoiselle.security.SecurityContext; +import br.gov.frameworkdemoiselle.util.Beans; +import br.gov.frameworkdemoiselle.util.NameQualifier; +import br.gov.frameworkdemoiselle.util.ResourceBundle; + +import test.Tests; + +@RunWith(Arquillian.class) +public class LoggedInInterceptorTest { + + @Inject + private DummyProtectedClass protectedClass; + + @Inject + private SecurityContext context; + + @Deployment + public static JavaArchive createDeployment() { + JavaArchive deployment = Tests.createDeployment(); + deployment.addClass(DummyProtectedClass.class); + deployment.addClass(CustomAuthenticator.class); + return deployment; + } + + @Test + public void callProtectedClassAttribNotLogged() { + try { + protectedClass.getDummyAttrib(); + } catch (NotLoggedInException cause) { + assertEquals(Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-core-bundle")) + .getString("user-not-authenticated"), cause.getMessage()); + } + } + + @Test + public void callProtectedClassAttribLogged() { + SessionContext ctx = Beans.getReference(SessionContext.class); + ctx.activate(); + + context.login(); + protectedClass.getDummyAttrib(); + + ctx.deactivate(); + } +} diff --git a/impl/core/src/test/resources/beans.xml b/impl/core/src/test/resources/beans.xml index d85497f..8d34e59 100644 --- a/impl/core/src/test/resources/beans.xml +++ b/impl/core/src/test/resources/beans.xml @@ -5,6 +5,7 @@ br.gov.frameworkdemoiselle.transaction.TransactionalInterceptor br.gov.frameworkdemoiselle.security.RequiredPermissionInterceptor br.gov.frameworkdemoiselle.security.RequiredRoleInterceptor + br.gov.frameworkdemoiselle.security.LoggedInInterceptor br.gov.frameworkdemoiselle.exception.ExceptionHandlerInterceptor -- libgit2 0.21.2