1111import fr .insee .genesis .infrastructure .repository .RundeckExecutionDBRepository ;
1212import fr .insee .genesis .infrastructure .repository .SurveyUnitMongoDBRepository ;
1313import fr .insee .genesis .infrastructure .repository .VariableTypeMongoDBRepository ;
14+ import org .junit .jupiter .api .Assertions ;
1415import org .junit .jupiter .api .DisplayName ;
1516import org .junit .jupiter .api .Test ;
1617import org .junit .jupiter .params .ParameterizedTest ;
1718import org .junit .jupiter .params .provider .Arguments ;
1819import org .junit .jupiter .params .provider .MethodSource ;
1920import org .springframework .beans .factory .annotation .Autowired ;
21+ import org .springframework .beans .factory .annotation .Value ;
2022import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
2123import org .springframework .boot .autoconfigure .data .mongo .MongoDataAutoConfiguration ;
2224import org .springframework .boot .autoconfigure .mongo .MongoAutoConfiguration ;
3133import org .springframework .test .web .servlet .request .MockHttpServletRequestBuilder ;
3234
3335import java .util .HashMap ;
34- import java .util .List ;
35- import java .util .Map ;
3636import java .util .stream .Stream ;
3737
3838import static org .hamcrest .Matchers .oneOf ;
5656class ControllerAccessTest {
5757
5858 // Constants for user roles
59- private static final String USER_KRAFTWERK = "USER_KRAFTWERK" ;
60- private static final String USER_PLATINE = "USER_PLATINE" ;
61- private static final String USER_BACK_OFFICE = "USER_BACK_OFFICE" ;
62- private static final String ADMIN = "ADMIN" ;
63- private static final String READER = "READER" ;
6459 // JWT claim properties loaded from application properties
6560 @ Value ("${fr.insee.genesis.security.token.oidc-claim-role}" )
6661 private String claimRoleDotRoles ;
@@ -116,10 +111,6 @@ private static Stream<Arguments> endpointsReader() {
116111
117112 private static Stream <Arguments > responseEndpoint () {
118113 return Stream .of (
119- Arguments .of (GET ,"/response/lunatic-json/get/unprocessed" ),
120- Arguments .of (GET ,"/response/lunatic-json/get/by-interrogation-mode-and-campaign" ),
121- Arguments .of (POST ,"/response/lunatic-json/process" ),
122- Arguments .of (GET ,"/response/lunatic-json/campaignId=TOTO" ),
123114 Arguments .of (GET ,"/responses/raw/lunatic-json/get/unprocessed" ),
124115 Arguments .of (GET ,"/responses/raw/lunatic-json/get/by-interrogation-mode-and-campaign?interrogationId=test&campaignName=test&mode=WEB" ),
125116 Arguments .of (GET ,"/responses/raw/lunatic-json/campaignId=TOTO" ),
@@ -185,23 +176,19 @@ void platine_users_should_access_reader_allowed_services(String endpointURI) thr
185176 @ MethodSource ("backOfficeEndpointProd" )
186177 @ DisplayName ("Back office users should access prod services" )
187178 void back_office_users_should_access_prod_services (HttpMethod method , String endpointURI ) throws Exception {
188- Jwt jwt = generateJwt (List .of ("utilisateur_Back_Office" ), USER_BACK_OFFICE );
189- when (jwtDecoder .decode (anyString ())).thenReturn (jwt );
190- MockHttpServletRequestBuilder requestBuilder ;
191- if (method == HttpMethod .GET ) {
192- requestBuilder = get (endpointURI );
193- } else if (method == HttpMethod .POST ) {
194- requestBuilder = post (endpointURI );
195- } else if (method == PUT ) {
196- requestBuilder = put (endpointURI );
197- } else if (method == HttpMethod .DELETE ) {
198- requestBuilder = delete (endpointURI );
199- } else {
200- throw new IllegalArgumentException ("Unsupported HTTP method: " + method );
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 ()));
201191 }
202-
203- mockMvc .perform (requestBuilder .header ("Authorization" , "bearer token_blabla" ))
204- .andExpect (status ().is (oneOf (200 , 400 , 404 )));
205192 }
206193
207194 /**
@@ -211,10 +198,11 @@ void back_office_users_should_access_prod_services(HttpMethod method, String end
211198 @ MethodSource ("endpointsReader" )
212199 @ DisplayName ("Back office users should access reader-allowed services" )
213200 void back_office_users_should_access_reader_allowed_services (String endpointURI ) throws Exception {
214- Jwt jwt = generateJwt (List .of ("utilisateur_Back_Office" ), USER_BACK_OFFICE );
215- when (jwtDecoder .decode (anyString ())).thenReturn (jwt );
216- mockMvc .perform (get (endpointURI ).header ("Authorization" , "bearer token_blabla" ))
217- .andExpect (status ().is (oneOf (200 , 404 )));
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 )));
218206 }
219207
220208 /**
@@ -336,6 +324,4 @@ void invalid_roles_should_access_schedules_services() throws Exception {
336324 jwt ().authorities (new SimpleGrantedAuthority ("ROLE_invalid" ))))
337325 .andExpect (status ().isForbidden ());
338326 }
339-
340-
341327}
0 commit comments