1
1
package com .bfwg .rest ;
2
2
3
+ import com .bfwg .model .User ;
4
+ import com .bfwg .security .TokenHelper ;
5
+ import com .bfwg .security .UserDetailsDummy ;
6
+ import io .jsonwebtoken .ExpiredJwtException ;
7
+ import org .joda .time .DateTimeUtils ;
3
8
import org .junit .Before ;
4
9
import org .junit .Test ;
5
10
import org .junit .runner .RunWith ;
6
11
import org .springframework .beans .factory .annotation .Autowired ;
7
12
import org .springframework .boot .test .context .SpringBootTest ;
8
- import org .springframework .security .test .context .support .WithAnonymousUser ;
13
+ import org .springframework .boot .test .mock .mockito .MockBean ;
14
+ import org .springframework .security .core .userdetails .UserDetailsService ;
9
15
import org .springframework .test .context .junit4 .SpringRunner ;
10
16
import org .springframework .test .web .servlet .MockMvc ;
17
+ import org .springframework .test .web .servlet .ResultActions ;
11
18
import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
12
19
import org .springframework .web .context .WebApplicationContext ;
13
20
21
+ import static org .mockito .Matchers .eq ;
22
+ import static org .mockito .Mockito .when ;
14
23
import static org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers .springSecurity ;
15
24
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
16
25
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
@@ -24,6 +33,12 @@ public class AuthenticationControllerTest {
24
33
25
34
private MockMvc mvc ;
26
35
36
+ @ Autowired
37
+ private TokenHelper tokenHelper ;
38
+
39
+ @ MockBean
40
+ private UserDetailsService userDetailsService ;
41
+
27
42
@ Autowired
28
43
private WebApplicationContext context ;
29
44
@@ -33,15 +48,30 @@ public void setup() {
33
48
.webAppContextSetup (context )
34
49
.apply (springSecurity ())
35
50
.build ();
51
+
52
+ DateTimeUtils .setCurrentMillisSystem ();
53
+ User user = new User ();
54
+ user .setUsername ("username" );
55
+ when (this .userDetailsService .loadUserByUsername (eq ("test-user" ))).thenReturn (user );
36
56
}
37
57
38
58
39
59
@ Test
40
- @ WithAnonymousUser
41
- public void shouldGetUnauthorizedWithAnonymousUser () throws Exception {
42
- this .mvc .perform (get ("/auth/refresh" ))
43
- .andExpect (status ().isUnauthorized ());
60
+ public void shouldGet200WhenGivenValidOldToken () throws Exception {
61
+
62
+ String token = tokenHelper .generateToken (new UserDetailsDummy ("test-user" ).getUsername ());
63
+ this .mvc .perform (get ("/auth/refresh" ).header ("Authorization" , "Bearer " + token ))
64
+ .andExpect (status ().is (200 ));
65
+
66
+ }
44
67
68
+ @ Test (expected = ExpiredJwtException .class )
69
+ public void shouldNotGet200WhenGivenInvalidOldToken () throws Exception {
70
+ DateTimeUtils .setCurrentMillisFixed (1L ); // set time back to 1970
71
+ String token = tokenHelper .generateToken (new UserDetailsDummy ("test-user" ).getUsername ());
72
+ DateTimeUtils .setCurrentMillisSystem (); // back to now
73
+ ResultActions action = null ;
74
+ this .mvc .perform (get ("/auth/refresh" ).header ("Authorization" , "Bearer " + token ));
45
75
}
46
76
47
77
}
0 commit comments