-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathControllerAccessIT.java
More file actions
260 lines (236 loc) · 11 KB
/
Copy pathControllerAccessIT.java
File metadata and controls
260 lines (236 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package fr.insee.genesis.controller.rest;
import fr.insee.genesis.controller.IntegrationTestAbstract;
import fr.insee.genesis.domain.ports.api.DataProcessingContextApiPort;
import fr.insee.genesis.domain.ports.api.LunaticJsonRawDataApiPort;
import fr.insee.genesis.domain.ports.api.RawResponseApiPort;
import fr.insee.genesis.domain.ports.api.SurveyUnitApiPort;
import fr.insee.genesis.infrastructure.repository.RawResponseInputRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import java.util.HashMap;
import java.util.stream.Stream;
import static org.hamcrest.Matchers.oneOf;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.HttpMethod.PUT;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
class ControllerAccessIT extends IntegrationTestAbstract {
// Constants for user roles
@Autowired
private MockMvc mockMvc; // Simulates HTTP requests to the REST endpoints
@MockitoBean
private DataProcessingContextApiPort dataProcessingContextApiPort;
/** MOCKS for initializing context, not used **/
@MockitoBean
private SurveyUnitApiPort surveyUnitApiPort;
@MockitoBean
private LunaticJsonRawDataApiPort lunaticJsonRawDataApiPort;
@MockitoBean
private RawResponseApiPort rawResponseApiPort;
@MockitoBean
private RawResponseInputRepository rawRepository;
/**
* Provides a stream of URIs that are allowed for reader.
*/
private static Stream<Arguments> endpointsReader() {
return Stream.of(
Arguments.of("/questionnaires/with-campaigns"),
Arguments.of("/questionnaires/by-campaign?campaignId=CAMPAIGNTEST"),
Arguments.of("/questionnaires/"),
Arguments.of("/modes/by-questionnaire?collectionInstrumentId=QUESTTEST"),
Arguments.of("/modes/by-campaign?campaignId=CAMPAIGNTEST"),
Arguments.of("/interrogations/by-questionnaire?questionnaireId=QUESTTEST"),
Arguments.of("/campaigns/with-questionnaires"),
Arguments.of("/campaigns/"),
Arguments.of("/lunatic-model/get?questionnaireId=QUESTTEST")
);
}
private static Stream<Arguments> responseEndpoint() {
return Stream.of(
Arguments.of(GET,"/responses/raw/lunatic-json/TOTO"),
Arguments.of(GET,"/raw-responses/TOTO"),
Arguments.of(POST,"/responses/raw/lunatic-json/test/process")
);
}
private static Stream<Arguments> backOfficeEndpointProd() {
return Stream.of(
Arguments.of(PUT,"/lunatic-model/save?questionnaireId=TEST", new HashMap<>()),
Arguments.of(POST,"/edited/previous/json?questionnaireId=TEST&mode=WEB&jsonFileName=truc.json"),
Arguments.of(POST,"/edited/external/json?questionnaireId=TEST&mode=WEB&jsonFileName=truc.json")
);
}
/**
* Tests that users with the "ADMIN" role can access read-only endpoints.
*/
@ParameterizedTest
@MethodSource("endpointsReader")
@DisplayName("Admins should access reader-allowed services")
void admin_should_access_reader_allowed_services(String endpointURI) throws Exception{
mockMvc.perform(
get(endpointURI).with(
jwt().authorities(new SimpleGrantedAuthority("ROLE_ADMIN")))
)
.andExpect(status().is(oneOf(200,404)));
}
/**
* Tests that users with the "USER_KRAFTWERK" role can access read-only endpoints.
*/
@ParameterizedTest
@MethodSource("endpointsReader")
@DisplayName("Kraftwerk users should access reader-allowed services")
void kraftwerk_users_should_access_reader_allowed_services(String endpointURI) throws Exception {
mockMvc.perform(
get(endpointURI).with(
jwt().authorities(new SimpleGrantedAuthority("ROLE_USER_KRAFTWERK")))
)
.andExpect(status().is(oneOf(200,404)));
}
/**
* Tests that users with the "USER_PLATINE" role can access read-only endpoints.
*/
@ParameterizedTest
@MethodSource("endpointsReader")
@DisplayName("Platine users should access reader-allowed services")
void platine_users_should_access_reader_allowed_services(String endpointURI) throws Exception {
mockMvc.perform(
get(endpointURI).with(
jwt().authorities(new SimpleGrantedAuthority("ROLE_USER_PLATINE")))
)
.andExpect(status().is(oneOf(200,404)));
}
/**
* Tests that users with the "USER_BACK_OFFICE" role can access read-only endpoints.
*/
@ParameterizedTest
@MethodSource("backOfficeEndpointProd")
@DisplayName("Back office users should access prod services")
void back_office_users_should_access_prod_services(HttpMethod method, String endpointURI) throws Exception {
switch (method.name()){
case "PUT" -> mockMvc.perform(
put(endpointURI).with(
jwt().authorities(new SimpleGrantedAuthority("ROLE_USER_BACK_OFFICE")))
)
.andExpect(status().is(oneOf(200,400,404)));
case "POST" -> mockMvc.perform(
post(endpointURI).with(
jwt().authorities(new SimpleGrantedAuthority("ROLE_USER_BACK_OFFICE")))
)
.andExpect(status().is(oneOf(200,400,404)));
default -> Assertions.fail("Method %s not supported".formatted(method.name()));
}
}
/**
* Tests that users with the "USER_BACK_OFFICE" role can access read-only endpoints.
*/
@ParameterizedTest
@MethodSource("endpointsReader")
@DisplayName("Back office users should access reader-allowed services")
void back_office_users_should_access_reader_allowed_services(String endpointURI) throws Exception {
mockMvc.perform(
get(endpointURI).with(
jwt().authorities(new SimpleGrantedAuthority("ROLE_USER_BACK_OFFICE")))
)
.andExpect(status().is(oneOf(200,400,404)));
}
/**
* Tests that users with the "READER" role can access read-only endpoints.
*/
@ParameterizedTest
@MethodSource("endpointsReader")
@DisplayName("Readers should access reader-allowed services")
void reader_should_access_reader_allowed_services(String endpointURI) throws Exception {
mockMvc.perform(
get(endpointURI).with(
jwt().authorities(new SimpleGrantedAuthority("ROLE_READER")))
)
.andExpect(status().is(oneOf(200,404)));
}
/**
* Tests that users with invalid role are denied.
*/
@ParameterizedTest
@MethodSource("endpointsReader")
@DisplayName("User with invalid roles should not access reader-allowed services")
void invalid_user_should_not_access_reader_allowed_services(String endpointURI) throws Exception {
mockMvc.perform(
get(endpointURI).with(
jwt().authorities(new SimpleGrantedAuthority("ROLE_invalid")))
)
.andExpect(status().isForbidden());
}
/**
* Test that reader can access the schedule/all endpoint.
*/
@Test
@DisplayName("Reader should access schedule/all endpoint")
void reader_should_access_schedules_services() throws Exception{
mockMvc.perform(
get("/schedule/all").with(jwt()
.authorities(new SimpleGrantedAuthority("ROLE_READER")))
)
.andExpect(status().is(oneOf(200,404)));
}
/**
* Test that reader can not access other schedule endpoints.
*/
@ParameterizedTest
@MethodSource("responseEndpoint")
@DisplayName("Reader should not access /responses endpoints")
void reader_should_not_access_response_services(HttpMethod method,String endpointURI) throws Exception {
String requestBody = "[\"id1\"]";
MockHttpServletRequestBuilder requestBuilder;
if (method == HttpMethod.GET) {
requestBuilder = get(endpointURI);
} else if (method == HttpMethod.POST) {
requestBuilder = post(endpointURI).contentType("application/json").content(requestBody);
} else if (method == HttpMethod.PUT) {
requestBuilder = put(endpointURI);
} else if (method == HttpMethod.DELETE) {
requestBuilder = delete(endpointURI);
} else {
throw new IllegalArgumentException("Unsupported HTTP method: " + method);
}
mockMvc.perform(
requestBuilder.with(
jwt().authorities(new SimpleGrantedAuthority("ROLE_READER")))
)
.andExpect(status().isForbidden());
}
/**
* Test that kraftwerk users can't access the schedule endpoints.
*/
@Test
@DisplayName("Kraftwerk users should access schedules service")
void kraftwerk_users_should_not_access_schedules_services() throws Exception {
mockMvc.perform(
get("/context/schedules/all").with(
jwt().authorities(new SimpleGrantedAuthority("ROLE_USER_KRAFTWERK"))
))
.andExpect(status().is(oneOf(200, 404)));
}
/**
* Test that admins can access the schedule endpoints.
*/
@Test
@DisplayName("Admins should access schedules service")
void admins_should_access_schedules_services() throws Exception {
mockMvc.perform(get("/context/schedules/all").with(
jwt().authorities(new SimpleGrantedAuthority("ROLE_ADMIN"))))
.andExpect(status().is(oneOf(200, 404)));
}
}