aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/test/java/org/onap/portalng/bff/users/ListUsersIntegrationTest.java
blob: 27a8ecd6292f9bf494ed778431bce38ff5b25c49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 *
 * Copyright (c) 2022. Deutsche Telekom AG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 *
 */

package org.onap.portalng.bff.users;

import static org.assertj.core.api.Assertions.assertThat;

import com.github.tomakehurst.wiremock.client.WireMock;
import io.restassured.http.Header;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.onap.portalng.bff.BaseIntegrationTest;
import org.onap.portalng.bff.openapi.client_portal_keycloak.model.ErrorResponseKeycloakDto;
import org.onap.portalng.bff.openapi.client_portal_keycloak.model.RoleKeycloakDto;
import org.onap.portalng.bff.openapi.client_portal_keycloak.model.UserKeycloakDto;
import org.onap.portalng.bff.openapi.server.model.ProblemApiDto;
import org.onap.portalng.bff.openapi.server.model.UserListResponseApiDto;
import org.onap.portalng.bff.openapi.server.model.UserResponseApiDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

class ListUsersIntegrationTest extends BaseIntegrationTest {
  private final RoleKeycloakDto ONAP_ADMIN =
      new RoleKeycloakDto().id(randomUUID()).name("onap_admin");
  private final RoleKeycloakDto OFFLINE_ACCESS =
      new RoleKeycloakDto().id(randomUUID()).name("offline_access");

  @Test
  void listOfUsersWithDefaultPaginationCanBeProvided() throws Exception {
    final UserKeycloakDto tAdmin =
        new UserKeycloakDto()
            .id("8f05caaf-0e36-4bcd-b9b3-0ae3d531acc2")
            .username("t-admin")
            .email("t-admin@example.xyz")
            .firstName("FirstName4t-admin")
            .lastName("LastName4t-admin")
            .enabled(true);

    final UserKeycloakDto tDesigner =
        new UserKeycloakDto()
            .id("04ed5525-740d-42da-bc4c-2d3fcf955ee9")
            .username("t-designer")
            .email("t-designer@example.xyz")
            .firstName("FirstName4t-designer")
            .lastName("LastName4t-designer")
            .enabled(true);

    mockGetUserCount(2);
    mockListUsers(List.of(tAdmin, tDesigner), 0, 10);
    mockListRealmRoles(List.of(ONAP_ADMIN, OFFLINE_ACCESS));
    mockListRoleUsers(OFFLINE_ACCESS.getName(), List.of(tAdmin, tDesigner));
    mockListRoleUsers(ONAP_ADMIN.getName(), List.of(tAdmin));

    final UserResponseApiDto expectedTAdmin =
        new UserResponseApiDto()
            .id("8f05caaf-0e36-4bcd-b9b3-0ae3d531acc2")
            .username("t-admin")
            .email("t-admin@example.xyz")
            .firstName("FirstName4t-admin")
            .lastName("LastName4t-admin")
            .enabled(true)
            .addRealmRolesItem("onap_admin")
            .addRealmRolesItem("offline_access");
    final UserResponseApiDto expectedTDesigner =
        new UserResponseApiDto()
            .id("04ed5525-740d-42da-bc4c-2d3fcf955ee9")
            .username("t-designer")
            .email("t-designer@example.xyz")
            .firstName("FirstName4t-designer")
            .lastName("LastName4t-designer")
            .enabled(true)
            .addRealmRolesItem("offline_access");

    final UserListResponseApiDto response = listUsers();
    assertThat(response).isNotNull();
    assertThat(response.getTotalCount()).isEqualTo(2);
    assertThat(response.getItems().get(0).getRealmRoles())
        .containsExactlyInAnyOrder(
            expectedTAdmin.getRealmRoles().get(0), expectedTAdmin.getRealmRoles().get(1));
    assertThat(response.getItems().get(1).getRealmRoles())
        .containsExactly(expectedTDesigner.getRealmRoles().get(0));
  }

  @Test
  void listOfUsersWithSpecifiedPaginationCanBeProvided() throws Exception {
    final UserKeycloakDto keycloakUser =
        new UserKeycloakDto()
            .id("1")
            .username("user1")
            .email("user1@localhost")
            .firstName("User1")
            .lastName("Test")
            .enabled(true);

    mockGetUserCount(1);
    mockListUsers(List.of(keycloakUser), 60, 30);
    mockListRealmRoles(Collections.emptyList());

    final UserListResponseApiDto response = listUsers(Optional.of(3), Optional.of(30));
    assertThat(response).isNotNull();
    assertThat(response.getTotalCount()).isEqualTo(1);
    assertThat(response.getItems())
        .containsExactly(
            new UserResponseApiDto()
                .id("1")
                .username("user1")
                .enabled(true)
                .email("user1@localhost")
                .firstName("User1")
                .lastName("Test")
                .realmRoles(java.util.List.of()));
  }

  @Test
  void listOfUsersWithSpecifiedPaginationCanNotBeProvided() throws Exception {
    final ErrorResponseKeycloakDto keycloakErrorResponse =
        new ErrorResponseKeycloakDto().errorMessage("Some error message");

    mockGetUserCount(55);
    mockListUsersWithProblems(keycloakErrorResponse, 60, 30);
    mockListRealmRoles(Collections.emptyList());

    ProblemApiDto response =
        requestSpecification()
            .given()
            .accept(MediaType.APPLICATION_JSON_VALUE)
            .header(new Header("X-Request-Id", "addf6005-3075-4c80-b7bc-2c70b7d42b57"))
            .when()
            .get(adjustPath("/users", Optional.of(3), Optional.of(30)))
            .then()
            .statusCode(HttpStatus.BAD_GATEWAY.value())
            .extract()
            .body()
            .as(ProblemApiDto.class);

    assertThat(response).isNotNull();
    assertThat(response.getTitle()).isEqualTo(HttpStatus.BAD_REQUEST.toString());
    assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_GATEWAY.value());
    assertThat(response.getDownstreamSystem())
        .isEqualTo(ProblemApiDto.DownstreamSystemEnum.KEYCLOAK);
  }

  protected void mockGetUserCount(Integer userCount) {
    WireMock.stubFor(
        WireMock.get(
                WireMock.urlMatching(String.format("/auth/admin/realms/%s/users/count", realm)))
            .willReturn(
                WireMock.aResponse()
                    .withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
                    .withBody(userCount.toString())));
  }

  protected void mockListUsers(List<UserKeycloakDto> keycloakUsers, Integer first, Integer max)
      throws Exception {
    WireMock.stubFor(
        WireMock.get(
                WireMock.urlMatching(
                    String.format(
                        "/auth/admin/realms/%s/users\\?first=%s&max=%s", realm, first, max)))
            .willReturn(
                WireMock.aResponse()
                    .withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
                    .withBody(objectMapper.writeValueAsString(keycloakUsers))));
  }

  protected void mockListRealmRoles(List<RoleKeycloakDto> roles) throws Exception {
    WireMock.stubFor(
        WireMock.get(WireMock.urlMatching(String.format("/auth/admin/realms/%s/roles", realm)))
            .willReturn(
                WireMock.aResponse()
                    .withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
                    .withBody(objectMapper.writeValueAsString(roles))));
  }

  protected void mockListRoleUsers(String roleName, List<UserKeycloakDto> response)
      throws Exception {
    WireMock.stubFor(
        WireMock.get(
                WireMock.urlMatching(
                    String.format("/auth/admin/realms/%s/roles/%s/users", realm, roleName)))
            .willReturn(
                WireMock.aResponse()
                    .withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
                    .withBody(objectMapper.writeValueAsString(response))));
  }

  protected UserListResponseApiDto listUsers() {
    return listUsers(Optional.empty(), Optional.empty());
  }

  protected UserListResponseApiDto listUsers(Optional<Integer> page, Optional<Integer> pageSize) {
    return requestSpecification()
        .given()
        .accept(MediaType.APPLICATION_JSON_VALUE)
        .header(new Header("X-Request-Id", "addf6005-3075-4c80-b7bc-2c70b7d42b57"))
        .when()
        .get(adjustPath("/users", page, pageSize))
        .then()
        .statusCode(HttpStatus.OK.value())
        .extract()
        .body()
        .as(UserListResponseApiDto.class);
  }

  protected void mockListUsersWithProblems(
      ErrorResponseKeycloakDto keycloakErrorResponse, Integer first, Integer max) throws Exception {
    WireMock.stubFor(
        WireMock.get(
                WireMock.urlMatching(
                    String.format(
                        "/auth/admin/realms/%s/users\\?first=%s&max=%s", realm, first, max)))
            .willReturn(
                WireMock.aResponse()
                    .withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
                    .withStatus(400)
                    .withBody(objectMapper.writeValueAsString(keycloakErrorResponse))));
  }
}