use of org.springframework.security.core.userdetails.UserDetailsService in project pentaho-platform by pentaho.
the class PentahoPlatformExporterTest method testExportUsersAndRoles.
@Test
public void testExportUsersAndRoles() {
IUserRoleListService mockDao = mock(IUserRoleListService.class);
IAnyUserSettingService userSettingService = mock(IAnyUserSettingService.class);
UserDetailsService userDetailsService = mock(UserDetailsService.class);
PentahoSystem.registerObject(mockDao);
PentahoSystem.registerObject(userSettingService);
PentahoSystem.registerObject(userDetailsService);
IRoleAuthorizationPolicyRoleBindingDao roleBindingDao = mock(IRoleAuthorizationPolicyRoleBindingDao.class);
PentahoSystem.registerObject(roleBindingDao);
String tenantPath = "path";
when(session.getAttribute(IPentahoSession.TENANT_ID_KEY)).thenReturn(tenantPath);
List<String> userList = new ArrayList<>();
String user = "testUser";
String role = "testRole";
userList.add(user);
when(mockDao.getAllUsers(any(ITenant.class))).thenReturn(userList);
List<String> roleList = new ArrayList<>();
roleList.add(role);
when(mockDao.getAllRoles()).thenReturn(roleList);
Map<String, List<String>> map = new HashMap<>();
List<String> permissions = new ArrayList<>();
permissions.add("read");
map.put("testRole", permissions);
RoleBindingStruct struct = mock(RoleBindingStruct.class);
struct.bindingMap = map;
when(roleBindingDao.getRoleBindingStruct(nullable(String.class))).thenReturn(struct);
ArgumentCaptor<UserExport> userCaptor = ArgumentCaptor.forClass(UserExport.class);
ArgumentCaptor<RoleExport> roleCaptor = ArgumentCaptor.forClass(RoleExport.class);
ExportManifest manifest = mock(ExportManifest.class);
exporter.setExportManifest(manifest);
List<IUserSetting> settings = new ArrayList<>();
IUserSetting setting = mock(IUserSetting.class);
settings.add(setting);
when(userSettingService.getUserSettings(user)).thenReturn(settings);
when(userSettingService.getGlobalUserSettings()).thenReturn(settings);
List<GrantedAuthority> authList = new ArrayList<>();
UserDetails userDetails = new User("testUser", "testPassword", true, true, true, true, authList);
when(userDetailsService.loadUserByUsername(nullable(String.class))).thenReturn(userDetails);
exporter.exportUsersAndRoles();
verify(manifest).addUserExport(userCaptor.capture());
verify(manifest).addRoleExport(roleCaptor.capture());
verify(userSettingService).getGlobalUserSettings();
verify(manifest).addGlobalUserSetting(any(ExportManifestUserSetting.class));
assertEquals(settings.size(), userCaptor.getValue().getUserSettings().size());
UserExport userExport = userCaptor.getValue();
assertEquals("testUser", userExport.getUsername());
RoleExport roleExport = roleCaptor.getValue();
assertEquals("testRole", roleExport.getRolename());
}
use of org.springframework.security.core.userdetails.UserDetailsService in project pentaho-platform by pentaho.
the class ChainedUserDetailsServiceTest method testLoadUserByUsername.
@Test
public void testLoadUserByUsername() throws Exception {
final UserDetailsService mock1 = mock(UserDetailsService.class);
UserDetails joeDetails = mock(UserDetails.class);
UserDetails adminDetails = mock(UserDetails.class);
when(mock1.loadUserByUsername("joe")).thenReturn(joeDetails);
final UserDetailsService mock2 = mock(UserDetailsService.class);
when(mock1.loadUserByUsername("admin")).thenReturn(adminDetails);
ChainedUserDetailsService userDetailsService = new ChainedUserDetailsService(Arrays.asList(mock1, mock2));
final UserDetails joe = userDetailsService.loadUserByUsername("joe");
assertSame(joeDetails, joe);
final UserDetails admin = userDetailsService.loadUserByUsername("admin");
assertSame(adminDetails, admin);
}
use of org.springframework.security.core.userdetails.UserDetailsService in project spring-security-oauth by spring-projects.
the class DefaultTokenServicesTests method testRefreshAccessTokenWithReauthentication.
@Test
public void testRefreshAccessTokenWithReauthentication() {
UserDetails user = createMockUser("joeuser", "PROCESSOR");
UserDetailsService userDetailsService = Mockito.mock(UserDetailsService.class);
Mockito.when(tokenStore.readRefreshToken(Mockito.anyString())).thenReturn(new DefaultOAuth2RefreshToken("FOO"));
Mockito.when(tokenStore.readAuthenticationForRefreshToken(Mockito.any(OAuth2RefreshToken.class))).thenReturn(createMockOAuth2Authentication("myclient", user, "some more details"));
Mockito.when(userDetailsService.loadUserByUsername(Mockito.anyString())).thenReturn(user);
services.setSupportRefreshToken(true);
services.setAuthenticationManager(createAuthenticationManager(userDetailsService));
OAuth2AccessToken refreshedAccessToken = services.refreshAccessToken("FOO", createMockTokenRequest("myclient"));
ArgumentCaptor<OAuth2Authentication> refreshedAuthenticationCaptor = ArgumentCaptor.forClass(OAuth2Authentication.class);
Mockito.verify(tokenStore).storeAccessToken(Mockito.eq(refreshedAccessToken), refreshedAuthenticationCaptor.capture());
OAuth2Authentication refreshedAuthentication = refreshedAuthenticationCaptor.getValue();
Authentication authentication = refreshedAuthentication.getUserAuthentication();
Assert.assertEquals(user, authentication.getPrincipal());
Assert.assertEquals("some more details", authentication.getDetails());
}
use of org.springframework.security.core.userdetails.UserDetailsService in project spring-security-oauth by spring-projects.
the class DefaultUserAuthenticationConverterTests method shouldExtractAuthenticationWhenUserDetailsProvided.
@Test
public void shouldExtractAuthenticationWhenUserDetailsProvided() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put(UserAuthenticationConverter.USERNAME, "test_user");
UserDetailsService userDetailsService = Mockito.mock(UserDetailsService.class);
Mockito.when(userDetailsService.loadUserByUsername("test_user")).thenReturn(new User("foo", "bar", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_SPAM")));
converter.setUserDetailsService(userDetailsService);
Authentication authentication = converter.extractAuthentication(map);
assertEquals("ROLE_SPAM", authentication.getAuthorities().iterator().next().toString());
}
Aggregations