Skip to content

Commit baed0f3

Browse files
committed
fix one test
1 parent 7809f2c commit baed0f3

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/test/java/fr/insee/genesis/controller/rest/ControllerAccessTest.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
2323
import org.springframework.boot.test.context.SpringBootTest;
2424
import org.springframework.data.mongodb.core.MongoTemplate;
25+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
2526
import org.springframework.security.oauth2.jwt.Jwt;
2627
import org.springframework.security.oauth2.jwt.JwtDecoder;
2728
import org.springframework.test.context.ActiveProfiles;
@@ -37,6 +38,7 @@
3738
import static org.mockito.ArgumentMatchers.anyString;
3839
import static org.mockito.Mockito.doNothing;
3940
import static org.mockito.Mockito.when;
41+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
4042
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
4143
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4244
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -114,6 +116,17 @@ void admin_should_access_reader_allowed_services(String endpointURI) throws Exce
114116
.andExpect(status().is(oneOf(200,404)));
115117
}
116118

119+
@Test
120+
@DisplayName("Reader should access schedule/all endpoint")
121+
void reader_should_access_schedules_services() throws Exception{
122+
mockMvc.perform(
123+
get("/schedule/all").with(jwt()
124+
.authorities(new SimpleGrantedAuthority("ROLE_READER")))
125+
)
126+
.andExpect(status().is(oneOf(200,404)));
127+
}
128+
129+
117130
/**
118131
* Tests that users with the "USER_KRAFTWERK" role can access read-only endpoints.
119132
*/
@@ -166,17 +179,7 @@ void invalid_user_should_not_access_reader_allowed_services(String endpointURI)
166179
.andExpect(status().isForbidden());
167180
}
168181

169-
/**
170-
* Test that reader can access the schedule/all endpoint.
171-
*/
172-
@Test
173-
@DisplayName("Reader should access schedule/all endpoint")
174-
void reader_should_access_schedules_services() throws Exception{
175-
Jwt jwt = generateJwt(List.of("lecteur_traiter"), READER);
176-
when(jwtDecoder.decode(anyString())).thenReturn(jwt);
177-
mockMvc.perform(get("/schedule/all").header("Authorization", "bearer token_blabla"))
178-
.andExpect(status().is(oneOf(200,404)));
179-
}
182+
180183

181184
/**
182185
* Test that reader can not access other schedule endpoints.
@@ -203,6 +206,8 @@ void kraftwerk_users_should_not_access_schedules_services() throws Exception{
203206
.andExpect(status().is(oneOf(200,404)));
204207
}
205208

209+
210+
206211
/**
207212
* Test that admins can access the schedule endpoints.
208213
*/

0 commit comments

Comments
 (0)