|
25 | 25 | import org.springframework.security.oauth2.jwt.JwtDecoder;
|
26 | 26 | import org.springframework.test.context.junit4.SpringRunner;
|
27 | 27 | import org.springframework.test.web.servlet.MockMvc;
|
28 |
| -import org.springframework.security.oauth2.jwt.Jwt; |
29 | 28 |
|
30 | 29 | import static org.hamcrest.CoreMatchers.is;
|
31 | 30 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
|
32 | 31 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
33 | 32 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
34 | 33 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
35 | 34 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
36 |
| -import static org.mockito.ArgumentMatchers.anyString; |
37 |
| -import static org.mockito.Mockito.when; |
38 | 35 |
|
39 | 36 | /**
|
40 | 37 | *
|
@@ -77,41 +74,32 @@ public void messageCanNotBeReadWithoutScopeMessageReadAuthority() throws Excepti
|
77 | 74 |
|
78 | 75 | @Test
|
79 | 76 | public void messageCanNotBeCreatedWithoutAnyScope() throws Exception {
|
80 |
| - Jwt jwt = Jwt.withTokenValue("token") |
81 |
| - .header("alg", "none") |
82 |
| - .claim("scope", "") |
83 |
| - .build(); |
84 |
| - when(jwtDecoder.decode(anyString())).thenReturn(jwt); |
85 | 77 | mockMvc.perform(post("/message")
|
86 | 78 | .content("Hello message")
|
87 |
| - .header("Authorization", "Bearer " + jwt.getTokenValue())) |
| 79 | + .with(jwt(jwt -> jwt.claim("scope", "message:read")))) |
88 | 80 | .andExpect(status().isForbidden());
|
89 | 81 | }
|
90 | 82 |
|
91 | 83 | @Test
|
92 | 84 | public void messageCanNotBeCreatedWithScopeMessageReadAuthority() throws Exception {
|
93 |
| - Jwt jwt = Jwt.withTokenValue("token") |
94 |
| - .header("alg", "none") |
95 |
| - .claim("scope", "message:read") |
96 |
| - .build(); |
97 |
| - when(jwtDecoder.decode(anyString())).thenReturn(jwt); |
98 | 85 | mockMvc.perform(post("/message")
|
99 |
| - .content("Hello message") |
100 |
| - .header("Authorization", "Bearer " + jwt.getTokenValue())) |
| 86 | + .with(jwt(jwt -> jwt.claim("scope", "message:read"))) |
| 87 | + .content("Hello message")) |
101 | 88 | .andExpect(status().isForbidden());
|
102 | 89 | }
|
103 | 90 |
|
104 | 91 | @Test
|
105 | 92 | public void messageCanBeCreatedWithScopeMessageWriteAuthority()
|
106 | 93 | throws Exception {
|
107 |
| - Jwt jwt = Jwt.withTokenValue("token") |
108 |
| - .header("alg", "none") |
109 |
| - .claim("scope", "message:write") |
110 |
| - .build(); |
111 |
| - when(jwtDecoder.decode(anyString())).thenReturn(jwt); |
112 | 94 | mockMvc.perform(post("/message")
|
113 |
| - .content("Hello message") |
114 |
| - .header("Authorization", "Bearer " + jwt.getTokenValue())) |
| 95 | + .with(jwt(jwt -> jwt.claim("scope", "message:write"))) |
| 96 | + .content("Hello message")) |
| 97 | + .andExpect(status().isOk()) |
| 98 | + .andExpect(content().string(is("Message was created. Content: Hello message"))); |
| 99 | + |
| 100 | + mockMvc.perform(post("/message") |
| 101 | + .with(jwt().authorities(new SimpleGrantedAuthority(("SCOPE_message:write")))) |
| 102 | + .content("Hello message")) |
115 | 103 | .andExpect(status().isOk())
|
116 | 104 | .andExpect(content().string(is("Message was created. Content: Hello message")));
|
117 | 105 | }
|
|
0 commit comments