Search in sources :

Example 6 with TestingAuthenticationToken

use of org.springframework.security.providers.TestingAuthenticationToken in project gocd by gocd.

the class GoVelocityViewTest method shouldSetTemplateAdministratorIfUserIsTemplateAdministrator.

@Test
public void shouldSetTemplateAdministratorIfUserIsTemplateAdministrator() throws Exception {
    securityContext.setAuthentication(new TestingAuthenticationToken("jez", "badger", new GrantedAuthority[] { new GrantedAuthorityImpl(GoAuthority.ROLE_TEMPLATE_SUPERVISOR.toString()) }));
    request.getSession().setAttribute(SPRING_SECURITY_CONTEXT_KEY, securityContext);
    view.exposeHelpers(velocityContext, request);
    assertThat(velocityContext.get(GoVelocityView.TEMPLATE_ADMINISTRATOR), is(true));
}
Also used : GrantedAuthorityImpl(org.springframework.security.GrantedAuthorityImpl) GrantedAuthority(org.springframework.security.GrantedAuthority) TestingAuthenticationToken(org.springframework.security.providers.TestingAuthenticationToken) Test(org.junit.Test)

Example 7 with TestingAuthenticationToken

use of org.springframework.security.providers.TestingAuthenticationToken in project gocd by gocd.

the class GoVelocityViewTest method shouldSetViewAdministratorRightsIfUserHasAnyLevelOfAdministratorRights.

@Test
public void shouldSetViewAdministratorRightsIfUserHasAnyLevelOfAdministratorRights() throws Exception {
    securityContext.setAuthentication(new TestingAuthenticationToken("jez", "badger", new GrantedAuthority[] { new GrantedAuthorityImpl(GoAuthority.ROLE_TEMPLATE_SUPERVISOR.toString()) }));
    request.getSession().setAttribute(SPRING_SECURITY_CONTEXT_KEY, securityContext);
    view.exposeHelpers(velocityContext, request);
    assertThat(velocityContext.get(GoVelocityView.VIEW_ADMINISTRATOR_RIGHTS), is(true));
    securityContext.setAuthentication(new TestingAuthenticationToken("jez", "badger", new GrantedAuthority[] { new GrantedAuthorityImpl(GoAuthority.ROLE_GROUP_SUPERVISOR.toString()) }));
    request.getSession().setAttribute(SPRING_SECURITY_CONTEXT_KEY, securityContext);
    view.exposeHelpers(velocityContext, request);
    assertThat(velocityContext.get(GoVelocityView.VIEW_ADMINISTRATOR_RIGHTS), is(true));
    securityContext.setAuthentication(new TestingAuthenticationToken("jez", "badger", new GrantedAuthority[] { new GrantedAuthorityImpl(GoAuthority.ROLE_SUPERVISOR.toString()) }));
    request.getSession().setAttribute(SPRING_SECURITY_CONTEXT_KEY, securityContext);
    view.exposeHelpers(velocityContext, request);
    assertThat(velocityContext.get(GoVelocityView.VIEW_ADMINISTRATOR_RIGHTS), is(true));
    securityContext.setAuthentication(new TestingAuthenticationToken("jez", "badger", new GrantedAuthority[] { new GrantedAuthorityImpl(GoAuthority.ROLE_TEMPLATE_VIEW_USER.toString()) }));
    request.getSession().setAttribute(SPRING_SECURITY_CONTEXT_KEY, securityContext);
    view.exposeHelpers(velocityContext, request);
    assertThat(velocityContext.get(GoVelocityView.VIEW_ADMINISTRATOR_RIGHTS), is(true));
    securityContext.setAuthentication(new TestingAuthenticationToken("jez", "badger", new GrantedAuthority[] { new GrantedAuthorityImpl(GoAuthority.ROLE_USER.toString()) }));
    request.getSession().setAttribute(SPRING_SECURITY_CONTEXT_KEY, securityContext);
    view.exposeHelpers(velocityContext, request);
    assertThat(velocityContext.get(GoVelocityView.VIEW_ADMINISTRATOR_RIGHTS), is(nullValue()));
}
Also used : GrantedAuthorityImpl(org.springframework.security.GrantedAuthorityImpl) GrantedAuthority(org.springframework.security.GrantedAuthority) TestingAuthenticationToken(org.springframework.security.providers.TestingAuthenticationToken) Test(org.junit.Test)

Example 8 with TestingAuthenticationToken

use of org.springframework.security.providers.TestingAuthenticationToken in project gocd by gocd.

the class UserHelperTest method shouldGetFullNameFromLdapUserDetails.

@Test
public void shouldGetFullNameFromLdapUserDetails() {
    TestingAuthenticationToken authentication = new TestingAuthenticationToken(new LdapUserDetailsImpl() {

        public String getUsername() {
            return "test1";
        }

        public String getDn() {
            return "cn=Test User, ou=Beijing, ou=Employees, ou=Enterprise, ou=Principal";
        }
    }, null, null);
    assertThat(UserHelper.getUserName(authentication).getDisplayName(), is("Test User"));
}
Also used : LdapUserDetailsImpl(org.springframework.security.userdetails.ldap.LdapUserDetailsImpl) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) TestingAuthenticationToken(org.springframework.security.providers.TestingAuthenticationToken) Test(org.junit.Test)

Example 9 with TestingAuthenticationToken

use of org.springframework.security.providers.TestingAuthenticationToken in project gocd by gocd.

the class GoVelocityViewTest method shouldRetriveLdapCompleteNameFromSessionWhenAuthenticated.

@Test
public void shouldRetriveLdapCompleteNameFromSessionWhenAuthenticated() throws Exception {
    securityContext.setAuthentication(new TestingAuthenticationToken(new LdapUserDetailsImpl() {

        public String getUsername() {
            return "test1";
        }

        public String getDn() {
            return "cn=Test User, ou=Beijing, ou=Employees, ou=Enterprise, ou=Principal";
        }
    }, null, null));
    request.getSession().setAttribute(SPRING_SECURITY_CONTEXT_KEY, securityContext);
    view.exposeHelpers(velocityContext, request);
    assertThat(velocityContext.get(GoVelocityView.PRINCIPAL), is("Test User"));
}
Also used : LdapUserDetailsImpl(org.springframework.security.userdetails.ldap.LdapUserDetailsImpl) TestingAuthenticationToken(org.springframework.security.providers.TestingAuthenticationToken) Test(org.junit.Test)

Example 10 with TestingAuthenticationToken

use of org.springframework.security.providers.TestingAuthenticationToken in project gocd by gocd.

the class ReAuthenticationFilterTest method setupAuthentication.

private Authentication setupAuthentication() {
    GrantedAuthority[] authorities = {};
    Authentication authentication = new TestingAuthenticationToken(new User("user", "password", true, true, true, true, authorities), null, authorities);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    authentication.setAuthenticated(true);
    return authentication;
}
Also used : User(org.springframework.security.userdetails.User) Authentication(org.springframework.security.Authentication) GrantedAuthority(org.springframework.security.GrantedAuthority) TestingAuthenticationToken(org.springframework.security.providers.TestingAuthenticationToken)

Aggregations

TestingAuthenticationToken (org.springframework.security.providers.TestingAuthenticationToken)17 Test (org.junit.Test)14 GrantedAuthority (org.springframework.security.GrantedAuthority)10 GrantedAuthorityImpl (org.springframework.security.GrantedAuthorityImpl)9 Authentication (org.springframework.security.Authentication)4 User (org.springframework.security.userdetails.User)4 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)3 LdapUserDetailsImpl (org.springframework.security.userdetails.ldap.LdapUserDetailsImpl)3 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)2 Username (com.thoughtworks.go.server.domain.Username)1