Search in sources :

Example 26 with UserEntity

use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiSubscribersResourceTest method shouldGetApiSubscribersNotAsPrimaryOwner.

@Test
public void shouldGetApiSubscribersNotAsPrimaryOwner() {
    ApiEntity mockApi = new ApiEntity();
    mockApi.setId(API);
    UserEntity user = new UserEntity();
    user.setId("ANOTHER_NAME");
    PrimaryOwnerEntity primaryOwner = new PrimaryOwnerEntity(user);
    mockApi.setPrimaryOwner(primaryOwner);
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(any(), argThat(q -> singletonList(API).equals(q.getIds())));
    TopHitsAnalytics mockAnalytics = new TopHitsAnalytics();
    Map<String, Long> mockedValues = new HashMap<>();
    mockedValues.put("A", 10L);
    mockedValues.put("B", 20L);
    mockedValues.put("C", 30L);
    mockAnalytics.setValues(mockedValues);
    doReturn(mockAnalytics).when(analyticsService).execute(any(GroupByQuery.class));
    SubscriptionEntity subA1 = new SubscriptionEntity();
    subA1.setApplication("A");
    subA1.setApi(API);
    SubscriptionEntity subC1 = new SubscriptionEntity();
    subC1.setApplication("C");
    subC1.setApi(API);
    doReturn(Arrays.asList(subA1, subC1)).when(subscriptionService).search(any());
    ApplicationEntity appA = new ApplicationEntity();
    appA.setId("A");
    doReturn(appA).when(applicationService).findById("A");
    doReturn(new Application().id("A")).when(applicationMapper).convert(eq(appA), any());
    ApplicationEntity appC = new ApplicationEntity();
    appC.setId("C");
    doReturn(appC).when(applicationService).findById("C");
    doReturn(new Application().id("C")).when(applicationMapper).convert(eq(appC), any());
    ApplicationListItem appLIA = new ApplicationListItem();
    appLIA.setId("A");
    ApplicationListItem appLIC = new ApplicationListItem();
    appLIC.setId("C");
    doReturn(new HashSet<>(Arrays.asList(appLIA, appLIC))).when(applicationService).findByUser(USER_NAME);
    final Response response = target(API).path("subscribers").request().get();
    assertEquals(OK_200, response.getStatus());
    ArgumentCaptor<SubscriptionQuery> queryCaptor = ArgumentCaptor.forClass(SubscriptionQuery.class);
    Mockito.verify(subscriptionService).search(queryCaptor.capture());
    SubscriptionQuery value = queryCaptor.getValue();
    assertNotNull(value.getApplications());
    assertEquals(2, value.getApplications().size());
    assertTrue(value.getApplications().contains("A"));
    assertTrue(value.getApplications().contains("C"));
    final ApplicationsResponse applicationsResponse = response.readEntity(ApplicationsResponse.class);
    assertNotNull(applicationsResponse);
    assertEquals(2, applicationsResponse.getData().size());
    assertEquals("C", applicationsResponse.getData().get(0).getId());
    assertEquals("A", applicationsResponse.getData().get(1).getId());
}
Also used : java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) OK_200(io.gravitee.common.http.HttpStatusCode.OK_200) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Error(io.gravitee.rest.api.portal.rest.model.Error) Collections.singletonList(java.util.Collections.singletonList) Application(io.gravitee.rest.api.portal.rest.model.Application) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) ArgumentCaptor(org.mockito.ArgumentCaptor) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Collections.emptySet(java.util.Collections.emptySet) NOT_FOUND_404(io.gravitee.common.http.HttpStatusCode.NOT_FOUND_404) IOException(java.io.IOException) Test(org.junit.Test) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Mockito(org.mockito.Mockito) Response(javax.ws.rs.core.Response) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) Assert(org.junit.Assert) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) UserEntity(io.gravitee.rest.api.model.UserEntity) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) Application(io.gravitee.rest.api.portal.rest.model.Application) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) Test(org.junit.Test)

Example 27 with UserEntity

use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class UserResourceTest method shouldGetCurrentUserWithEmptyManagementConfig.

@Test
public void shouldGetCurrentUserWithEmptyManagementConfig() {
    when(userService.findByIdWithRoles(USER_NAME)).thenReturn(new UserEntity());
    when(permissionService.hasManagementRights(USER_NAME)).thenReturn(Boolean.TRUE);
    when(configService.getConsoleSettings()).thenReturn(new ConsoleSettingsEntity());
    final Response response = target().request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    ArgumentCaptor<String> userId = ArgumentCaptor.forClass(String.class);
    Mockito.verify(userService).findByIdWithRoles(userId.capture());
    assertEquals(USER_NAME, userId.getValue());
    User user = response.readEntity(User.class);
    assertNotNull(user);
    assertNull(user.getConfig());
    assertNotNull(user.getLinks());
}
Also used : Response(javax.ws.rs.core.Response) ConsoleSettingsEntity(io.gravitee.rest.api.model.settings.ConsoleSettingsEntity) User(io.gravitee.rest.api.portal.rest.model.User) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 28 with UserEntity

use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class UserResourceTest method shouldUpdateCurrentUser.

@Test
public void shouldUpdateCurrentUser() {
    UserInput userInput = new UserInput();
    final String newAvatar = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
    final String expectedAvatar = "data:image/gif;base64,R0lGODlhyADIAPAAAAAAAP///ywAAAAAyADIAEAC/4SPqcvtD6OctNqL" + "s968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5LL5jE6r1+y2+w2Py+" + "f0uv2Oz+v3/L7/DxgoOEhYaHiImKi4yNjo+AgZKTlJWWl5iZmpucnZ6fkJGio6SlpqeoqaqrrK2ur6ChsrO0tba3uLm6u7y9vr" + "+wscLDxMXGx8jJysvMzc7PwMHS09TV1tfY2drb3N3e39DR4uPk5ebn6Onq6+zt7u/g4fLz9PX29/j5+vv8/f7/8PMKDAgQQLGj" + "yIMKHChQwbOnwIMaLEiRQrWryIMaPGjQYcO3osUwAAOw==";
    userInput.setAvatar(newAvatar);
    userInput.setId(USER_NAME);
    when(userService.update(eq(USER_NAME), any())).thenReturn(new UserEntity());
    final Response response = target().request().put(Entity.json(userInput));
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    ArgumentCaptor<UpdateUserEntity> user = ArgumentCaptor.forClass(UpdateUserEntity.class);
    Mockito.verify(userService).update(eq(USER_NAME), user.capture());
    final UpdateUserEntity updateUserEntity = user.getValue();
    assertNotNull(updateUserEntity);
    assertEquals(expectedAvatar, updateUserEntity.getPicture());
    assertNull(updateUserEntity.getStatus());
    User updateUser = response.readEntity(User.class);
    assertNotNull(updateUser);
}
Also used : Response(javax.ws.rs.core.Response) User(io.gravitee.rest.api.portal.rest.model.User) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UserInput(io.gravitee.rest.api.portal.rest.model.UserInput) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 29 with UserEntity

use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class UserResourceTest method shouldGetCurrentUserWithManagementConfigWithoutUrl.

@Test
public void shouldGetCurrentUserWithManagementConfigWithoutUrl() {
    when(userService.findByIdWithRoles(USER_NAME)).thenReturn(new UserEntity());
    when(permissionService.hasManagementRights(USER_NAME)).thenReturn(Boolean.TRUE);
    ConsoleSettingsEntity consoleConfigEntity = new ConsoleSettingsEntity();
    when(configService.getConsoleSettings()).thenReturn(consoleConfigEntity);
    final Response response = target().request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    ArgumentCaptor<String> userId = ArgumentCaptor.forClass(String.class);
    Mockito.verify(userService).findByIdWithRoles(userId.capture());
    assertEquals(USER_NAME, userId.getValue());
    User user = response.readEntity(User.class);
    assertNotNull(user);
    assertNull(user.getConfig());
    assertNotNull(user.getLinks());
}
Also used : Response(javax.ws.rs.core.Response) ConsoleSettingsEntity(io.gravitee.rest.api.model.settings.ConsoleSettingsEntity) User(io.gravitee.rest.api.portal.rest.model.User) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 30 with UserEntity

use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class UserResourceTest method shouldGetUserAvatar.

@Test
public void shouldGetUserAvatar() throws IOException {
    doReturn(new UserEntity()).when(userService).findById(any());
    final Response response = target().path("avatar").request().get();
    assertEquals(OK_200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Aggregations

UserEntity (io.gravitee.rest.api.model.UserEntity)57 Test (org.junit.Test)36 User (io.gravitee.rest.api.portal.rest.model.User)14 Response (javax.ws.rs.core.Response)14 RoleEntity (io.gravitee.rest.api.model.RoleEntity)13 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)12 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)12 Date (java.util.Date)10 PrimaryOwnerEntity (io.gravitee.rest.api.model.PrimaryOwnerEntity)9 UserService (io.gravitee.rest.api.service.UserService)9 NewExternalUserEntity (io.gravitee.rest.api.model.NewExternalUserEntity)8 List (java.util.List)8 Instant (java.time.Instant)7 UserDetails (io.gravitee.rest.api.idp.api.authentication.UserDetails)6 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)6 MembershipService (io.gravitee.rest.api.service.MembershipService)6 HashMap (java.util.HashMap)6 Collectors (java.util.stream.Collectors)6 MembershipPayload (io.gravitee.cockpit.api.command.membership.MembershipPayload)5 MembershipReply (io.gravitee.cockpit.api.command.membership.MembershipReply)5