use of org.graylog2.shared.users.UserService in project graylog2-server by Graylog2.
the class UserContextTest method runAs.
@Test
void runAs() {
// Simulate what we do in the DefaultSecurityManagerProvider
DefaultSecurityManager sm = new DefaultSecurityManager();
SecurityUtils.setSecurityManager(sm);
final DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
final DefaultSessionStorageEvaluator sessionStorageEvaluator = new DefaultSessionStorageEvaluator() {
@Override
public boolean isSessionStorageEnabled(Subject subject) {
// save to session if we already have a session. do not create on just for saving the subject
return subject.getSession(false) != null;
}
};
sessionStorageEvaluator.setSessionStorageEnabled(false);
subjectDAO.setSessionStorageEvaluator(sessionStorageEvaluator);
sm.setSubjectDAO(subjectDAO);
final User user = new UserImpl(mock(PasswordAlgorithmFactory.class), mock(Permissions.class), ImmutableMap.of());
when(userService.load(anyString())).thenReturn(user);
when(userService.loadById(anyString())).thenReturn(user);
final String USERID = "123456";
UserContext.<Void>runAs(USERID, () -> {
final UserContext userContext = new UserContext.Factory(userService).create();
assertThat(userContext.getUserId()).isEqualTo(USERID);
assertThat(userContext.getUser()).isEqualTo(user);
return null;
});
}
use of org.graylog2.shared.users.UserService in project graylog2-server by Graylog2.
the class ViewFacadeTest method setUp.
@Before
public void setUp() {
objectMapper.registerSubtypes(new NamedType(AggregationConfigDTO.class, AggregationConfigDTO.NAME));
objectMapper.registerSubtypes(new NamedType(MessageListConfigDTO.class, MessageListConfigDTO.NAME));
objectMapper.registerSubtypes(new NamedType(TimeHistogramConfigDTO.class, TimeHistogramConfigDTO.NAME));
objectMapper.registerSubtypes(new NamedType(OrFilter.class, OrFilter.NAME));
objectMapper.registerSubtypes(new NamedType(StreamFilter.class, StreamFilter.NAME));
objectMapper.registerSubtypes(new NamedType(QueryStringFilter.class, QueryStringFilter.NAME));
objectMapper.registerSubtypes(new NamedType(AutoIntervalDTO.class, AutoIntervalDTO.type));
objectMapper.registerSubtypes(MessageListEntity.class);
objectMapper.registerSubtypes(PivotEntity.class);
objectMapper.registerSubtypes(EventListEntity.class);
objectMapper.registerSubtypes(MessageList.class);
objectMapper.registerSubtypes(Pivot.class);
objectMapper.registerSubtypes(EventList.class);
final MongoConnection mongoConnection = mongodb.mongoConnection();
final MongoJackObjectMapperProvider mapper = new MongoJackObjectMapperProvider(objectMapper);
searchDbService = new TestSearchDBService(mongoConnection, mapper);
viewService = new TestViewService(mongoConnection, mapper, null);
viewSummaryService = new TestViewSummaryService(mongoConnection, mapper);
userService = mock(UserService.class);
facade = new SearchFacade(objectMapper, searchDbService, viewService, viewSummaryService, userService);
}
use of org.graylog2.shared.users.UserService in project graylog2-server by Graylog2.
the class ProvisionerServiceTest method testFullNameOnlySuccess.
@Test
public void testFullNameOnlySuccess() throws ValidationException {
when(authServiceBackend.backendId()).thenReturn(BACKEND_ID);
when(authServiceBackend.backendType()).thenReturn(BACKEND_TYPE);
final UserDetails.Builder detailsBuilder = provisionerService.newDetails(authServiceBackend);
assertNotNull(detailsBuilder);
detailsBuilder.fullName(FULL_NAME).base64AuthServiceUid("id").username(USERNAME).accountIsEnabled(true).email(EMAIL).defaultRoles(Collections.emptySet());
final UserDetails userDetails = detailsBuilder.build();
assertEquals(BACKEND_ID, userDetails.authServiceId());
assertEquals(BACKEND_TYPE, userDetails.authServiceType());
final User user = mock(User.class);
when(userService.create()).thenReturn(user);
when(userService.save(isA(User.class))).thenReturn(USER_ID);
provisionerService.provision(userDetails);
verify(userService, times(1)).save(isA(User.class));
verify(user, times(1)).setFullName(FULL_NAME);
}
use of org.graylog2.shared.users.UserService in project graylog2-server by Graylog2.
the class MigrationHelpersTest method ensureUserWithoutExpectedRoles.
@Test
public void ensureUserWithoutExpectedRoles() throws Exception {
final Permissions permissions = new Permissions(ImmutableSet.of());
final User existingUser = newUser(permissions);
existingUser.setName("test-user");
existingUser.setFirstLastFullNames("Test", "User");
existingUser.setPassword("password");
existingUser.setEmail("test@example.com");
existingUser.setTimeZone(DateTimeZone.UTC);
// Set invalid role IDs so the use gets updated
existingUser.setRoleIds(ImmutableSet.of());
when(userService.load("test-user")).thenReturn(existingUser);
when(userService.save(any(User.class))).thenReturn("new-id");
assertThat(migrationHelpers.ensureUser("test-user", "pass", "Test", "User", "test@example.com", ImmutableSet.of("54e3deadbeefdeadbeef0001", "54e3deadbeefdeadbeef0002"))).isEqualTo("new-id");
final ArgumentCaptor<User> userArg = ArgumentCaptor.forClass(User.class);
verify(userService, times(1)).save(userArg.capture());
assertThat(userArg.getValue()).satisfies(user -> {
assertThat(user.getName()).describedAs("user name").isEqualTo("test-user");
assertThat(user.getFullName()).describedAs("user full-name").isEqualTo("Test User");
assertThat(user.getHashedPassword()).describedAs("user hashed password").isNotBlank();
assertThat(user.getEmail()).describedAs("user email").isEqualTo("test@example.com");
assertThat(user.isReadOnly()).describedAs("user is read-only").isFalse();
assertThat(user.getPermissions()).describedAs("user permissions").containsOnlyElementsOf(permissions.userSelfEditPermissions("test-user"));
assertThat(user.getRoleIds()).describedAs("user roles").containsOnly("54e3deadbeefdeadbeef0001", "54e3deadbeefdeadbeef0002");
assertThat(user.getTimeZone()).describedAs("user timezone").isEqualTo(DateTimeZone.UTC);
});
}
use of org.graylog2.shared.users.UserService in project graylog2-server by Graylog2.
the class RolesToGrantsMigrationTest method setUp.
@BeforeEach
void setUp(MongoDBTestService mongodb, MongoJackObjectMapperProvider mongoJackObjectMapperProvider, GRNRegistry grnRegistry, TestUserService userService) {
when(permissions.readerBasePermissions()).thenReturn(ImmutableSet.of());
when(validator.validate(any())).thenReturn(ImmutableSet.of());
this.grnRegistry = grnRegistry;
roleService = new RoleServiceImpl(mongodb.mongoConnection(), mongoJackObjectMapperProvider, permissions, validator);
dbGrantService = new DBGrantService(mongodb.mongoConnection(), mongoJackObjectMapperProvider, grnRegistry);
this.userService = userService;
DBGrantService dbGrantService = new DBGrantService(mongodb.mongoConnection(), mongoJackObjectMapperProvider, grnRegistry);
migration = new RolesToGrantsMigration(roleService, userService, dbGrantService, grnRegistry, "admin");
}
Aggregations