Skip to content

Commit

Permalink
ldap, console: new field orgUniqueId georchestra#4374 georchestra#4372
Browse files Browse the repository at this point in the history
…georchestra/georchestra-gateway#159

sec-proxy-spring-integ - method to get org by field orgUniqueId

Reverse console package-lock

Fix & valide console tests

Change way to validate org unicity

console - use mvn formatter

console - change test
  • Loading branch information
Gaetanbrl committed Jan 8, 2025
1 parent 870d656 commit 9e6d548
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public Org updateOrgInfos(@PathVariable String commonName, HttpServletRequest re
// Parse Json
JSONObject json = this.parseRequest(request);

if (!this.validation.validateOrgUnicity(json)) {
if (!this.validation.validateOrgUnicity(orgDao, json)) {
throw new IOException("Organization : already exists");
}

Expand Down Expand Up @@ -284,7 +284,7 @@ public Org createOrg(HttpServletRequest request) throws IOException, JSONExcepti
// Parse Json
JSONObject json = this.parseRequest(request);

if (!this.validation.validateOrgUnicity(json)) {
if (!this.validation.validateOrgUnicity(orgDao, json)) {
throw new IOException("An organization with this identification number already exists.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import javax.servlet.http.HttpSession;

import com.google.common.annotations.VisibleForTesting;
import org.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -410,7 +412,6 @@ private void validateFields(@ModelAttribute AccountFormBean formBean, BindingRes
validation.validateUserField("title", formBean.getTitle(), result);
validation.validateUserField("description", formBean.getDescription(), result);

// unique orgUniqueId value
if (formBean.getCreateOrg() && !result.hasErrors()) {
validation.validateOrgField("name", formBean.getOrgName(), result);
validation.validateOrgField("shortName", formBean.getOrgShortName(), result);
Expand All @@ -422,7 +423,8 @@ private void validateFields(@ModelAttribute AccountFormBean formBean, BindingRes
validation.validateOrgField("orgUniqueId", formBean.getOrgUniqueId(), result);
validation.validateUrlFieldWithSpecificMsg("orgUrl", formBean.getOrgUrl(), result);

validation.validateOrgUniqueIdField(formBean.getOrgUniqueId(), null, result);
JSONObject orgToValidate = new JSONObject().put("orgUniqueId", formBean.getOrgUniqueId());
validation.validateOrgUniqueIdField(this.orgDao, orgToValidate, result);
} else {
validation.validateUserField("org", formBean.getOrg(), result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@
*/
public class Validation {

@Autowired
private OrgsDao orgDao;

private Set<String> requiredUserFields;
private Set<String> requiredOrgFields;

Expand Down Expand Up @@ -200,40 +197,59 @@ public boolean validateUrlFieldWithSpecificMsg(String fullyQualifiedField, Strin
return true;
}

public boolean validateOrgUnicityByUniqueId(String orgUniqueId, String uuid) {
public boolean validateOrgUnicityByUniqueId(OrgsDao orgDao, JSONObject changes) {
String orgUniqueId = "";
String uuid = "";

if (changes == null) {
return true;
}
// case - not value to verify
if (!changes.has("orgUniqueId")) {
return true;
}

orgUniqueId = changes.getString("orgUniqueId");

if (orgUniqueId == null || orgUniqueId.isEmpty()) {
return true;
}

// test if org with same value already exists
Org findByOrgUniqueId = this.orgDao.findByOrgUniqueId(orgUniqueId);
Org findByOrgUniqueId = orgDao.findByOrgUniqueId(orgUniqueId);
// No org exists with this field value
if (findByOrgUniqueId == null) {
return true;
}

// No uuid to compare means that's an org creation
// and at this step, an org already exists with this orgUniqueId
if (uuid == null) {
if (!changes.has("uuid")) {
// we can't validate this orgUniqueId that already exists
return false;
}

uuid = changes.getString("uuid");

if (uuid.isEmpty() || uuid == null) {
return false;
}
// Control if this is same org update
return Objects.equals(findByOrgUniqueId.getUniqueIdentifier(), UUID.fromString(uuid));
}

public boolean validateOrgUniqueIdField(String orgUniqueId, String uniqueId, Errors errors) {
if (!this.validateOrgUnicityByUniqueId(orgUniqueId, uniqueId)) {
public boolean validateOrgUniqueIdField(OrgsDao orgDao, JSONObject changes, Errors errors) {
if (changes.has("orgUniqueId") && !this.validateOrgUnicityByUniqueId(orgDao, changes)) {
errors.rejectValue("orgUniqueId", "error.orgUniqueIdExists", "orgUniqueIdExists");
return false;
}
return true;
}

public boolean validateOrgUnicity(JSONObject json) {
boolean isUniqueOrg = true;
if (json.has("orgUniqueId")) {
isUniqueOrg = this.validateOrgUnicityByUniqueId(json.getString("orgUniqueId"), json.getString("uuid"));
public boolean validateOrgUnicity(OrgsDao orgDao, JSONObject changes) {
if (changes.has("orgUniqueId") && !this.validateOrgUnicityByUniqueId(orgDao, changes)) {
return false;
}
return isUniqueOrg;
return true;
}
}
6 changes: 0 additions & 6 deletions console/src/main/webapp/manager/brunch-config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
module.exports = {
config: {
// Changer le port par défaut ici
server: {
port: 4000, // Changer 4000 par le port de votre choix
path: 'http://localhost:4000',
run: true
},
plugins: {
ng_templates: {
module: 'manager',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.eq;

import java.io.IOException;
import java.io.PrintWriter;
Expand All @@ -29,6 +30,7 @@
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
Expand Down Expand Up @@ -101,6 +103,9 @@ public void updateOrgUpateDelegation() throws IOException, SQLException {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContent(reqUsr.toString().getBytes());

// when(toTest.validation.validateOrgUnicity(mockOrgsDao,
// reqUsr)).thenReturn(true);

toTest.updateOrgInfos("csc", request);

verify(mockOrgsDao).update(mockOrg);
Expand All @@ -121,6 +126,9 @@ private OrgsController createToTest() throws SQLException {
Validation mockValidation = mock(Validation.class);
when(mockValidation.validateOrgField(anyString(), any(JSONObject.class))).thenReturn(true);
when(mockValidation.validateUrl(anyString())).thenReturn(true);
JSONObject mockChanges = new JSONObject();
when(mockValidation.validateOrgUnicity(mockOrgsDao, mockChanges)).thenReturn(true);
when(mockValidation.validateOrgUnicity(eq(mockOrgsDao), any(JSONObject.class))).thenReturn(true);
OrgsController toTest = new OrgsController(mockOrgsDao);
toTest.delegationDao = delegationDaoMock;
toTest.advancedDelegationDao = advancedDelegationDaoMock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void setUp() throws Exception {
formBean.setAddress("48 Avenue du Lac du Bourget. 73377 Le Bourget-du-Lac");
formBean.setUrl("https://georchestra.org");
formBean.setId("georTest");
formBean.setOrgUniqueId("171335413");

Org org = new Org();
org.setId("georTest");
Expand All @@ -68,6 +69,7 @@ public void setUp() throws Exception {
org.setAddress("fake address");
org.setUrl("https://georchestra.org");
org.setDescription("A test desc");
org.setOrgUniqueId("541874843435");

Mockito.when(this.orgsDao.findByCommonName(Mockito.eq("georTest"))).thenReturn(org);
Mockito.when(this.orgsDao.findByCommonName(Mockito.eq(""))).thenReturn(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
package org.georchestra.console.ws.utils;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import org.georchestra.ds.orgs.Org;
import org.georchestra.ds.orgs.OrgsDao;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.validation.Errors;
import org.springframework.validation.MapBindingResult;

import org.json.JSONObject;

public class ValidationTest {

private OrgsDao mockOrgsDao;
private Org mockOrg;

@Test
public void testHardCodedUserFields() {

Expand Down Expand Up @@ -123,4 +134,55 @@ public void validateBadUrl() {

Assert.assertTrue(v.validateUrlFieldWithSpecificMsg("orgUrl", "http://www.hereisthefish.org", errors));
}

@Test
public void validateBadOrgUnicityUpdate() {
Validation v = new Validation("");

UUID fakeUUID = UUID.randomUUID();
String fakeOrgUniqueId = "5413513131";

mockOrgsDao = mock(OrgsDao.class);

Org mockOrg = new Org();
mockOrg.setId("georTest");
mockOrg.setName("geOrchestra testing LLC");
mockOrg.setOrgType("Non profit");
mockOrg.setAddress("fake address");
mockOrg.setUrl("https://georchestra.org");
mockOrg.setDescription("A test desc");
mockOrg.setOrgUniqueId(fakeOrgUniqueId);
mockOrg.setUniqueIdentifier(fakeUUID);

when(mockOrgsDao.findByOrgUniqueId(fakeOrgUniqueId)).thenReturn(mockOrg);

/** Fake JSON from request */
JSONObject mockChanges = new JSONObject();
mockChanges.put("name", "foo");
mockChanges.put("orgType", "Non profit");

// create org without orguniqueId + without uuid
Assert.assertTrue(v.validateOrgUnicity(mockOrgsDao, mockChanges));

// create org but orgUniqueId already exists
mockChanges.put("orgUniqueId", fakeOrgUniqueId);
Assert.assertFalse(v.validateOrgUnicity(mockOrgsDao, mockChanges));

// create org with orgUniqueId that not exists
mockChanges.put("orgUniqueId", "11113513");
Assert.assertTrue(v.validateOrgUnicity(mockOrgsDao, mockChanges));

// update org - no UUID and orgUniqueId already exists
mockChanges.put("orgUniqueId", fakeOrgUniqueId);
Assert.assertFalse(v.validateOrgUnicityByUniqueId(mockOrgsDao, mockChanges));

// update org - unknown UUID + orgUniqueId not already exists
mockChanges.put("uuid", UUID.randomUUID().toString());
Assert.assertFalse(v.validateOrgUnicityByUniqueId(mockOrgsDao, mockChanges));

// update org - verified UUID + orgUniqueId not already exists
mockChanges.put("uuid", fakeUUID.toString());
Assert.assertTrue(v.validateOrgUnicityByUniqueId(mockOrgsDao, mockChanges));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ public Optional<Organization> findByShortName(String shortName) {
return client.get("/console/internal/organizations/shortname/{name}", Organization.class, shortName);
}

@Override
public Optional<Organization> findByOrgUniqueId(String orgUniqueId) {
return client.get("/console/internal/organizations/unique/{id}", Organization.class, orgUniqueId);
}

@Override
public Optional<byte[]> getLogo(String id) {
return client.get("/console/internal/organizations/id/{id}/logo", byte[].class, id);
}

}

0 comments on commit 9e6d548

Please sign in to comment.