org.springframework.security.test.context.TestSecurityContextHolder Java Examples
The following examples show how to use
org.springframework.security.test.context.TestSecurityContextHolder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AccountResourceIT.java From java-microservices-examples with Apache License 2.0 | 6 votes |
@Test @Transactional public void testGetExistingAccount() throws Exception { // create security-aware mockMvc restUserMockMvc = MockMvcBuilders .webAppContextSetup(context) .apply(springSecurity()) .build(); Map<String, Object> userDetails = new HashMap<>(); userDetails.put("sub", "test"); userDetails.put("email", "[email protected]"); Collection<GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority(AuthoritiesConstants.ADMIN)); OAuth2User user = new DefaultOAuth2User(authorities, userDetails, "sub"); OAuth2AuthenticationToken authentication = new OAuth2AuthenticationToken(user, authorities, "oidc"); TestSecurityContextHolder.getContext().setAuthentication(authentication); restUserMockMvc.perform(get("/api/account") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(jsonPath("$.login").value("test")) .andExpect(jsonPath("$.email").value("[email protected]")) .andExpect(jsonPath("$.authorities").value(AuthoritiesConstants.ADMIN)); }
Example #2
Source File: Oauth2AuthenticationManagerTest.java From ods-provisioning-app with Apache License 2.0 | 6 votes |
@Test public void givenNotSupportedAuthenticationWasConfigured_whenOAuth2ManagerResolvesGetUserName_thenException() { SecurityContext contextHolder = TestSecurityContextHolder.getContext(); Authentication authentication = new CustomAuthentication("string_as_principal"); contextHolder.setAuthentication(authentication); Oauth2AuthenticationManager manager = new Oauth2AuthenticationManager(); try { manager.getUserName(); fail(); } catch (Exception e) { assertTrue(e.getMessage().contains("Unsupported Principal object")); } }
Example #3
Source File: Oauth2AuthenticationManagerTest.java From ods-provisioning-app with Apache License 2.0 | 4 votes |
@Test public void givenUserLoggedInWithBasicAuth_whenOAuth2ManagerResolvesGetUserName_thenBasicAuthUsername() { SecurityContext contextHolder = TestSecurityContextHolder.getContext(); String username = "username"; when(crowdUserDetails.getUsername()).thenReturn(username); Authentication authentication = new CustomAuthentication(crowdUserDetails); contextHolder.setAuthentication(authentication); Oauth2AuthenticationManager manager = new Oauth2AuthenticationManager(); assertEquals(username, manager.getUserName()); }
Example #4
Source File: Oauth2AuthenticationManagerTest.java From ods-provisioning-app with Apache License 2.0 | 3 votes |
@Test public void givenUserLoggedInWithOAuth2_whenOAuth2ManagerResolvesGetUserName_thenReturnEmail() { SecurityContext contextHolder = TestSecurityContextHolder.getContext(); String email = "[email protected]"; when(defaultOidcUser.getEmail()).thenReturn(email); Authentication authentication = new CustomAuthentication(defaultOidcUser); contextHolder.setAuthentication(authentication); Oauth2AuthenticationManager manager = new Oauth2AuthenticationManager(); assertEquals(email, manager.getUserName()); }