11package fr .insee .genesis .controller .rest ;
22
33import fr .insee .genesis .domain .ports .api .ScheduleApiPort ;
4+ import fr .insee .genesis .domain .ports .api .SurveyUnitApiPort ;
45import fr .insee .genesis .infrastructure .repository .LunaticJsonMongoDBRepository ;
56import fr .insee .genesis .infrastructure .repository .LunaticXmlMongoDBRepository ;
67import fr .insee .genesis .infrastructure .repository .RundeckExecutionDBRepository ;
78import fr .insee .genesis .infrastructure .repository .ScheduleMongoDBRepository ;
89import fr .insee .genesis .infrastructure .repository .SurveyUnitMongoDBRepository ;
910import org .junit .jupiter .api .DisplayName ;
1011import org .junit .jupiter .api .Test ;
12+ import org .junit .jupiter .params .ParameterizedTest ;
13+ import org .junit .jupiter .params .provider .Arguments ;
14+ import org .junit .jupiter .params .provider .MethodSource ;
1115import org .mockito .MockedStatic ;
1216import org .springframework .beans .factory .annotation .Autowired ;
1317import org .springframework .beans .factory .annotation .Value ;
2731import java .util .Date ;
2832import java .util .List ;
2933import java .util .Map ;
34+ import java .util .stream .Stream ;
3035
3136import static org .mockito .ArgumentMatchers .any ;
3237import static org .mockito .ArgumentMatchers .anyInt ;
@@ -59,6 +64,8 @@ class ControllerAccessTest {
5964 @ MockitoBean
6065 private ScheduleApiPort scheduleApiPort ;
6166 @ MockitoBean
67+ private SurveyUnitApiPort surveyUnitApiPort ;
68+ @ MockitoBean
6269 private SurveyUnitMongoDBRepository surveyUnitMongoDBRepository ;
6370 @ MockitoBean
6471 private LunaticJsonMongoDBRepository lunaticJsonMongoDBRepository ;
@@ -76,6 +83,87 @@ class ControllerAccessTest {
7683 private static final String ADMIN = "ADMIN" ;
7784 private static final String READER = "READER" ;
7885
86+ /**
87+ * Provides a stream of URIs that are allowed for reader.
88+ */
89+ private static Stream <Arguments > endpointsReader (){
90+ return Stream .of (
91+ Arguments .of ("/questionnaires/with-campaigns" ),
92+ Arguments .of ("/questionnaires/by-campaign?campaignId=CAMPAIGNTEST" ),
93+ Arguments .of ("/questionnaires/" ),
94+ Arguments .of ("/modes/by-questionnaire?questionnaireId=QUESTTEST" ),
95+ Arguments .of ("/modes/by-campaign?campaignId=CAMPAIGNTEST" ),
96+ Arguments .of ("/interrogations/by-questionnaire?questionnaireId=QUESTTEST" ),
97+ Arguments .of ("/campaigns/with-questionnaires" ),
98+ Arguments .of ("/campaigns/" )
99+ );
100+ }
101+
102+ /**
103+ * Tests that users with the "ADMIN" role can access read-only endpoints.
104+ */
105+ @ ParameterizedTest
106+ @ MethodSource ("endpointsReader" )
107+ @ DisplayName ("Admins should access reader-allowed services" )
108+ void admin_should_access_reader_allowed_services (String endpointURI ) throws Exception {
109+ Jwt jwt = generateJwt (List .of ("administrateur_traiter" ), ADMIN );
110+ when (jwtDecoder .decode (anyString ())).thenReturn (jwt );
111+ mockMvc .perform (get (endpointURI ).header ("Authorization" , "bearer token_blabla" ))
112+ .andExpect (status ().isOk ());
113+ }
114+
115+ /**
116+ * Tests that users with the "USER_KRAFTWERK" role can access read-only endpoints.
117+ */
118+ @ ParameterizedTest
119+ @ MethodSource ("endpointsReader" )
120+ @ DisplayName ("Kraftwerk users should access reader-allowed services" )
121+ void kraftwerk_users_should_access_reader_allowed_services (String endpointURI ) throws Exception {
122+ Jwt jwt = generateJwt (List .of ("utilisateur_Kraftwerk" ), USER_KRAFTWERK );
123+ when (jwtDecoder .decode (anyString ())).thenReturn (jwt );
124+ mockMvc .perform (get (endpointURI ).header ("Authorization" , "bearer token_blabla" ))
125+ .andExpect (status ().isOk ());
126+ }
127+
128+ /**
129+ * Tests that users with the "USER_PLATINE" role can access read-only endpoints.
130+ */
131+ @ ParameterizedTest
132+ @ MethodSource ("endpointsReader" )
133+ @ DisplayName ("Platine users should access reader-allowed services" )
134+ void platine_users_should_access_reader_allowed_services (String endpointURI ) throws Exception {
135+ Jwt jwt = generateJwt (List .of ("utilisateur_Platine" ), USER_PLATINE );
136+ when (jwtDecoder .decode (anyString ())).thenReturn (jwt );
137+ mockMvc .perform (get (endpointURI ).header ("Authorization" , "bearer token_blabla" ))
138+ .andExpect (status ().isOk ());
139+ }
140+
141+ /**
142+ * Tests that users with the "READER" role can access read-only endpoints.
143+ */
144+ @ ParameterizedTest
145+ @ MethodSource ("endpointsReader" )
146+ @ DisplayName ("Readers should access reader-allowed services" )
147+ void reader_should_access_reader_allowed_services (String endpointURI ) throws Exception {
148+ Jwt jwt = generateJwt (List .of ("lecteur_traiter" ), "reader" );
149+ when (jwtDecoder .decode (anyString ())).thenReturn (jwt );
150+ mockMvc .perform (get (endpointURI ).header ("Authorization" , "bearer token_blabla" ))
151+ .andExpect (status ().isOk ());
152+ }
153+
154+ /**
155+ * Tests that users with invalid role are denied.
156+ */
157+ @ ParameterizedTest
158+ @ MethodSource ("endpointsReader" )
159+ @ DisplayName ("User with invalid roles should not access reader-allowed services" )
160+ void invalid_user_should_not_access_reader_allowed_services (String endpointURI ) throws Exception {
161+ Jwt jwt = generateJwt (List .of ("toto" ), "invalid_role" );
162+ when (jwtDecoder .decode (anyString ())).thenReturn (jwt );
163+ mockMvc .perform (get (endpointURI ).header ("Authorization" , "bearer token_blabla" ))
164+ .andExpect (status ().isForbidden ());
165+ }
166+
79167 /**
80168 * Test that reader can access the schedule/all endpoint.
81169 */
0 commit comments