Skip to content

Commit

Permalink
feat: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Jan 14, 2025
1 parent ad97828 commit ac7aa0e
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,6 @@ public ResponseEntity<Object> getOperationsWithReport(@PathVariable(Constants.ID
}



/**
* CREATE
* @param body
* @return response
*/
@PreAuthorize("hasAnyRole(T(fr.insee.rmes.config.auth.roles.Roles).ADMIN)")
@PostMapping(value = "/series",
consumes = MediaType.APPLICATION_JSON_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import fr.insee.rmes.config.auth.security.OpenIDConnectSecurityContext;
import fr.insee.rmes.webservice.operations.IndicatorsResources;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
Expand All @@ -24,7 +26,7 @@
import static fr.insee.rmes.integration.authorizations.TokenForTestsConfiguration.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(controllers = IndicatorsResources.class,
Expand Down Expand Up @@ -61,6 +63,37 @@ class TestIndicatorsResourcesAuthorizationsEnvProd {
private final String timbre = "XX59-YYY";


@ValueSource(strings = {
"/operations/indicators/",
"/operations/indicators/withSims",
"/operations/indicators/advanced-search",
"/operations/indicator/1"
})
@ParameterizedTest
void getWithAdmin_Ok(String url) throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.ADMIN));
mvc.perform(get(url).header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@ValueSource(strings = {
"/operations/indicators/",
"/operations/indicators/withSims",
"/operations/indicators/advanced-search",
"/operations/indicator/1"
})
@ParameterizedTest
void getWithContributor_Ok(String url) throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.SERIES_CONTRIBUTOR));
mvc.perform(get(url).header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}


@Test
void postIndicatorWhenAdmin_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.ADMIN));
Expand Down Expand Up @@ -94,4 +127,66 @@ void postIndicatorWhenIndicatorFakeRole_ko() throws Exception {
.andExpect(status().isForbidden());
}

@Test
void putIndicatorWhenAdmin_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.ADMIN));
when(operationsService.setIndicator(anyString())).thenReturn("1");
mvc.perform(put("/operations/indicator/1").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{\"id\": \"1\"} "))
.andExpect(status().is2xxSuccessful());
}

@Test
void putIndicatorWhenIndicatorContributor_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.INDICATOR_CONTRIBUTOR));
when(operationsService.setIndicator(anyString())).thenReturn("1");
mvc.perform(put("/operations/indicator/1").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{\"id\": \"1\"} "))
.andExpect(status().is2xxSuccessful());
}

@Test
void putIndicatorWhenIndicatorFakeRole_ko() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of("fake"));
when(operationsService.setIndicator(anyString())).thenReturn("1");
mvc.perform(put("/operations/indicator/1").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{\"id\": \"1\"} "))
.andExpect(status().isForbidden());
}

@Test
void validateIndicatorWhenAdmin_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.ADMIN));
when(operationsService.setIndicator(anyString())).thenReturn("1");
mvc.perform(put("/operations/indicator/validate/1").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void validateIndicatorWhenIndicatorContributor_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.INDICATOR_CONTRIBUTOR));
when(operationsService.setIndicator(anyString())).thenReturn("1");
mvc.perform(put("/operations/indicator/validate/1").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void validateIndicatorWhenIndicatorFakeRole_ko() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of("fake"));
when(operationsService.setIndicator(anyString())).thenReturn("1");
mvc.perform(put("/operations/indicator/validate/1").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isForbidden());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import fr.insee.rmes.config.auth.user.Stamp;
import fr.insee.rmes.webservice.operations.SeriesResources;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand Down Expand Up @@ -68,131 +70,42 @@ class TestSeriesResourcesEnvProd {
private final String timbre = "XX59-YYY";
int seriesId = 10;

@Test
void getSeriesAdmin_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.ADMIN));
mvc.perform(get("/operations/series/").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getSeriesContributor_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.SERIES_CONTRIBUTOR));
mvc.perform(get("/operations/series/").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getSeriesWithSimsAdmin_ok() throws Exception {
@ValueSource(strings = {
"/operations/series/",
"/operations/series/withSims",
"/operations/series/1",
"/operations/series/advanced-search",
"/operations/series/advanced-search/stamp",
"/operations/series/1/operationsWithReport",
"/operations/series/1/operationsWithoutReport"
})
@ParameterizedTest
void getWithAdmin_Ok(String url) throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.ADMIN));
mvc.perform(get("/operations/series/withSims").header("Authorization", "Bearer toto")
mvc.perform(get(url).header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getSeriesWithSimsContributor_ok() throws Exception {
@ValueSource(strings = {
"/operations/series/",
"/operations/series/withSims",
"/operations/series/1",
"/operations/series/advanced-search",
"/operations/series/advanced-search/stamp",
"/operations/series/1/operationsWithReport",
"/operations/series/1/operationsWithoutReport"
})
@ParameterizedTest
void getWithContributor_Ok(String url) throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.SERIES_CONTRIBUTOR));
mvc.perform(get("/operations/series/withSims").header("Authorization", "Bearer toto")
mvc.perform(get(url).header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getSeriesByIdAdmin_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.ADMIN));
mvc.perform(get("/operations/series/1").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getSeriesByIdContributor_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.SERIES_CONTRIBUTOR));
mvc.perform(get("/operations/series/1").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getAdvancedSearchAdmin_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.ADMIN));
mvc.perform(get("/operations/series/advanced-search").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getAdvancedSearchContributor_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.SERIES_CONTRIBUTOR));
mvc.perform(get("/operations/series/advanced-search").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getAdvancedSearchByStampAdmin_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.ADMIN));
mvc.perform(get("/operations/series/advanced-search/stamp").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getAdvancedSearchByStampContributor_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.SERIES_CONTRIBUTOR));
mvc.perform(get("/operations/series/advanced-search/stamp").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getOperationsWithoutReportAdmin_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.ADMIN));
mvc.perform(get("/operations/series/1/operationsWithoutReport").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getOperationsWithoutReportContributor_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.SERIES_CONTRIBUTOR));
mvc.perform(get("/operations/series/1/operationsWithoutReport").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getOperationsWithReportAdmin_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.ADMIN));
mvc.perform(get("/operations/series/1/operationsWithReport").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void getOperationsWithReportContributor_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.SERIES_CONTRIBUTOR));
mvc.perform(get("/operations/series/1/operationsWithReport").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
@Test
void putSeriesAdmin_ok() throws Exception {
configureJwtDecoderMock(jwtDecoder, idep, timbre, List.of(Roles.ADMIN));
Expand Down

0 comments on commit ac7aa0e

Please sign in to comment.