Results 1 to 3 of 3

Thread: login via acegi from TestCase

  1. #1
    Join Date
    Jun 2006
    Posts
    22

    Default login via acegi from TestCase

    Hi!
    To test some of my units a user have to be logged in to the system (a web app) since these classes need some information retrieved by ((SecurityContext) SecurityContextHolder.getContext()).getAuthenticat ion().getPrincipal()

    How do I write a test case (extending AbstractTransactionalSpringContextTests) that log in a user?

    thanks
    -Martin Börlin

  2. #2
    Join Date
    Dec 2005
    Posts
    22

    Default

    I set self the authentication in the SecurityContextHolder : SecurityContextHolder.getContext().setAuthenticati on(((AuthenticationManager) context.getBean("authenticationManager")).authenti cate(new UsernamePasswordAuthenticationToken("test", "test")));

  3. #3
    Join Date
    Jun 2006
    Posts
    22

    Default

    Thanks for the reply!
    ...I found out how to do by my self. Very similar to your solution though.

    cheers
    -Martin

    class MyTestClass extends AbstractDependencyInjectionSpringContextTests {
    AuthenticationProvider authenticationProvider;

    [setUp and test methods]

    private void createSecureContext(final String username, final String password) {
    Authentication auth = authenticationProvider.authenticate(new UsernamePasswordAuthenticationToken(username, password));
    SecurityContextHolder.getContext().setAuthenticati on(auth);
    }

    //Set by the Spring framework automagically when running the test
    //if you got a bean called 'authenticationProvider' in you bean definition file
    public void setAuthenticationProvider(AuthenticationProvider authenticationProvider) {
    this.authenticationProvider = authenticationProvider;
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •