Revize fbce8e75
Přidáno uživatelem Václav Hrabík před téměř 2 roky(ů)
pom.xml | ||
---|---|---|
140 | 140 |
<scope>test</scope> |
141 | 141 |
</dependency> |
142 | 142 |
|
143 |
<dependency> |
|
144 |
<groupId>org.mockito</groupId> |
|
145 |
<artifactId>mockito-inline</artifactId> |
|
146 |
<version>3.4.6</version> |
|
147 |
<scope>test</scope> |
|
148 |
</dependency> |
|
149 |
|
|
143 | 150 |
<!-- json library with minimal overhead --> |
144 | 151 |
<dependency> |
145 | 152 |
<groupId>com.googlecode.json-simple</groupId> |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthServiceImpl.java | ||
---|---|---|
46 | 46 |
|
47 | 47 |
private String AUTH_URL_REFRESH = "http://localhost:8081/refresh"; |
48 | 48 |
|
49 |
@Autowired |
|
50 |
private RequestBuilder requestBuilder; |
|
49 | 51 |
/** |
50 | 52 |
* |
51 | 53 |
*/ |
... | ... | |
57 | 59 |
|
58 | 60 |
public ResponseEntity<String> authenticate(String token) { |
59 | 61 |
|
60 |
return RequestBuilder.sendRequestResponse(AUTH_URL_AUTH, token);
|
|
62 |
return requestBuilder.sendRequestResponse(AUTH_URL_AUTH, token);
|
|
61 | 63 |
} |
62 | 64 |
|
63 | 65 |
public ResponseEntity<String> loginUser(User user) { |
... | ... | |
71 | 73 |
|
72 | 74 |
requestBody.put("name", userName); |
73 | 75 |
|
74 |
return RequestBuilder.sendRequestResponse(AUTH_URL_LOGIN, requestBody);
|
|
76 |
return requestBuilder.sendRequestResponse(AUTH_URL_LOGIN, requestBody);
|
|
75 | 77 |
} |
76 | 78 |
|
77 | 79 |
public ResponseEntity<String> logoutUser(User user) { |
... | ... | |
88 | 90 |
requestBody.put("name", userName); |
89 | 91 |
requestBody.put("token", token); |
90 | 92 |
|
91 |
return RequestBuilder.sendRequestResponse(AUTH_URL_LOGOUT, requestBody);
|
|
93 |
return requestBuilder.sendRequestResponse(AUTH_URL_LOGOUT, requestBody);
|
|
92 | 94 |
} |
93 | 95 |
|
94 | 96 |
@Override |
95 | 97 |
public ResponseEntity<String> refreshToken(String token) { |
96 |
return RequestBuilder.sendRequestResponse(AUTH_URL_REFRESH, token, true);
|
|
98 |
return requestBuilder.sendRequestResponse(AUTH_URL_REFRESH, token, true);
|
|
97 | 99 |
} |
98 | 100 |
|
99 | 101 |
@Override |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/RequestBuilder.java | ||
---|---|---|
3 | 3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.httpErrorHandler.CustomErrorHandler; |
4 | 4 |
import org.json.simple.JSONObject; |
5 | 5 |
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
6 |
import org.springframework.stereotype.Component; |
|
6 | 7 |
import org.springframework.web.client.HttpClientErrorException; |
7 | 8 |
import org.springframework.web.client.HttpServerErrorException; |
8 | 9 |
import org.springframework.web.client.HttpStatusCodeException; |
... | ... | |
17 | 18 |
|
18 | 19 |
import javax.servlet.http.HttpServletRequest; |
19 | 20 |
|
21 |
@Component |
|
20 | 22 |
public class RequestBuilder { |
21 | 23 |
private static Logger logger = Logger.getLogger(RequestBuilder.class.getName()); |
22 | 24 |
|
23 |
|
|
24 |
|
|
25 |
public static ResponseEntity<String> sendRequestResponse(String url, Map<String, Object> body) { |
|
25 |
public ResponseEntity<String> sendRequestResponse(String url, Map<String, Object> body) { |
|
26 | 26 |
RestTemplate restTemplate = new RestTemplate(); |
27 | 27 |
restTemplate.setErrorHandler(new CustomErrorHandler()); |
28 | 28 |
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); |
... | ... | |
35 | 35 |
return restTemplate.postForEntity(url, entity, String.class); |
36 | 36 |
} |
37 | 37 |
|
38 |
public static ResponseEntity<String> sendRequestResponse(String url, String token) {
|
|
38 |
public ResponseEntity<String> sendRequestResponse(String url, String token) { |
|
39 | 39 |
RestTemplate restTemplate = new RestTemplate(); |
40 | 40 |
//custom error handler to handle http response |
41 | 41 |
restTemplate.setErrorHandler(new CustomErrorHandler()); |
... | ... | |
52 | 52 |
|
53 | 53 |
} |
54 | 54 |
|
55 |
public static ResponseEntity<String> sendRequestResponse(String url, String token, boolean get) {
|
|
55 |
public ResponseEntity<String> sendRequestResponse(String url, String token, boolean get) { |
|
56 | 56 |
RestTemplate restTemplate = new RestTemplate(); |
57 | 57 |
restTemplate.setErrorHandler(new CustomErrorHandler()); |
58 | 58 |
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); |
... | ... | |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
|
71 |
public static ResponseEntity<String> sendRequestResponse(String url, Map<String, Object> body, String token) {
|
|
71 |
public ResponseEntity<String> sendRequestResponse(String url, Map<String, Object> body, String token) { |
|
72 | 72 |
RestTemplate restTemplate = new RestTemplate(); |
73 | 73 |
restTemplate.setErrorHandler(new CustomErrorHandler()); |
74 | 74 |
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); |
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/BaseTest.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
4 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserRepository; |
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
|
6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.RequestBuilder; |
|
5 | 7 |
import io.restassured.module.mockmvc.RestAssuredMockMvc; |
6 | 8 |
import org.junit.jupiter.api.BeforeEach; |
7 | 9 |
import org.springframework.beans.factory.annotation.Autowired; |
8 | 10 |
import org.springframework.boot.test.context.SpringBootTest; |
9 | 11 |
import org.springframework.boot.test.mock.mockito.MockBean; |
12 |
import org.springframework.http.ResponseEntity; |
|
10 | 13 |
import org.springframework.web.context.WebApplicationContext; |
11 | 14 |
|
12 |
import static org.mockito.ArgumentMatchers.any; |
|
15 |
import java.util.HashMap; |
|
16 |
import java.util.Map; |
|
17 |
|
|
18 |
import static org.mockito.ArgumentMatchers.*; |
|
13 | 19 |
import static org.mockito.Mockito.when; |
14 | 20 |
|
15 | 21 |
@SpringBootTest() |
... | ... | |
21 | 27 |
@MockBean |
22 | 28 |
private UserRepository userRepository; |
23 | 29 |
|
30 |
@MockBean |
|
31 |
private RequestBuilder requestBuilder; |
|
32 |
|
|
24 | 33 |
@BeforeEach |
25 | 34 |
public void init() { |
26 |
User user = new User("foo", "foo@foo.foo", "foo"); |
|
35 |
User user = new User("foo", "foo@foo.foo", "76D3BC41C9F588F7FCD0D5BF4718F8F84B1C41B20882703100B9EB9413807C01"); |
|
36 |
User userWrongPassword = new User("foo", "foo@foo.foo", "foo"); |
|
37 |
ResponseEntity<String> loginResponse = ResponseEntity.ok("token"); |
|
27 | 38 |
|
28 |
when(userRepository.findByName("non-existing")).thenReturn(null); |
|
29 |
when(userRepository.findByName("foo")).thenReturn(user); |
|
39 |
|
|
40 |
/* Registration of user */ |
|
41 |
when(userRepository.findByName("new_user")).thenReturn(null); |
|
42 |
when(userRepository.findByName("existing")).thenReturn(user); |
|
30 | 43 |
when(userRepository.save(any(User.class))).thenReturn(user); |
44 |
|
|
45 |
/* Login of user */ |
|
46 |
when(userRepository.findByName("existing")).thenReturn(user); |
|
47 |
when(requestBuilder.sendRequestResponse(eq("http://localhost:8081/login"), anyMap())).thenReturn(loginResponse); |
|
48 |
when(userRepository.findByName("non_existing")).thenReturn(null); |
|
49 |
when(userRepository.findByName("foo")).thenReturn(userWrongPassword); |
|
50 |
|
|
51 |
/* Filter mock */ |
|
52 |
Map<String,Object> filterJson = new HashMap<>(); |
|
53 |
filterJson.put("message", "User authorized"); |
|
54 |
String filterBody = JSONBuilder.buildJSON(filterJson); |
|
55 |
ResponseEntity<String> oauth = ResponseEntity.ok(filterBody); |
|
56 |
when(requestBuilder.sendRequestResponse(anyString(), eq("token"))).thenReturn(oauth); |
|
57 |
|
|
58 |
Map<String,Object> filterJson2 = new HashMap<>(); |
|
59 |
filterJson2.put("message", "User unauthorized"); |
|
60 |
String filterBody2 = JSONBuilder.buildJSON(filterJson2); |
|
61 |
ResponseEntity<String> oauth2 = ResponseEntity.status(500).body(filterBody2); |
|
62 |
when(requestBuilder.sendRequestResponse(anyString(), eq("wrong"))).thenReturn(oauth2); |
|
63 |
|
|
64 |
/* Logout of user */ |
|
65 |
Map<String,Object> logoutJson = new HashMap<>(); |
|
66 |
logoutJson.put("message", "User logged out"); |
|
67 |
String logoutBody = JSONBuilder.buildJSON(logoutJson); |
|
68 |
ResponseEntity<String> logoutResponse = ResponseEntity.ok(logoutBody); |
|
69 |
when(requestBuilder.sendRequestResponse(eq("http://localhost:8081/logout"), anyMap())).thenReturn(logoutResponse); |
|
70 |
|
|
31 | 71 |
RestAssuredMockMvc.webAppContextSetup(webApplicationContext); |
32 | 72 |
} |
33 | 73 |
|
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthServiceImplTest.java | ||
---|---|---|
1 |
//package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service;
|
|
2 |
// |
|
3 |
//import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes;
|
|
4 |
//import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User;
|
|
5 |
//import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder;
|
|
6 |
//import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.RequestBuilder;
|
|
7 |
//import org.junit.Test;
|
|
8 |
//import org.junit.runner.RunWith;
|
|
9 |
//import org.mockito.MockedStatic;
|
|
10 |
//import org.mockito.Mockito;
|
|
11 |
//import org.springframework.beans.factory.annotation.Autowired;
|
|
12 |
//import org.springframework.boot.test.context.SpringBootTest;
|
|
13 |
//import org.springframework.boot.test.mock.mockito.MockBean;
|
|
14 |
//import org.springframework.http.HttpStatus; |
|
15 |
//import org.springframework.http.ResponseEntity;
|
|
16 |
//import org.springframework.test.context.junit4.SpringRunner;
|
|
17 |
//import java.util.HashMap;
|
|
18 |
//import java.util.Map; |
|
19 |
//
|
|
20 |
//import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
21 |
//import static org.mockito.ArgumentMatchers.*; |
|
22 |
//import static org.mockito.Mockito.mock;
|
|
23 |
//import static org.mockito.Mockito.when;
|
|
24 |
// |
|
25 |
//@RunWith(SpringRunner.class)
|
|
26 |
//@SpringBootTest
|
|
27 |
//public class OAuthServiceImplTest { |
|
28 |
//
|
|
29 |
// @Autowired
|
|
30 |
// private OAuthService oAuthService;
|
|
31 |
//
|
|
32 |
// /**
|
|
33 |
// * Mocked User
|
|
34 |
// */
|
|
35 |
// private final User mockUser = new User("foo", "foo@foo.cz", "foo");
|
|
36 |
// private final String sampleToken = "eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIyMGU4NmJiMC1lMDU4LTQwYTMtYjQyZC02ZTBjOWIyMmQ5MWQiL" +
|
|
37 |
// "CJzdWIiOiJmb28iLCJpYXQiOjE2ODE0MTY2ODAsImV4cCI6MTY4MTQxNjk4MH0.YJTwPEI4njQqYRuLGilf_oVl0gGD5BWFmdolk1O" +
|
|
38 |
// "vYWIgTcoIAFwh6bwqrW7XM6Hlj-ItYj9EmihYIqN5gfJVtg";
|
|
39 |
// // valid user logged in - http response with code 200 and token in body is expected |
|
40 |
// @Test
|
|
41 |
// public void loginValidUser(){
|
|
42 |
// ResponseEntity<String> response = ResponseEntity.ok().body(sampleToken);
|
|
43 |
// try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
44 |
// requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyMap())).thenReturn(response);
|
|
45 |
// ResponseEntity<String> response1 = oAuthService.loginUser(mockUser); |
|
46 |
//
|
|
47 |
// assertEquals(response, response1);
|
|
48 |
// }
|
|
49 |
// }
|
|
50 |
// |
|
51 |
// // return 200 response if valid user is trying to authenticate
|
|
52 |
// @Test
|
|
53 |
// public void authenticateValidUser(){
|
|
54 |
// ResponseEntity<String> expectedResponse = ResponseEntity.ok().body(mockUser.getName()); |
|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User;
|
|
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder;
|
|
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.RequestBuilder;
|
|
6 |
import org.junit.jupiter.api.Test;
|
|
7 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
8 |
import org.springframework.boot.test.context.SpringBootTest;
|
|
9 |
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
10 |
import org.springframework.http.HttpStatus;
|
|
11 |
import org.springframework.http.ResponseEntity;
|
|
12 |
import java.util.HashMap;
|
|
13 |
import java.util.Map;
|
|
14 |
|
|
15 |
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
16 |
import static org.mockito.ArgumentMatchers.*;
|
|
17 |
import static org.mockito.Mockito.when;
|
|
18 |
|
|
19 |
@SpringBootTest
|
|
20 |
public class OAuthServiceImplTest {
|
|
21 |
|
|
22 |
@Autowired
|
|
23 |
private OAuthService oAuthService;
|
|
24 |
|
|
25 |
@MockBean
|
|
26 |
private RequestBuilder requestBuilder;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* Mocked User
|
|
30 |
*/
|
|
31 |
private final User mockUser = new User("foo", "foo@foo.cz", "foo");
|
|
32 |
private final String sampleToken = "eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIyMGU4NmJiMC1lMDU4LTQwYTMtYjQyZC02ZTBjOWIyMmQ5MWQiL" +
|
|
33 |
"CJzdWIiOiJmb28iLCJpYXQiOjE2ODE0MTY2ODAsImV4cCI6MTY4MTQxNjk4MH0.YJTwPEI4njQqYRuLGilf_oVl0gGD5BWFmdolk1O" +
|
|
34 |
"vYWIgTcoIAFwh6bwqrW7XM6Hlj-ItYj9EmihYIqN5gfJVtg";
|
|
35 |
// valid user logged in - http response with code 200 and token in body is expected
|
|
36 |
@Test
|
|
37 |
public void loginValidUser() {
|
|
38 |
ResponseEntity<String> response = ResponseEntity.ok().body(sampleToken);
|
|
39 |
|
|
40 |
when(requestBuilder.sendRequestResponse(anyString(), anyMap())).thenReturn(response);
|
|
41 |
ResponseEntity<String> response1 = oAuthService.loginUser(mockUser);
|
|
42 |
assertEquals(response, response1);
|
|
43 |
|
|
44 |
}
|
|
45 |
|
|
46 |
// return 200 response if valid user is trying to authenticate
|
|
47 |
@Test
|
|
48 |
public void authenticateValidUser() {
|
|
49 |
ResponseEntity<String> expectedResponse = ResponseEntity.ok().body(mockUser.getName());
|
|
50 |
|
|
51 |
when(requestBuilder.sendRequestResponse(anyString(), anyString())).thenReturn(expectedResponse);
|
|
52 |
ResponseEntity<String> response1 = oAuthService.authenticate(sampleToken);
|
|
53 |
assertEquals(expectedResponse, response1);
|
|
54 |
|
|
55 | 55 |
// try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
56 | 56 |
// requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyString())).thenReturn(expectedResponse); |
57 | 57 |
// ResponseEntity<String> response = oAuthService.authenticate(sampleToken); |
58 | 58 |
// assertEquals(expectedResponse, response); |
59 | 59 |
// } |
60 |
// |
|
61 |
// } |
|
62 |
// |
|
63 |
// // return 401 if unauthorized user is trying to authenticate |
|
64 |
// @Test |
|
65 |
// public void authenticateInvalidUser(){ |
|
66 |
// ResponseEntity<String> expectedResponse = ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(mockUser.getName()); |
|
60 |
|
|
61 |
} |
|
62 |
|
|
63 |
// return 401 if unauthorized user is trying to authenticate |
|
64 |
@Test |
|
65 |
public void authenticateInvalidUser() { |
|
66 |
ResponseEntity<String> expectedResponse = ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(mockUser.getName()); |
|
67 |
|
|
68 |
when(requestBuilder.sendRequestResponse(anyString(), anyString())).thenReturn(expectedResponse); |
|
69 |
ResponseEntity<String> response1 = oAuthService.authenticate(sampleToken); |
|
70 |
assertEquals(expectedResponse, response1); |
|
71 |
|
|
67 | 72 |
// try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
68 | 73 |
// requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyString())).thenReturn(expectedResponse); |
69 | 74 |
// ResponseEntity<String> response = oAuthService.authenticate(sampleToken); |
70 | 75 |
// assertEquals(expectedResponse, response); |
71 | 76 |
// } |
72 |
// |
|
73 |
// } |
|
74 |
// @Test |
|
75 |
// public void logoutUser(){ |
|
76 |
// Map<String,Object> json = new HashMap<>(); |
|
77 |
// json.put("message","ok"); |
|
78 |
// //have to set token to the user - in this scenario we assume user is logged in |
|
79 |
// mockUser.setToken(sampleToken); |
|
80 |
// String jsonString = JSONBuilder.buildJSON(json); |
|
81 |
// ResponseEntity<String> expectedResponse = ResponseEntity.status(HttpStatus.OK).body(jsonString); |
|
77 |
|
|
78 |
} |
|
79 |
@Test |
|
80 |
public void logoutUser() { |
|
81 |
Map<String,Object> json = new HashMap<>(); |
|
82 |
json.put("message","ok"); |
|
83 |
//have to set token to the user - in this scenario we assume user is logged in |
|
84 |
mockUser.setToken(sampleToken); |
|
85 |
String jsonString = JSONBuilder.buildJSON(json); |
|
86 |
ResponseEntity<String> expectedResponse = ResponseEntity.status(HttpStatus.OK).body(jsonString); |
|
87 |
|
|
88 |
when(requestBuilder.sendRequestResponse(anyString(), anyMap())).thenReturn(expectedResponse); |
|
89 |
ResponseEntity<String> response1 = oAuthService.logoutUser(mockUser); |
|
90 |
assertEquals(expectedResponse, response1); |
|
91 |
|
|
82 | 92 |
// try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
83 | 93 |
// requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyMap())).thenReturn(expectedResponse); |
84 | 94 |
// ResponseEntity<String> response = oAuthService.logoutUser(mockUser); |
85 | 95 |
// assertEquals(expectedResponse, response); |
86 | 96 |
// } |
87 |
// |
|
88 |
// |
|
89 |
// } |
|
90 |
// |
|
91 |
// |
|
92 |
// |
|
93 |
// |
|
94 |
// |
|
95 |
// |
|
96 |
// |
|
97 |
// |
|
98 |
// |
|
99 |
// |
|
100 |
//} |
|
97 |
|
|
98 |
|
|
99 |
} |
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
} |
src/test/resources/contracts/loginUser_200.groovy | ||
---|---|---|
1 |
package contracts |
|
2 |
|
|
3 |
import org.springframework.cloud.contract.spec.Contract |
|
4 |
|
|
5 |
Contract.make { |
|
6 |
description "Login of registered user." |
|
7 |
request { |
|
8 |
method POST() |
|
9 |
url("/v2/user/login") |
|
10 |
headers { |
|
11 |
contentType applicationJson() |
|
12 |
} |
|
13 |
body( |
|
14 |
"name": "existing", |
|
15 |
"password": "foo" |
|
16 |
) |
|
17 |
} |
|
18 |
response { |
|
19 |
body( |
|
20 |
"message": "User logged in successfully", |
|
21 |
"jwtToken": "token" |
|
22 |
) |
|
23 |
status 200 |
|
24 |
} |
|
25 |
} |
src/test/resources/contracts/loginUser_401_1.groovy | ||
---|---|---|
1 |
package contracts |
|
2 |
|
|
3 |
import org.springframework.cloud.contract.spec.Contract |
|
4 |
|
|
5 |
Contract.make { |
|
6 |
description "Login of unregistered user." |
|
7 |
request { |
|
8 |
method POST() |
|
9 |
url("/v2/user/login") |
|
10 |
headers { |
|
11 |
contentType applicationJson() |
|
12 |
} |
|
13 |
body( |
|
14 |
"name": "non_existing", |
|
15 |
"password": "foo" |
|
16 |
) |
|
17 |
} |
|
18 |
response { |
|
19 |
body( |
|
20 |
"message" : "User login failed" |
|
21 |
) |
|
22 |
status 401 |
|
23 |
} |
|
24 |
} |
src/test/resources/contracts/loginUser_401_2.groovy | ||
---|---|---|
1 |
package contracts |
|
2 |
|
|
3 |
import org.springframework.cloud.contract.spec.Contract |
|
4 |
|
|
5 |
Contract.make { |
|
6 |
description "Login of registered user with wrong password." |
|
7 |
request { |
|
8 |
method POST() |
|
9 |
url("/v2/user/login") |
|
10 |
headers { |
|
11 |
contentType applicationJson() |
|
12 |
} |
|
13 |
body( |
|
14 |
"name": "foo", |
|
15 |
"password": "wrong" |
|
16 |
) |
|
17 |
} |
|
18 |
response { |
|
19 |
body( |
|
20 |
"message" : "User login failed" |
|
21 |
) |
|
22 |
status 401 |
|
23 |
} |
|
24 |
} |
src/test/resources/contracts/logoutUser_200.groovy | ||
---|---|---|
1 |
package contracts |
|
2 |
|
|
3 |
import org.springframework.cloud.contract.spec.Contract |
|
4 |
|
|
5 |
Contract.make { |
|
6 |
description "Logout of logged in user." |
|
7 |
request { |
|
8 |
method POST() |
|
9 |
url("/v2/user/logout") |
|
10 |
headers { |
|
11 |
header("Authorization": "Bearer token") |
|
12 |
contentType applicationJson() |
|
13 |
} |
|
14 |
body( |
|
15 |
"name": "existing" |
|
16 |
) |
|
17 |
} |
|
18 |
response { |
|
19 |
body( |
|
20 |
"message": "User logged out" |
|
21 |
) |
|
22 |
status 200 |
|
23 |
} |
|
24 |
} |
src/test/resources/contracts/registerNewUser_201.groovy | ||
---|---|---|
11 | 11 |
contentType applicationJson() |
12 | 12 |
} |
13 | 13 |
body( |
14 |
"name": "non-existing",
|
|
14 |
"name": "new_user",
|
|
15 | 15 |
"password": "foo", |
16 | 16 |
"email": "foo@foo.foo" |
17 | 17 |
) |
src/test/resources/contracts/registerNewUser_400.groovy | ||
---|---|---|
11 | 11 |
contentType applicationJson() |
12 | 12 |
} |
13 | 13 |
body( |
14 |
"name": "foo",
|
|
14 |
"name": "existing",
|
|
15 | 15 |
"password": "foo", |
16 | 16 |
"email": "foo@foo.foo" |
17 | 17 |
) |
Také k dispozici: Unified diff
#10519 vytvoření kotraktů pro login, registraci a častečně logout