-
Notifications
You must be signed in to change notification settings - Fork 51
/
AuthControllerTest.java
76 lines (63 loc) · 3.04 KB
/
AuthControllerTest.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
package com.example.demo;
import com.example.demo.security.jwt.JwtTokenProvider;
import com.example.demo.web.AuthController;
import com.example.demo.web.AuthenticationRequest;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.BodyInserters;
import reactor.core.publisher.Mono;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
@WebFluxTest(
controllers = AuthController.class,
excludeAutoConfiguration = {
ReactiveUserDetailsServiceAutoConfiguration.class,
ReactiveSecurityAutoConfiguration.class
}
)
@Slf4j
class AuthControllerTest {
@MockBean
private JwtTokenProvider tokenProvider;
@MockBean
private ReactiveAuthenticationManager authenticationManager;
@Autowired
private WebTestClient client;
@Test
void testFindByUsername() {
var usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
"test",
"password",
AuthorityUtils.createAuthorityList("ROLE_USER")
);
when(this.authenticationManager.authenticate(any(UsernamePasswordAuthenticationToken.class)))
.thenReturn(Mono.just(usernamePasswordAuthenticationToken));
when(this.tokenProvider.createToken(any(Authentication.class))).thenReturn("atesttoken");
var req = new AuthenticationRequest("test", "password");
this.client.post()
.uri("/auth/login")
.body(BodyInserters.fromValue(req))
.exchange()
.expectHeader().valueEquals(HttpHeaders.AUTHORIZATION, "Bearer atesttoken")
.expectBody()
.jsonPath("$.access_token").isEqualTo("atesttoken");
verify(this.authenticationManager, times(1)).authenticate(any(UsernamePasswordAuthenticationToken.class));
verify(this.tokenProvider, times(1)).createToken(any(Authentication.class));
verifyNoMoreInteractions(this.authenticationManager);
verifyNoMoreInteractions(this.tokenProvider);
}
}