|
15 | 15 | */
|
16 | 16 | package rest;
|
17 | 17 |
|
| 18 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; |
18 | 19 | import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
|
19 | 20 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
20 | 21 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
|
24 | 25 | import org.junit.Test;
|
25 | 26 | import org.junit.runner.RunWith;
|
26 | 27 | import org.springframework.beans.factory.annotation.Autowired;
|
| 28 | +import org.springframework.security.test.context.support.WithMockUser; |
27 | 29 | import org.springframework.session.ExpiringSession;
|
28 | 30 | import org.springframework.session.web.http.SessionRepositoryFilter;
|
29 | 31 | import org.springframework.test.context.ContextConfiguration;
|
@@ -56,12 +58,29 @@ public void setup() {
|
56 | 58 | mvc = MockMvcBuilders.webAppContextSetup(context)
|
57 | 59 | .alwaysDo(print())
|
58 | 60 | .addFilters(sessionRepositoryFilter)
|
59 |
| - .apply(springSecurity()).build(); |
| 61 | + .apply(springSecurity()) |
| 62 | + .build(); |
60 | 63 | }
|
61 | 64 |
|
62 | 65 | @Test
|
63 | 66 | public void noSessionOnNoCredentials() throws Exception {
|
64 | 67 | mvc.perform(get("/"))
|
65 |
| - .andExpect(header().doesNotExist("x-auth-token")); |
| 68 | + .andExpect(header().doesNotExist("x-auth-token")) |
| 69 | + .andExpect(status().isUnauthorized()); |
| 70 | + } |
| 71 | + |
| 72 | + @WithMockUser |
| 73 | + @Test |
| 74 | + public void autheticatedAnnotation() throws Exception { |
| 75 | + mvc.perform(get("/")) |
| 76 | + .andExpect(content().string("{\"username\":\"user\"}")); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void autheticatedRequestPostProcessor() throws Exception { |
| 82 | + mvc.perform(get("/").with(user("user"))) |
| 83 | + .andExpect(content().string("{\"username\":\"user\"}")); |
| 84 | + |
66 | 85 | }
|
67 | 86 | }
|
0 commit comments