diff --git a/dbrepo-metadata-service/rest-service/src/main/java/at/tuwien/endpoints/IdentifierEndpoint.java b/dbrepo-metadata-service/rest-service/src/main/java/at/tuwien/endpoints/IdentifierEndpoint.java index a6d9f56143ea208b62444e7366d6e473e17d5e2d..518b68180c8c5861ce744808fa6a858efef5c545 100644 --- a/dbrepo-metadata-service/rest-service/src/main/java/at/tuwien/endpoints/IdentifierEndpoint.java +++ b/dbrepo-metadata-service/rest-service/src/main/java/at/tuwien/endpoints/IdentifierEndpoint.java @@ -37,9 +37,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import java.security.Principal; -import java.time.Instant; import java.util.List; -import java.util.UUID; import java.util.stream.Collectors; @Log4j2 @@ -183,18 +181,18 @@ public class IdentifierEndpoint { final Database database = databaseService.find(data.getDatabaseId()); switch (data.getType()) { case VIEW -> { - if (data.getDatabaseId() == null || data.getQueryId() != null || data.getViewId() == null || data.getTableId() != null) { + if (data.getQueryId() != null || data.getViewId() == null || data.getTableId() != null) { log.error("Failed to create view identifier: only parameters database_id & view_id must be present"); throw new IdentifierRequestException("Failed to create view identifier: only parameters database_id & view_id must be present"); } - final View view = viewService.findById(data.getViewId()); + final View view = viewService.findById(data.getDatabaseId(), data.getViewId()); if (!endpointValidator.validateOnlyMineOrReadAccessOrHasRole(view.getCreatedBy(), principal, access, "create-foreign-identifier")) { log.error("Failed to create view identifier: insufficient access or role"); throw new IdentifierRequestException("Failed to create view identifier: insufficient access or role"); } } case TABLE -> { - if (data.getDatabaseId() == null || data.getQueryId() != null || data.getViewId() != null || data.getTableId() == null) { + if (data.getQueryId() != null || data.getViewId() != null || data.getTableId() == null) { log.error("Failed to create table identifier: only parameters database_id & table_id must be present"); throw new IdentifierRequestException("Failed to create table identifier: only parameters database_id & table_id must be present"); } @@ -205,7 +203,7 @@ public class IdentifierEndpoint { } } case SUBSET -> { - if (data.getDatabaseId() == null || data.getQueryId() == null || data.getViewId() != null || data.getTableId() != null) { + if (data.getQueryId() == null || data.getViewId() != null || data.getTableId() != null) { log.error("Failed to create subset identifier: only parameters database_id & query_id must be present"); throw new IdentifierRequestException("Failed to create subset identifier: only parameters database_id & query_id must be present"); } @@ -217,7 +215,7 @@ public class IdentifierEndpoint { } } case DATABASE -> { - if (data.getDatabaseId() == null || data.getQueryId() != null || data.getViewId() != null || data.getTableId() != null) { + if (data.getQueryId() != null || data.getViewId() != null || data.getTableId() != null) { log.error("Failed to create database identifier: only parameters database_id must be present"); throw new IdentifierRequestException("Failed to create database identifier: only parameters database_id must be present"); } diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/DatabaseEndpointIntegrationTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/DatabaseEndpointIntegrationTest.java deleted file mode 100644 index 221cc92b70bd35d53d41f434511d60bf9a8d481d..0000000000000000000000000000000000000000 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/DatabaseEndpointIntegrationTest.java +++ /dev/null @@ -1,107 +0,0 @@ -package at.tuwien.endpoints; - -import at.tuwien.BaseUnitTest; -import at.tuwien.annotations.MockAmqp; -import at.tuwien.annotations.MockOpensearch; -import at.tuwien.api.database.*; -import at.tuwien.config.MariaDbContainerConfig; -import at.tuwien.exception.*; -import at.tuwien.repository.mdb.ContainerRepository; -import at.tuwien.repository.mdb.ImageRepository; -import at.tuwien.repository.mdb.UserRepository; -import dasniko.testcontainers.keycloak.KeycloakContainer; -import lombok.extern.log4j.Log4j2; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.test.context.DynamicPropertyRegistry; -import org.springframework.test.context.DynamicPropertySource; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.testcontainers.containers.MariaDBContainer; -import org.testcontainers.containers.RabbitMQContainer; -import org.testcontainers.images.PullPolicy; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; - -import java.util.Set; - -import static org.junit.jupiter.api.Assertions.*; - -@Log4j2 -@SpringBootTest -@ExtendWith(SpringExtension.class) -@Testcontainers -@MockAmqp -@MockOpensearch -public class DatabaseEndpointIntegrationTest extends BaseUnitTest { - - @Autowired - private ImageRepository imageRepository; - - @Autowired - private ContainerRepository containerRepository; - - @Autowired - private UserRepository userRepository; - - @Autowired - private DatabaseEndpoint databaseEndpoint; - - @Container - private static final RabbitMQContainer rabbitContainer = new RabbitMQContainer("rabbitmq:3-management") - .withUser(USER_1_USERNAME, USER_1_PASSWORD, Set.of("administrator")) - .withVhost("dbrepo"); - - @Container - private static MariaDBContainer<?> mariaDBContainer = MariaDbContainerConfig.getContainer(); - - @Container - private static KeycloakContainer keycloakContainer = new KeycloakContainer("quay.io/keycloak/keycloak:21.0") - .withImagePullPolicy(PullPolicy.alwaysPull()) - .withAdminUsername("fda") - .withAdminPassword("fda") - .withRealmImportFile("./dbrepo-realm.json") - .withEnv("KC_HOSTNAME_STRICT_HTTPS", "false"); - - @DynamicPropertySource - static void keycloakProperties(DynamicPropertyRegistry registry) { - registry.add("fda.keycloak.endpoint", () -> "http://localhost:" + keycloakContainer.getMappedPort(8080)); - registry.add("fda.broker.endpoint", rabbitContainer::getHttpUrl); - registry.add("spring.rabbitmq.host", rabbitContainer::getHost); - registry.add("spring.rabbitmq.port", rabbitContainer::getAmqpPort); - registry.add("spring.rabbitmq.username", rabbitContainer::getAdminUsername); - registry.add("spring.rabbitmq.password", rabbitContainer::getAdminPassword); - } - - @BeforeEach - public void beforeEach() { - imageRepository.save(IMAGE_1); - containerRepository.save(CONTAINER_1); - userRepository.save(USER_1); - } - - @Test - @WithMockUser(username = USER_1_USERNAME, authorities = {"create-database"}) - public void create_succeeds() throws UserNotFoundException, BrokerVirtualHostGrantException, - DatabaseNameExistsException, NotAllowedException, ContainerConnectionException, DatabaseMalformedException, - QueryStoreException, DatabaseConnectionException, QueryMalformedException, DatabaseNotFoundException, - ImageNotSupportedException, AmqpException, BrokerVirtualHostModificationException, ContainerNotFoundException, - KeycloakRemoteException, AccessDeniedException, BrokerRemoteException { - final DatabaseCreateDto request = DatabaseCreateDto.builder() - .cid(CONTAINER_1_ID) - .name(DATABASE_1_NAME) - .isPublic(DATABASE_1_PUBLIC) - .build(); - - /* test */ - final ResponseEntity<DatabaseBriefDto> response = databaseEndpoint.create(request, USER_1_PRINCIPAL); - assertEquals(HttpStatus.CREATED, response.getStatusCode()); - assertNotNull(response.getBody()); - } - -} diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/IdentifierEndpointUnitTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/IdentifierEndpointUnitTest.java index 26c180c5e21457d49e6108326e5d077a55c4d565..2081cf3cc069a7f22dda63109989308df3fd7347 100644 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/IdentifierEndpointUnitTest.java +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/IdentifierEndpointUnitTest.java @@ -8,10 +8,8 @@ import at.tuwien.config.EndpointConfig; import at.tuwien.entities.database.Database; import at.tuwien.entities.database.DatabaseAccess; import at.tuwien.entities.identifier.Identifier; -import at.tuwien.entities.user.User; import at.tuwien.exception.*; import at.tuwien.repository.mdb.DatabaseRepository; -import at.tuwien.repository.mdb.IdentifierRepository; import at.tuwien.service.AccessService; import at.tuwien.service.IdentifierService; import at.tuwien.service.StoreService; @@ -38,7 +36,6 @@ import java.util.Optional; import java.util.UUID; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; @@ -57,9 +54,6 @@ public class IdentifierEndpointUnitTest extends BaseUnitTest { @MockBean private UserService userService; - @MockBean - private IdentifierRepository identifierRepository; - @MockBean private AccessService accessService; @@ -83,9 +77,9 @@ public class IdentifierEndpointUnitTest extends BaseUnitTest { @Test @WithAnonymousUser public void find_json_succeeds() throws IdentifierNotFoundException, QueryNotFoundException, - RemoteUnavailableException, IdentifierRequestException, UserNotFoundException, QueryStoreException, - TableMalformedException, DatabaseConnectionException, QueryMalformedException, DatabaseNotFoundException, - ImageNotSupportedException, FileStorageException, DataDbSidecarException { + IdentifierRequestException, UserNotFoundException, QueryStoreException, TableMalformedException, + DatabaseConnectionException, QueryMalformedException, DatabaseNotFoundException, ImageNotSupportedException, + FileStorageException, DataDbSidecarException { final String accept = "application/json"; /* mock */ @@ -172,13 +166,10 @@ public class IdentifierEndpointUnitTest extends BaseUnitTest { @Test @WithMockUser(username = USER_1_USERNAME, authorities = {"create-identifier"}) - public void create_hasRoleDatabase_succeeds() throws IdentifierAlreadyExistsException, - UserNotFoundException, QueryNotFoundException, DatabaseNotFoundException, RemoteUnavailableException, - IdentifierPublishingNotAllowedException, IdentifierRequestException, NotAllowedException, + public void create_hasRoleDatabase_succeeds() throws UserNotFoundException, QueryNotFoundException, + DatabaseNotFoundException, RemoteUnavailableException, IdentifierRequestException, NotAllowedException, ViewNotFoundException, at.tuwien.exception.AccessDeniedException, QueryStoreException, - DatabaseConnectionException, ImageNotSupportedException, IdentifierNotFoundException, - TableNotFoundException, TableMalformedException, QueryMalformedException, FileStorageException, - DataDbSidecarException { + DatabaseConnectionException, ImageNotSupportedException, TableNotFoundException { /* test */ generic_create(DATABASE_1_ID, DATABASE_1, DATABASE_1_USER_1_READ_ACCESS, IDENTIFIER_1_DTO_REQUEST, IDENTIFIER_1, USER_1_PRINCIPAL, USER_1_ID); @@ -206,13 +197,10 @@ public class IdentifierEndpointUnitTest extends BaseUnitTest { @Test @WithMockUser(username = USER_2_USERNAME, authorities = {"create-identifier"}) - public void create_hasRoleReadAccessQuery_succeeds() throws IdentifierAlreadyExistsException, - UserNotFoundException, QueryNotFoundException, DatabaseNotFoundException, RemoteUnavailableException, - IdentifierPublishingNotAllowedException, IdentifierRequestException, NotAllowedException, - at.tuwien.exception.AccessDeniedException, ViewNotFoundException, QueryStoreException, - DatabaseConnectionException, ImageNotSupportedException, IdentifierNotFoundException, - TableNotFoundException, TableMalformedException, QueryMalformedException, FileStorageException, - DataDbSidecarException { + public void create_hasRoleReadAccessQuery_succeeds() throws UserNotFoundException, TableNotFoundException, + AccessDeniedException, QueryStoreException, NotAllowedException, DatabaseConnectionException, + QueryNotFoundException, DatabaseNotFoundException, ImageNotSupportedException, RemoteUnavailableException, + IdentifierRequestException, ViewNotFoundException { /* test */ generic_create(DATABASE_2_ID, DATABASE_2, DATABASE_2_USER_1_READ_ACCESS, IDENTIFIER_5_DTO_REQUEST, IDENTIFIER_5, USER_2_PRINCIPAL, USER_2_ID); @@ -263,6 +251,98 @@ public class IdentifierEndpointUnitTest extends BaseUnitTest { }); } + @Test + @WithMockUser(username = USER_1_USERNAME, authorities = {"create-identifier"}) + public void create_invalidView_fails() { + final IdentifierSaveDto request = IdentifierSaveDto.builder() + .tableId(1L) // <-- + .databaseId(DATABASE_1_ID) + .descriptions(List.of(IDENTIFIER_1_DESCRIPTION_1_CREATE_DTO)) + .titles(List.of(IDENTIFIER_1_TITLE_1_CREATE_DTO)) + .relatedIdentifiers(List.of(IDENTIFIER_1_RELATED_IDENTIFIER_5_CREATE_DTO)) + .publicationDay(IDENTIFIER_5_PUBLICATION_DAY) + .publicationMonth(IDENTIFIER_5_PUBLICATION_MONTH) + .publicationYear(IDENTIFIER_5_PUBLICATION_YEAR) + .creators(List.of(IDENTIFIER_5_CREATOR_1_CREATE_DTO, IDENTIFIER_5_CREATOR_2_CREATE_DTO)) + .publisher(IDENTIFIER_5_PUBLISHER) + .type(IdentifierTypeDto.VIEW) + .build(); + + /* test */ + assertThrows(IdentifierRequestException.class, () -> { + generic_create(DATABASE_1_ID, DATABASE_1, DATABASE_1_USER_1_READ_ACCESS, request, null, USER_1_PRINCIPAL, USER_1_ID); + }); + } + + @Test + @WithMockUser(username = USER_1_USERNAME, authorities = {"create-identifier"}) + public void create_viewNotFound_fails() { + final IdentifierSaveDto request = IdentifierSaveDto.builder() + .viewId(9999L) // <-- + .databaseId(DATABASE_1_ID) + .descriptions(List.of(IDENTIFIER_1_DESCRIPTION_1_CREATE_DTO)) + .titles(List.of(IDENTIFIER_1_TITLE_1_CREATE_DTO)) + .relatedIdentifiers(List.of(IDENTIFIER_1_RELATED_IDENTIFIER_5_CREATE_DTO)) + .publicationDay(IDENTIFIER_5_PUBLICATION_DAY) + .publicationMonth(IDENTIFIER_5_PUBLICATION_MONTH) + .publicationYear(IDENTIFIER_5_PUBLICATION_YEAR) + .creators(List.of(IDENTIFIER_5_CREATOR_1_CREATE_DTO, IDENTIFIER_5_CREATOR_2_CREATE_DTO)) + .publisher(IDENTIFIER_5_PUBLISHER) + .type(IdentifierTypeDto.VIEW) + .build(); + + /* test */ + assertThrows(ViewNotFoundException.class, () -> { + generic_create(DATABASE_1_ID, DATABASE_1, DATABASE_1_USER_1_READ_ACCESS, request, null, USER_1_PRINCIPAL, USER_1_ID); + }); + } + + @Test + @WithMockUser(username = USER_1_USERNAME, authorities = {"create-identifier"}) + public void create_invalidTable_fails() { + final IdentifierSaveDto request = IdentifierSaveDto.builder() + .viewId(1L) // <-- + .databaseId(DATABASE_1_ID) + .descriptions(List.of(IDENTIFIER_1_DESCRIPTION_1_CREATE_DTO)) + .titles(List.of(IDENTIFIER_1_TITLE_1_CREATE_DTO)) + .relatedIdentifiers(List.of(IDENTIFIER_1_RELATED_IDENTIFIER_5_CREATE_DTO)) + .publicationDay(IDENTIFIER_5_PUBLICATION_DAY) + .publicationMonth(IDENTIFIER_5_PUBLICATION_MONTH) + .publicationYear(IDENTIFIER_5_PUBLICATION_YEAR) + .creators(List.of(IDENTIFIER_5_CREATOR_1_CREATE_DTO, IDENTIFIER_5_CREATOR_2_CREATE_DTO)) + .publisher(IDENTIFIER_5_PUBLISHER) + .type(IdentifierTypeDto.TABLE) + .build(); + + /* test */ + assertThrows(IdentifierRequestException.class, () -> { + generic_create(DATABASE_1_ID, DATABASE_1, DATABASE_1_USER_1_READ_ACCESS, request, null, USER_1_PRINCIPAL, USER_1_ID); + }); + } + + @Test + @WithMockUser(username = USER_1_USERNAME, authorities = {"create-identifier"}) + public void create_tableNotFound_fails() { + final IdentifierSaveDto request = IdentifierSaveDto.builder() + .tableId(9999L) // <-- + .databaseId(DATABASE_1_ID) + .descriptions(List.of(IDENTIFIER_1_DESCRIPTION_1_CREATE_DTO)) + .titles(List.of(IDENTIFIER_1_TITLE_1_CREATE_DTO)) + .relatedIdentifiers(List.of(IDENTIFIER_1_RELATED_IDENTIFIER_5_CREATE_DTO)) + .publicationDay(IDENTIFIER_5_PUBLICATION_DAY) + .publicationMonth(IDENTIFIER_5_PUBLICATION_MONTH) + .publicationYear(IDENTIFIER_5_PUBLICATION_YEAR) + .creators(List.of(IDENTIFIER_5_CREATOR_1_CREATE_DTO, IDENTIFIER_5_CREATOR_2_CREATE_DTO)) + .publisher(IDENTIFIER_5_PUBLISHER) + .type(IdentifierTypeDto.TABLE) + .build(); + + /* test */ + assertThrows(TableNotFoundException.class, () -> { + generic_create(DATABASE_1_ID, DATABASE_1, DATABASE_1_USER_1_READ_ACCESS, request, null, USER_1_PRINCIPAL, USER_1_ID); + }); + } + @Test @WithMockUser(username = USER_1_USERNAME, authorities = {"create-identifier"}) public void create_queryForeign_fails() { @@ -279,11 +359,10 @@ public class IdentifierEndpointUnitTest extends BaseUnitTest { protected void generic_create(Long databaseId, Database database, DatabaseAccess access, IdentifierSaveDto data, Identifier identifier, Principal principal, UUID userId) - throws QueryNotFoundException, RemoteUnavailableException, IdentifierAlreadyExistsException, - UserNotFoundException, DatabaseNotFoundException, IdentifierPublishingNotAllowedException, + throws QueryNotFoundException, RemoteUnavailableException, UserNotFoundException, DatabaseNotFoundException, IdentifierRequestException, NotAllowedException, at.tuwien.exception.AccessDeniedException, ViewNotFoundException, QueryStoreException, DatabaseConnectionException, ImageNotSupportedException, - IdentifierNotFoundException, TableNotFoundException, TableMalformedException, QueryMalformedException, FileStorageException, DataDbSidecarException { + TableNotFoundException { /* mock */ when(databaseRepository.findById(databaseId)) @@ -316,10 +395,10 @@ public class IdentifierEndpointUnitTest extends BaseUnitTest { } protected ResponseEntity<?> generic_find(String accept, InputStreamResource resource, Principal principal) - throws IdentifierNotFoundException, QueryNotFoundException, RemoteUnavailableException, - IdentifierRequestException, UserNotFoundException, QueryStoreException, TableMalformedException, - DatabaseConnectionException, QueryMalformedException, DatabaseNotFoundException, ImageNotSupportedException, - FileStorageException, DataDbSidecarException { + throws IdentifierNotFoundException, QueryNotFoundException, IdentifierRequestException, + UserNotFoundException, QueryStoreException, TableMalformedException, DatabaseConnectionException, + QueryMalformedException, DatabaseNotFoundException, ImageNotSupportedException, FileStorageException, + DataDbSidecarException { /* mock */ when(identifierService.find(IDENTIFIER_1_ID)) diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/ImageEndpointIntegrationTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/ImageEndpointIntegrationTest.java deleted file mode 100644 index 7c15a099e6f00884a315c22a9360482ae3e56b41..0000000000000000000000000000000000000000 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/ImageEndpointIntegrationTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package at.tuwien.endpoints; - -import at.tuwien.BaseUnitTest; -import at.tuwien.annotations.MockAmqp; -import at.tuwien.annotations.MockOpensearch; -import at.tuwien.exception.ImageAlreadyExistsException; -import at.tuwien.exception.ImageInvalidException; -import at.tuwien.exception.ImageNotFoundException; -import at.tuwien.exception.UserNotFoundException; -import lombok.extern.log4j.Log4j2; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -@Log4j2 -@ExtendWith(SpringExtension.class) -@SpringBootTest -@MockAmqp -@MockOpensearch -public class ImageEndpointIntegrationTest extends BaseUnitTest { - - @Autowired - private ImageEndpoint imageEndpoint; - - @Test - @WithMockUser(username = USER_2_USERNAME, authorities = {"create-image"}) - public void create_succeeds() throws UserNotFoundException, ImageAlreadyExistsException, - ImageNotFoundException, ImageInvalidException { - - - /* test */ - imageEndpoint.create(IMAGE_1_CREATE_DTO, USER_2_PRINCIPAL); - } - -} diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/TableDataEndpointUnitTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/TableDataEndpointUnitTest.java index cf5b8dd25cee8571cbb3fe2570d235834efd73ed..de3269a1671045b98f59556a4b082a875b91bb8d 100644 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/TableDataEndpointUnitTest.java +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/TableDataEndpointUnitTest.java @@ -424,9 +424,7 @@ public class TableDataEndpointUnitTest extends BaseUnitTest { public void generic_import(Long databaseId, Database database, Long tableId, Table table, UUID userId, DatabaseAccess access, Principal principal) throws DatabaseNotFoundException, - TableNotFoundException, NotAllowedException, UserNotFoundException, TableMalformedException, - DatabaseConnectionException, QueryMalformedException, ImageNotSupportedException, - AccessDeniedException, DataDbSidecarException { + TableNotFoundException, AccessDeniedException, TableMalformedException, NotAllowedException, DataDbSidecarException { final ImportDto request = ImportDto.builder().location("test:csv/csv_01.csv").build(); /* mock */ @@ -445,9 +443,8 @@ public class TableDataEndpointUnitTest extends BaseUnitTest { public void generic_insert(Long databaseId, Long tableId, Database database, Table table, UUID userId, DatabaseAccess access, TableCsvDto data, Principal principal) - throws DatabaseNotFoundException, TableNotFoundException, NotAllowedException, UserNotFoundException, - TableMalformedException, DatabaseConnectionException, ImageNotSupportedException, - ContainerNotFoundException, AccessDeniedException { + throws DatabaseNotFoundException, TableNotFoundException, AccessDeniedException, TableMalformedException, + NotAllowedException { /* mock */ when(databaseService.find(databaseId)) @@ -465,10 +462,9 @@ public class TableDataEndpointUnitTest extends BaseUnitTest { public void generic_getAll(Long databaseId, Long tableId, Database database, Table table, UUID userId, DatabaseAccess access, Principal principal, Instant timestamp, Long page, Long size, - SortType sortDirection, String sortColumn) throws UserNotFoundException, - TableMalformedException, NotAllowedException, PaginationException, TableNotFoundException, SortException, - DatabaseConnectionException, QueryMalformedException, DatabaseNotFoundException, ImageNotSupportedException, - AccessDeniedException { + SortType sortDirection, String sortColumn) throws TableMalformedException, + NotAllowedException, PaginationException, TableNotFoundException, SortException, QueryMalformedException, + DatabaseNotFoundException, ImageNotSupportedException, AccessDeniedException { /* mock */ when(databaseService.find(databaseId)) @@ -492,9 +488,8 @@ public class TableDataEndpointUnitTest extends BaseUnitTest { public void generic_getCount(Long databaseId, Long tableId, Database database, Table table, UUID userId, DatabaseAccess access, Principal principal, Instant timestamp) - throws UserNotFoundException, TableMalformedException, NotAllowedException, TableNotFoundException, - QueryStoreException, DatabaseConnectionException, QueryMalformedException, DatabaseNotFoundException, - ImageNotSupportedException, AccessDeniedException { + throws TableMalformedException, NotAllowedException, TableNotFoundException, QueryStoreException, + QueryMalformedException, DatabaseNotFoundException, ImageNotSupportedException, AccessDeniedException { /* mock */ when(databaseService.find(databaseId)) diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/TableEndpointUnitTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/TableEndpointUnitTest.java index c5501238b39c56a74be2ab1eb122e81e0377ab63..f07af86a8262d2edee475a3b3a9f789bc20999fd 100644 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/TableEndpointUnitTest.java +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/TableEndpointUnitTest.java @@ -449,11 +449,55 @@ public class TableEndpointUnitTest extends BaseUnitTest { @Test @WithMockUser(username = USER_4_USERNAME) - public void delete_privateNoRole_succeeds() throws TableNotFoundException, DatabaseNotFoundException, - at.tuwien.exception.AccessDeniedException, QueueNotFoundException, BrokerRemoteException { + public void delete_privateNoRole_fails() { /* test */ - generic_findById(DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, USER_4_ID, USER_4_PRINCIPAL, null); + assertThrows(org.springframework.security.access.AccessDeniedException.class, () -> { + generic_delete(USER_4_PRINCIPAL, TABLE_1); + }); + } + + @Test + @WithMockUser(username = USER_1_USERNAME, authorities = {"delete-table"}) + public void delete_succeeds() throws TableNotFoundException, TableMalformedException, NotAllowedException, + QueryMalformedException, DatabaseNotFoundException, ImageNotSupportedException { + + /* test */ + generic_delete(USER_1_PRINCIPAL, TABLE_1); + } + + @Test + @WithMockUser(username = USER_3_USERNAME, authorities = {"delete-table"}) + public void delete_foreign_fails() { + + /* test */ + assertThrows(NotAllowedException.class, () -> { + generic_delete(USER_3_PRINCIPAL, TABLE_1); + }); + } + + @Test + @WithMockUser(username = USER_2_USERNAME, authorities = {"delete-foreign-table"}) + public void delete_foreign_succeeds() throws TableNotFoundException, TableMalformedException, NotAllowedException, + QueryMalformedException, DatabaseNotFoundException, ImageNotSupportedException { + + /* test */ + generic_delete(USER_2_PRINCIPAL, TABLE_1); + } + + @Test + @WithMockUser(username = USER_2_USERNAME, authorities = {"delete-table"}) + public void delete_hasIdentifiers_fails() { + final Table response = Table.builder() + .identifiers(List.of(IDENTIFIER_1)) + .owner(USER_1) + .ownedBy(USER_1_ID) + .build(); + + /* test */ + assertThrows(NotAllowedException.class, () -> { + generic_delete(USER_1_PRINCIPAL, response); + }); } /* ################################################################################################### */ @@ -494,9 +538,8 @@ public class TableEndpointUnitTest extends BaseUnitTest { protected ResponseEntity<TableBriefDto> generic_create(Long databaseId, Database database, TableCreateDto data, UUID userId, Principal principal, DatabaseAccess access) - throws DatabaseNotFoundException, NotAllowedException, UserNotFoundException, TableMalformedException, - QueryMalformedException, ImageNotSupportedException, AmqpException, TableNameExistsException, - ContainerNotFoundException, at.tuwien.exception.AccessDeniedException, TableNotFoundException { + throws DatabaseNotFoundException, NotAllowedException, TableMalformedException, QueryMalformedException, + ImageNotSupportedException, TableNameExistsException, AccessDeniedException, TableNotFoundException { /* mock */ if (database != null) { @@ -528,7 +571,8 @@ public class TableEndpointUnitTest extends BaseUnitTest { protected ResponseEntity<TableDto> generic_findById(Long databaseId, Long tableId, Database database, Table table, UUID userId, Principal principal, DatabaseAccess access) throws DatabaseNotFoundException, - TableNotFoundException, at.tuwien.exception.AccessDeniedException, QueueNotFoundException, BrokerRemoteException { + TableNotFoundException, at.tuwien.exception.AccessDeniedException, QueueNotFoundException, + BrokerRemoteException { /* mock */ if (table != null) { @@ -565,4 +609,16 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ return tableEndpoint.findById(databaseId, tableId, principal); } + + protected ResponseEntity<?> generic_delete(Principal principal, Table table) throws TableNotFoundException, + TableMalformedException, NotAllowedException, QueryMalformedException, DatabaseNotFoundException, + ImageNotSupportedException { + + /* mock */ + when(tableService.find(anyLong(), anyLong())) + .thenReturn(table); + + /* test */ + return tableEndpoint.delete(DATABASE_1_ID, TABLE_1_ID, principal); + } } diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/ViewEndpointIntegrationTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/ViewEndpointIntegrationTest.java deleted file mode 100644 index 2b4ae7503f06db8ce63c67bf9f7e9258c4c50053..0000000000000000000000000000000000000000 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/ViewEndpointIntegrationTest.java +++ /dev/null @@ -1,107 +0,0 @@ -package at.tuwien.endpoints; - -import at.tuwien.BaseUnitTest; -import at.tuwien.annotations.MockAmqp; -import at.tuwien.annotations.MockOpensearch; -import at.tuwien.api.database.ViewBriefDto; -import at.tuwien.config.MariaDbConfig; -import at.tuwien.config.MariaDbContainerConfig; -import at.tuwien.exception.*; -import at.tuwien.repository.mdb.*; -import lombok.extern.log4j.Log4j2; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.testcontainers.containers.MariaDBContainer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; - -import java.sql.SQLException; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; - -@Log4j2 -@SpringBootTest -@Testcontainers -@ExtendWith(SpringExtension.class) -@MockAmqp -@MockOpensearch -public class ViewEndpointIntegrationTest extends BaseUnitTest { - - @Autowired - private ImageRepository imageRepository; - - @Autowired - private LicenseRepository licenseRepository; - - @Autowired - private UserRepository userRepository; - - @Autowired - private ContainerRepository containerRepository; - - @Autowired - private DatabaseRepository databaseRepository; - - @Autowired - private ViewEndpoint viewEndpoint; - - @Container - private static MariaDBContainer<?> mariaDBContainer = MariaDbContainerConfig.getContainer(); - - @BeforeEach - public void beforeEach() throws SQLException { - TABLE_1.setColumns(TABLE_1_COLUMNS); - TABLE_2.setColumns(TABLE_2_COLUMNS); - TABLE_3.setColumns(TABLE_3_COLUMNS); - TABLE_4.setColumns(TABLE_4_COLUMNS); - /* metadata database */ - imageRepository.save(IMAGE_1); - licenseRepository.save(LICENSE_1); - userRepository.saveAll(List.of(USER_1, USER_2, USER_3)); - containerRepository.save(CONTAINER_1); - databaseRepository.save(DATABASE_1); - MariaDbConfig.dropAllDatabases(CONTAINER_1); - MariaDbConfig.createInitDatabase(CONTAINER_1, DATABASE_1); - } - - @Test - @WithMockUser(username = USER_1_USERNAME, authorities = {"create-database-view"}) - public void create_privateDatabasePublicView_succeeds() throws UserNotFoundException, NotAllowedException, - DatabaseConnectionException, ViewMalformedException, QueryMalformedException, DatabaseNotFoundException { - - /* test */ - final ResponseEntity<ViewBriefDto> response = viewEndpoint.create(DATABASE_1_ID, VIEW_1_CREATE_DTO, USER_1_PRINCIPAL); - assertEquals(HttpStatus.CREATED, response.getStatusCode()); - final ViewBriefDto body = response.getBody(); - assertNotNull(body); - assertEquals(VIEW_1_NAME, body.getName()); - assertEquals(VIEW_1_INTERNAL_NAME, body.getInternalName()); - assertEquals(VIEW_1_QUERY, body.getQuery()); - assertEquals(VIEW_1_PUBLIC, body.getIsPublic()); - } - - @Test - @WithMockUser(username = USER_1_USERNAME, authorities = {"create-database-view"}) - public void count_privateDatabasePublicView_succeeds() throws UserNotFoundException, DatabaseConnectionException, - QueryMalformedException, DatabaseNotFoundException, QueryStoreException, TableMalformedException, - ImageNotSupportedException, ContainerNotFoundException, ViewNotFoundException, SQLException { - final String request = "CREATE VIEW `" + VIEW_1_INTERNAL_NAME + "` AS (" + VIEW_1_QUERY + ");"; - - /* mock */ - MariaDbConfig.execute(DATABASE_1, request); - - /* test */ - final ResponseEntity<Long> response = viewEndpoint.count(DATABASE_1_ID, VIEW_1_ID, USER_1_PRINCIPAL); - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(3L, response.getBody()); - } - -} diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/QueryServiceIntegrationTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/QueryServiceIntegrationTest.java index 78298edfea16ae9b62e7f1133e6225d42b754670..5730ee743b70b58ce83f388f7924465c37f86ff1 100644 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/QueryServiceIntegrationTest.java +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/QueryServiceIntegrationTest.java @@ -117,14 +117,14 @@ public class QueryServiceIntegrationTest extends BaseUnitTest { databaseRepository.saveAll(List.of(DATABASE_1, DATABASE_2)); /* mock */ MariaDbConfig.dropAllDatabases(CONTAINER_1); - MariaDbConfig.createInitDatabase(CONTAINER_1, DATABASE_2); MariaDbConfig.createInitDatabase(CONTAINER_1, DATABASE_1); + MariaDbConfig.createInitDatabase(CONTAINER_1, DATABASE_2); } @Test public void findAll_succeeds() throws DatabaseNotFoundException, ImageNotSupportedException, - TableMalformedException, TableNotFoundException, DatabaseConnectionException, - PaginationException, QueryMalformedException, UserNotFoundException { + TableMalformedException, TableNotFoundException, DatabaseConnectionException, PaginationException, + QueryMalformedException, UserNotFoundException { /* test */ final QueryResultDto result = queryService.tableFindAll(DATABASE_1_ID, TABLE_1_ID, Instant.now(), @@ -148,8 +148,8 @@ public class QueryServiceIntegrationTest extends BaseUnitTest { } @Test - public void selectAll_succeeds() throws TableNotFoundException, DatabaseNotFoundException, - ImageNotSupportedException, TableMalformedException, QueryMalformedException { + public void selectAll_succeeds() throws TableNotFoundException, DatabaseNotFoundException, TableMalformedException, + ImageNotSupportedException, QueryMalformedException { final Long page = 0L; final Long size = 10L; @@ -181,8 +181,8 @@ public class QueryServiceIntegrationTest extends BaseUnitTest { } @Test - public void insert_date_succeeds() throws TableNotFoundException, TableMalformedException, - DatabaseNotFoundException, SQLException { + public void insert_date_succeeds() throws TableNotFoundException, TableMalformedException, SQLException, + DatabaseNotFoundException { final TableCsvDto request = TableCsvDto.builder() .data(new HashMap<>() {{ put("id", 4L); @@ -291,18 +291,16 @@ public class QueryServiceIntegrationTest extends BaseUnitTest { } @Test - public void findAll_timestampMissing_succeeds() throws TableNotFoundException, DatabaseConnectionException, - TableMalformedException, DatabaseNotFoundException, ImageNotSupportedException, PaginationException, - QueryMalformedException, UserNotFoundException { + public void findAll_timestampMissing_succeeds() throws TableNotFoundException, TableMalformedException, + DatabaseNotFoundException, ImageNotSupportedException, QueryMalformedException { /* test */ queryService.tableFindAll(DATABASE_1_ID, TABLE_1_ID, null, null, null, USER_1_PRINCIPAL); } @Test - public void findAll_timestampBeforeCreation_succeeds() throws TableNotFoundException, DatabaseConnectionException, - TableMalformedException, DatabaseNotFoundException, ImageNotSupportedException, PaginationException, - QueryMalformedException, UserNotFoundException { + public void findAll_timestampBeforeCreation_succeeds() throws TableNotFoundException, TableMalformedException, + DatabaseNotFoundException, ImageNotSupportedException, QueryMalformedException { final Instant timestamp = DATABASE_1_CREATED.minus(1, ChronoUnit.SECONDS); /* test */ @@ -311,12 +309,11 @@ public class QueryServiceIntegrationTest extends BaseUnitTest { } @Test - public void execute_succeeds() throws DatabaseConnectionException, TableMalformedException, - DatabaseNotFoundException, ImageNotSupportedException, QueryMalformedException, UserNotFoundException, - QueryStoreException, ColumnParseException, InterruptedException, KeycloakRemoteException, - AccessDeniedException, QueryNotFoundException { + public void execute_succeeds() throws TableMalformedException, DatabaseNotFoundException, + ImageNotSupportedException, QueryMalformedException, UserNotFoundException, QueryStoreException, + ColumnParseException, InterruptedException, QueryNotFoundException { final ExecuteStatementDto request = ExecuteStatementDto.builder() - .statement("SELECT n.`firstname`, n.`lastname`, n.`birth`, n.`reminder`, z.`animal_name`, z.`legs` FROM `likes` l JOIN `names` n ON l.`name_id` = n.`id` JOIN `mock_view` z ON z.`id` = l.`zoo_id` ORDER BY animal_name ASC") + .statement("SELECT n.`id`, n.`firstname`, n.`lastname`, n.`birth`, n.`reminder`, z.`animal_name`, z.`legs` FROM `likes` l JOIN `names` n ON l.`name_id` = n.`id` JOIN `mock_view` z ON z.`id` = l.`zoo_id` ORDER BY id, animal_name ASC") .build(); /* pre-condition */ @@ -327,22 +324,26 @@ public class QueryServiceIntegrationTest extends BaseUnitTest { assertEquals(4L, response.getResultNumber()); assertNotNull(response.getResult()); final List<Map<String, Object>> result = response.getResult(); + assertEquals(BigInteger.valueOf(1L), result.get(0).get("id")); assertEquals(4, result.get(0).get("legs")); assertEquals("boar", result.get(0).get("animal_name")); assertEquals("Moritz", result.get(0).get("firstname")); assertEquals("Staudinger", result.get(0).get("lastname")); assertEquals("1990", result.get(0).get("birth")); assertEquals("11:22:33", result.get(0).get("reminder")); + assertEquals(BigInteger.valueOf(1L), result.get(1).get("id")); assertEquals(4, result.get(1).get("legs")); assertEquals("cavy", result.get(1).get("animal_name")); assertEquals("Moritz", result.get(1).get("firstname")); assertEquals("Staudinger", result.get(1).get("lastname")); assertEquals("1990", result.get(1).get("birth")); assertEquals("11:22:33", result.get(1).get("reminder")); + assertEquals(BigInteger.valueOf(3L), result.get(2).get("id")); assertEquals(4, result.get(2).get("legs")); assertEquals("bear", result.get(2).get("animal_name")); assertEquals("Eva", result.get(2).get("firstname")); assertEquals("Gergely", result.get(2).get("lastname")); + assertEquals(BigInteger.valueOf(4L), result.get(3).get("id")); assertEquals(4, result.get(3).get("legs")); assertEquals("bear", result.get(3).get("animal_name")); assertEquals("Cornelia", result.get(3).get("firstname")); @@ -350,10 +351,9 @@ public class QueryServiceIntegrationTest extends BaseUnitTest { } @Test - public void execute_withoutNullField_succeeds() throws DatabaseConnectionException, TableMalformedException, - DatabaseNotFoundException, ImageNotSupportedException, QueryMalformedException, UserNotFoundException, - QueryStoreException, ColumnParseException, InterruptedException, KeycloakRemoteException, - AccessDeniedException, QueryNotFoundException { + public void execute_withoutNullField_succeeds() throws TableMalformedException, DatabaseNotFoundException, + ImageNotSupportedException, QueryMalformedException, UserNotFoundException, QueryStoreException, + ColumnParseException, InterruptedException, QueryNotFoundException { final ExecuteStatementDto request = ExecuteStatementDto.builder() .statement("SELECT `location`, `lng` FROM `weather_location` WHERE `lat` IS NULL") .build(); @@ -373,10 +373,9 @@ public class QueryServiceIntegrationTest extends BaseUnitTest { } @Test - public void execute_withoutNullField2_succeeds() throws DatabaseConnectionException, TableMalformedException, - DatabaseNotFoundException, ImageNotSupportedException, QueryMalformedException, UserNotFoundException, - QueryStoreException, ColumnParseException, InterruptedException, KeycloakRemoteException, - AccessDeniedException, QueryNotFoundException { + public void execute_withoutNullField2_succeeds() throws TableMalformedException, DatabaseNotFoundException, + ImageNotSupportedException, QueryMalformedException, UserNotFoundException, QueryStoreException, + ColumnParseException, InterruptedException, QueryNotFoundException { final ExecuteStatementDto request = ExecuteStatementDto.builder() .statement("SELECT `location` FROM `weather_location` WHERE `lat` IS NULL") .build(); @@ -396,10 +395,9 @@ public class QueryServiceIntegrationTest extends BaseUnitTest { } @Test - public void execute_withNullField_succeeds() throws DatabaseConnectionException, TableMalformedException, - DatabaseNotFoundException, ImageNotSupportedException, QueryMalformedException, UserNotFoundException, - QueryStoreException, ColumnParseException, InterruptedException, KeycloakRemoteException, - AccessDeniedException, QueryNotFoundException { + public void execute_withNullField_succeeds() throws TableMalformedException, DatabaseNotFoundException, + ImageNotSupportedException, QueryMalformedException, UserNotFoundException, QueryStoreException, + ColumnParseException, InterruptedException, QueryNotFoundException { final ExecuteStatementDto request = ExecuteStatementDto.builder() .statement("SELECT `lat`, `lng` FROM `weather_location` WHERE `lat` IS NULL") .build(); @@ -415,10 +413,9 @@ public class QueryServiceIntegrationTest extends BaseUnitTest { } @Test - public void execute_aliases_succeeds() throws DatabaseConnectionException, TableMalformedException, - DatabaseNotFoundException, ImageNotSupportedException, QueryMalformedException, UserNotFoundException, - QueryStoreException, ColumnParseException, InterruptedException, KeycloakRemoteException, - AccessDeniedException, QueryNotFoundException { + public void execute_aliases_succeeds() throws TableMalformedException, DatabaseNotFoundException, + ImageNotSupportedException, QueryMalformedException, UserNotFoundException, QueryStoreException, + ColumnParseException, InterruptedException, QueryNotFoundException { final ExecuteStatementDto request = ExecuteStatementDto.builder() .statement("SELECT aus.location as a, loc.location from weather_aus aus, weather_location loc") .build(); @@ -452,10 +449,9 @@ public class QueryServiceIntegrationTest extends BaseUnitTest { } @Test - public void execute_aliasesWithDatabaseName_succeeds() throws DatabaseConnectionException, TableMalformedException, - DatabaseNotFoundException, ImageNotSupportedException, QueryMalformedException, UserNotFoundException, - QueryStoreException, ColumnParseException, InterruptedException, KeycloakRemoteException, - AccessDeniedException, QueryNotFoundException { + public void execute_aliasesWithDatabaseName_succeeds() throws TableMalformedException, DatabaseNotFoundException, + ImageNotSupportedException, QueryMalformedException, UserNotFoundException, QueryStoreException, + ColumnParseException, InterruptedException, QueryNotFoundException { final ExecuteStatementDto request = ExecuteStatementDto.builder() .statement("SELECT aus.location as a, loc.location from weather.weather_aus aus, weather.weather_location loc") .build(); @@ -519,9 +515,9 @@ public class QueryServiceIntegrationTest extends BaseUnitTest { } @Test - public void findOne_emptySet_succeeds() throws DatabaseConnectionException, DatabaseNotFoundException, - ImageNotSupportedException, QueryMalformedException, UserNotFoundException, QueryStoreException, - QueryNotFoundException, FileStorageException, SQLException, IOException { + public void findOne_emptySet_succeeds() throws DatabaseNotFoundException, ImageNotSupportedException, + QueryMalformedException, QueryStoreException, QueryNotFoundException, FileStorageException, SQLException, + IOException { final String filename = RandomStringUtils.randomAlphabetic(40) + ".csv"; final Query query = Query.builder() .id(QUERY_1_ID) diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/validator/EndpointValidatorUnitTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/validator/EndpointValidatorUnitTest.java index a937cf531c34b7bbb281972bbaf018c2592a7608..e6132055b7c79ef2c4fedd852d1995af875b5b77 100644 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/validator/EndpointValidatorUnitTest.java +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/validator/EndpointValidatorUnitTest.java @@ -10,7 +10,6 @@ import at.tuwien.api.database.table.columns.ColumnTypeDto; import at.tuwien.api.identifier.IdentifierSaveDto; import at.tuwien.entities.database.table.Table; import at.tuwien.exception.*; -import at.tuwien.repository.mdb.IdentifierRepository; import at.tuwien.service.AccessService; import at.tuwien.service.DatabaseService; import at.tuwien.service.TableService; @@ -593,4 +592,39 @@ public class EndpointValidatorUnitTest extends BaseUnitTest { assertTrue(endpointValidator.validatePublicationDate(request)); } + @Test + public void validateOnlyMineOrWriteAccessOrHasRole_noAccess_fails() { + + /* test */ + assertFalse(endpointValidator.validateOnlyMineOrWriteAccessOrHasRole(USER_1_ID, USER_1_PRINCIPAL, null, "nobody-role")); + } + + @Test + public void validateOnlyMineOrWriteAccessOrHasRole_readAccess_fails() { + + /* test */ + assertFalse(endpointValidator.validateOnlyMineOrWriteAccessOrHasRole(USER_1_ID, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS, "nobody-role")); + } + + @Test + public void validateOnlyMineOrWriteAccessOrHasRole_ownerOnlyWriteOwn_succeeds() { + + /* test */ + assertTrue(endpointValidator.validateOnlyMineOrWriteAccessOrHasRole(USER_1_ID, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS, "nobody-role")); + } + + @Test + public void validateOnlyMineOrWriteAccessOrHasRole_notOwnerOnlyWriteOwn_fails() { + + /* test */ + assertFalse(endpointValidator.validateOnlyMineOrWriteAccessOrHasRole(USER_2_ID, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS, "nobody-role")); + } + + @Test + public void validateOnlyMineOrWriteAccessOrHasRole_notOwnerWriteAll_succeeds() { + + /* test */ + assertTrue(endpointValidator.validateOnlyMineOrWriteAccessOrHasRole(USER_2_ID, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_ALL_ACCESS, "nobody-role")); + } + } diff --git a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/ViewService.java b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/ViewService.java index 48803195a6589bd956a95df2287901e9527ceab0..e0df5026fb79604a02bfc642edd337f14e28893f 100644 --- a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/ViewService.java +++ b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/ViewService.java @@ -9,7 +9,16 @@ import java.util.List; public interface ViewService { - View findById(Long id) throws ViewNotFoundException, DatabaseNotFoundException; + /** + * Find a view of a database with id. + * + * @param databaseId The database id. + * @param viewId The view id. + * @return The view, if successful. + * @throws ViewNotFoundException The view was not found in the metadata database. + * @throws DatabaseNotFoundException The database was not found in the metadata database. + */ + View findById(Long databaseId, Long viewId) throws ViewNotFoundException, DatabaseNotFoundException; /** * Find all views by database id. diff --git a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/IdentifierServiceImpl.java b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/IdentifierServiceImpl.java index 4b2724a9aa55006abe48e3c033d86b68dd7bce91..7c02d46a2dba4181aa185b8ef101c010c6917bca 100644 --- a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/IdentifierServiceImpl.java +++ b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/IdentifierServiceImpl.java @@ -176,7 +176,7 @@ public class IdentifierServiceImpl implements IdentifierService { } case VIEW -> { log.debug("identifier type: view with id {} and database with id {}", data.getViewId(), data.getDatabaseId()); - final View view = viewService.findById(data.getViewId()); + final View view = viewService.findById(data.getDatabaseId(), data.getViewId()); entity.setViewId(view.getId()); entity.setQuery(view.getQuery()); entity.setQueryNormalized(view.getQuery()); diff --git a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/ViewServiceImpl.java b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/ViewServiceImpl.java index dae45fb07ea2081bd0f48f633c24975c09ec1ee6..2a57cadf190fa111233efb30c5fa13ba384411cb 100644 --- a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/ViewServiceImpl.java +++ b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/ViewServiceImpl.java @@ -53,15 +53,15 @@ public class ViewServiceImpl extends HibernateConnector implements ViewService { } @Override - public View findById(Long id) throws ViewNotFoundException, DatabaseNotFoundException { - final Optional<View> optional = databaseService.find(id) + public View findById(Long databaseId, Long viewId) throws ViewNotFoundException, DatabaseNotFoundException { + final Optional<View> optional = databaseService.find(databaseId) .getViews() .stream() - .filter(v -> v.getId().equals(id)) + .filter(v -> v.getId().equals(viewId)) .findFirst(); if (optional.isEmpty()) { - log.error("Failed to find view with id {} in metadata database", id); - throw new ViewNotFoundException("Failed to find view with id " + id + " in metadata database"); + log.error("Failed to find view with id {} in metadata database", viewId); + throw new ViewNotFoundException("Failed to find view with id " + viewId + " in metadata database"); } return optional.get(); } diff --git a/dbrepo-metadata-service/test/src/main/java/at/tuwien/test/BaseTest.java b/dbrepo-metadata-service/test/src/main/java/at/tuwien/test/BaseTest.java index f4d85aa196fc07d53198a0d3f169e0895d7f9c70..43194d7daf641478bb1c9d9bcf714ade9f0cb4d8 100644 --- a/dbrepo-metadata-service/test/src/main/java/at/tuwien/test/BaseTest.java +++ b/dbrepo-metadata-service/test/src/main/java/at/tuwien/test/BaseTest.java @@ -1346,6 +1346,7 @@ public abstract class BaseTest { .name(TABLE_1_NAME) .queueName(TABLE_1_QUEUE_NAME) .routingKey(TABLE_1_ROUTING_KEY) + .identifiers(List.of()) .columns(List.of() /* TABLE_1_COLUMNS */) .constraints(null /* TABLE_1_CONSTRAINTS */) .createdBy(USER_1_ID) @@ -1365,6 +1366,7 @@ public abstract class BaseTest { .name(TABLE_1_NAME) .queueName(TABLE_1_QUEUE_NAME) .routingKey(TABLE_1_ROUTING_KEY) + .identifiers(List.of()) .columns(List.of() /* TABLE_1_COLUMNS */) .constraints(null /* TABLE_1_CONSTRAINTS */) .createdBy(USER_1_ID) diff --git a/dbrepo-ui/README.md b/dbrepo-ui/README.md index c4f407ba90f72ae0af0398394613b55c6cea031f..fbd83426ca01b7b4acb8aad17690bf577cd73945 100644 --- a/dbrepo-ui/README.md +++ b/dbrepo-ui/README.md @@ -1,73 +1,43 @@ -# fda-ui +# User Interface -## Prerequisit +## Prerequisites -We use Ubuntu 20.04 LTS. +* Node.js 14+ ([how to install](https://www.stewright.me/2021/03/install-nodejs-14-on-ubuntu-20-04/)) +* Yarn 1.22.0+ ([how to install](https://classic.yarnpkg.com/lang/en/docs/install/#debian-stable)) -## Build environment: yarn +Install the runtime dependencies into `node_modules`: -Install yarn: - -First we need curl: - - sudo apt install curl - -Add yarn GPG-Key - - curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - - -Add repo - - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list - -Install yarn - - sudo apt update && sudo apt install yarn - -Run yarn - - $ yarn - -which gives yarn install v1.22.5 - -## Nuxt +```bash +yarn install +``` -With yarn installed, install nuxt.js +## Run -See https://nuxtjs.org/ +Then, start a local development server at port 3001. The development server has a local proxy that rewrites the paths +and does not rely on the `gateway-service` (a NGINX-based proxy to bundle the REST API). - yarn add nuxt +```bash +yarn dev +``` +Visit [http://localhost:3001](http://localhost:3001) in your browser. The development server watches for changes in +`dbrepo-ui` and will reload the frontend. -## Prepare +## Configure -Configure the `.env` file for the IP and port running or run through terminal: +To change most display settings, modify the `dbrepo.config.json` in the root folder. Extend it for any configuration +that the user needs to do, e.g. change the title, logo, display a list of links. -```bash -API=http://fda-gateway-service npm --prefix ./fda-ui run dev -``` +## Test -Of course you need to add `fda-gateway-service` to your `/etc/hosts` file for Docker "DNS" to your containers: +TO run the unit tests: ```bash -172.29.0.6 fda-gateway-service +yarn run test:unit ``` -## Build Setup +Optionally, generate a coverage report: ```bash -# install dependencies -$ yarn install - -# serve with hot reload at localhost:3000 -$ yarn dev - -# build for production and launch server -$ yarn build -$ yarn start - -# generate static project -$ yarn generate -``` - -For detailed explanation on how things work, check out [Nuxt.js docs](https://nuxtjs.org). +yarn run coverage +``` \ No newline at end of file