You could look up the user's full information by injecting the
userManager bean and calling getUser() on that. You could also just
call getObject(User.class, id) on your existing manager. However, I
know you're looking for the Acegi-based solution. In your test, you
can do something like the following (possibly in your setUp() method)
to set the user's information.
SecurityContext context = new SecurityContextImpl();
UsernamePasswordAuthenticationToken token = new
UsernamePasswordAuthenticationToken("user",
"password",
new GrantedAuthority[] {new
GrantedAuthorityImpl(Constants.USER_ROLE)});
context.setAuthentication(token);
SecurityContextHolder.setContext(context);
Of course, you'll to put your User object in the context instead of
the token, but I've never done that. Hopefully it won't be too hard
to figure out. ;-)
Matt
On 11/28/06, thinkboy <[EMAIL PROTECTED]> wrote:
matt,
thanks so much. u r so helpful.
sorry to trouble for one more question.
aside from username, if I need from User object that is special, e.g
department code
how am i going to handle security context dependency, do u friend do this ?
User currentUser = (User)
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String depCode = currentUser.getDepartmentCode()
thanks for your great help
~thinkboy
Matt Raible-3 wrote:
>
> The easiest way to do this is to use the remoteUser. In your test:
>
> MockHttpServletRequest request = ...
> request.setRemoteUser("foo");
>
> Then in your Controller, use:
>
> String username = request.getRemoteUser();
>
> Matt
>
> On 11/28/06, Man-Chi Leung <[EMAIL PROTECTED]> wrote:
>> hi,
>>
>> in my spring mvc controller, I need to get username from acegi security
>> context and pass this username to my service manager. but bcos of this
>> acegi dependency, I have difficulty to do unit test for my mvc
>> controller.
>>
>> may I know what is the best practice?
>>
>> should I refactor the mvc controller, injecting denpendency of
>> acegi security context to my Controller via spring from
>> setAcegiContext(), then use Jmock to mock a acegi security context in
>> my MyControllerTest ?
>>
>> i am a bit puzzled on this. pls advise. thanks so much.
>>
>> ~thinkboy.
>>
>>
>> public ModelAndView handleRequest(HttpServletRequest request,
>> HttpServletResponse response) throws Exception {
>>
>> User currentUser = (User)
>> SecurityContextHolder.getContext()
>> .getAuthentication().getPrincipal();
>> String username = currentUser.getUsername()
>>
>>
>> return new ModelAndView("serviceList",
>> Constants.SERVICE_LIST, mgr
>> .lookupServicesByUsername(username));
>> }
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>
>
> --
> http://raibledesigns.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
--
View this message in context:
http://www.nabble.com/to-test-a-spring-mvc-controller-that-has-acegi-dependency--tf2722652s2369.html#a7594161
Sent from the AppFuse - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
http://raibledesigns.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]