Skip to content

Commit

Permalink
Merge pull request #248 from kbss-cvut/development
Browse files Browse the repository at this point in the history
Fix vocabulary import ACL issue
  • Loading branch information
ledsoft authored Sep 27, 2023
2 parents 42a81c8 + 87889d0 commit 7d99cd5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<org.apache.tika.tika-core.version>2.7.0</org.apache.tika.tika-core.version>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
<org.springdoc.version>2.2.0</org.springdoc.version>
<cz.cvut.kbss.jopa.version>1.1.2</cz.cvut.kbss.jopa.version>
<cz.cvut.kbss.jopa.version>1.1.3</cz.cvut.kbss.jopa.version>
<cz.cvut.kbss.jsonld.version>0.13.2</cz.cvut.kbss.jsonld.version>
<org.aspectj.version>1.9.20</org.aspectj.version>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,14 @@ public Set<URI> getRelatedVocabularies(Vocabulary entity) {
* matching the one in the imported data
* already exists
*/
@Transactional
@PreAuthorize("@vocabularyAuthorizationService.canCreate()")
public Vocabulary importVocabulary(boolean rename, MultipartFile file) {
return repositoryService.importVocabulary(rename, file);
final Vocabulary imported = repositoryService.importVocabulary(rename, file);
final AccessControlList acl = aclService.createFor(imported);
imported.setAcl(acl.getUri());
eventPublisher.publishEvent(new VocabularyCreatedEvent(imported));
return imported;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import cz.cvut.kbss.termit.model.acl.AccessLevel;
import cz.cvut.kbss.termit.model.acl.UserAccessControlRecord;
import cz.cvut.kbss.termit.model.changetracking.AbstractChangeRecord;
import cz.cvut.kbss.termit.model.util.HasIdentifier;
import cz.cvut.kbss.termit.persistence.context.VocabularyContextMapper;
import cz.cvut.kbss.termit.persistence.snapshot.SnapshotCreator;
import cz.cvut.kbss.termit.service.business.AccessControlListService;
Expand All @@ -25,12 +26,17 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.StandardCharsets;
import java.util.*;

import static cz.cvut.kbss.termit.environment.Environment.termsToDtos;
Expand Down Expand Up @@ -301,4 +307,36 @@ void createSnapshotClonesAccessControlListForNewVocabulary() {
verify(aclService).clone(originalAcl);
assertEquals(cloneAcl.getUri(), snapshotVoc.getAcl());
}

@Test
void importNewVocabularyCreatesAccessControlListForImportedVocabulary() {
final MultipartFile fileToImport = new MockMultipartFile("test.ttl", "content to import".getBytes(
StandardCharsets.UTF_8));
final Vocabulary persisted = Generator.generateVocabularyWithId();
when(repositoryService.importVocabulary(anyBoolean(), any(MultipartFile.class))).thenReturn(persisted);
final AccessControlList acl = Generator.generateAccessControlList(false);
when(aclService.createFor(any(HasIdentifier.class))).thenReturn(acl);

sut.importVocabulary(false, fileToImport);
final InOrder inOrder = inOrder(repositoryService, aclService);
inOrder.verify(repositoryService).importVocabulary(false, fileToImport);
inOrder.verify(aclService).createFor(persisted);
assertEquals(acl.getUri(), persisted.getAcl());
}

@Test
void importNewVocabularyPublishesVocabularyCreatedEvent() {
final MultipartFile fileToImport = new MockMultipartFile("test.ttl", "content to import".getBytes(
StandardCharsets.UTF_8));
final Vocabulary persisted = Generator.generateVocabularyWithId();
when(repositoryService.importVocabulary(anyBoolean(), any(MultipartFile.class))).thenReturn(persisted);
final AccessControlList acl = Generator.generateAccessControlList(false);
when(aclService.createFor(any(HasIdentifier.class))).thenReturn(acl);

sut.importVocabulary(false, fileToImport);
final ArgumentCaptor<ApplicationEvent> captor = ArgumentCaptor.forClass(ApplicationEvent.class);
verify(eventPublisher).publishEvent(captor.capture());
assertInstanceOf(VocabularyCreatedEvent.class, captor.getValue());
assertEquals(persisted, captor.getValue().getSource());
}
}

0 comments on commit 7d99cd5

Please sign in to comment.