Skip to content

Commit

Permalink
feat: when checking stamps for structure and component, contributors … (
Browse files Browse the repository at this point in the history
#821)

* feat: when checking stamps for structure and component, contributors is an arrau

* fix: solve unit test

* fix: solve sonar issue
  • Loading branch information
EmmanuelDemey authored Dec 3, 2024
1 parent b5ffed3 commit cc3f75e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.insee.rmes.config.auth.roles.Roles;
import fr.insee.rmes.config.auth.user.Stamp;
import fr.insee.rmes.exceptions.RmesRuntimeBadRequestException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -173,6 +174,9 @@ private boolean checkStampIsContributor(String body) {
private static @Nullable String extractContributorStampFromBody(String body) {
return (new JSONObject(body)).optString("contributor");
}
private static @Nullable JSONArray extractContributorStampsFromBody(String body) {
return (new JSONObject(body)).optJSONArray("contributor");
}

//for PUT and DELETE structure
public boolean isStructureContributor(String structureId){
Expand All @@ -183,7 +187,13 @@ public boolean isStructureContributor(String structureId){
// for POST structure or component
public boolean isStructureAndComponentContributor(String body) {
logger.trace("Check if {} can create the structure or component", methodSecurityExpressionRoot.getPrincipal());
return hasRole(Roles.STRUCTURES_CONTRIBUTOR)&& checkStampIsContributor(body);
Optional<Stamp> stamp = getStamp();
JSONArray contributors = extractContributorStampsFromBody(body);

if(contributors == null){
return false;
}
return hasRole(Roles.STRUCTURES_CONTRIBUTOR) && contributors.toList().stream().anyMatch(s -> ((String) s).equalsIgnoreCase(stamp.get().stamp()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import fr.insee.rmes.config.auth.security.DefaultSecurityContext;
import fr.insee.rmes.config.auth.security.OpenIDConnectSecurityContext;
import fr.insee.rmes.config.auth.user.Stamp;
import fr.insee.rmes.model.ValidationStatus;
import fr.insee.rmes.webservice.StructureResources;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand All @@ -28,7 +27,6 @@

import static fr.insee.rmes.integration.authorizations.TokenForTestsConfiguration.*;
import static fr.insee.rmes.integration.authorizations.TokenForTestsConfiguration.KEY_FOR_ROLES_IN_ROLE_CLAIM;
import static fr.insee.rmes.model.ValidationStatus.UNPUBLISHED;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -68,7 +66,6 @@ class TestStructuresResourcesEnvProd {

int structureId=10;
int componentId=12;
ValidationStatus status= UNPUBLISHED;

@Test
void putStructureAdmin_ok() throws Exception {
Expand Down Expand Up @@ -139,7 +136,7 @@ void postStructureAsStructureContributor_ok() throws Exception {
mvc.perform(post("/structures/structure").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{\"id\": \"1\",\"contributor\": \""+timbre+"\"}"))
.content("{\"id\": \"1\",\"contributor\": [\""+timbre+"\"]}"))
.andExpect(status().isOk());
}

Expand Down Expand Up @@ -168,7 +165,7 @@ void postStructureAsStructureContributorWrongStamp_ko() throws Exception {
mvc.perform(post("/structures/structure").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{\"id\": \"1\",\"contributor\": \"wrong\"}"))
.content("{\"id\": \"1\",\"contributor\": [\"wrong\"]}"))
.andExpect(status().isForbidden());
}

Expand Down Expand Up @@ -228,7 +225,7 @@ void postComponentAsStructureContributor_ok() throws Exception {
mvc.perform(post("/structures/components").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{\"id\": \"1\",\"contributor\": \""+timbre+"\"}"))
.content("{\"id\": \"1\",\"contributor\": [\""+timbre+"\"]}"))
.andExpect(status().isCreated());
}

Expand Down Expand Up @@ -257,7 +254,7 @@ void postComponentAsStructureContributorWrongStamp_ko() throws Exception {
mvc.perform(post("/structures/components").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{\"id\": \"1\",\"contributor\": \"wrong\"}"))
.content("{\"id\": \"1\",\"contributor\": [\"wrong\"]}"))
.andExpect(status().isForbidden());
}

Expand Down

0 comments on commit cc3f75e

Please sign in to comment.