Search in sources :

Example 11 with UserImpl

use of org.graylog2.users.UserImpl in project graylog2-server by Graylog2.

the class LdapUserAuthenticatorTest method testSyncFromLdapEntry.

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void testSyncFromLdapEntry() {
    final LdapUserAuthenticator authenticator = spy(new LdapUserAuthenticator(ldapConnector, ldapSettingsService, userService, mock(RoleService.class), DateTimeZone.UTC));
    final LdapEntry userEntry = new LdapEntry();
    final LdapSettings ldapSettings = mock(LdapSettings.class);
    when(ldapSettings.getDisplayNameAttribute()).thenReturn("displayName");
    when(ldapSettings.getDefaultGroupId()).thenReturn("54e3deadbeefdeadbeef0001");
    when(ldapSettings.getAdditionalDefaultGroupIds()).thenReturn(Collections.emptySet());
    when(userService.create()).thenReturn(new UserImpl(null, new Permissions(Collections.emptySet()), Maps.newHashMap()));
    final User ldapUser = authenticator.syncFromLdapEntry(userEntry, ldapSettings, "user");
    assertThat(ldapUser).isNotNull();
    assertThat(ldapUser.isExternalUser()).isTrue();
    assertThat(ldapUser.getName()).isEqualTo("user");
    assertThat(ldapUser.getEmail()).isEqualTo("user@localhost");
    assertThat(ldapUser.getHashedPassword()).isEqualTo("User synced from LDAP.");
    assertThat(ldapUser.getTimeZone()).isEqualTo(DateTimeZone.UTC);
    assertThat(ldapUser.getRoleIds()).containsOnly("54e3deadbeefdeadbeef0001");
    assertThat(ldapUser.getPermissions()).isNotEmpty();
}
Also used : User(org.graylog2.plugin.database.users.User) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) LdapEntry(org.graylog2.shared.security.ldap.LdapEntry) LdapSettings(org.graylog2.shared.security.ldap.LdapSettings) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) Test(org.junit.Test)

Example 12 with UserImpl

use of org.graylog2.users.UserImpl in project graylog2-server by Graylog2.

the class NotificationFacadeTest method createNativeEntity.

@Test
public void createNativeEntity() {
    final EntityV1 entityV1 = createTestEntity();
    final JobDefinitionDto jobDefinitionDto = mock(JobDefinitionDto.class);
    when(jobDefinitionService.save(any(JobDefinitionDto.class))).thenReturn(jobDefinitionDto);
    final UserImpl kmerzUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "kmerz"));
    when(userService.load("kmerz")).thenReturn(kmerzUser);
    final NativeEntity<NotificationDto> nativeEntity = facade.createNativeEntity(entityV1, ImmutableMap.of(), ImmutableMap.of(), "kmerz");
    assertThat(nativeEntity).isNotNull();
    final NotificationDto notificationDto = nativeEntity.entity();
    assertThat(notificationDto.title()).isEqualTo("title");
    assertThat(notificationDto.description()).isEqualTo("descriptions");
    assertThat(notificationDto.config().type()).isEqualTo("http-notification-v1");
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) NotificationDto(org.graylog.events.notifications.NotificationDto) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) Test(org.junit.Test)

Example 13 with UserImpl

use of org.graylog2.users.UserImpl in project graylog2-server by Graylog2.

the class ViewsResourceTest method creatingViewAddsCurrentUserAsOwner.

@Test
public void creatingViewAddsCurrentUserAsOwner() throws Exception {
    final ViewDTO.Builder builder = mock(ViewDTO.Builder.class);
    when(view.toBuilder()).thenReturn(builder);
    when(view.type()).thenReturn(ViewDTO.Type.DASHBOARD);
    when(view.searchId()).thenReturn("6141d457d3a6b9d73c8ac55a");
    when(builder.owner(any())).thenReturn(builder);
    when(builder.build()).thenReturn(view);
    final UserImpl testUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "testuser"));
    final UserContext userContext = mock(UserContext.class);
    when(userContext.getUser()).thenReturn(testUser);
    when(userContext.getUserId()).thenReturn("testuser");
    when(currentUser.isLocalAdmin()).thenReturn(true);
    when(searchUser.username()).thenReturn("testuser");
    this.viewsResource.create(view, userContext, searchUser);
    final ArgumentCaptor<String> ownerCaptor = ArgumentCaptor.forClass(String.class);
    verify(builder, times(1)).owner(ownerCaptor.capture());
    assertThat(ownerCaptor.getValue()).isEqualTo("testuser");
}
Also used : ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) UserContext(org.graylog.security.UserContext) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) Test(org.junit.Test)

Example 14 with UserImpl

use of org.graylog2.users.UserImpl in project graylog2-server by Graylog2.

the class DashboardV1FacadeTest method setUp.

@Before
public void setUp() throws IOException {
    objectMapper.registerSubtypes(new NamedType(AggregationConfigDTO.class, AggregationConfigDTO.NAME));
    objectMapper.registerSubtypes(new NamedType(MessageListConfigDTO.class, MessageListConfigDTO.NAME));
    objectMapper.registerSubtypes(new NamedType(LineVisualizationConfigDTO.class, LineVisualizationConfigDTO.NAME));
    objectMapper.registerSubtypes(new NamedType(BarVisualizationConfigDTO.class, BarVisualizationConfigDTO.NAME));
    objectMapper.registerSubtypes(new NamedType(NumberVisualizationConfigDTO.class, NumberVisualizationConfigDTO.NAME));
    objectMapper.registerSubtypes(new NamedType(TimeHistogramConfigDTO.class, TimeHistogramConfigDTO.NAME));
    objectMapper.registerSubtypes(new NamedType(ValueConfigDTO.class, ValueConfigDTO.NAME));
    objectMapper.registerSubtypes(new NamedType(PivotSortConfig.class, PivotSortConfig.Type));
    objectMapper.registerSubtypes(new NamedType(PivotEntity.class, PivotEntity.NAME));
    objectMapper.registerSubtypes(new NamedType(PivotSort.class, PivotSort.Type));
    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));
    final MongoConnection mongoConnection = mongodb.mongoConnection();
    final MongoJackObjectMapperProvider mapper = new MongoJackObjectMapperProvider(objectMapper);
    searchDbService = new ViewFacadeTest.TestSearchDBService(mongoConnection, mapper);
    viewService = new ViewFacadeTest.TestViewService(mongoConnection, mapper, null);
    viewSummaryService = new ViewFacadeTest.TestViewSummaryService(mongoConnection, mapper);
    userService = mock(UserService.class);
    final UserImpl fakeUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "testuser"));
    when(userService.load("testuser")).thenReturn(fakeUser);
    final DashboardWidgetConverter dashboardWidgetConverter = new DashboardWidgetConverter();
    final EntityConverter entityConverter = new EntityConverter(dashboardWidgetConverter);
    facade = new DashboardV1Facade(objectMapper, searchDbService, entityConverter, viewService, viewSummaryService, userService);
    final URL resourceUrl = Resources.getResource(DashboardV1Facade.class, "content-pack-dashboard-v1.json");
    final ContentPack contentPack = objectMapper.readValue(resourceUrl, ContentPack.class);
    assertThat(contentPack).isInstanceOf(ContentPackV1.class);
    final ContentPackV1 contentPackV1 = (ContentPackV1) contentPack;
    final Entity entity = contentPackV1.entities().iterator().next();
    final StreamImpl stream = new StreamImpl(Collections.emptyMap());
    final Map<EntityDescriptor, Object> nativeEntities = new HashMap<>(1);
    nativeEntities.put(EntityDescriptor.create("58b3d55a-51ad-4b3e-865c-85776016a151", ModelTypes.STREAM_V1), stream);
    final NativeEntity<ViewDTO> nativeEntity = facade.createNativeEntity(entity, ImmutableMap.of(), nativeEntities, "testuser");
    assertThat(nativeEntity).isNotNull();
    viewDTO = nativeEntity.entity();
}
Also used : NumberVisualizationConfigDTO(org.graylog.plugins.views.search.views.widgets.aggregation.NumberVisualizationConfigDTO) EntityConverter(org.graylog2.contentpacks.facades.dashboardV1.EntityConverter) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) PivotEntity(org.graylog2.contentpacks.model.entities.PivotEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) HashMap(java.util.HashMap) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) TimeHistogramConfigDTO(org.graylog.plugins.views.search.views.widgets.aggregation.TimeHistogramConfigDTO) AutoIntervalDTO(org.graylog.plugins.views.search.views.widgets.aggregation.AutoIntervalDTO) PivotSortConfig(org.graylog.plugins.views.search.views.widgets.aggregation.sort.PivotSortConfig) StreamFilter(org.graylog.plugins.views.search.filter.StreamFilter) QueryStringFilter(org.graylog.plugins.views.search.filter.QueryStringFilter) DashboardV1Facade(org.graylog2.contentpacks.facades.dashboardV1.DashboardV1Facade) URL(java.net.URL) AggregationConfigDTO(org.graylog.plugins.views.search.views.widgets.aggregation.AggregationConfigDTO) ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) MessageListConfigDTO(org.graylog.plugins.views.search.views.widgets.messagelist.MessageListConfigDTO) ContentPackV1(org.graylog2.contentpacks.model.ContentPackV1) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) DashboardWidgetConverter(org.graylog2.contentpacks.facades.dashboardV1.DashboardWidgetConverter) BarVisualizationConfigDTO(org.graylog.plugins.views.search.views.widgets.aggregation.BarVisualizationConfigDTO) UserService(org.graylog2.shared.users.UserService) MongoJackObjectMapperProvider(org.graylog2.bindings.providers.MongoJackObjectMapperProvider) ContentPack(org.graylog2.contentpacks.model.ContentPack) OrFilter(org.graylog.plugins.views.search.filter.OrFilter) LineVisualizationConfigDTO(org.graylog.plugins.views.search.views.widgets.aggregation.LineVisualizationConfigDTO) PivotSort(org.graylog.plugins.views.search.searchtypes.pivot.PivotSort) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) StreamImpl(org.graylog2.streams.StreamImpl) ValueConfigDTO(org.graylog.plugins.views.search.views.widgets.aggregation.ValueConfigDTO) MongoConnection(org.graylog2.database.MongoConnection) PivotEntity(org.graylog2.contentpacks.model.entities.PivotEntity) Before(org.junit.Before)

Example 15 with UserImpl

use of org.graylog2.users.UserImpl in project graylog2-server by Graylog2.

the class UserServiceImplTest method testGetRoleNames.

@Test
public void testGetRoleNames() throws Exception {
    final UserImplFactory factory = new UserImplFactory(new Configuration(), permissions);
    final UserImpl user = factory.create(new HashMap<>());
    final Role role = createRole("Foo");
    final ImmutableMap<String, Role> map = ImmutableMap.<String, Role>builder().put(role.getId(), role).build();
    when(roleService.loadAllIdMap()).thenReturn(map);
    assertThat(userService.getRoleNames(user)).isEmpty();
    user.setRoleIds(Sets.newHashSet(role.getId()));
    assertThat(userService.getRoleNames(user)).containsOnly("Foo");
    when(roleService.loadAllIdMap()).thenReturn(new HashMap<>());
    assertThat(userService.getRoleNames(user)).isEmpty();
}
Also used : Role(org.graylog2.shared.users.Role) Configuration(org.graylog2.Configuration) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 Permissions (org.graylog2.shared.security.Permissions)13 UserImpl (org.graylog2.users.UserImpl)11 PasswordAlgorithmFactory (org.graylog2.security.PasswordAlgorithmFactory)8 User (org.graylog2.plugin.database.users.User)5 HashMap (java.util.HashMap)3 ViewDTO (org.graylog.plugins.views.search.views.ViewDTO)3 Role (org.graylog2.shared.users.Role)3 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)2 NotificationDto (org.graylog.events.notifications.NotificationDto)2 Configuration (org.graylog2.Configuration)2 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)2 MongoConnection (org.graylog2.database.MongoConnection)2 ValidationResult (org.graylog2.plugin.database.validators.ValidationResult)2 LdapEntry (org.graylog2.shared.security.ldap.LdapEntry)2 LdapSettings (org.graylog2.shared.security.ldap.LdapSettings)2 UserService (org.graylog2.shared.users.UserService)2 Before (org.junit.Before)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)1