r831 - in trunk/src/test/java/org/ourproject/kune/platf: integration server server/auth
Author: vjrj Date: 2008-08-06 04:36:00 +0200 (Wed, 06 Aug 2008) New Revision: 831 Added: trunk/src/test/java/org/ourproject/kune/platf/server/auth/ trunk/src/test/java/org/ourproject/kune/platf/server/auth/AuthenticatedMethodInterceptorTest.java Modified: trunk/src/test/java/org/ourproject/kune/platf/integration/IntegrationTestHelper.java Log: Incomplete - task 38: Auth*MethodInterceptors Tests Modified: trunk/src/test/java/org/ourproject/kune/platf/integration/IntegrationTestHelper.java =================================================================== --- trunk/src/test/java/org/ourproject/kune/platf/integration/IntegrationTestHelper.java 2008-08-05 17:43:43 UTC (rev 830) +++ trunk/src/test/java/org/ourproject/kune/platf/integration/IntegrationTestHelper.java 2008-08-06 02:36:00 UTC (rev 831) @@ -18,24 +18,26 @@ public class IntegrationTestHelper { public static Injector createInjector() { - final Injector injector = Guice.createInjector(new PlatformServerModule(), new DocumentServerModule(), - new ChatServerModule(), new AbstractModule() { - @Override - protected void configure() { - bindScope(SessionScoped.class, Scopes.SINGLETON); - // test: use memory - // test_db: use mysql - bindConstant().annotatedWith(JpaUnit.class).to("test"); - bindConstant().annotatedWith(PropertiesFileName.class).to("kune.properties"); - bind(HttpServletRequest.class).to(HttpServletRequestMocked.class); - } - }); - return injector; + final Injector injector = Guice.createInjector(new PlatformServerModule(), new DocumentServerModule(), + new ChatServerModule(), new AbstractModule() { + @Override + protected void configure() { + bindScope(SessionScoped.class, Scopes.SINGLETON); + // test: use memory + // test_db: use mysql + bindConstant().annotatedWith(JpaUnit.class).to("test"); + bindConstant().annotatedWith(PropertiesFileName.class).to("kune.properties"); + bind(HttpServletRequest.class).to(HttpServletRequestMocked.class); + } + }); + return injector; } - public IntegrationTestHelper(final Object test) { - final Injector injector = createInjector(); - injector.getInstance(KunePersistenceService.class).start(); - injector.injectMembers(test); + public IntegrationTestHelper(final Object... tests) { + final Injector injector = createInjector(); + injector.getInstance(KunePersistenceService.class).start(); + for (final Object test : tests) { + injector.injectMembers(test); + } } } Added: trunk/src/test/java/org/ourproject/kune/platf/server/auth/AuthenticatedMethodInterceptorTest.java =================================================================== --- trunk/src/test/java/org/ourproject/kune/platf/server/auth/AuthenticatedMethodInterceptorTest.java 2008-08-05 17:43:43 UTC (rev 830) +++ trunk/src/test/java/org/ourproject/kune/platf/server/auth/AuthenticatedMethodInterceptorTest.java 2008-08-06 02:36:00 UTC (rev 831) @@ -0,0 +1,81 @@ +package org.ourproject.kune.platf.server.auth; + +import java.lang.reflect.AccessibleObject; + +import org.aopalliance.intercept.MethodInvocation; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.ourproject.kune.platf.client.errors.SessionExpiredException; +import org.ourproject.kune.platf.client.errors.UserMustBeLoggedException; +import org.ourproject.kune.platf.integration.IntegrationTest; +import org.ourproject.kune.platf.integration.IntegrationTestHelper; + +public class AuthenticatedMethodInterceptorTest extends IntegrationTest { + + private AuthenticatedMethodInterceptor auth; + private MethodInvocation invocation; + private Authenticated authAnnotation; + + @Before + public void before() { + auth = new AuthenticatedMethodInterceptor(); + new IntegrationTestHelper(auth, this); + invocation = Mockito.mock(MethodInvocation.class); + final AccessibleObject accessibleObject = Mockito.mock(AccessibleObject.class); + Mockito.stub(invocation.getMethod()).toReturn(this.getClass().getMethods()[0]); + Mockito.stub(invocation.getStaticPart()).toReturn(accessibleObject); + authAnnotation = Mockito.mock(Authenticated.class); + Mockito.stub(accessibleObject.getAnnotation(Authenticated.class)).toReturn(authAnnotation); + } + + @Test(expected = UserMustBeLoggedException.class) + public void hashNullAndMandatoryMustDoNothing() throws Throwable { + Mockito.stub(authAnnotation.mandatory()).toReturn(true); + final Object[] result = { null }; + Mockito.stub(invocation.getArguments()).toReturn(result); + auth.invoke(invocation); + } + + @Test + public void hashNullAndNotMandatoryMustDoNothing() throws Throwable { + Mockito.stub(authAnnotation.mandatory()).toReturn(false); + final Object[] result = { null }; + Mockito.stub(invocation.getArguments()).toReturn(result); + auth.invoke(invocation); + } + + @Test + public void hashNullAsStringAndNotMandatoryMustDoNothing() throws Throwable { + Mockito.stub(authAnnotation.mandatory()).toReturn(false); + final Object[] result = { "null" }; + Mockito.stub(invocation.getArguments()).toReturn(result); + auth.invoke(invocation); + } + + @Test(expected = SessionExpiredException.class) + public void otherHashAndMandatoryAndLoggedMustSessionExp() throws Throwable { + doLogin(); + Mockito.stub(authAnnotation.mandatory()).toReturn(true); + final Object[] result = { "other-hash" }; + Mockito.stub(invocation.getArguments()).toReturn(result); + auth.invoke(invocation); + } + + @Test + public void sameHashAndMandatoryAndLoggedMustSessionExp() throws Throwable { + doLogin(); + Mockito.stub(authAnnotation.mandatory()).toReturn(true); + final Object[] result = { getHash() }; + Mockito.stub(invocation.getArguments()).toReturn(result); + auth.invoke(invocation); + } + + @Test(expected = SessionExpiredException.class) + public void someHashAndMandatoryAndNotLoggedMustSessionExp() throws Throwable { + Mockito.stub(authAnnotation.mandatory()).toReturn(true); + final Object[] result = { "some-hash" }; + Mockito.stub(invocation.getArguments()).toReturn(result); + auth.invoke(invocation); + } +}
participants (1)
-
vjrj