use of org.springframework.security.Authentication in project gocd by gocd.
the class AnonymousProcessingFilterTest method shouldGiveAnonymousUserRoleSupervisorAuthorityWhenSecurityIsOFFInCruiseConfig.
@Test
public void shouldGiveAnonymousUserRoleSupervisorAuthorityWhenSecurityIsOFFInCruiseConfig() throws Exception {
context.checking(new Expectations() {
{
allowing(goConfigService).isSecurityEnabled();
will(returnValue(false));
}
});
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
Authentication authentication = filter.createAuthentication(mockHttpServletRequest);
assertThat(authentication.getAuthorities().length, is(1));
final String role = authentication.getAuthorities()[0].getAuthority();
assertThat(role, is(GoAuthority.ROLE_SUPERVISOR.toString()));
assertTrue(authentication.getDetails() instanceof WebAuthenticationDetails);
}
use of org.springframework.security.Authentication in project gocd by gocd.
the class AnonymousProcessingFilterTest method shouldGiveAnonymousUserRoleAnonymousAuthorityWhenSecurityIsONInCruiseConfig.
@Test
public void shouldGiveAnonymousUserRoleAnonymousAuthorityWhenSecurityIsONInCruiseConfig() throws Exception {
context.checking(new Expectations() {
{
allowing(goConfigService).isSecurityEnabled();
will(returnValue(true));
}
});
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
Authentication authentication = filter.createAuthentication(mockHttpServletRequest);
assertThat(authentication.getAuthorities().length, is(1));
final String role = authentication.getAuthorities()[0].getAuthority();
assertThat(role, is(GoAuthority.ROLE_ANONYMOUS.toString()));
}
use of org.springframework.security.Authentication in project gocd by gocd.
the class PreAuthenticatedRequestsProcessingFilterTest method shouldAuthenticateUsersWithCredentials.
@Test
public void shouldAuthenticateUsersWithCredentials() throws IOException, ServletException {
PreAuthenticatedAuthenticationToken token = mock(PreAuthenticatedAuthenticationToken.class);
HashMap<String, String[]> params = new HashMap<>();
params.put("code", new String[] { "some_auth_code" });
SecurityAuthConfig githubAuthConfig = new SecurityAuthConfig("github", "github.oauth");
securityConfig.securityAuthConfigs().add(githubAuthConfig);
when(request.getRequestURI()).thenReturn("/go/plugin/github.oauth/authenticate");
when(request.getHeaderNames()).thenReturn(Collections.enumeration(Arrays.asList("Authorization")));
when(request.getHeader("Authorization")).thenReturn("qwe123");
when(request.getParameterMap()).thenReturn(params);
when(authorizationExtension.fetchAccessToken("github.oauth", Collections.singletonMap("Authorization", "qwe123"), Collections.singletonMap("code", "some_auth_code"), Collections.singletonList(githubAuthConfig))).thenReturn(Collections.singletonMap("access_token", "token"));
when(authenticationManager.authenticate(any(PreAuthenticatedAuthenticationToken.class))).thenReturn(token);
filter.setDefaultTargetUrl("/");
filter.doFilter(request, response, filterChain);
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
assertThat(authentication, is(token));
}
use of org.springframework.security.Authentication in project gocd by gocd.
the class UserEnabledCheckFilterTest method shouldAllowNormalChainingOfRequestIfUserEnabled.
@Test
public void shouldAllowNormalChainingOfRequestIfUserEnabled() throws IOException, ServletException {
SecurityContextHelper.setCurrentUser("winner");
Long userId = 1L;
User user = getUser("winner", userId);
Authentication actual = SecurityContextHolder.getContext().getAuthentication();
when(session.getAttribute(USERID_ATTR)).thenReturn(userId);
when(userService.load(userId)).thenReturn(user);
filter.doFilterHttp(req, res, chain);
assertThat(SecurityContextHolder.getContext().getAuthentication(), is(actual));
verify(chain).doFilter(req, res);
}
use of org.springframework.security.Authentication in project gocd by gocd.
the class UserEnabledCheckFilterTest method shouldSetUserIdInSession.
@Test
public void shouldSetUserIdInSession() throws IOException, ServletException {
SecurityContextHelper.setCurrentUser("winner");
Long userId = 1L;
User user = getUser("winner", userId);
Authentication actual = SecurityContextHolder.getContext().getAuthentication();
when(session.getAttribute(USERID_ATTR)).thenReturn(null);
when(userService.findUserByName(user.getName())).thenReturn(user);
filter.doFilterHttp(req, res, chain);
assertThat(SecurityContextHolder.getContext().getAuthentication(), is(actual));
verify(session).setAttribute(USERID_ATTR, userId);
verify(chain).doFilter(req, res);
}
Aggregations