-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUserProfileServiceTest.java
69 lines (57 loc) · 2.9 KB
/
UserProfileServiceTest.java
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
// SPDX-FileCopyrightText: 2023 iteratec GmbH
//
// SPDX-License-Identifier: Apache-2.0
package io.securecodebox.persistence.defectdojo.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.securecodebox.persistence.defectdojo.config.DefectDojoConfig;
import io.securecodebox.persistence.defectdojo.models.User;
import io.securecodebox.persistence.defectdojo.models.UserProfile;
import java.net.URISyntaxException;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.MockRestServiceServer;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
// This test is special because the defectdojo api does not return a list,
// but the generic code assumes every endpoint returns a list
public class UserProfileServiceTest {
DefectDojoConfig config;
UserProfileService underTest;
MockRestServiceServer mockServer;
// This string does not contain every field of the api response as those are not implemented
String apiResponse = """
{
"user": {
"id": 0,
"username": "GdqmXprK.j7R+OYE49SzL3mM2U6I0DyLRHnDg87i9It0AfP-kxvswW3qOI2i+31-@0",
"first_name": "string",
"last_name": "string",
"email": "user@example.com",
"last_login": "2022-11-01T16:20:19.373Z",
"is_active": true,
"is_superuser": true,
"configuration_permissions": [0]
}
}
""";
@BeforeEach
void setup() {
config = new DefectDojoConfig("https://defectdojo.example.com", "abc", "test-user", 42);
underTest = new UserProfileService(config);
mockServer = MockRestServiceServer.createServer(underTest.getRestTemplate());
}
@Test
void testSearch() throws JsonProcessingException, URISyntaxException {
var url = config.getUrl() + "/api/v2/" + underTest.getUrlPath() + "/?offset=0&limit=100";
mockServer.expect(requestTo(url)).andRespond(withSuccess(apiResponse, MediaType.APPLICATION_JSON));
var user = new User(0L, "GdqmXprK.j7R+OYE49SzL3mM2U6I0DyLRHnDg87i9It0AfP-kxvswW3qOI2i+31-@0", "string", "string");
var userProfile = new UserProfile(user);
var expected = Arrays.asList(userProfile);
var actual = underTest.search();
mockServer.verify();
assertIterableEquals(expected, actual);
}
}