|
11 | 11 | import fr.insee.genesis.infrastructure.repository.RundeckExecutionDBRepository; |
12 | 12 | import fr.insee.genesis.infrastructure.repository.SurveyUnitMongoDBRepository; |
13 | 13 | import fr.insee.genesis.infrastructure.repository.VariableTypeMongoDBRepository; |
| 14 | +import org.junit.jupiter.api.Assertions; |
14 | 15 | import org.junit.jupiter.api.DisplayName; |
15 | 16 | import org.junit.jupiter.api.Test; |
16 | 17 | import org.junit.jupiter.params.ParameterizedTest; |
17 | 18 | import org.junit.jupiter.params.provider.Arguments; |
18 | 19 | import org.junit.jupiter.params.provider.MethodSource; |
19 | 20 | import org.springframework.beans.factory.annotation.Autowired; |
| 21 | +import org.springframework.beans.factory.annotation.Value; |
20 | 22 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
21 | 23 | import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; |
22 | 24 | import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; |
|
30 | 32 | import org.springframework.test.web.servlet.MockMvc; |
31 | 33 | import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; |
32 | 34 |
|
| 35 | +import java.util.HashMap; |
33 | 36 | import java.util.stream.Stream; |
34 | 37 |
|
35 | 38 | import static org.hamcrest.Matchers.oneOf; |
36 | 39 | import static org.mockito.ArgumentMatchers.anyString; |
37 | 40 | import static org.mockito.Mockito.doNothing; |
38 | 41 | import static org.springframework.http.HttpMethod.GET; |
39 | 42 | import static org.springframework.http.HttpMethod.POST; |
| 43 | +import static org.springframework.http.HttpMethod.PUT; |
40 | 44 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt; |
41 | 45 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; |
42 | 46 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
|
51 | 55 | @EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) |
52 | 56 | class ControllerAccessTest { |
53 | 57 |
|
| 58 | + // Constants for user roles |
| 59 | + // JWT claim properties loaded from application properties |
| 60 | + @Value("${fr.insee.genesis.security.token.oidc-claim-role}") |
| 61 | + private String claimRoleDotRoles; |
| 62 | + @Value("${fr.insee.genesis.security.token.oidc-claim-username}") |
| 63 | + private String claimName; |
54 | 64 | @Autowired |
55 | 65 | private MockMvc mockMvc; // Simulates HTTP requests to the REST endpoints |
56 | 66 |
|
@@ -108,6 +118,15 @@ private static Stream<Arguments> responseEndpoint() { |
108 | 118 | ); |
109 | 119 | } |
110 | 120 |
|
| 121 | + private static Stream<Arguments> backOfficeEndpointProd() { |
| 122 | + return Stream.of( |
| 123 | + Arguments.of(PUT,"/lunatic-model/save?questionnaireId=TEST", new HashMap<>()), |
| 124 | + Arguments.of(POST,"/edited/previous/json?questionnaireId=TEST&mode=WEB&jsonFileName=truc.json"), |
| 125 | + Arguments.of(POST,"/edited/external/json?questionnaireId=TEST&mode=WEB&jsonFileName=truc.json"), |
| 126 | + Arguments.of(PUT,"/context/review?partitionId=TEST") |
| 127 | + ); |
| 128 | + } |
| 129 | + |
111 | 130 | /** |
112 | 131 | * Tests that users with the "ADMIN" role can access read-only endpoints. |
113 | 132 | */ |
@@ -150,6 +169,42 @@ void platine_users_should_access_reader_allowed_services(String endpointURI) thr |
150 | 169 | .andExpect(status().is(oneOf(200,404))); |
151 | 170 | } |
152 | 171 |
|
| 172 | + /** |
| 173 | + * Tests that users with the "USER_BACK_OFFICE" role can access read-only endpoints. |
| 174 | + */ |
| 175 | + @ParameterizedTest |
| 176 | + @MethodSource("backOfficeEndpointProd") |
| 177 | + @DisplayName("Back office users should access prod services") |
| 178 | + void back_office_users_should_access_prod_services(HttpMethod method, String endpointURI) throws Exception { |
| 179 | + switch (method.name()){ |
| 180 | + case "PUT" -> mockMvc.perform( |
| 181 | + put(endpointURI).with( |
| 182 | + jwt().authorities(new SimpleGrantedAuthority("ROLE_USER_BACK_OFFICE"))) |
| 183 | + ) |
| 184 | + .andExpect(status().is(oneOf(200,400,404))); |
| 185 | + case "POST" -> mockMvc.perform( |
| 186 | + post(endpointURI).with( |
| 187 | + jwt().authorities(new SimpleGrantedAuthority("ROLE_USER_BACK_OFFICE"))) |
| 188 | + ) |
| 189 | + .andExpect(status().is(oneOf(200,400,404))); |
| 190 | + default -> Assertions.fail("Method %s not supported".formatted(method.name())); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Tests that users with the "USER_BACK_OFFICE" role can access read-only endpoints. |
| 196 | + */ |
| 197 | + @ParameterizedTest |
| 198 | + @MethodSource("endpointsReader") |
| 199 | + @DisplayName("Back office users should access reader-allowed services") |
| 200 | + void back_office_users_should_access_reader_allowed_services(String endpointURI) throws Exception { |
| 201 | + mockMvc.perform( |
| 202 | + get(endpointURI).with( |
| 203 | + jwt().authorities(new SimpleGrantedAuthority("ROLE_USER_BACK_OFFICE"))) |
| 204 | + ) |
| 205 | + .andExpect(status().is(oneOf(200,400,404))); |
| 206 | + } |
| 207 | + |
153 | 208 | /** |
154 | 209 | * Tests that users with the "READER" role can access read-only endpoints. |
155 | 210 | */ |
@@ -269,6 +324,4 @@ void invalid_roles_should_access_schedules_services() throws Exception { |
269 | 324 | jwt().authorities(new SimpleGrantedAuthority("ROLE_invalid")))) |
270 | 325 | .andExpect(status().isForbidden()); |
271 | 326 | } |
272 | | - |
273 | | - |
274 | 327 | } |
0 commit comments