|
| 1 | +package com.reflectoring.security.web; |
| 2 | + |
| 3 | +import com.reflectoring.security.config.BasicAuthProperties; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 7 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 8 | +import org.springframework.boot.test.context.SpringBootTest; |
| 9 | +import org.springframework.security.test.context.support.WithMockUser; |
| 10 | +import org.springframework.security.test.context.support.WithUserDetails; |
| 11 | +import org.springframework.test.context.ActiveProfiles; |
| 12 | +import org.springframework.test.context.jdbc.Sql; |
| 13 | +import org.springframework.test.context.jdbc.SqlGroup; |
| 14 | +import org.springframework.test.web.servlet.MockMvc; |
| 15 | + |
| 16 | +import static org.hamcrest.Matchers.hasSize; |
| 17 | +import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_METHOD; |
| 18 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 19 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 20 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 21 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 22 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; |
| 23 | + |
| 24 | +@SpringBootTest |
| 25 | +@AutoConfigureMockMvc |
| 26 | +@SqlGroup({ |
| 27 | + @Sql(value = "classpath:init/first.sql", executionPhase = BEFORE_TEST_METHOD), |
| 28 | + @Sql(value = "classpath:init/second.sql", executionPhase = BEFORE_TEST_METHOD) |
| 29 | +}) |
| 30 | +public class BookControllerTest { |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private MockMvc mockMvc; |
| 34 | + |
| 35 | + @Test |
| 36 | + @WithMockUser(username = "bookadmin", roles = {"USER"}) |
| 37 | + void successIfSecurityApplies() throws Exception { |
| 38 | + mockMvc.perform(get("/library/books") |
| 39 | + .param("genre", "Fiction") |
| 40 | + .param("user", "bookadmin") |
| 41 | + .header("X-Application-Name", "Library")) |
| 42 | + .andDo(print()) |
| 43 | + .andExpect(status().isOk()) |
| 44 | + .andExpect(jsonPath("$", hasSize(3))) |
| 45 | + ; |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + @WithMockUser(username = "bookadmin", roles = {"ADMIN"}) |
| 50 | + void failsForWrongAuthorization() throws Exception { |
| 51 | + mockMvc.perform(get("/library/books") |
| 52 | + .param("genre", "Fiction") |
| 53 | + .param("user", "bookadmin") |
| 54 | + .header("X-Application-Name", "Library")) |
| 55 | + .andDo(print()) |
| 56 | + .andExpect(status().isForbidden()) |
| 57 | + ; |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void failsIfSecurityApplies() throws Exception { |
| 62 | + mockMvc.perform(get("/library/books") |
| 63 | + .param("genre", "Fiction") |
| 64 | + .param("user", "bookadmin") |
| 65 | + .header("X-Application-Name", "Library")) |
| 66 | + .andDo(print()) |
| 67 | + .andExpect(status().isUnauthorized()) |
| 68 | + ; |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @WithUserDetails(value="bookadmin", userDetailsServiceBeanName="userDetailsService") |
| 73 | + void testBookWithConfiguredUserDetails() throws Exception { |
| 74 | + mockMvc.perform(get("/library/books") |
| 75 | + .param("genre", "Fantasy") |
| 76 | + .param("user", "bookadmin") |
| 77 | + .header("X-Application-Name", "Library")) |
| 78 | + .andDo(print()) |
| 79 | + .andExpect(status().isOk()) |
| 80 | + .andExpect(jsonPath("$", hasSize(1))) |
| 81 | + ; |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + @WithUserDetails(value="bookadmin", userDetailsServiceBeanName="userDetailsService") |
| 86 | + void failsIfMandatoryHeaderIsMissing() throws Exception { |
| 87 | + mockMvc.perform(get("/library/books") |
| 88 | + .param("genre", "Fantasy") |
| 89 | + .param("user", "bookadmin")) |
| 90 | + //.header("X-Application-Name", "Library")) |
| 91 | + .andDo(print()) |
| 92 | + .andExpect(status().isForbidden()) |
| 93 | + ; |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + @WithUserDetails(value="bookadmin", userDetailsServiceBeanName="userDetailsService") |
| 98 | + void failsIfPreAuthorizeConditionFails() throws Exception { |
| 99 | + mockMvc.perform(get("/library/books") |
| 100 | + .param("genre", "Fantasy") |
| 101 | + .param("user", "bookuser") |
| 102 | + .header("X-Application-Name", "Library")) |
| 103 | + .andDo(print()) |
| 104 | + .andExpect(status().isForbidden()) |
| 105 | + ; |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + //@WithUserDetails(value="bookadmin", userDetailsServiceBeanName="userDetailsService") |
| 110 | + void testBookWithWrongCredentialsUserDetails() throws Exception { |
| 111 | + mockMvc.perform(get("/library/books") |
| 112 | + .param("genre", "Fantasy") |
| 113 | + .param("user", "bookadmin") |
| 114 | + .header("X-Application-Name", "Library") |
| 115 | + .with(httpBasic("bookadmin", "password"))) |
| 116 | + .andDo(print()) |
| 117 | + .andExpect(status().isUnauthorized()); |
| 118 | + } |
| 119 | +} |
0 commit comments