/
IntegrationTests.java
85 lines (70 loc) · 2.55 KB
/
IntegrationTests.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.example.authservice;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import java.time.Duration;
import java.util.Map;
import java.util.function.Consumer;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationTests {
@LocalServerPort
int port;
WebTestClient client;
@Before
public void setup() {
this.client = WebTestClient.bindToServer()
.responseTimeout(Duration.ofDays(1))
.baseUrl("http://localhost:" + this.port)
.filter(basicAuthentication())
.build();
}
@Test
public void getUserInfoWithoutAuthWillReturn401() {
client
.get()
.uri("/user")
.exchange()
.expectStatus().isEqualTo(HttpStatus.UNAUTHORIZED);
}
@Test
public void getUserInfoWithInvalidCredentialsWillReturn401() {
client
.get()
.uri("/user")
.attributes(invalidCredentials())
.exchange()
.expectStatus().isEqualTo(HttpStatus.UNAUTHORIZED);
}
@Test
public void getUserInfoWithValidBasicAuthWillBeOk() {
client
.mutate().filter(basicAuthentication("user", "password")).build()
.get()
.uri("/user")
.exchange()
.expectStatus().isEqualTo(HttpStatus.OK);
}
@Test
public void getUserInfoWithUserCredentialsWillBeOk() {
client
.get()
.uri("/user")
.attributes(userCredentials())
.exchange()
.expectStatus().isEqualTo(HttpStatus.OK);
}
private Consumer<Map<String, Object>> userCredentials() {
return basicAuthenticationCredentials("user", "password");
}
private Consumer<Map<String, Object>> invalidCredentials() {
return basicAuthenticationCredentials("user", "INVALID");
}
}