Improve security filter code.

This commit is contained in:
Paulo Gustavo Veiga 2024-01-22 23:08:04 -08:00
parent cdbeaf5216
commit 861b4f22fd
2 changed files with 212 additions and 214 deletions

View File

@ -46,7 +46,7 @@ public class RestAccountControllerTest {
@Autowired @Autowired
private TestRestTemplate restTemplate; private TestRestTemplate restTemplate;
static public RestAccountControllerTest create(TestRestTemplate restTemplate) { static public RestAccountControllerTest create(@NotNull TestRestTemplate restTemplate) {
final RestAccountControllerTest result = new RestAccountControllerTest(); final RestAccountControllerTest result = new RestAccountControllerTest();
result.restTemplate = restTemplate; result.restTemplate = restTemplate;
return result; return result;

View File

@ -3,6 +3,7 @@ package com.wisemapping.test.rest;
import com.wisemapping.config.common.CommonConfig; import com.wisemapping.config.common.CommonConfig;
import com.wisemapping.config.rest.RestAppConfig; import com.wisemapping.config.rest.RestAppConfig;
import com.wisemapping.exceptions.WiseMappingException;
import com.wisemapping.rest.AdminController; import com.wisemapping.rest.AdminController;
import com.wisemapping.rest.MindmapController; import com.wisemapping.rest.MindmapController;
import com.wisemapping.rest.UserController; import com.wisemapping.rest.UserController;
@ -22,6 +23,7 @@ import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import static com.wisemapping.test.rest.RestHelper.createHeaders; import static com.wisemapping.test.rest.RestHelper.createHeaders;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -201,198 +203,193 @@ public class RestMindmapControllerTest {
assertEquals(response.getXml(), xml); assertEquals(response.getXml(), xml);
} }
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function") @Test
// public void updateStarred(final @NotNull MediaType mediaType) { // Configure media types ... public void updateStarred() { // Configure media types ...
// final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
// RestTemplate template = createTemplate(userEmail); final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
//
// // Create a sample map ... // Create a sample map ...
// final String title1 = "Stared Map user 1"; final String title1 = "Stared Map user 1";
// URI mapUri = addNewMap(template, title1); URI mapUri = addNewMap(restTemplate, title1);
//
// // Update starred ... // Update starred ...
// final String resourceUrl = HOST_PORT + mapUri.toString() + "/starred"; final String resourceUrl = mapUri.toString() + "/starred";
// requestHeaders.setContentType(MediaType.APPLICATION_JSON); requestHeaders.setContentType(MediaType.APPLICATION_JSON);
//
// final HttpHeaders textContentType = new HttpHeaders(); final HttpHeaders textContentType = new HttpHeaders();
// textContentType.setContentType(MediaType.TEXT_PLAIN); textContentType.setContentType(MediaType.TEXT_PLAIN);
// final HttpEntity<String> updateEntity = new HttpEntity<>("true", textContentType); final HttpEntity<String> updateEntity = new HttpEntity<>("true", textContentType);
// template.put(resourceUrl, updateEntity); restTemplate.put(resourceUrl, updateEntity);
//
// // Has been updated ?. // Has been updated ?.
//
// final HttpEntity findLabelEntity = new HttpEntity(createHeaders(MediaType.TEXT_PLAIN)); final HttpEntity<String> findLabelEntity = new HttpEntity<>(createHeaders(MediaType.TEXT_PLAIN));
// final ResponseEntity<String> response = template.exchange(resourceUrl, HttpMethod.GET, findLabelEntity, String.class); final ResponseEntity<String> response = restTemplate.exchange(resourceUrl, HttpMethod.GET, findLabelEntity, String.class);
//
// assertTrue(Boolean.parseBoolean(response.getBody()), "Starred has been updated"); assertTrue(Boolean.parseBoolean(response.getBody()), "Starred has been updated");
// } }
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void verifyMapOwnership(final @NotNull MediaType mediaType) { // Configure media types ... @Test
// final RestAdminITCase restAdminITCase = new RestAdminITCase(); public void verifyMapOwnership() {
// final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
// RestTemplate template = createTemplate(userEmail); final TestRestTemplate firstUser = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
//
// // Create a sample map ... // Create a sample map ...
// final String title1 = "verifyMapOwnership Map user 1"; final String title1 = "verifyMapOwnership Map user 1";
// addNewMap(template, title1); addNewMap(firstUser, title1);
//
// //create another user //create another user
// RestUser secondUser = restAdminITCase.createNewUserAndGetUser(MediaType.APPLICATION_JSON); final RestUser secondUser = RestAccountControllerTest.create(this.restTemplate).createNewUser();
// final RestTemplate secondTemplate = createTemplate(secondUser.getEmail() + ":admin"); final TestRestTemplate secondTemplate = this.restTemplate.withBasicAuth(secondUser.getEmail(), secondUser.getPassword());
//
// final String title2 = "verifyMapOwnership Map user 2"; final String title2 = "verifyMapOwnership Map user 2";
// addNewMap(secondTemplate, title2); addNewMap(secondTemplate, title2);
//
// // Delete user ... final TestRestTemplate superadminTemplate = this.restTemplate.withBasicAuth("admin@wisemapping.org", "test");
// String authorisation = "admin@wisemapping.org" + ":" + "test"; superadminTemplate.delete("/admin/users/" + secondUser.getId());
// RestTemplate superadminTemplate = createTemplate(authorisation);
// // Validate that the two maps are there ...
// superadminTemplate.delete(BASE_REST_URL + "/admin/users/" + secondUser.getId()); final RestMindmapList body = fetchMaps(requestHeaders, firstUser);
// final List<RestMindmapInfo> mindmaps = body.getMindmapsInfo();
// // Validate that the two maps are there ...
// final RestMindmapList body = fetchMaps(requestHeaders, template); final Optional<RestMindmapInfo> any = mindmaps.stream().filter(m -> m.getTitle().equals(title1)).findAny();
// final List<RestMindmapInfo> mindmaps = body.getMindmapsInfo(); assertTrue(any.isPresent(), "Map could not be found");
// }
// boolean found1 = false;
// for (RestMindmapInfo mindmap : mindmaps) { @Test
// if (mindmap.getTitle().equals(title1)) { public void updateMap() throws IOException, WiseMappingException {
// found1 = true; final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
// break; final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// }
// } // Create a sample map ...
// assertTrue(found1, "Map could not be found"); final String title = "Update sample ";
// } final URI resourceUri = addNewMap(restTemplate, title);
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function") // Build map to update ...
// public void updateMap(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ... final RestMindmap mapToUpdate = new RestMindmap();
// final HttpHeaders requestHeaders = createHeaders(mediaType); mapToUpdate.setXml("<map>this is not valid</map>");
// final RestTemplate template = createTemplate(userEmail); mapToUpdate.setProperties("{zoom:x}");
//
// // Create a sample map ... // Create lock ...
// final String title = "Update sample " + mediaType; final HttpHeaders lockHeaders = createHeaders(MediaType.APPLICATION_JSON);
// final URI resourceUri = addNewMap(template, title); lockHeaders.setContentType(MediaType.TEXT_PLAIN);
//
// // Build map to update ... // Update map ...
// final RestMindmap mapToUpdate = new RestMindmap(); final String resourceUrl = resourceUri.toString() + "/document";
// mapToUpdate.setXml("<map>this is not valid</map>"); requestHeaders.setContentType(MediaType.APPLICATION_JSON);
// mapToUpdate.setProperties("{zoom:x}");
// final HttpEntity<RestMindmap> updateEntity = new HttpEntity<>(mapToUpdate, requestHeaders);
// // Create lock ... restTemplate.put(resourceUrl, updateEntity);
// final HttpHeaders lockHeaders = createHeaders(mediaType);
// lockHeaders.setContentType(MediaType.TEXT_PLAIN); // Check that the map has been updated ...
// HttpEntity<RestUser> findMapEntity = new HttpEntity<>(requestHeaders);
// // Update map ... final ResponseEntity<RestMindmap> response = restTemplate.exchange(resourceUri, HttpMethod.GET, findMapEntity, RestMindmap.class);
// final String resourceUrl = HOST_PORT + resourceUri.toString() + "/document"; assertEquals(response.getBody().getXml(), mapToUpdate.getXml());
// requestHeaders.setContentType(MediaType.APPLICATION_JSON); assertEquals(response.getBody().getProperties(), mapToUpdate.getProperties());
// final HttpEntity<RestMindmap> updateEntity = new HttpEntity<>(mapToUpdate, requestHeaders);
// template.put(resourceUrl, updateEntity); // Unlock ...
// HttpEntity<String> lockEntity = new HttpEntity<>("false", lockHeaders);
// // Check that the map has been updated ... restTemplate.exchange(resourceUri + "/lock", HttpMethod.PUT, lockEntity, RestLockInfo.class);
// HttpEntity<RestUser> findMapEntity = new HttpEntity<>(requestHeaders); }
// final ResponseEntity<RestMindmap> response = template.exchange(HOST_PORT + resourceUri, HttpMethod.GET, findMapEntity, RestMindmap.class);
// assertEquals(response.getBody().getXml(), mapToUpdate.getXml());
// assertEquals(response.getBody().getProperties(), mapToUpdate.getProperties()); @Test
// public void addCollabs() {
// // Unlock ... final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
// HttpEntity<String> lockEntity = new HttpEntity<>("false", lockHeaders); final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// template.exchange(HOST_PORT + resourceUri + "/lock", HttpMethod.PUT, lockEntity, RestLockInfo.class);
// } // Create a sample map ...
// final URI resourceUri = addNewMap(restTemplate, "Map for addCollabs - ");
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void addCollabs(final @NotNull MediaType mediaType) { String newCollab = addNewCollaboration(requestHeaders, restTemplate, resourceUri);
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// final RestTemplate template = createTemplate(userEmail); // Has been added ?
// RestCollaborationList responseCollbs = fetchAndGetCollabs(requestHeaders, restTemplate, resourceUri);
// // Create a sample map ...
// final URI resourceUri = addNewMap(template, "Map for addCollabs - " + mediaType); // Has been added ?
// assertEquals(responseCollbs.getCount(), 2);
// String newCollab = addNewCollaboration(requestHeaders, template, resourceUri);
// final Optional<RestCollaboration> addedCollab = responseCollbs.getCollaborations().stream().filter(c -> c.getEmail().equals(newCollab)).findAny();
// // Has been added ? assertTrue(addedCollab.isPresent());
// RestCollaborationList responseCollbs = fetchAndGetCollabs(requestHeaders, template, resourceUri); assertEquals(addedCollab.get().getRole(), "editor");
// }
// // Has been added ?
// assertEquals(responseCollbs.getCount(), 2); @Test
// public void updateCollabType() {
// final Optional<RestCollaboration> addedCollab = responseCollbs.getCollaborations().stream().filter(c -> c.getEmail().equals(newCollab)).findAny(); final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
// assertTrue(addedCollab.isPresent()); final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// assertEquals(addedCollab.get().getRole(), "editor");
// } // Create a sample map ...
// final URI resourceUri = addNewMap(restTemplate, "Map for updateCollabType");
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void updateCollabType(final @NotNull MediaType mediaType) { // Add a new collaboration ...
// final HttpHeaders requestHeaders = createHeaders(mediaType); requestHeaders.setContentType(MediaType.APPLICATION_JSON);
// final RestTemplate template = createTemplate(userEmail); final RestCollaborationList collabs = new RestCollaborationList();
// collabs.setMessage("Adding new permission");
// // Create a sample map ...
// final URI resourceUri = addNewMap(template, "Map for updateCollabType - " + mediaType); final String newCollab = "new-collab@example.com";
// String role = "editor";
// // Add a new collaboration ...
// requestHeaders.setContentType(MediaType.APPLICATION_JSON); final RestCollaboration collab = addCollabToList(newCollab, role, collabs);
// final RestCollaborationList collabs = new RestCollaborationList();
// collabs.setMessage("Adding new permission"); final HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
// restTemplate.put(resourceUri + "/collabs/", updateEntity);
// final String newCollab = "new-collab@example.com";
// String role = "editor"; // Has been added ?
// RestCollaborationList responseCollbs = fetchAndGetCollabs(requestHeaders, restTemplate, resourceUri);
// final RestCollaboration collab = addCollabToList(newCollab, role, collabs); assertEquals(responseCollbs.getCount(), 2);
//
// final HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders); // Update the collaboration type ...
// template.put(HOST_PORT + resourceUri + "/collabs/", updateEntity); collab.setRole("viewer");
// restTemplate.put(resourceUri + "/collabs/", updateEntity);
// // Has been added ?
// RestCollaborationList responseCollbs = fetchAndGetCollabs(requestHeaders, template, resourceUri); // Has been added ?
// assertEquals(responseCollbs.getCount(), 2); final ResponseEntity<RestCollaborationList> afterResponse = fetchCollabs(requestHeaders, restTemplate, resourceUri);
// final Optional<RestCollaboration> updatedCollab = afterResponse.getBody().getCollaborations().stream().filter(c -> c.getEmail().equals(newCollab)).findAny();
// // Update the collaboration type ... assertTrue(updatedCollab.isPresent());
// collab.setRole("viewer"); assertEquals(updatedCollab.get().getRole(), "viewer");
// template.put(HOST_PORT + resourceUri + "/collabs/", updateEntity); }
//
// // Has been added ?
// final ResponseEntity<RestCollaborationList> afterResponse = fetchCollabs(requestHeaders, template, resourceUri); @Test
// final Optional<RestCollaboration> updatedCollab = afterResponse.getBody().getCollaborations().stream().filter(c -> c.getEmail().equals(newCollab)).findAny(); public void deleteCollabs() {
// assertTrue(updatedCollab.isPresent()); final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
// assertEquals(updatedCollab.get().getRole(), "viewer"); final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// }
// // Create a sample map ...
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function") final URI resourceUri = addNewMap(restTemplate, "Map for deleteCollabs - ");
// public void deleteCollabs(final @NotNull MediaType mediaType) {
// final HttpHeaders requestHeaders = createHeaders(mediaType); String newCollab = addNewCollaboration(requestHeaders, restTemplate, resourceUri);
// final RestTemplate template = createTemplate(userEmail);
// // Has been added ?
// // Create a sample map ... RestCollaborationList responseCollbs = fetchAndGetCollabs(requestHeaders, restTemplate, resourceUri);
// final URI resourceUri = addNewMap(template, "Map for deleteCollabs - " + mediaType);
// // Has been added ?
// String newCollab = addNewCollaboration(requestHeaders, template, resourceUri); assertEquals(responseCollbs.getCount(), 2);
//
// // Has been added ? // Now, remove it ...
// RestCollaborationList responseCollbs = fetchAndGetCollabs(requestHeaders, template, resourceUri); restTemplate.delete(resourceUri + "/collabs?email=" + newCollab);
//
// // Has been added ? // Check that it has been removed ...
// assertEquals(responseCollbs.getCount(), 2); final ResponseEntity<RestCollaborationList> afterDeleteResponse = fetchCollabs(requestHeaders, restTemplate, resourceUri);
// assertEquals(afterDeleteResponse.getBody().getCollaborations().size(), 1);
// // Now, remove it ... }
// template.delete(HOST_PORT + resourceUri + "/collabs?email=" + newCollab);
// private String addNewCollaboration(final HttpHeaders requestHeaders, final TestRestTemplate template, final URI resourceUri) throws RestClientException {
// // Check that it has been removed ... // Add a new collaboration ...
// final ResponseEntity<RestCollaborationList> afterDeleteResponse = fetchCollabs(requestHeaders, template, resourceUri); requestHeaders.setContentType(MediaType.APPLICATION_JSON);
// assertEquals(afterDeleteResponse.getBody().getCollaborations().size(), 1); final RestCollaborationList collabs = new RestCollaborationList();
// } collabs.setMessage("Adding new permission");
// final String newCollab = "new-collab@example.com";
// private String addNewCollaboration(final HttpHeaders requestHeaders, final RestTemplate template, final URI resourceUri) throws RestClientException { String role = "editor";
// // Add a new collaboration ... addCollabToList(newCollab, role, collabs);
// requestHeaders.setContentType(MediaType.APPLICATION_JSON); final HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
// final RestCollaborationList collabs = new RestCollaborationList(); template.put(resourceUri + "/collabs/", updateEntity);
// collabs.setMessage("Adding new permission"); return newCollab;
// final String newCollab = "new-collab@example.com"; }
// String role = "editor";
// addCollabToList(newCollab, role, collabs);
// final HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
// template.put(HOST_PORT + resourceUri + "/collabs/", updateEntity);
// return newCollab;
// }
// //
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function") // @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void deleteCollabsWithInvalidEmail(final @NotNull MediaType mediaType) { // public void deleteCollabsWithInvalidEmail(final @NotNull MediaType mediaType) {
@ -457,11 +454,12 @@ public class RestMindmapControllerTest {
// } // }
// } // }
// //
// @NotNull @NotNull
// private ResponseEntity<RestCollaborationList> fetchCollabs(HttpHeaders requestHeaders, RestTemplate template, URI resourceUri) { private ResponseEntity<RestCollaborationList> fetchCollabs(HttpHeaders requestHeaders, TestRestTemplate template, URI resourceUri) {
// final HttpEntity findCollabs = new HttpEntity(requestHeaders); final HttpEntity<RestCollaborationList> findCollabs = new HttpEntity(requestHeaders);
// return template.exchange(HOST_PORT + resourceUri + "/collabs", HttpMethod.GET, findCollabs, RestCollaborationList.class); return template.exchange(resourceUri + "/collabs", HttpMethod.GET, findCollabs, RestCollaborationList.class);
// } }
// //
// @Test(dataProviderClass = RestHelper.class, expectedExceptions = {HttpClientErrorException.class}, dataProvider = "ContentType-Provider-Function") // @Test(dataProviderClass = RestHelper.class, expectedExceptions = {HttpClientErrorException.class}, dataProvider = "ContentType-Provider-Function")
// public void addCollabsInvalidOwner(final @NotNull MediaType mediaType) { // public void addCollabsInvalidOwner(final @NotNull MediaType mediaType) {
@ -795,21 +793,21 @@ public class RestMindmapControllerTest {
return Objects.requireNonNull(response.getBody()); return Objects.requireNonNull(response.getBody());
} }
//
// private RestCollaborationList fetchAndGetCollabs(final HttpHeaders requestHeaders, final RestTemplate template, final URI resourceUri) { private RestCollaborationList fetchAndGetCollabs(final HttpHeaders requestHeaders, final TestRestTemplate template, final URI resourceUri) {
// final ResponseEntity<RestCollaborationList> response = fetchCollabs(requestHeaders, template, resourceUri); final ResponseEntity<RestCollaborationList> response = fetchCollabs(requestHeaders, template, resourceUri);
// RestCollaborationList responseCollbs = response.getBody(); RestCollaborationList responseCollbs = response.getBody();
// return responseCollbs; return responseCollbs;
// } }
//
// private RestCollaboration addCollabToList(String newCollab, String role, RestCollaborationList collabs) { private RestCollaboration addCollabToList(String newCollab, String role, RestCollaborationList collabs) {
// RestCollaboration collab = new RestCollaboration(); RestCollaboration collab = new RestCollaboration();
// collab.setEmail(newCollab); collab.setEmail(newCollab);
// collab.setRole(role); collab.setRole(role);
// collabs.addCollaboration(collab); collabs.addCollaboration(collab);
// return collab; return collab;
// } }
//
private RestMindmap findMap(@NotNull HttpHeaders requestHeaders, @NotNull TestRestTemplate template, URI resourceUri) { private RestMindmap findMap(@NotNull HttpHeaders requestHeaders, @NotNull TestRestTemplate template, URI resourceUri) {
final HttpEntity findMapEntity = new HttpEntity(requestHeaders); final HttpEntity findMapEntity = new HttpEntity(requestHeaders);
final ResponseEntity<RestMindmap> response = template.exchange("http://localhost:8081/" + resourceUri.toString(), HttpMethod.GET, findMapEntity, RestMindmap.class); final ResponseEntity<RestMindmap> response = template.exchange("http://localhost:8081/" + resourceUri.toString(), HttpMethod.GET, findMapEntity, RestMindmap.class);