You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Michał Jedynak <m....@gmail.com> on 2008/12/22 12:42:05 UTC

Tapestry 5 with Spring Security - problem with unit tests.

Hi there,

I've integrated spring security to my sample tapestry5 application
(I'm using version 5.0.18) based on tapestry-spring-security
(http://www.localhost.nu/java/tapestry-spring-security), but now my
unit tests that use PageTester don't work, because there is no
Authentication object in SecurityContext.

I thought about creating SecurityContext, something like:

SecurityContext securityContext = new SecurityContextImpl();
User user = new User();
user.setId(1L);
user.setUsername("user");
user.setPassword("password");
UsernamePasswordAuthenticationToken token =
    new UsernamePasswordAuthenticationToken(
	user.getUsername(), user.getPassword(), user.getAuthorities());
token.setDetails(user);
securityContext.setAuthentication(token);

and adding it to the rendered page through PageTester, as it is
described in http://tapestry.apache.org/tapestry5/guide/unit-testing-pages.html:

PageTester tester = new PageTester(appPackage, appName, "src/main/webapp");
Object[] context = new Object[]{ "abc", 123 };
Document doc = tester.invoke(new ComponentInvocation(new
PageLinkTarget("MyPage"), context));

However this doesn't work, ComponentInvocation is an interface and I
don't know how to initialize properly ComponentInvocationImpl.
Anyone care to help?





-- 
Michał Jedynak

Re: Tapestry 5 with Spring Security - problem with unit tests.

Posted by Michał Jedynak <m....@gmail.com>.
Thanks for the reply!

I came up with a similar solution:
I pass SecurityModel class from tapestry-spring-security to instance of
PageTester:

tester = new PageTester(appPackage, appName,
				"src/main/webapp",SecurityModule.class);

and then I just create SecurityContext (the same way like in my first post)
and 
set it in SecurityContextHolder:

SecurityContextHolder.setContext(securityContext);

I don't create registry or use loadUserByUsername(...) method.

However, test() method from
nu.localhost.tapestry5.springsecurity.components.IfLoggedIn
throws NullPointerException during executing unit test, and I had to modify
it by adding (that's probably very sloppy ;-) ):

if (requestGlobals.getHTTPServletRequest()==null) {
	if (((UserDetails)SecurityContextHolder.getContext()
			.getAuthentication().getDetails()).getUsername()!=null) {
		return true;
	}
}


Did you handle it in a better way? (That's probably in yours handleIfLogIn() 
method ;-) )




Mark Horn-2 wrote:
> 
> We also use spring-security and ran into some issues as well.  What we
> ended up doing was adding something like this to the JUnit #setUp
> method
> 
>    /* (non-Javadoc)
>     * @see junit.framework.TestCase#setUp()
>     */
>        protected void setUp() throws Exception {
>        super.setUp();
> 
>        _tester = new PageTester(_ROOT_PACKAGE_NAME, _APP_MODULE,
>                PageTester.DEFAULT_CONTEXT_PATH, TestHarnessModule.class);
> 
>        _encoder = _tester.getService(ContextPathEncoder.class);
> 
>        Registry registry = _tester.getRegistry();
>        registry.performRegistryStartup();
> 
>        UserDetailsService userDetailsSvc =
> _tester.getService(UserDetailsService.class);
>        UserDetails ud = userDetailsSvc.loadUserByUsername(someuser);
> 
>        handleIfLogIn();
> 
>        SecurityContext context = new SecurityContextImpl();
>        Authentication currentUser = new
> UsernamePasswordAuthenticationToken(ud.getUsername(),
>                        somepassword);
>        context.setAuthentication(currentUser);
>        SecurityContextHolder.setContext(context);
>    }
> 
> Hope that helps.. and if anyone has a better solution please let me know.
> 
> -Mark
> 
> PS (sorry for the double post, but wanted to make sure my comment was
> attached to the appropriate thread)
> 
> On Mon, Dec 22, 2008 at 10:28 AM, SergeEby <sd...@hotmail.com> wrote:
>>
>> Hi,
>>
>> This seems to be related to this thread:
>> http://www.nabble.com/PageLinkTarget%2C-where-did-it-go--to21022001.html#a21022001
>>
>> /Serge
>>
>>
>> Michał Jedynak wrote:
>>>
>>> Hi there,
>>>
>>> I've integrated spring security to my sample tapestry5 application
>>> (I'm using version 5.0.18) based on tapestry-spring-security
>>> (http://www.localhost.nu/java/tapestry-spring-security), but now my
>>> unit tests that use PageTester don't work, because there is no
>>> Authentication object in SecurityContext.
>>>
>>> I thought about creating SecurityContext, something like:
>>>
>>> SecurityContext securityContext = new SecurityContextImpl();
>>> User user = new User();
>>> user.setId(1L);
>>> user.setUsername("user");
>>> user.setPassword("password");
>>> UsernamePasswordAuthenticationToken token =
>>>     new UsernamePasswordAuthenticationToken(
>>>       user.getUsername(), user.getPassword(), user.getAuthorities());
>>> token.setDetails(user);
>>> securityContext.setAuthentication(token);
>>>
>>> and adding it to the rendered page through PageTester, as it is
>>> described in
>>> http://tapestry.apache.org/tapestry5/guide/unit-testing-pages.html:
>>>
>>> PageTester tester = new PageTester(appPackage, appName,
>>> "src/main/webapp");
>>> Object[] context = new Object[]{ "abc", 123 };
>>> Document doc = tester.invoke(new ComponentInvocation(new
>>> PageLinkTarget("MyPage"), context));
>>>
>>> However this doesn't work, ComponentInvocation is an interface and I
>>> don't know how to initialize properly ComponentInvocationImpl.
>>> Anyone care to help?
>>>
>>>
>>>
>>>
>>>
>>> --
>>> Michał Jedynak
>>>
>>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Tapestry-5-with-Spring-Security---problem-with-unit-tests.-tp21126486p21129652.html
>> Sent from the Tapestry - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
> 
> 
q
-- 
View this message in context: http://www.nabble.com/Tapestry-5-with-Spring-Security---problem-with-unit-tests.-tp21126486p21176964.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry 5 with Spring Security - problem with unit tests.

Posted by Mark Horn <ma...@agilesrc.com>.
We also use spring-security and ran into some issues as well.  What we
ended up doing was adding something like this to the JUnit #setUp
method

   /* (non-Javadoc)
    * @see junit.framework.TestCase#setUp()
    */
       protected void setUp() throws Exception {
       super.setUp();

       _tester = new PageTester(_ROOT_PACKAGE_NAME, _APP_MODULE,
               PageTester.DEFAULT_CONTEXT_PATH, TestHarnessModule.class);

       _encoder = _tester.getService(ContextPathEncoder.class);

       Registry registry = _tester.getRegistry();
       registry.performRegistryStartup();

       UserDetailsService userDetailsSvc =
_tester.getService(UserDetailsService.class);
       UserDetails ud = userDetailsSvc.loadUserByUsername(someuser);

       handleIfLogIn();

       SecurityContext context = new SecurityContextImpl();
       Authentication currentUser = new
UsernamePasswordAuthenticationToken(ud.getUsername(),
                       somepassword);
       context.setAuthentication(currentUser);
       SecurityContextHolder.setContext(context);
   }

Hope that helps.. and if anyone has a better solution please let me know.

-Mark

PS (sorry for the double post, but wanted to make sure my comment was
attached to the appropriate thread)

On Mon, Dec 22, 2008 at 10:28 AM, SergeEby <sd...@hotmail.com> wrote:
>
> Hi,
>
> This seems to be related to this thread:
> http://www.nabble.com/PageLinkTarget%2C-where-did-it-go--to21022001.html#a21022001
>
> /Serge
>
>
> Michał Jedynak wrote:
>>
>> Hi there,
>>
>> I've integrated spring security to my sample tapestry5 application
>> (I'm using version 5.0.18) based on tapestry-spring-security
>> (http://www.localhost.nu/java/tapestry-spring-security), but now my
>> unit tests that use PageTester don't work, because there is no
>> Authentication object in SecurityContext.
>>
>> I thought about creating SecurityContext, something like:
>>
>> SecurityContext securityContext = new SecurityContextImpl();
>> User user = new User();
>> user.setId(1L);
>> user.setUsername("user");
>> user.setPassword("password");
>> UsernamePasswordAuthenticationToken token =
>>     new UsernamePasswordAuthenticationToken(
>>       user.getUsername(), user.getPassword(), user.getAuthorities());
>> token.setDetails(user);
>> securityContext.setAuthentication(token);
>>
>> and adding it to the rendered page through PageTester, as it is
>> described in
>> http://tapestry.apache.org/tapestry5/guide/unit-testing-pages.html:
>>
>> PageTester tester = new PageTester(appPackage, appName,
>> "src/main/webapp");
>> Object[] context = new Object[]{ "abc", 123 };
>> Document doc = tester.invoke(new ComponentInvocation(new
>> PageLinkTarget("MyPage"), context));
>>
>> However this doesn't work, ComponentInvocation is an interface and I
>> don't know how to initialize properly ComponentInvocationImpl.
>> Anyone care to help?
>>
>>
>>
>>
>>
>> --
>> Michał Jedynak
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Tapestry-5-with-Spring-Security---problem-with-unit-tests.-tp21126486p21129652.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Tapestry 5 with Spring Security - problem with unit tests.

Posted by SergeEby <sd...@hotmail.com>.
Hi,

This seems to be related to this thread:
http://www.nabble.com/PageLinkTarget%2C-where-did-it-go--to21022001.html#a21022001

/Serge


Michał Jedynak wrote:
> 
> Hi there,
> 
> I've integrated spring security to my sample tapestry5 application
> (I'm using version 5.0.18) based on tapestry-spring-security
> (http://www.localhost.nu/java/tapestry-spring-security), but now my
> unit tests that use PageTester don't work, because there is no
> Authentication object in SecurityContext.
> 
> I thought about creating SecurityContext, something like:
> 
> SecurityContext securityContext = new SecurityContextImpl();
> User user = new User();
> user.setId(1L);
> user.setUsername("user");
> user.setPassword("password");
> UsernamePasswordAuthenticationToken token =
>     new UsernamePasswordAuthenticationToken(
> 	user.getUsername(), user.getPassword(), user.getAuthorities());
> token.setDetails(user);
> securityContext.setAuthentication(token);
> 
> and adding it to the rendered page through PageTester, as it is
> described in
> http://tapestry.apache.org/tapestry5/guide/unit-testing-pages.html:
> 
> PageTester tester = new PageTester(appPackage, appName,
> "src/main/webapp");
> Object[] context = new Object[]{ "abc", 123 };
> Document doc = tester.invoke(new ComponentInvocation(new
> PageLinkTarget("MyPage"), context));
> 
> However this doesn't work, ComponentInvocation is an interface and I
> don't know how to initialize properly ComponentInvocationImpl.
> Anyone care to help?
> 
> 
> 
> 
> 
> -- 
> Michał Jedynak
> 
> 

-- 
View this message in context: http://www.nabble.com/Tapestry-5-with-Spring-Security---problem-with-unit-tests.-tp21126486p21129652.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org