Skip to content

Commit

Permalink
Merge branch 'dev' into #343_suggest_change_and_log
Browse files Browse the repository at this point in the history
# Conflicts:
#	registry-service/src/main/java/org/gbif/registry/service/collections/PrimaryCollectionEntityService.java
#	registry-ws/src/main/java/org/gbif/registry/ws/resources/collections/PersonResource.java
  • Loading branch information
marcos-lg committed Apr 29, 2021
2 parents 49cb02c + de459b9 commit ff4f526
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.gbif.api.model.registry.Installation;
import org.gbif.api.model.registry.Network;
import org.gbif.api.model.registry.Organization;
import org.gbif.api.model.registry.Tag;
import org.gbif.api.service.registry.DatasetService;
import org.gbif.registry.events.ChangedComponentEvent;
import org.gbif.registry.events.CreateEvent;
Expand Down Expand Up @@ -100,14 +99,17 @@ public final <T> void updated(UpdateEvent<T> event) {
public final <T> void deleted(DeleteEvent<T> event) {
if (event.getObjectClass().equals(Dataset.class)) {
indexService.delete((Dataset) event.getOldObject());
} else if (event.getObjectClass().equals(Organization.class)) {
indexService.index((Organization) event.getOldObject());
} else if (event.getObjectClass().equals(Network.class)) {
indexService.index((Network) event.getOldObject());
}
}

@Subscribe
public final void updatedComponent(ChangedComponentEvent event) {
// only fire in case of tagged datasets
if (event.getTargetClass().equals(Dataset.class)
&& event.getComponentClass().equals(Tag.class)) {
if (event.getTargetClass().equals(Dataset.class)) {
// we only put tagged datasets onto the queue for this event type!
UUID key = event.getTargetEntityKey();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.gbif.registry.database.TestCaseDatabaseInitializer;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.test.TestDataFactory;
import org.gbif.registry.ws.client.NetworkEntityClient;
import org.gbif.registry.ws.it.fixtures.TestConstants;
import org.gbif.ws.client.filter.SimplePrincipalProvider;
import org.gbif.ws.security.KeyStore;
Expand Down Expand Up @@ -62,11 +63,11 @@
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ContextConfiguration;

import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import org.springframework.test.context.ContextConfiguration;

import static org.gbif.registry.ws.it.LenientAssert.assertLenientEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -79,7 +80,11 @@
* A generic test for all network entities that implement all interfaces required by the
* BaseNetworkEntityResource.
*/
@ContextConfiguration(initializers = {BaseItTest.ContextInitializer.class, BaseItTest.EsContainerContextInitializer.class})
@ContextConfiguration(
initializers = {
BaseItTest.ContextInitializer.class,
BaseItTest.EsContainerContextInitializer.class
})
public abstract class NetworkEntityIT<
T extends
NetworkEntity & Contactable & Taggable & MachineTaggable & Commentable & Endpointable
Expand All @@ -91,8 +96,7 @@ public abstract class NetworkEntityIT<

private final TestDataFactory testDataFactory;

@Autowired
private DataSource dataSource;
@Autowired private DataSource dataSource;

@RegisterExtension
public TestCaseDatabaseInitializer databaseRule = new TestCaseDatabaseInitializer();
Expand Down Expand Up @@ -673,6 +677,16 @@ public void testIdentifierSearch(ServiceType serviceType) {
assertEquals(0, res.getResults().size());
}

@Test
public void updateEntityKeyMismatchTest() {
ServiceType serviceType = ServiceType.CLIENT;
NetworkEntityClient<T> crudClient = (NetworkEntityClient<T>) client;
T entity = create(newEntity(serviceType), serviceType, 1);

assertThrows(
IllegalArgumentException.class, () -> crudClient.updateResource(UUID.randomUUID(), entity));
}

/** @return a new example instance */
protected abstract T newEntity(ServiceType serviceType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ private void setupPrincipal(String name, Enum... roles) {
}
}

/**
* It is not in the scope of this test to test the email bits.
*/
/** It is not in the scope of this test to test the email bits. */
@ParameterizedTest
@EnumSource(ServiceType.class)
public void testEndorsements(ServiceType serviceType) {
Expand Down Expand Up @@ -345,14 +343,28 @@ public void testSetEndorsementsByNonAdmin(ServiceType serviceType) {

// reset principal - use USER role
setupPrincipal(TEST_USER, USER);

OrganizationService userService =
getService(serviceType, organizationResource, userOrganizationClient);

Organization createdOrganization = userService.get(organization.getKey());
createdOrganization.setEndorsementApproved(true);

// make sure an app can not change the endorsementApproved directly
assertThrows(AccessDeniedException.class, () -> userService.update(createdOrganization));
if (serviceType == ServiceType.RESOURCE) {
// we use the resource class directly to use the update method of the API, which is the one that is secured
OrganizationResource userServiceResource =
(OrganizationResource)
getService(serviceType, organizationResource, userOrganizationClient);

// make sure an app can not change the endorsementApproved directly
assertThrows(
AccessDeniedException.class,
() -> userServiceResource.update(createdOrganization.getKey(), createdOrganization));
} else if (serviceType == ServiceType.CLIENT) {
// the client always uses the secured method
// make sure an app can not change the endorsementApproved directly
assertThrows(AccessDeniedException.class, () -> userService.update(createdOrganization));
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.gbif.registry.database.TestCaseDatabaseInitializer;
import org.gbif.registry.identity.service.IdentityService;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.ws.client.collections.CrudClient;
import org.gbif.registry.ws.it.BaseItTest;
import org.gbif.ws.NotFoundException;
import org.gbif.ws.client.filter.SimplePrincipalProvider;
Expand All @@ -46,6 +47,7 @@

import javax.validation.ValidationException;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
Expand Down Expand Up @@ -187,6 +189,19 @@ public void updateInvalidEntityTest(ServiceType serviceType) {
assertThrows(ValidationException.class, () -> service.update(newEntity));
}

@Test
public void updateEntityKeyMismatchTest() {
CrudClient<T> crudClient = (CrudClient<T>) client;

T entity = newEntity();
UUID key = crudClient.create(entity);
T entityCreated = crudClient.get(key);

assertThrows(
IllegalArgumentException.class,
() -> crudClient.updateResource(UUID.randomUUID(), entityCreated));
}

@ParameterizedTest
@EnumSource(ServiceType.class)
public void getMissingEntity(ServiceType serviceType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,10 @@
</select>

<select id="listNetworks" resultType="java.util.UUID">
SELECT network_key
FROM dataset_network
WHERE dataset_key = #{targetEntityKey,jdbcType=OTHER}
SELECT nk.network_key
FROM dataset_network AS nk
JOIN network n ON n.key = nk.network_key AND n.deleted IS NULL
WHERE nk.dataset_key = #{targetEntityKey,jdbcType=OTHER}
</select>

<!-- Joined delete of comment to trigger cascade delete of the join table -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,16 @@ public PagingResponse<T> listByIdentifier(String identifier, Pageable page) {
* @param entity entity that extends NetworkEntity
*/
@PutMapping(value = "{key}", consumes = MediaType.APPLICATION_JSON_VALUE)
@Secured({ADMIN_ROLE, EDITOR_ROLE, IPT_ROLE})
@Validated({PostPersist.class, Default.class})
@Trim
@Transactional
@Secured({ADMIN_ROLE, EDITOR_ROLE, IPT_ROLE})
public void update(@PathVariable("key") UUID key, @Valid @RequestBody @Trim T entity) {
checkArgument(key.equals(entity.getKey()));
update(entity);
}

@Transactional
@Override
public void update(T entity) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class NetworkResource extends BaseNetworkEntityResource<Network> implemen
private final DatasetMapper datasetMapper;
private final NetworkMapper networkMapper;
private final OrganizationMapper organizationMapper;
private final EventManager eventManager;

public NetworkResource(
MapperServiceLocator mapperServiceLocator,
Expand All @@ -74,6 +75,7 @@ public NetworkResource(
Network.class,
eventManager,
withMyBatis);
this.eventManager = eventManager;
this.datasetMapper = mapperServiceLocator.getDatasetMapper();
this.networkMapper = mapperServiceLocator.getNetworkMapper();
this.organizationMapper = mapperServiceLocator.getOrganizationMapper();
Expand Down Expand Up @@ -125,7 +127,7 @@ public PagingResponse<Dataset> listConstituents(
@Override
public void addConstituent(@PathVariable("key") UUID networkKey, @PathVariable UUID datasetKey) {
networkMapper.addDatasetConstituent(networkKey, datasetKey);
ChangedComponentEvent.newInstance(datasetKey, Dataset.class, Network.class);
eventManager.post(ChangedComponentEvent.newInstance(datasetKey, Dataset.class, Network.class));
}

@DeleteMapping("{key}/constituents/{datasetKey}")
Expand All @@ -134,7 +136,7 @@ public void addConstituent(@PathVariable("key") UUID networkKey, @PathVariable U
public void removeConstituent(
@PathVariable("key") UUID networkKey, @PathVariable UUID datasetKey) {
networkMapper.deleteDatasetConstituent(networkKey, datasetKey);
ChangedComponentEvent.newInstance(datasetKey, Dataset.class, Network.class);
eventManager.post(ChangedComponentEvent.newInstance(datasetKey, Dataset.class, Network.class));
}

@GetMapping("suggest")
Expand Down

0 comments on commit ff4f526

Please sign in to comment.