3333import org .springframework .test .web .servlet .request .MockHttpServletRequestBuilder ;
3434
3535import java .util .Date ;
36+ import java .util .HashMap ;
3637import java .util .List ;
3738import java .util .Map ;
3839import java .util .stream .Stream ;
4344import static org .mockito .Mockito .when ;
4445import static org .springframework .http .HttpMethod .GET ;
4546import static org .springframework .http .HttpMethod .POST ;
47+ import static org .springframework .http .HttpMethod .PUT ;
4648import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .delete ;
4749import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
4850import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
@@ -58,6 +60,7 @@ class ControllerAccessTest {
5860 // Constants for user roles
5961 private static final String USER_KRAFTWERK = "USER_KRAFTWERK" ;
6062 private static final String USER_PLATINE = "USER_PLATINE" ;
63+ private static final String USER_BACK_OFFICE = "USER_BACK_OFFICE" ;
6164 private static final String ADMIN = "ADMIN" ;
6265 private static final String READER = "READER" ;
6366 // JWT claim properties loaded from application properties
@@ -115,9 +118,18 @@ private static Stream<Arguments> endpointsReader() {
115118 private static Stream <Arguments > responseEndpoint () {
116119 return Stream .of (
117120 Arguments .of (GET ,"/response/lunatic-json/get/unprocessed" ),
118- Arguments .of (GET ,"/response//lunatic-json/get/by-interrogation-mode-and-campaign" ),
119- Arguments .of (POST ,"/response//lunatic-json/process" ),
120- Arguments .of (GET ,"/response//lunatic-json/campaignId=TOTO" )
121+ Arguments .of (GET ,"/response/lunatic-json/get/by-interrogation-mode-and-campaign" ),
122+ Arguments .of (POST ,"/response/lunatic-json/process" ),
123+ Arguments .of (GET ,"/response/lunatic-json/campaignId=TOTO" )
124+ );
125+ }
126+
127+ private static Stream <Arguments > backOfficeEndpointProd () {
128+ return Stream .of (
129+ Arguments .of (PUT ,"/lunatic-model/save?questionnaireId=TEST" , new HashMap <>()),
130+ Arguments .of (POST ,"/edited/previous/json?questionnaireId=TEST&mode=WEB&jsonFileName=truc.json" ),
131+ Arguments .of (POST ,"/edited/external/json?questionnaireId=TEST&mode=WEB&jsonFileName=truc.json" ),
132+ Arguments .of (PUT ,"/context/review?partitionId=TEST" )
121133 );
122134 }
123135
@@ -160,6 +172,45 @@ void platine_users_should_access_reader_allowed_services(String endpointURI) thr
160172 .andExpect (status ().is (oneOf (200 , 404 )));
161173 }
162174
175+ /**
176+ * Tests that users with the "USER_BACK_OFFICE" role can access read-only endpoints.
177+ */
178+ @ ParameterizedTest
179+ @ MethodSource ("backOfficeEndpointProd" )
180+ @ DisplayName ("Back office users should access prod services" )
181+ void back_office_users_should_access_prod_services (HttpMethod method , String endpointURI ) throws Exception {
182+ Jwt jwt = generateJwt (List .of ("utilisateur_Back_Office" ), USER_BACK_OFFICE );
183+ when (jwtDecoder .decode (anyString ())).thenReturn (jwt );
184+ MockHttpServletRequestBuilder requestBuilder ;
185+ if (method == HttpMethod .GET ) {
186+ requestBuilder = get (endpointURI );
187+ } else if (method == HttpMethod .POST ) {
188+ requestBuilder = post (endpointURI );
189+ } else if (method == PUT ) {
190+ requestBuilder = put (endpointURI );
191+ } else if (method == HttpMethod .DELETE ) {
192+ requestBuilder = delete (endpointURI );
193+ } else {
194+ throw new IllegalArgumentException ("Unsupported HTTP method: " + method );
195+ }
196+
197+ mockMvc .perform (requestBuilder .header ("Authorization" , "bearer token_blabla" ))
198+ .andExpect (status ().is (oneOf (200 , 400 , 404 )));
199+ }
200+
201+ /**
202+ * Tests that users with the "USER_BACK_OFFICE" role can access read-only endpoints.
203+ */
204+ @ ParameterizedTest
205+ @ MethodSource ("endpointsReader" )
206+ @ DisplayName ("Back office users should access reader-allowed services" )
207+ void back_office_users_should_access_reader_allowed_services (String endpointURI ) throws Exception {
208+ Jwt jwt = generateJwt (List .of ("utilisateur_Back_Office" ), USER_BACK_OFFICE );
209+ when (jwtDecoder .decode (anyString ())).thenReturn (jwt );
210+ mockMvc .perform (get (endpointURI ).header ("Authorization" , "bearer token_blabla" ))
211+ .andExpect (status ().is (oneOf (200 , 404 )));
212+ }
213+
163214 /**
164215 * Tests that users with the "READER" role can access read-only endpoints.
165216 */
@@ -226,7 +277,7 @@ void reader_should_not_access_response_services(HttpMethod method,String endpoin
226277 requestBuilder = get (endpointURI );
227278 } else if (method == HttpMethod .POST ) {
228279 requestBuilder = post (endpointURI );
229- } else if (method == HttpMethod . PUT ) {
280+ } else if (method == PUT ) {
230281 requestBuilder = put (endpointURI );
231282 } else if (method == HttpMethod .DELETE ) {
232283 requestBuilder = delete (endpointURI );
0 commit comments