24
24
import org .junit .jupiter .api .extension .RegisterExtension ;
25
25
26
26
import org .springframework .beans .factory .annotation .Autowired ;
27
- import org .springframework .boot .test .autoconfigure .web .servlet . AutoConfigureMockMvc ;
27
+ import org .springframework .boot .test .autoconfigure .web .reactive . AutoConfigureWebTestClient ;
28
28
import org .springframework .boot .test .context .SpringBootTest ;
29
- import org .springframework .test .web .servlet .MockMvc ;
29
+ import org .springframework .test .web .reactive .server .WebTestClient ;
30
+ import org .springframework .web .reactive .function .BodyInserters ;
30
31
import org .springframework .web .util .UriComponents ;
31
32
import org .springframework .web .util .UriComponentsBuilder ;
32
33
33
34
import static org .assertj .core .api .Assertions .assertThat ;
34
- import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .csrf ;
35
- import static org .springframework .security .test .web .servlet .response .SecurityMockMvcResultMatchers .authenticated ;
36
- import static org .springframework .security .test .web .servlet .response .SecurityMockMvcResultMatchers .unauthenticated ;
37
- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
38
- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .redirectedUrl ;
39
- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
35
+ import static org .springframework .security .test .web .reactive .server .SecurityMockServerConfigurers .csrf ;
40
36
41
- @ SpringBootTest ( webEnvironment = SpringBootTest . WebEnvironment . RANDOM_PORT )
42
- @ AutoConfigureMockMvc
37
+ @ SpringBootTest
38
+ @ AutoConfigureWebTestClient ( timeout = "30s" )
43
39
class MagicLinkApplicationTests {
44
40
45
41
@ RegisterExtension
46
42
static GreenMailExtension greenMail = new GreenMailExtension (ServerSetupTest .SMTP );
47
43
48
44
@ Autowired
49
- MockMvc mockMvc ;
45
+ WebTestClient web ;
50
46
51
47
@ Test
52
48
void ottLoginWhenUserExistsThenSendEmailAndAuthenticate () throws Exception {
53
- this .mockMvc .perform (post ("/ott/generate" ).param ("username" , "user" ).with (csrf ()))
54
- .andExpectAll (status ().isFound (), redirectedUrl ("/ott/sent" ));
49
+ this .web .mutateWith (csrf ())
50
+ .post ()
51
+ .uri ("/ott/generate" )
52
+ .body (BodyInserters .fromFormData ("username" , "user" ))
53
+ .exchange ()
54
+ .expectStatus ()
55
+ .isFound ()
56
+ .expectHeader ()
57
+ .location ("/ott/sent" );
55
58
56
59
greenMail .waitForIncomingEmail (1 );
57
60
MimeMessage receivedMessage = greenMail .getReceivedMessages ()[0 ];
@@ -62,19 +65,40 @@ void ottLoginWhenUserExistsThenSendEmailAndAuthenticate() throws Exception {
62
65
63
66
assertThat (token ).isNotEmpty ();
64
67
65
- this .mockMvc .perform (post ("/login/ott" ).param ("token" , token ).with (csrf ()))
66
- .andExpectAll (status ().isFound (), redirectedUrl ("/" ), authenticated ());
68
+ this .web .mutateWith (csrf ())
69
+ .post ()
70
+ .uri ("/login/ott" )
71
+ .body (BodyInserters .fromFormData ("token" , token ))
72
+ .exchange ()
73
+ .expectStatus ()
74
+ .isFound ()
75
+ .expectHeader ()
76
+ .location ("/" );
67
77
}
68
78
69
79
@ Test
70
80
void ottLoginWhenInvalidTokenThenFails () throws Exception {
71
- this .mockMvc .perform (post ("/ott/generate" ).param ("username" , "user" ).with (csrf ()))
72
- .andExpectAll (status ().isFound (), redirectedUrl ("/ott/sent" ));
81
+ this .web .mutateWith (csrf ())
82
+ .post ()
83
+ .uri ("/ott/generate" )
84
+ .body (BodyInserters .fromFormData ("username" , "user" ))
85
+ .exchange ()
86
+ .expectStatus ()
87
+ .isFound ()
88
+ .expectHeader ()
89
+ .location ("/ott/sent" );
73
90
74
91
String token = "1234;" ;
75
92
76
- this .mockMvc .perform (post ("/login/ott" ).param ("token" , token ).with (csrf ()))
77
- .andExpectAll (status ().isFound (), redirectedUrl ("/login?error" ), unauthenticated ());
93
+ this .web .mutateWith (csrf ())
94
+ .post ()
95
+ .uri ("/login/ott" )
96
+ .body (BodyInserters .fromFormData ("token" , token ))
97
+ .exchange ()
98
+ .expectStatus ()
99
+ .isFound ()
100
+ .expectHeader ()
101
+ .location ("/login?error" );
78
102
}
79
103
80
104
}
0 commit comments