Commit 551723da9f220d0913f15474ef46d4bf17c6c2f8
1 parent
f2959d98
Exists in
master
Adição de testes para o interceptor LoggedIn.
Showing
4 changed files
with
236 additions
and
0 deletions
Show diff stats
impl/core/src/test/java/security/interceptor/loggedin/CustomAuthenticator.java
0 → 100644
| ... | ... | @@ -0,0 +1,81 @@ |
| 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.interceptor.loggedin; | |
| 38 | + | |
| 39 | +import javax.enterprise.context.SessionScoped; | |
| 40 | + | |
| 41 | +import br.gov.frameworkdemoiselle.security.Authenticator; | |
| 42 | +import br.gov.frameworkdemoiselle.security.User; | |
| 43 | + | |
| 44 | +@SessionScoped | |
| 45 | +public class CustomAuthenticator implements Authenticator { | |
| 46 | + | |
| 47 | + private static final long serialVersionUID = 1L; | |
| 48 | + | |
| 49 | + private User currentUser; | |
| 50 | + | |
| 51 | + @Override | |
| 52 | + public void authenticate() { | |
| 53 | + this.currentUser = new User() { | |
| 54 | + | |
| 55 | + private static final long serialVersionUID = 1L; | |
| 56 | + | |
| 57 | + public String getId() { | |
| 58 | + return "demoiselle"; | |
| 59 | + } | |
| 60 | + | |
| 61 | + @Override | |
| 62 | + public Object getAttribute(Object key) { | |
| 63 | + return null; | |
| 64 | + } | |
| 65 | + | |
| 66 | + @Override | |
| 67 | + public void setAttribute(Object key, Object value) { | |
| 68 | + } | |
| 69 | + }; | |
| 70 | + } | |
| 71 | + | |
| 72 | + @Override | |
| 73 | + public void unauthenticate() { | |
| 74 | + this.currentUser = null; | |
| 75 | + } | |
| 76 | + | |
| 77 | + @Override | |
| 78 | + public User getUser() { | |
| 79 | + return this.currentUser; | |
| 80 | + } | |
| 81 | +} | ... | ... |
impl/core/src/test/java/security/interceptor/loggedin/DummyProtectedClass.java
0 → 100644
| ... | ... | @@ -0,0 +1,54 @@ |
| 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.interceptor.loggedin; | |
| 38 | + | |
| 39 | +import br.gov.frameworkdemoiselle.security.LoggedIn; | |
| 40 | + | |
| 41 | +@LoggedIn | |
| 42 | +public class DummyProtectedClass { | |
| 43 | + | |
| 44 | + private String dummyAttrib; | |
| 45 | + | |
| 46 | + public String getDummyAttrib() { | |
| 47 | + return dummyAttrib; | |
| 48 | + } | |
| 49 | + | |
| 50 | + public void setDummyAttrib(String dummyAttrib) { | |
| 51 | + this.dummyAttrib = dummyAttrib; | |
| 52 | + } | |
| 53 | + | |
| 54 | +} | ... | ... |
impl/core/src/test/java/security/interceptor/loggedin/LoggedInInterceptorTest.java
0 → 100644
| ... | ... | @@ -0,0 +1,100 @@ |
| 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.interceptor.loggedin; | |
| 38 | + | |
| 39 | +import static junit.framework.Assert.assertEquals; | |
| 40 | +import static org.junit.Assert.assertEquals; | |
| 41 | +import static org.junit.Assert.assertNotNull; | |
| 42 | +import static org.junit.Assert.assertTrue; | |
| 43 | + | |
| 44 | +import javax.inject.Inject; | |
| 45 | + | |
| 46 | +import org.jboss.arquillian.container.test.api.Deployment; | |
| 47 | +import org.jboss.arquillian.junit.Arquillian; | |
| 48 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; | |
| 49 | +import org.junit.Test; | |
| 50 | +import org.junit.runner.RunWith; | |
| 51 | + | |
| 52 | +import br.gov.frameworkdemoiselle.context.RequestContext; | |
| 53 | +import br.gov.frameworkdemoiselle.context.SessionContext; | |
| 54 | +import br.gov.frameworkdemoiselle.security.AuthenticationException; | |
| 55 | +import br.gov.frameworkdemoiselle.security.NotLoggedInException; | |
| 56 | +import br.gov.frameworkdemoiselle.security.SecurityContext; | |
| 57 | +import br.gov.frameworkdemoiselle.util.Beans; | |
| 58 | +import br.gov.frameworkdemoiselle.util.NameQualifier; | |
| 59 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | |
| 60 | + | |
| 61 | +import test.Tests; | |
| 62 | + | |
| 63 | +@RunWith(Arquillian.class) | |
| 64 | +public class LoggedInInterceptorTest { | |
| 65 | + | |
| 66 | + @Inject | |
| 67 | + private DummyProtectedClass protectedClass; | |
| 68 | + | |
| 69 | + @Inject | |
| 70 | + private SecurityContext context; | |
| 71 | + | |
| 72 | + @Deployment | |
| 73 | + public static JavaArchive createDeployment() { | |
| 74 | + JavaArchive deployment = Tests.createDeployment(); | |
| 75 | + deployment.addClass(DummyProtectedClass.class); | |
| 76 | + deployment.addClass(CustomAuthenticator.class); | |
| 77 | + return deployment; | |
| 78 | + } | |
| 79 | + | |
| 80 | + @Test | |
| 81 | + public void callProtectedClassAttribNotLogged() { | |
| 82 | + try { | |
| 83 | + protectedClass.getDummyAttrib(); | |
| 84 | + } catch (NotLoggedInException cause) { | |
| 85 | + assertEquals(Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-core-bundle")) | |
| 86 | + .getString("user-not-authenticated"), cause.getMessage()); | |
| 87 | + } | |
| 88 | + } | |
| 89 | + | |
| 90 | + @Test | |
| 91 | + public void callProtectedClassAttribLogged() { | |
| 92 | + SessionContext ctx = Beans.getReference(SessionContext.class); | |
| 93 | + ctx.activate(); | |
| 94 | + | |
| 95 | + context.login(); | |
| 96 | + protectedClass.getDummyAttrib(); | |
| 97 | + | |
| 98 | + ctx.deactivate(); | |
| 99 | + } | |
| 100 | +} | ... | ... |
impl/core/src/test/resources/beans.xml
| ... | ... | @@ -5,6 +5,7 @@ |
| 5 | 5 | <class>br.gov.frameworkdemoiselle.transaction.TransactionalInterceptor</class> |
| 6 | 6 | <class>br.gov.frameworkdemoiselle.security.RequiredPermissionInterceptor</class> |
| 7 | 7 | <class>br.gov.frameworkdemoiselle.security.RequiredRoleInterceptor</class> |
| 8 | + <class>br.gov.frameworkdemoiselle.security.LoggedInInterceptor</class> | |
| 8 | 9 | <class>br.gov.frameworkdemoiselle.exception.ExceptionHandlerInterceptor</class> |
| 9 | 10 | </interceptors> |
| 10 | 11 | ... | ... |