diff --git a/dbrepo-auth-service/dbrepo-realm.json b/dbrepo-auth-service/dbrepo-realm.json index 1c703b83750c5aa21d4da06c7895d74211122ae9..bac2ddc9782822da0fac897c7bbc9a2f8ae84ed6 100644 --- a/dbrepo-auth-service/dbrepo-realm.json +++ b/dbrepo-auth-service/dbrepo-realm.json @@ -1475,6 +1475,39 @@ "claim.name" : "language", "jsonType.label" : "String" } + }, { + "id" : "9bdc3e60-09b8-4241-915e-29f083434026", + "name" : "provider", + "protocol" : "openid-connect", + "protocolMapper" : "oidc-usersessionmodel-note-mapper", + "consentRequired" : false, + "config" : { + "user.session.note" : "identity_provider", + "introspection.token.claim" : "true", + "userinfo.token.claim" : "true", + "id.token.claim" : "true", + "lightweight.claim" : "false", + "access.token.claim" : "true", + "claim.name" : "identity_provider", + "jsonType.label" : "String", + "access.tokenResponse.claim" : "false" + } + }, { + "id" : "e567cb5c-8856-4124-8b86-f19cd53d7c71", + "name" : "setup_finished", + "protocol" : "openid-connect", + "protocolMapper" : "oidc-usermodel-attribute-mapper", + "consentRequired" : false, + "config" : { + "introspection.token.claim" : "true", + "userinfo.token.claim" : "true", + "user.attribute" : "SETUP_FINISHED", + "id.token.claim" : "true", + "lightweight.claim" : "false", + "access.token.claim" : "true", + "claim.name" : "setup_finished", + "jsonType.label" : "boolean" + } }, { "id" : "b817424d-7f91-43d8-b7d0-6a32582377fb", "name" : "family name", @@ -2376,7 +2409,7 @@ "subType" : "anonymous", "subComponents" : { }, "config" : { - "allowed-protocol-mapper-types" : [ "oidc-usermodel-property-mapper", "saml-user-property-mapper", "saml-user-attribute-mapper", "saml-role-list-mapper", "oidc-full-name-mapper", "oidc-usermodel-attribute-mapper", "oidc-address-mapper", "oidc-sha256-pairwise-sub-mapper" ] + "allowed-protocol-mapper-types" : [ "saml-role-list-mapper", "saml-user-property-mapper", "oidc-address-mapper", "oidc-usermodel-property-mapper", "oidc-full-name-mapper", "saml-user-attribute-mapper", "oidc-usermodel-attribute-mapper", "oidc-sha256-pairwise-sub-mapper" ] } }, { "id" : "1849e52a-b8c9-44a8-af3d-ee19376a1ed1", @@ -2402,7 +2435,7 @@ "subType" : "authenticated", "subComponents" : { }, "config" : { - "allowed-protocol-mapper-types" : [ "oidc-usermodel-property-mapper", "oidc-usermodel-attribute-mapper", "oidc-full-name-mapper", "saml-role-list-mapper", "oidc-address-mapper", "saml-user-property-mapper", "saml-user-attribute-mapper", "oidc-sha256-pairwise-sub-mapper" ] + "allowed-protocol-mapper-types" : [ "oidc-full-name-mapper", "saml-role-list-mapper", "saml-user-attribute-mapper", "oidc-usermodel-attribute-mapper", "oidc-sha256-pairwise-sub-mapper", "oidc-usermodel-property-mapper", "saml-user-property-mapper", "oidc-address-mapper" ] } } ], "org.keycloak.userprofile.UserProfileProvider" : [ { @@ -2426,8 +2459,8 @@ "config" : { "ldap.attribute" : [ "createTimestamp" ], "is.mandatory.in.ldap" : [ "false" ], - "always.read.value.from.ldap" : [ "true" ], "read.only" : [ "true" ], + "always.read.value.from.ldap" : [ "true" ], "user.model.attribute" : [ "createTimestamp" ] } }, { @@ -2438,8 +2471,8 @@ "config" : { "ldap.attribute" : [ "sn" ], "is.mandatory.in.ldap" : [ "true" ], - "read.only" : [ "false" ], "always.read.value.from.ldap" : [ "true" ], + "read.only" : [ "false" ], "user.model.attribute" : [ "lastName" ] } }, { @@ -2450,8 +2483,8 @@ "config" : { "ldap.attribute" : [ "cn" ], "is.mandatory.in.ldap" : [ "true" ], - "always.read.value.from.ldap" : [ "true" ], "read.only" : [ "false" ], + "always.read.value.from.ldap" : [ "true" ], "user.model.attribute" : [ "firstName" ] } }, { @@ -2462,8 +2495,8 @@ "config" : { "ldap.attribute" : [ "mail" ], "is.mandatory.in.ldap" : [ "false" ], - "always.read.value.from.ldap" : [ "false" ], "read.only" : [ "false" ], + "always.read.value.from.ldap" : [ "false" ], "user.model.attribute" : [ "email" ] } }, { @@ -2476,15 +2509,15 @@ "membership.attribute.type" : [ "DN" ], "user.roles.retrieve.strategy" : [ "LOAD_GROUPS_BY_MEMBER_ATTRIBUTE" ], "group.name.ldap.attribute" : [ "cn" ], - "membership.user.ldap.attribute" : [ "uid" ], - "ignore.missing.groups" : [ "false" ], "preserve.group.inheritance" : [ "false" ], "membership.ldap.attribute" : [ "member" ], - "memberof.ldap.attribute" : [ "memberOf" ], - "group.object.classes" : [ "groupOfNames" ], + "ignore.missing.groups" : [ "false" ], + "membership.user.ldap.attribute" : [ "uid" ], "groups.dn" : [ "ou=users,dc=dbrepo,dc=at" ], - "groups.path" : [ "/" ], - "drop.non.existing.groups.during.sync" : [ "false" ] + "group.object.classes" : [ "groupOfNames" ], + "memberof.ldap.attribute" : [ "memberOf" ], + "drop.non.existing.groups.during.sync" : [ "false" ], + "groups.path" : [ "/" ] } }, { "id" : "b6ff3285-35af-4e86-8bb4-d94b8e0d70bb", @@ -2518,8 +2551,8 @@ "fullSyncPeriod" : [ "-1" ], "pagination" : [ "false" ], "startTls" : [ "false" ], - "connectionPooling" : [ "true" ], "usersDn" : [ "ou=users,dc=dbrepo,dc=at" ], + "connectionPooling" : [ "true" ], "cachePolicy" : [ "DEFAULT" ], "useKerberosForPasswordAuthentication" : [ "false" ], "importEnabled" : [ "true" ], @@ -2531,8 +2564,8 @@ "lastSync" : [ "1719252666" ], "vendor" : [ "other" ], "uuidLDAPAttribute" : [ "entryUUID" ], - "allowKerberosAuthentication" : [ "false" ], "connectionUrl" : [ "ldap://identity-service:1389" ], + "allowKerberosAuthentication" : [ "false" ], "syncRegistrations" : [ "true" ], "authType" : [ "simple" ], "useTruststoreSpi" : [ "always" ], diff --git a/dbrepo-auth-service/listeners/src/main/java/at/tuwien/Client.java b/dbrepo-auth-service/listeners/src/main/java/at/tuwien/Client.java index 769ec49097223e5fd49f76d855d9acef1cfbe35c..c63e88618b792f4d85e52b4aee55c80e1a2f1db8 100644 --- a/dbrepo-auth-service/listeners/src/main/java/at/tuwien/Client.java +++ b/dbrepo-auth-service/listeners/src/main/java/at/tuwien/Client.java @@ -31,8 +31,7 @@ public class Client { if (systemPassword == null || systemPassword.isEmpty()) { throw new IllegalArgumentException("Environment variable SYSTEM_PASSWORD is not set or is empty."); } - - URL url = URI.create(urlString).toURL(); + final URL url = URI.create(urlString + "/api/user").toURL(); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); diff --git a/dbrepo-auth-service/listeners/src/main/java/at/tuwien/CreateEventListenerProvider.java b/dbrepo-auth-service/listeners/src/main/java/at/tuwien/CreateEventListenerProvider.java index 93f2b2919b81940e8803b8b451c5388163f7d5dd..ea4aa7794b6f26167da704e4c12057cf9d8c5c42 100644 --- a/dbrepo-auth-service/listeners/src/main/java/at/tuwien/CreateEventListenerProvider.java +++ b/dbrepo-auth-service/listeners/src/main/java/at/tuwien/CreateEventListenerProvider.java @@ -57,7 +57,6 @@ public class CreateEventListenerProvider implements EventListenerProvider { final String userData = "{" + quoteAttr("id", user.getId()) + ", " + quoteAttr("username", user.getUsername()) + ", " + - quoteAttr("email", user.getEmail()) + ", " + quoteAttr("ldap_id", user.getFirstAttribute("LDAP_ID")) + ", " + quoteAttr("given_name", user.getFirstName()) + ", " + quoteAttr("family_name", user.getLastName()) + diff --git a/dbrepo-auth-service/listeners/target/create-event-listener.jar b/dbrepo-auth-service/listeners/target/create-event-listener.jar index a23243d39509ec3821219e5799a25740c93e2ca1..a970096eeaa5015d9e95064f6b341fffdf8aae5f 100644 Binary files a/dbrepo-auth-service/listeners/target/create-event-listener.jar and b/dbrepo-auth-service/listeners/target/create-event-listener.jar differ diff --git a/dbrepo-metadata-service/api/src/main/java/at/tuwien/api/auth/CreateUserDto.java b/dbrepo-metadata-service/api/src/main/java/at/tuwien/api/auth/CreateUserDto.java index 16f45aec4d625639f1188e0e853b3a81bd71811f..9742986ae08df9f9d00f5c1bc9da15b245bad8fc 100644 --- a/dbrepo-metadata-service/api/src/main/java/at/tuwien/api/auth/CreateUserDto.java +++ b/dbrepo-metadata-service/api/src/main/java/at/tuwien/api/auth/CreateUserDto.java @@ -40,7 +40,4 @@ public class CreateUserDto { @Schema(example = "bar") private String familyName; - @Schema(example = "foo.bar@example.com") - private String email; - } diff --git a/dbrepo-metadata-service/entities/src/main/java/at/tuwien/entities/user/User.java b/dbrepo-metadata-service/entities/src/main/java/at/tuwien/entities/user/User.java index ba86e3d29c6913d45d51ae0498fdac8d3092b657..156fc3b4c8efbd44e4d8389b50a7cb0f65814aac 100644 --- a/dbrepo-metadata-service/entities/src/main/java/at/tuwien/entities/user/User.java +++ b/dbrepo-metadata-service/entities/src/main/java/at/tuwien/entities/user/User.java @@ -65,7 +65,7 @@ public class User { @Column(name = "mariadb_password", nullable = false) private String mariadbPassword; - @Column(name = "is_internal", nullable = false, updatable = false) + @Column(name = "is_internal", nullable = false, updatable = false, columnDefinition = "bool default false") private Boolean isInternal; } diff --git a/dbrepo-metadata-service/rest-service/src/main/java/at/tuwien/endpoints/UserEndpoint.java b/dbrepo-metadata-service/rest-service/src/main/java/at/tuwien/endpoints/UserEndpoint.java index 51f323c30f1581314df88dab86ec2900775c215e..5ca14a5a34605e80ab48c5f2309f6dd15bea714f 100644 --- a/dbrepo-metadata-service/rest-service/src/main/java/at/tuwien/endpoints/UserEndpoint.java +++ b/dbrepo-metadata-service/rest-service/src/main/java/at/tuwien/endpoints/UserEndpoint.java @@ -106,47 +106,15 @@ public class UserEndpoint extends AbstractEndpoint { @ApiResponse(responseCode = "400", description = "Parameters are not well-formed (likely email)", content = {@Content(mediaType = "application/json")}), - @ApiResponse(responseCode = "403", - description = "Internal authentication to the auth service is invalid", - content = {@Content( - mediaType = "application/json", - schema = @Schema(implementation = ApiErrorDto.class))}), - @ApiResponse(responseCode = "404", - description = "Default role not found", - content = {@Content( - mediaType = "application/json", - schema = @Schema(implementation = ApiErrorDto.class))}), - @ApiResponse(responseCode = "409", - description = "User with username already exists", - content = {@Content( - mediaType = "application/json", - schema = @Schema(implementation = ApiErrorDto.class))}), - @ApiResponse(responseCode = "417", - description = "User with e-mail already exists", - content = {@Content( - mediaType = "application/json", - schema = @Schema(implementation = ApiErrorDto.class))}), - @ApiResponse(responseCode = "502", - description = "Failed to create in auth service", - content = {@Content( - mediaType = "application/json", - schema = @Schema(implementation = ApiErrorDto.class))}), - @ApiResponse(responseCode = "503", - description = "Failed to create in auth service", - content = {@Content( - mediaType = "application/json", - schema = @Schema(implementation = ApiErrorDto.class))}), }) - public ResponseEntity<UserBriefDto> create(@NotNull @Valid @RequestBody CreateUserDto data) - throws UserExistsException, EmailExistsException, AuthServiceException, AuthServiceConnectionException, - UserNotFoundException, CredentialsInvalidException { + public ResponseEntity<UserBriefDto> create(@NotNull @Valid @RequestBody CreateUserDto data) { log.debug("endpoint create user, data.id={}, data.username={}", data.getId(), data.getUsername()); return ResponseEntity.status(HttpStatus.CREATED) .body(userMapper.userToUserBriefDto( userService.create(data))); } - @GetMapping("/{userId}") + @RequestMapping(value = "/{userId}", method = {RequestMethod.GET, RequestMethod.HEAD}) @Transactional(readOnly = true) @PreAuthorize("isAuthenticated()") @Observed(name = "dbrepo_user_find") @@ -181,12 +149,14 @@ public class UserEndpoint extends AbstractEndpoint { throw new NotAllowedException("Failed to find user: foreign user"); } if (user.getIsInternal()) { - throw new UserNotFoundException("Failed to find user with username: " + user.getUsername()); + log.error("Failed to find user: internal user"); + throw new NotAllowedException("Failed to find user: internal user"); } final HttpHeaders headers = new HttpHeaders(); if (isSystem(principal)) { headers.set("X-Username", user.getUsername()); headers.set("X-Password", user.getMariadbPassword()); + headers.set("Access-Control-Expose-Headers", "X-Username X-Password"); } return ResponseEntity.status(HttpStatus.OK) .headers(headers) @@ -282,18 +252,18 @@ public class UserEndpoint extends AbstractEndpoint { @NotNull @Valid @RequestBody UserPasswordDto data, @NotNull Principal principal) throws NotAllowedException, UserNotFoundException, DatabaseNotFoundException, DataServiceException, - DataServiceConnectionException { + DataServiceConnectionException, AuthServiceException { log.debug("endpoint modify a user password, userId={}, principal.name={}", userId, principal.getName()); final User user = userService.findById(userId); if (!user.getUsername().equals(principal.getName())) { log.error("Failed to modify user password: not current user"); throw new NotAllowedException("Failed to modify user password: not current user"); } - authenticationService.updatePassword(user, data); for (Database database : databaseService.findAllAtLestReadAccess(userId)) { databaseService.updatePassword(database, user); } userService.updatePassword(user, data); + authenticationService.setupFinished(user); return ResponseEntity.accepted() .build(); } diff --git a/dbrepo-metadata-service/rest-service/src/main/java/at/tuwien/validation/EndpointValidator.java b/dbrepo-metadata-service/rest-service/src/main/java/at/tuwien/validation/EndpointValidator.java index a54f616b01e61edad50d85d4b5f0d494c9e429d6..6fe29c118bd47683874cd4a92397c62317de3a83 100644 --- a/dbrepo-metadata-service/rest-service/src/main/java/at/tuwien/validation/EndpointValidator.java +++ b/dbrepo-metadata-service/rest-service/src/main/java/at/tuwien/validation/EndpointValidator.java @@ -131,7 +131,7 @@ public class EndpointValidator extends AbstractEndpoint { final Optional<CreateTableColumnDto> optional3 = data.getColumns() .stream() .filter(c -> c.getType().equals(ColumnTypeDto.SET)) - .filter(c -> c.getEnums() == null || c.getSets().isEmpty()) + .filter(c -> c.getSets() == null || c.getSets().isEmpty()) .findFirst(); if (optional3.isPresent()) { log.error("Validation failed: column {} needs at least 1 allowed set value", optional3.get().getName()); diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/ApplicationIntegrationTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/ApplicationIntegrationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..33c7bc76c552c70989dbc4ffc39e317fdb5826ac --- /dev/null +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/ApplicationIntegrationTest.java @@ -0,0 +1,23 @@ +package at.tuwien; + +import lombok.extern.log4j.Log4j2; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@Log4j2 +@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) +@SpringBootTest +@ExtendWith(SpringExtension.class) +public class ApplicationIntegrationTest { + + @Test + public void main_succeeds() { + + /* test */ + DbrepoMetadataServiceApplication.main(new String[]{}); + } + +} diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/DatabaseEndpointUnitTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/DatabaseEndpointUnitTest.java index fd91fb5655ad563ee0b4f8503dbdb5b60db6fea9..7ab4d2c16a6c2a8fc2faa5fd4c1d5aa599bb4c16 100644 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/DatabaseEndpointUnitTest.java +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/DatabaseEndpointUnitTest.java @@ -269,7 +269,7 @@ public class DatabaseEndpointUnitTest extends AbstractUnitTest { @Test @WithAnonymousUser - public void list_anonymous_succeeds() throws DatabaseNotFoundException, UserNotFoundException { + public void list_anonymous_succeeds() { /* mock */ when(databaseService.findAllPublicOrSchemaPublic()) @@ -281,7 +281,7 @@ public class DatabaseEndpointUnitTest extends AbstractUnitTest { @Test @WithMockUser(username = USER_1_USERNAME, authorities = {"list-databases"}) - public void list_hasRole_succeeds() throws DatabaseNotFoundException, UserNotFoundException { + public void list_hasRole_succeeds() { /* pre-condition */ assertTrue(DATABASE_3_PUBLIC); @@ -296,7 +296,7 @@ public class DatabaseEndpointUnitTest extends AbstractUnitTest { @Test @WithMockUser(username = USER_1_USERNAME, authorities = {"list-databases"}) - public void list_hasRoleForeign_succeeds() throws DatabaseNotFoundException, UserNotFoundException { + public void list_hasRoleForeign_succeeds() { /* pre-condition */ assertTrue(DATABASE_3_PUBLIC); @@ -311,7 +311,7 @@ public class DatabaseEndpointUnitTest extends AbstractUnitTest { @Test @WithMockUser(username = USER_1_USERNAME, authorities = {"list-databases"}) - public void list_hasRoleFilter_succeeds() throws DatabaseNotFoundException, UserNotFoundException { + public void list_hasRoleFilter_succeeds() { /* mock */ when(databaseService.findAllPublicOrSchemaPublicOrReadAccessByInternalName(USER_1_ID, DATABASE_3_INTERNALNAME)) @@ -323,7 +323,7 @@ public class DatabaseEndpointUnitTest extends AbstractUnitTest { @Test @WithMockUser(username = USER_1_USERNAME, authorities = {"list-databases"}) - public void list_hasRoleFilterNoResult_succeeds() throws DatabaseNotFoundException, UserNotFoundException { + public void list_hasRoleFilterNoResult_succeeds() { /* mock */ when(databaseService.findAllPublicOrSchemaPublicOrReadAccessByInternalName(USER_1_ID, "i_do_not_exist")) @@ -333,6 +333,18 @@ public class DatabaseEndpointUnitTest extends AbstractUnitTest { list_generic("i_do_not_exist", USER_1_PRINCIPAL, 0); } + @Test + @WithAnonymousUser + public void list_filterNoResult_succeeds() { + + /* mock */ + when(databaseService.findAllPublicOrSchemaPublicByInternalName("i_do_not_exist")) + .thenReturn(List.of()); + + /* test */ + list_generic("i_do_not_exist", null, 0); + } + @Test @WithAnonymousUser public void visibility_anonymous_fails() { @@ -569,7 +581,7 @@ public class DatabaseEndpointUnitTest extends AbstractUnitTest { /* test */ final DatabaseDto database = findById_generic(DATABASE_1_ID, DATABASE_1, USER_LOCAL_ADMIN_PRINCIPAL); - assertEquals(4, database.getTables().size()); + assertEquals(2, database.getTables().size()); assertEquals(2, database.getViews().size()); assertNotEquals(0, database.getAccesses().size()); } @@ -610,6 +622,58 @@ public class DatabaseEndpointUnitTest extends AbstractUnitTest { assertEquals(3, database.getAccesses().size()); } + @Test + @WithMockUser(username = USER_2_USERNAME) + public void findById_hiddenAccessRights_succeeds() throws DataServiceException, DataServiceConnectionException, + DatabaseNotFoundException, ExchangeNotFoundException, UserNotFoundException, NotAllowedException { + + /* mock */ + when(accessService.list(DATABASE_1)) + .thenReturn(List.of(DATABASE_1_USER_1_WRITE_ALL_ACCESS, DATABASE_1_USER_2_READ_ACCESS)); + + /* test */ + final DatabaseDto database = findById_generic(DATABASE_1_ID, DATABASE_1, USER_2_PRINCIPAL); + assertEquals(4, database.getTables().size()); + assertEquals(3, database.getViews().size()); + assertEquals(0, database.getAccesses().size()); + } + + @Test + @WithMockUser(username = USER_1_USERNAME) + public void findById_hiddenAccessRightsSeesOwn_succeeds() throws DataServiceException, DataServiceConnectionException, + DatabaseNotFoundException, ExchangeNotFoundException, UserNotFoundException, NotAllowedException { + + /* mock */ + when(accessService.list(DATABASE_1)) + .thenReturn(List.of(DATABASE_1_USER_1_WRITE_ALL_ACCESS, DATABASE_1_USER_2_READ_ACCESS)); + + /* test */ + final DatabaseDto database = findById_generic(DATABASE_1_ID, DATABASE_1, USER_1_PRINCIPAL); + assertEquals(4, database.getTables().size()); + assertEquals(3, database.getViews().size()); + assertEquals(3, database.getAccesses().size()); + } + + @Test + @WithMockUser(username = USER_1_USERNAME) + public void findById_privateDataPrivateSchemaNoAccess_fails() { + + /* test */ + assertThrows(NotAllowedException.class, () -> { + findById_generic(DATABASE_1_ID, DATABASE_1, USER_4_PRINCIPAL); + }); + } + + @Test + @WithMockUser(username = USER_1_USERNAME) + public void findById_anonymousPrivateDataPrivateSchema_fails() { + + /* test */ + assertThrows(NotAllowedException.class, () -> { + findById_generic(DATABASE_1_ID, DATABASE_1, null); + }); + } + @Test @WithAnonymousUser public void findPreviewImage_anonymous_succeeds() throws DatabaseNotFoundException { @@ -660,8 +724,7 @@ public class DatabaseEndpointUnitTest extends AbstractUnitTest { /* ## GENERIC TEST CASES ## */ /* ################################################################################################### */ - public void list_generic(String internalName, Principal principal, Integer expectedSize) - throws DatabaseNotFoundException, UserNotFoundException { + public void list_generic(String internalName, Principal principal, Integer expectedSize) { /* test */ final ResponseEntity<List<DatabaseBriefDto>> response = databaseEndpoint.list(internalName, principal); 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 a17d31649e6824442613261b91bca4ee923a4817..094673416bc69cae50e25e22f74a0b510e9c30c6 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 @@ -1,12 +1,12 @@ package at.tuwien.endpoints; -import at.tuwien.api.database.table.TableBriefDto; import at.tuwien.api.database.table.CreateTableDto; +import at.tuwien.api.database.table.TableBriefDto; import at.tuwien.api.database.table.TableDto; import at.tuwien.api.database.table.TableUpdateDto; -import at.tuwien.api.database.table.columns.CreateTableColumnDto; import at.tuwien.api.database.table.columns.ColumnDto; import at.tuwien.api.database.table.columns.ColumnTypeDto; +import at.tuwien.api.database.table.columns.CreateTableColumnDto; import at.tuwien.api.database.table.columns.concepts.ColumnSemanticsUpdateDto; import at.tuwien.api.database.table.constraints.CreateTableConstraintsDto; import at.tuwien.api.semantics.EntityDto; @@ -40,6 +40,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; import java.security.Principal; import java.util.List; +import java.util.UUID; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; @@ -524,7 +525,8 @@ public class TableEndpointUnitTest extends AbstractUnitTest { @Test @WithAnonymousUser public void findById_publicDatabasePrivateDataPrivateSchemaAnonymous_succeeds() throws UserNotFoundException, - TableNotFoundException, NotAllowedException, DataServiceException, DatabaseNotFoundException, AccessNotFoundException, QueueNotFoundException, DataServiceConnectionException { + TableNotFoundException, NotAllowedException, DataServiceException, DatabaseNotFoundException, + AccessNotFoundException, QueueNotFoundException, DataServiceConnectionException { /* test */ final ResponseEntity<TableDto> response = generic_findById(DATABASE_3_ID, DATABASE_3, TABLE_8_ID, TABLE_8, null, null, null); @@ -596,6 +598,16 @@ public class TableEndpointUnitTest extends AbstractUnitTest { }); } + @Test + @WithMockUser(username = USER_4_USERNAME, authorities = {"table-semantic-analyse"}) + public void analyseTable_notOwner_fails() { + + /* test */ + assertThrows(NotAllowedException.class, () -> { + analyseTable_generic(DATABASE_1_ID, DATABASE_1, TABLE_1_ID, TABLE_1, USER_4_PRINCIPAL); + }); + } + @Test @WithMockUser(username = USER_4_USERNAME) public void findAll_noRole_fails() { @@ -930,6 +942,29 @@ public class TableEndpointUnitTest extends AbstractUnitTest { generic_findById(DATABASE_1_ID, DATABASE_1, TABLE_2_ID, TABLE_2, null, null, null); } + @Test + @WithMockUser(username = USER_4_USERNAME) + public void findById_privateSchemaNotOwnerNoAccess_fails() { + + /* test */ + assertThrows(NotAllowedException.class, () -> { + generic_findById(DATABASE_1_ID, DATABASE_1, TABLE_4_ID, TABLE_4, USER_4_PRINCIPAL, USER_4, null); + }); + } + + @Test + @WithMockUser(username = USER_4_USERNAME) + public void findById_publicSchemaNotOwnerNoAccess_succeeds() throws UserNotFoundException, TableNotFoundException, + NotAllowedException, DataServiceException, DatabaseNotFoundException, AccessNotFoundException, + QueueNotFoundException, DataServiceConnectionException { + + /* test */ + final ResponseEntity<TableDto> response = generic_findById(DATABASE_1_ID, DATABASE_1, TABLE_2_ID, TABLE_2, USER_4_PRINCIPAL, USER_4, null); + assertEquals(HttpStatus.OK, response.getStatusCode()); + final TableDto body = response.getBody(); + assertNotNull(body); + } + @Test @WithMockUser(username = USER_1_USERNAME, authorities = "find-table") public void findById_privateHasRoleTableNotFound_fails() { @@ -970,7 +1005,7 @@ public class TableEndpointUnitTest extends AbstractUnitTest { AccessNotFoundException, QueueNotFoundException, DataServiceConnectionException { /* test */ - generic_findById(DATABASE_1_ID, DATABASE_1, TABLE_1_ID, TABLE_1, USER_4_PRINCIPAL, USER_4, null); + generic_findById(DATABASE_1_ID, DATABASE_1, TABLE_1_ID, TABLE_1, USER_4_PRINCIPAL, USER_4, DATABASE_1_USER_4_READ_ACCESS); } @Test @@ -1081,6 +1116,17 @@ public class TableEndpointUnitTest extends AbstractUnitTest { assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); } + @Test + @WithMockUser(username = USER_LOCAL_ADMIN_USERNAME, authorities = {"system", "update-table-statistic"}) + public void updateStatistic_internalUser_succeeds() throws TableNotFoundException, SearchServiceException, + MalformedException, NotAllowedException, DataServiceException, DatabaseNotFoundException, + SearchServiceConnectionException, DataServiceConnectionException { + + /* test */ + final ResponseEntity<Void> response = generic_updateStatistic(USER_LOCAL_ADMIN_PRINCIPAL); + assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); + } + /* ################################################################################################### */ /* ## GENERIC TEST CASES ## */ /* ################################################################################################### */ @@ -1148,7 +1194,7 @@ public class TableEndpointUnitTest extends AbstractUnitTest { } protected ResponseEntity<TableBriefDto> generic_create(Long databaseId, Database database, CreateTableDto data, - Principal principal, User user, DatabaseAccess access) + Principal principal, User user, DatabaseAccess access) throws MalformedException, NotAllowedException, DataServiceException, DataServiceConnectionException, UserNotFoundException, DatabaseNotFoundException, AccessNotFoundException, TableNotFoundException, TableExistsException, SearchServiceException, SearchServiceConnectionException, OntologyNotFoundException, @@ -1203,11 +1249,21 @@ public class TableEndpointUnitTest extends AbstractUnitTest { .when(tableService) .findById(any(Database.class), eq(tableId)); } - if (principal != null) { + if (user != null) { when(userService.findById(user.getId())) .thenReturn(user); + } else { + doThrow(UserNotFoundException.class) + .when(userService) + .findById(any(UUID.class)); + } + if (access != null) { when(accessService.find(any(Database.class), eq(user))) .thenReturn(access); + } else { + doThrow(AccessNotFoundException.class) + .when(accessService) + .find(any(Database.class), eq(user)); } /* test */ diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/UserEndpointUnitTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/UserEndpointUnitTest.java index 6ef4bd8779f08abaaa28d68e306e0a700eebd39b..de5c8993a118471b786710a72fbc8799499d4a82 100644 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/UserEndpointUnitTest.java +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/endpoints/UserEndpointUnitTest.java @@ -1,5 +1,6 @@ package at.tuwien.endpoints; +import at.tuwien.api.auth.CreateUserDto; import at.tuwien.api.user.UserBriefDto; import at.tuwien.api.user.UserDto; import at.tuwien.api.user.UserPasswordDto; @@ -19,6 +20,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.security.access.AccessDeniedException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.test.context.support.WithAnonymousUser; @@ -63,6 +65,15 @@ public class UserEndpointUnitTest extends AbstractUnitTest { assertEquals(2, response.size()); } + @Test + @WithAnonymousUser + public void findAll_filterInternalUserEmptyList_succeeds() throws UserNotFoundException { + + /* test */ + final List<UserBriefDto> response = findAll_generic(USER_LOCAL_ADMIN_USERNAME, null); + assertEquals(0, response.size()); + } + @Test @WithMockUser(username = USER_1_USERNAME) public void findAll_noRole_succeeds() throws UserNotFoundException { @@ -142,6 +153,18 @@ public class UserEndpointUnitTest extends AbstractUnitTest { assertEquals(USER_3_DATABASE_PASSWORD, response.getHeaders().get("X-Password").get(0)); } + @Test + @WithMockUser(username = USER_LOCAL_ADMIN_USERNAME, authorities = {"system"}) + public void find_internalUser_fails() { + final Principal principal = new UsernamePasswordAuthenticationToken(USER_LOCAL_ADMIN_DETAILS, USER_LOCAL_ADMIN_PASSWORD, List.of( + new SimpleGrantedAuthority("system"))); + + /* test */ + assertThrows(NotAllowedException.class, () -> { + find_generic(USER_LOCAL_ADMIN_ID, USER_LOCAL, principal); + }); + } + @Test @WithAnonymousUser public void modify_anonymous_fails() { @@ -233,7 +256,7 @@ public class UserEndpointUnitTest extends AbstractUnitTest { @Test @WithMockUser(username = USER_1_USERNAME) public void password_succeeds() throws NotAllowedException, DataServiceException, DataServiceConnectionException, - UserNotFoundException, DatabaseNotFoundException { + UserNotFoundException, DatabaseNotFoundException, AuthServiceException { final UserPasswordDto request = UserPasswordDto.builder() .password(USER_1_PASSWORD) .build(); @@ -242,6 +265,38 @@ public class UserEndpointUnitTest extends AbstractUnitTest { password_generic(USER_1_PRINCIPAL, request); } + @Test + @WithAnonymousUser + public void create_anonymous_fails() { + + /* test */ + assertThrows(AccessDeniedException.class, () -> { + generic_create(USER_1_CREATE_USER_DTO); + }); + } + + @Test + @WithMockUser(username = USER_2_USERNAME) + public void create_notInternalUser_fails() { + + /* test */ + assertThrows(AccessDeniedException.class, () -> { + generic_create(USER_1_CREATE_USER_DTO); + }); + } + + @Test + @WithMockUser(username = USER_LOCAL_ADMIN_USERNAME, authorities = {"system"}) + public void create_succeeds() { + + /* mock */ + when(userService.create(USER_1_CREATE_USER_DTO)) + .thenReturn(USER_1); + + /* test */ + generic_create(USER_1_CREATE_USER_DTO); + } + /* ################################################################################################### */ /* ## GENERIC TEST CASES ## */ /* ################################################################################################### */ @@ -260,7 +315,7 @@ public class UserEndpointUnitTest extends AbstractUnitTest { } } else { when(userService.findAll()) - .thenReturn(List.of(USER_1, USER_2)); + .thenReturn(List.of(USER_1, USER_2, USER_LOCAL)); } /* test */ @@ -310,14 +365,15 @@ public class UserEndpointUnitTest extends AbstractUnitTest { } protected void password_generic(Principal principal, UserPasswordDto data) throws NotAllowedException, - DataServiceException, DataServiceConnectionException, UserNotFoundException, DatabaseNotFoundException { + DataServiceException, DataServiceConnectionException, UserNotFoundException, DatabaseNotFoundException, + AuthServiceException { /* mock */ when(userService.findById(USER_1_ID)) .thenReturn(USER_1); doNothing() .when(authenticationService) - .updatePassword(USER_1, data); + .setupFinished(USER_1); doNothing() .when(userService) .updatePassword(USER_1, data); @@ -331,4 +387,13 @@ public class UserEndpointUnitTest extends AbstractUnitTest { final ResponseEntity<?> response = userEndpoint.password(USER_1_ID, data, principal); assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); } + + protected void generic_create(CreateUserDto data) { + + /* test */ + final ResponseEntity<UserBriefDto> response = userEndpoint.create(data); + assertEquals(HttpStatus.CREATED, response.getStatusCode()); + final UserBriefDto body = response.getBody(); + assertNotNull(body); + } } diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/gateway/KeycloakGatewayIntegrationTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/gateway/KeycloakGatewayIntegrationTest.java index e72cd7fa7591a7e641c74df6eab07845b0a193ea..58701adfc34f3d8c1be0b189a857307737bcfc47 100644 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/gateway/KeycloakGatewayIntegrationTest.java +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/gateway/KeycloakGatewayIntegrationTest.java @@ -92,23 +92,4 @@ public class KeycloakGatewayIntegrationTest extends AbstractUnitTest { }); } - @Test - public void updateUserCredentials_succeeds() throws UserNotFoundException { - - /* mock */ - keycloakUtils.createUser(USER_1_ID, USER_1_KEYCLOAK_SIGNUP_REQUEST); - - /* test */ - keycloakGateway.updateUserCredentials(keycloakUtils.getUserId(USER_1_USERNAME), USER_1_PASSWORD_DTO); - } - - @Test - public void updateUserCredentials_notFound_fails() { - - /* test */ - assertThrows(UserNotFoundException.class, () -> { - keycloakGateway.updateUserCredentials(keycloakUtils.getUserId(USER_1_USERNAME), USER_1_PASSWORD_DTO); - }); - } - } diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/handlers/ApiExceptionHandlerTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/handlers/ApiExceptionHandlerTest.java index 9075ec2a02d0420b9fe0cec8c307a9dc8ac1c13e..a58a4b49da03358371c1346b16d893ac8b51bb8e 100644 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/handlers/ApiExceptionHandlerTest.java +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/handlers/ApiExceptionHandlerTest.java @@ -1,12 +1,16 @@ package at.tuwien.handlers; +import at.tuwien.api.error.ApiErrorDto; +import at.tuwien.exception.*; import at.tuwien.test.AbstractUnitTest; import lombok.extern.log4j.Log4j2; import org.junit.jupiter.api.Assertions; 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.test.context.junit.jupiter.SpringExtension; import org.springframework.web.bind.annotation.ResponseStatus; @@ -18,12 +22,17 @@ import java.util.Optional; import static at.tuwien.test.utils.EndpointUtils.getErrorCodes; import static at.tuwien.test.utils.EndpointUtils.getExceptions; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; @Log4j2 @ExtendWith(SpringExtension.class) @SpringBootTest public class ApiExceptionHandlerTest extends AbstractUnitTest { + @Autowired + private ApiExceptionHandler apiExceptionHandler; + @Test public void handle_succeeds() throws ClassNotFoundException, IOException { final List<Method> handlers = Arrays.asList(ApiExceptionHandler.class.getMethods()); @@ -42,7 +51,903 @@ public class ApiExceptionHandlerTest extends AbstractUnitTest { Assertions.assertNotNull(exception.getDeclaredAnnotation(ResponseStatus.class).reason(), "Exception " + exception.getName() + " does not provide a reason code"); Assertions.assertTrue(errorCodes.contains(exception.getDeclaredAnnotation(ResponseStatus.class).reason()), "Exception code " + exception.getDeclaredAnnotation(ResponseStatus.class).reason() + " does have a reason code mapped in localized ui error messages"); /* handler method */ - Assertions.assertEquals(method.getDeclaredAnnotation(ResponseStatus.class).code(), exception.getDeclaredAnnotation(ResponseStatus.class).code()); + assertEquals(method.getDeclaredAnnotation(ResponseStatus.class).code(), exception.getDeclaredAnnotation(ResponseStatus.class).code()); } } + + @Test + public void handle_accessNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new AccessNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.access.missing", body.getCode()); + } + + + @Test + public void handle_accountNotSetupException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new AccountNotSetupException("msg")); + assertEquals(HttpStatus.PRECONDITION_REQUIRED, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.PRECONDITION_REQUIRED, body.getStatus()); + assertEquals("error.user.setup", body.getCode()); + } + + + @Test + public void handle_analyseServiceException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new AnalyseServiceException("msg")); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, body.getStatus()); + assertEquals("error.analyse.invalid", body.getCode()); + } + + + @Test + public void handle_authServiceConnectionException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new AuthServiceConnectionException("msg")); + assertEquals(HttpStatus.BAD_GATEWAY, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_GATEWAY, body.getStatus()); + assertEquals("error.auth.connection", body.getCode()); + } + + + @Test + public void handle_authServiceException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new AuthServiceException("msg")); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, body.getStatus()); + assertEquals("error.auth.invalid", body.getCode()); + } + + + @Test + public void handle_brokerServiceConnectionException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new BrokerServiceConnectionException("msg")); + assertEquals(HttpStatus.BAD_GATEWAY, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_GATEWAY, body.getStatus()); + assertEquals("error.broker.connection", body.getCode()); + } + + + @Test + public void handle_brokerServiceException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new BrokerServiceException("msg")); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, body.getStatus()); + assertEquals("error.broker.invalid", body.getCode()); + } + + + @Test + public void handle_conceptNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new ConceptNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.concept.missing", body.getCode()); + } + + + @Test + public void handle_containerAlreadyExistsException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new ContainerAlreadyExistsException("msg")); + assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.CONFLICT, body.getStatus()); + assertEquals("error.container.exists", body.getCode()); + } + + + @Test + public void handle_containerNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new ContainerNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.container.missing", body.getCode()); + } + + + @Test + public void handle_containerQuotaException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new ContainerQuotaException("msg")); + assertEquals(HttpStatus.LOCKED, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.LOCKED, body.getStatus()); + assertEquals("error.container.quota", body.getCode()); + } + + + @Test + public void handle_credentialsInvalidException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new CredentialsInvalidException("msg")); + assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.FORBIDDEN, body.getStatus()); + assertEquals("error.user.credentials", body.getCode()); + } + + + @Test + public void handle_dataServiceConnectionException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new DataServiceConnectionException("msg")); + assertEquals(HttpStatus.BAD_GATEWAY, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_GATEWAY, body.getStatus()); + assertEquals("error.data.connection", body.getCode()); + } + + + @Test + public void handle_dataServiceException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new DataServiceException("msg")); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, body.getStatus()); + assertEquals("error.data.invalid", body.getCode()); + } + + + @Test + public void handle_databaseMalformedException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new DatabaseMalformedException("msg")); + assertEquals(HttpStatus.EXPECTATION_FAILED, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.EXPECTATION_FAILED, body.getStatus()); + assertEquals("error.database.invalid", body.getCode()); + } + + + @Test + public void handle_databaseNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new DatabaseNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.database.missing", body.getCode()); + } + + + @Test + public void handle_databaseUnavailableException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new DatabaseUnavailableException("msg")); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, body.getStatus()); + assertEquals("error.database.connection", body.getCode()); + } + + + @Test + public void handle_doiNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new DoiNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.doi.missing", body.getCode()); + } + + + @Test + public void handle_emailExistsException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new EmailExistsException("msg")); + assertEquals(HttpStatus.EXPECTATION_FAILED, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.EXPECTATION_FAILED, body.getStatus()); + assertEquals("error.user.email-exists", body.getCode()); + } + + + @Test + public void handle_exchangeNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new ExchangeNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.exchange.missing", body.getCode()); + } + + + @Test + public void handle_externalServiceException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new ExternalServiceException("msg")); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, body.getStatus()); + assertEquals("error.external.invalid", body.getCode()); + } + + + @Test + public void handle_filterBadRequestException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new FilterBadRequestException("msg")); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_REQUEST, body.getStatus()); + assertEquals("error.semantic.filter", body.getCode()); + } + + + @Test + public void handle_formatNotAvailableException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new FormatNotAvailableException("msg")); + assertEquals(HttpStatus.NOT_ACCEPTABLE, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_ACCEPTABLE, body.getStatus()); + assertEquals("error.identifier.format", body.getCode()); + } + + + @Test + public void handle_identifierNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new IdentifierNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.identifier.missing", body.getCode()); + } + + + @Test + public void handle_identifierNotSupportedException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new IdentifierNotSupportedException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.identifier.unsupported", body.getCode()); + } + + + @Test + public void handle_imageAlreadyExistsException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new ImageAlreadyExistsException("msg")); + assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.CONFLICT, body.getStatus()); + assertEquals("error.image.exists", body.getCode()); + } + + + @Test + public void handle_imageInvalidException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new ImageInvalidException("msg")); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_REQUEST, body.getStatus()); + assertEquals("error.image.invalid", body.getCode()); + } + + + @Test + public void handle_imageNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new ImageNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.image.missing", body.getCode()); + } + + + @Test + public void handle_licenseNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new LicenseNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.license.missing", body.getCode()); + } + + + @Test + public void handle_malformedException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new MalformedException("msg")); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_REQUEST, body.getStatus()); + assertEquals("error.request.invalid", body.getCode()); + } + + + @Test + public void handle_messageNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new MessageNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.message.missing", body.getCode()); + } + + + @Test + public void handle_metadataServiceConnectionException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new MetadataServiceConnectionException("msg")); + assertEquals(HttpStatus.BAD_GATEWAY, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_GATEWAY, body.getStatus()); + assertEquals("error.metadata.connection", body.getCode()); + } + + + @Test + public void handle_metadataServiceException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new MetadataServiceException("msg")); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, body.getStatus()); + assertEquals("error.metadata.invalid", body.getCode()); + } + + + @Test + public void handle_notAllowedException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new NotAllowedException("msg")); + assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.FORBIDDEN, body.getStatus()); + assertEquals("error.request.forbidden", body.getCode()); + } + + + @Test + public void handle_ontologyNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new OntologyNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.ontology.missing", body.getCode()); + } + + + @Test + public void handle_orcidNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new OrcidNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.orcid.missing", body.getCode()); + } + + + @Test + public void handle_paginationException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new PaginationException("msg")); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_REQUEST, body.getStatus()); + assertEquals("error.request.pagination", body.getCode()); + } + + + @Test + public void handle_queryMalformedException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new QueryMalformedException("msg")); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_REQUEST, body.getStatus()); + assertEquals("error.query.invalid", body.getCode()); + } + + + @Test + public void handle_queryNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new QueryNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.query.missing", body.getCode()); + } + + + @Test + public void handle_queryNotSupportedException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new QueryNotSupportedException("msg")); + assertEquals(HttpStatus.NOT_IMPLEMENTED, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_IMPLEMENTED, body.getStatus()); + assertEquals("error.query.invalid", body.getCode()); + } + + + @Test + public void handle_queryStoreCreateException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new QueryStoreCreateException("msg")); + assertEquals(HttpStatus.EXPECTATION_FAILED, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.EXPECTATION_FAILED, body.getStatus()); + assertEquals("error.store.invalid", body.getCode()); + } + + + @Test + public void handle_queryStoreGCException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new QueryStoreGCException("msg")); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_REQUEST, body.getStatus()); + assertEquals("error.store.clean", body.getCode()); + } + + + @Test + public void handle_queryStoreInsertException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new QueryStoreInsertException("msg")); + assertEquals(HttpStatus.EXPECTATION_FAILED, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.EXPECTATION_FAILED, body.getStatus()); + assertEquals("error.store.insert", body.getCode()); + } + + + @Test + public void handle_queryStorePersistException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new QueryStorePersistException("msg")); + assertEquals(HttpStatus.EXPECTATION_FAILED, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.EXPECTATION_FAILED, body.getStatus()); + assertEquals("error.store.persist", body.getCode()); + } + + + @Test + public void handle_queueNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new QueueNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.queue.missing", body.getCode()); + } + + + @Test + public void handle_remoteUnavailableException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new RemoteUnavailableException("msg")); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, body.getStatus()); + assertEquals("error.metadata.privileged", body.getCode()); + } + + + @Test + public void handle_rorNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new RorNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.ror.missing", body.getCode()); + } + + + @Test + public void handle_searchServiceConnectionException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new SearchServiceConnectionException("msg")); + assertEquals(HttpStatus.BAD_GATEWAY, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_GATEWAY, body.getStatus()); + assertEquals("error.search.connection", body.getCode()); + } + + + @Test + public void handle_searchServiceException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new SearchServiceException("msg")); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, body.getStatus()); + assertEquals("error.search.invalid", body.getCode()); + } + + + @Test + public void handle_semanticEntityNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new SemanticEntityNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.semantic.missing", body.getCode()); + } + + + @Test + public void handle_sortException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new SortException("msg")); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_REQUEST, body.getStatus()); + assertEquals("error.request.sort", body.getCode()); + } + + + @Test + public void handle_storageNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new StorageNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.storage.missing", body.getCode()); + } + + + @Test + public void handle_storageUnavailableException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new StorageUnavailableException("msg")); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, body.getStatus()); + assertEquals("error.storage.invalid", body.getCode()); + } + + + @Test + public void handle_tableExistsException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new TableExistsException("msg")); + assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.CONFLICT, body.getStatus()); + assertEquals("error.table.exists", body.getCode()); + } + + + @Test + public void handle_tableMalformedException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new TableMalformedException("msg")); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_REQUEST, body.getStatus()); + assertEquals("error.table.invalid", body.getCode()); + } + + + @Test + public void handle_tableNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new TableNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.table.missing", body.getCode()); + } + + + @Test + public void handle_tableSchemaException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new TableSchemaException("msg")); + assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.CONFLICT, body.getStatus()); + assertEquals("error.schema.table", body.getCode()); + } + + + @Test + public void handle_unitNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new UnitNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.unit.missing", body.getCode()); + } + + + @Test + public void handle_uriMalformedException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new UriMalformedException("msg")); + assertEquals(HttpStatus.EXPECTATION_FAILED, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.EXPECTATION_FAILED, body.getStatus()); + assertEquals("error.semantics.uri", body.getCode()); + } + + + @Test + public void handle_userExistsException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new UserExistsException("msg")); + assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.CONFLICT, body.getStatus()); + assertEquals("error.user.exists", body.getCode()); + } + + + @Test + public void handle_userNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new UserNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.user.missing", body.getCode()); + } + + + @Test + public void handle_viewMalformedException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new ViewMalformedException("msg")); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.BAD_REQUEST, body.getStatus()); + assertEquals("error.view.invalid", body.getCode()); + } + + + @Test + public void handle_viewNotFoundException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new ViewNotFoundException("msg")); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, body.getStatus()); + assertEquals("error.view.missing", body.getCode()); + } + + + @Test + public void handle_viewSchemaException_succeeds() { + + /* test */ + final ResponseEntity<ApiErrorDto> response = apiExceptionHandler.handle(new ViewSchemaException("msg")); + assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); + final ApiErrorDto body = response.getBody(); + assertNotNull(body); + assertEquals("msg", body.getMessage()); + assertEquals(HttpStatus.CONFLICT, body.getStatus()); + assertEquals("error.schema.view", body.getCode()); + } + } diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/UserServiceIntegrationTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/UserServiceIntegrationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..39aed0d28ea800574177821a252dbbfbde647db9 --- /dev/null +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/UserServiceIntegrationTest.java @@ -0,0 +1,85 @@ +package at.tuwien.service; + +import at.tuwien.entities.user.User; +import at.tuwien.exception.AuthServiceException; +import at.tuwien.exception.UserNotFoundException; +import at.tuwien.gateway.KeycloakGateway; +import at.tuwien.repository.UserRepository; +import at.tuwien.test.AbstractUnitTest; +import at.tuwien.utils.KeycloakUtils; +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.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.testcontainers.images.PullPolicy; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@Log4j2 +@Testcontainers +@ExtendWith(SpringExtension.class) +@SpringBootTest +public class UserServiceIntegrationTest extends AbstractUnitTest { + + @Autowired + private UserRepository userRepository; + + @Autowired + private UserService userService; + + @Autowired + private KeycloakUtils keycloakUtils; + + @BeforeEach + public void beforeEach() { + genesis(); + /* keycloak */ + userRepository.deleteAll(); + keycloakUtils.deleteUser(USER_1_USERNAME); + } + + @Container + private static KeycloakContainer keycloakContainer = new KeycloakContainer(KEYCLOAK_IMAGE) + .withImagePullPolicy(PullPolicy.alwaysPull()) + .withAdminUsername("admin") + .withAdminPassword("admin") + .withRealmImportFile("./init/dbrepo-realm.json") + .withEnv("KC_HOSTNAME_STRICT_HTTPS", "false"); + + @DynamicPropertySource + static void keycloakProperties(DynamicPropertyRegistry registry) { + final String authServiceEndpoint = "http://localhost:" + keycloakContainer.getMappedPort(8080); + log.trace("set auth endpoint: {}", authServiceEndpoint); + registry.add("dbrepo.endpoints.authService", () -> authServiceEndpoint); + } + + @Test + public void create_succeeds() throws UserNotFoundException, AuthServiceException { + + /* test */ + final User response = userService.create(USER_1_CREATE_USER_DTO); + assertEquals(USER_1_ID, response.getId()); + assertEquals(USER_1_KEYCLOAK_ID, response.getKeycloakId()); + assertEquals(USER_1_USERNAME, response.getUsername()); + assertEquals(USER_1_THEME, response.getTheme()); + assertNotNull(response.getMariadbPassword()); + assertEquals(USER_1_LANGUAGE, response.getLanguage()); + assertEquals(USER_1_FIRSTNAME, response.getFirstname()); + assertEquals(USER_1_LASTNAME, response.getLastname()); + assertEquals(USER_1_IS_INTERNAL, response.getIsInternal()); + } + +} diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/UserServicePersistenceTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/UserServicePersistenceTest.java index 514d23b227dc3294741e2dcd212083f9ff23f619..eb228ab3c3c3f781a585fa199b63c7a512d222c3 100644 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/UserServicePersistenceTest.java +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/UserServicePersistenceTest.java @@ -42,7 +42,7 @@ public class UserServicePersistenceTest extends AbstractUnitTest { public void beforeEach() { genesis(); /* metadata database */ - userRepository.save(USER_1); + userRepository.saveAll(List.of(USER_1, USER_LOCAL)); } @Test @@ -54,6 +54,16 @@ public class UserServicePersistenceTest extends AbstractUnitTest { assertEquals(USER_1_USERNAME, response.getUsername()); } + @Test + public void findAllInternalUsers_succeeds() { + + /* test */ + final List<User> response = userService.findAllInternalUsers(); + assertEquals(1, response.size()); + final User user0 = response.get(0); + assertEquals(USER_LOCAL_ADMIN_ID, user0.getId()); + } + @Test public void findByUsername_fails() { @@ -68,7 +78,7 @@ public class UserServicePersistenceTest extends AbstractUnitTest { /* test */ final List<User> response = userService.findAll(); - assertEquals(1, response.size()); + assertEquals(2, response.size()); } @Test diff --git a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/UserServiceUnitTest.java b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/UserServiceUnitTest.java index 58d7cdc5e47f79cd45dd868d1b473a8051c79dce..df13d00b0858a28dd4ccc38c5542601790dc964a 100644 --- a/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/UserServiceUnitTest.java +++ b/dbrepo-metadata-service/rest-service/src/test/java/at/tuwien/service/UserServiceUnitTest.java @@ -93,12 +93,12 @@ public class UserServiceUnitTest extends AbstractUnitTest { } @Test - public void updatePassword_succeeds() throws UserNotFoundException { + public void updatePassword_succeeds() throws UserNotFoundException, AuthServiceException { /* mock */ doNothing() .when(keycloakGateway) - .updateUserCredentials(USER_1_ID, USER_1_PASSWORD_DTO); + .setupFinished(USER_1_ID); when(userRepository.findById(USER_1_ID)) .thenReturn(Optional.of(USER_1)); when(userRepository.save(any(User.class))) 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 486db28e5945737e776da4b8f0aaf6d6d977715e..8105a7fb8907b508a336876e9c5e6ce94916e59d 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 @@ -52,9 +52,27 @@ public class EndpointValidatorUnitTest extends AbstractUnitTest { public static Stream<Arguments> needSize_parameters() { return Stream.of( - Arguments.arguments(ColumnTypeDto.VARCHAR), - Arguments.arguments(ColumnTypeDto.BINARY), - Arguments.arguments(ColumnTypeDto.VARBINARY) + Arguments.arguments("varchar", ColumnTypeDto.VARCHAR), + Arguments.arguments("binary", ColumnTypeDto.BINARY), + Arguments.arguments("varbinary", ColumnTypeDto.VARBINARY) + ); + } + + public static Stream<Arguments> needSizeAndD_parameters() { + return Stream.of( + Arguments.arguments("double_size", ColumnTypeDto.DOUBLE, 40L, null), + Arguments.arguments("double_d", ColumnTypeDto.DOUBLE, null, 10L), + Arguments.arguments("decimal_size", ColumnTypeDto.DECIMAL, 40L, null), + Arguments.arguments("decimal_d", ColumnTypeDto.DECIMAL, null, 10L) + ); + } + + public static Stream<Arguments> enums_parameters() { + return Stream.of( + Arguments.arguments("enums_null", ColumnTypeDto.ENUM, null), + Arguments.arguments("enums_empty", ColumnTypeDto.ENUM, List.of()), + Arguments.arguments("sets_null", ColumnTypeDto.SET, null), + Arguments.arguments("sets_empty", ColumnTypeDto.SET, List.of()) ); } @@ -244,6 +262,20 @@ public class EndpointValidatorUnitTest extends AbstractUnitTest { endpointValidator.validateOnlyWriteOwnOrWriteAllAccess(TABLE_1, USER_1); } + @Test + public void validateOnlyWriteOwnOrWriteAllAccess_writeOwnAccess_succeeds() throws DatabaseNotFoundException, + TableNotFoundException, AccessNotFoundException, NotAllowedException { + + /* mock */ + when(tableService.findById(DATABASE_1, TABLE_1_ID)) + .thenReturn(TABLE_1); + when(accessService.find(eq(DATABASE_1), any(User.class))) + .thenReturn(DATABASE_1_USER_1_WRITE_OWN_ACCESS); + + /* test */ + endpointValidator.validateOnlyWriteOwnOrWriteAllAccess(TABLE_1, USER_1); + } + @Test public void validateOnlyWriteOwnOrWriteAllAccess_privateHasReadAccess_fails() throws DatabaseNotFoundException, TableNotFoundException, AccessNotFoundException { @@ -285,7 +317,7 @@ public class EndpointValidatorUnitTest extends AbstractUnitTest { @ParameterizedTest @MethodSource("needSize_parameters") - public void validateColumnCreateConstraints_needSize_fails(ColumnTypeDto type) { + public void validateColumnCreateConstraints_needSize_fails(String name, ColumnTypeDto type) { final CreateTableDto request = CreateTableDto.builder() .columns(List.of(CreateTableColumnDto.builder() .type(type) @@ -299,12 +331,13 @@ public class EndpointValidatorUnitTest extends AbstractUnitTest { }); } - @Test - public void validateColumnCreateConstraints_needEnum_fails() { + @ParameterizedTest + @MethodSource("enums_parameters") + public void validateColumnCreateConstraints_needEnum_fails(String name, ColumnTypeDto type, List<String> enums) { final CreateTableDto request = CreateTableDto.builder() .columns(List.of(CreateTableColumnDto.builder() - .type(ColumnTypeDto.ENUM) - .enums(null) // <<<<<<< + .type(type) + .enums(enums) .build())) .build(); @@ -314,12 +347,14 @@ public class EndpointValidatorUnitTest extends AbstractUnitTest { }); } - @Test - public void validateColumnCreateConstraints_needSet_fails() { + @ParameterizedTest + @MethodSource("needSizeAndD_parameters") + public void validateColumnCreateConstraints_needSizeAndD_fails(String name, ColumnTypeDto type, Long size, Long d) { final CreateTableDto request = CreateTableDto.builder() .columns(List.of(CreateTableColumnDto.builder() - .type(ColumnTypeDto.SET) - .sets(null) // <<<<<<< + .type(type) + .size(size) + .d(d) .build())) .build(); @@ -345,6 +380,34 @@ public class EndpointValidatorUnitTest extends AbstractUnitTest { }); } + @Test + public void validateOnlyOwnerOrWriteAll_writeOwnAccess_succeeds() throws DatabaseNotFoundException, + TableNotFoundException, AccessNotFoundException, NotAllowedException { + + /* mock */ + when(tableService.findById(DATABASE_1, TABLE_1_ID)) + .thenReturn(TABLE_1); + when(accessService.find(DATABASE_1, USER_1)) + .thenReturn(DATABASE_1_USER_1_WRITE_OWN_ACCESS); + + /* test */ + endpointValidator.validateOnlyOwnerOrWriteAll(TABLE_1, USER_1); + } + + @Test + public void validateOnlyOwnerOrWriteAll_writeAllAccess_succeeds() throws DatabaseNotFoundException, + TableNotFoundException, AccessNotFoundException, NotAllowedException { + + /* mock */ + when(tableService.findById(DATABASE_1, TABLE_1_ID)) + .thenReturn(TABLE_1); + when(accessService.find(DATABASE_1, USER_2)) + .thenReturn(DATABASE_1_USER_2_WRITE_ALL_ACCESS); + + /* test */ + endpointValidator.validateOnlyOwnerOrWriteAll(TABLE_1, USER_2); + } + @Test public void validateOnlyPrivateDataHasRole_publicDatabase_succeeds() throws NotAllowedException { @@ -555,6 +618,13 @@ public class EndpointValidatorUnitTest extends AbstractUnitTest { assertTrue(endpointValidator.validateOnlyMineOrWriteAccessOrHasRole(USER_1, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS, "nobody-role")); } + @Test + public void validateOnlyMineOrWriteAccessOrHasRole_ownerOnlyWriteAll_succeeds() { + + /* test */ + assertTrue(endpointValidator.validateOnlyMineOrWriteAccessOrHasRole(USER_1, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_ALL_ACCESS, "nobody-role")); + } + @Test public void validateOnlyMineOrWriteAccessOrHasRole_notOwnerOnlyWriteOwn_fails() { diff --git a/dbrepo-metadata-service/services/src/main/java/at/tuwien/gateway/KeycloakGateway.java b/dbrepo-metadata-service/services/src/main/java/at/tuwien/gateway/KeycloakGateway.java index cd5fd08a7ef64e88134a1fc19aa0840ad1e98020..296457b3d8ba1cd6e14e8a46be85a217b9cb8e13 100644 --- a/dbrepo-metadata-service/services/src/main/java/at/tuwien/gateway/KeycloakGateway.java +++ b/dbrepo-metadata-service/services/src/main/java/at/tuwien/gateway/KeycloakGateway.java @@ -1,7 +1,6 @@ package at.tuwien.gateway; import at.tuwien.api.keycloak.TokenDto; -import at.tuwien.api.user.UserPasswordDto; import at.tuwien.api.user.UserUpdateDto; import at.tuwien.exception.AuthServiceException; import at.tuwien.exception.UserNotFoundException; @@ -22,13 +21,7 @@ public interface KeycloakGateway { */ void deleteUser(UUID id) throws UserNotFoundException; - /** - * Update the credentials for a given user. - * - * @param id The user id. - * @param password The user credential. - */ - void updateUserCredentials(UUID id, UserPasswordDto password) throws UserNotFoundException; + void setupFinished(UUID id) throws AuthServiceException, UserNotFoundException; void updateUser(UUID id, UserUpdateDto data) throws AuthServiceException, UserNotFoundException; } diff --git a/dbrepo-metadata-service/services/src/main/java/at/tuwien/gateway/impl/KeycloakGatewayImpl.java b/dbrepo-metadata-service/services/src/main/java/at/tuwien/gateway/impl/KeycloakGatewayImpl.java index af54651d6c36197e136134003291ea88f5df2a49..270653ee8ffade88a9b96c24e2650843b21c5dd5 100644 --- a/dbrepo-metadata-service/services/src/main/java/at/tuwien/gateway/impl/KeycloakGatewayImpl.java +++ b/dbrepo-metadata-service/services/src/main/java/at/tuwien/gateway/impl/KeycloakGatewayImpl.java @@ -1,14 +1,12 @@ package at.tuwien.gateway.impl; import at.tuwien.api.keycloak.TokenDto; -import at.tuwien.api.user.UserPasswordDto; import at.tuwien.api.user.UserUpdateDto; import at.tuwien.config.KeycloakConfig; import at.tuwien.exception.AuthServiceException; import at.tuwien.exception.UserNotFoundException; import at.tuwien.gateway.KeycloakGateway; import at.tuwien.mapper.MetadataMapper; -import jakarta.ws.rs.BadRequestException; import jakarta.ws.rs.ForbiddenException; import jakarta.ws.rs.NotFoundException; import jakarta.ws.rs.core.Response; @@ -17,7 +15,6 @@ import org.keycloak.OAuth2Constants; import org.keycloak.admin.client.Keycloak; import org.keycloak.admin.client.KeycloakBuilder; import org.keycloak.admin.client.resource.UserResource; -import org.keycloak.representations.idm.CredentialRepresentation; import org.keycloak.representations.idm.UserRepresentation; import org.springframework.stereotype.Service; @@ -80,21 +77,25 @@ public class KeycloakGatewayImpl implements KeycloakGateway { } @Override - public void updateUserCredentials(UUID id, UserPasswordDto data) throws UserNotFoundException { - final CredentialRepresentation credential = new CredentialRepresentation(); - credential.setTemporary(false); - credential.setValue(data.getPassword()); - credential.setType(CredentialRepresentation.PASSWORD); + public void setupFinished(UUID id) throws AuthServiceException, UserNotFoundException { + final UserResource resource = keycloak.realm(keycloakConfig.getRealm()) + .users() + .get(String.valueOf(id)); + final UserRepresentation user; try { - keycloak.realm(keycloakConfig.getRealm()) - .users() - .get(String.valueOf(id)) - .resetPassword(credential); + user = resource.toRepresentation(); } catch (NotFoundException e) { - log.error("Failed to update user password: not found"); - throw new UserNotFoundException("Failed to update user password: not found", e); + log.error("Failed to update user setup: not found: {}", e.getMessage()); + throw new UserNotFoundException("Failed to update user setup: not found", e); + } + user.singleAttribute("SETUP_FINISHED", "true"); + try { + resource.update(user); + } catch (ForbiddenException e) { + log.error("Failed to update user setup: forbidden: {}", e.getMessage()); + throw new AuthServiceException("Failed to update user setup: forbidden", e); } - log.info("Updated user {} password at auth service", id); + log.info("Updated user {} setup at auth service", id); } @Override @@ -102,7 +103,7 @@ public class KeycloakGatewayImpl implements KeycloakGateway { final UserResource resource = keycloak.realm(keycloakConfig.getRealm()) .users() .get(String.valueOf(id)); - UserRepresentation user; + final UserRepresentation user; try { user = resource.toRepresentation(); } catch (NotFoundException e) { diff --git a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/AuthenticationService.java b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/AuthenticationService.java index 75b647bf954fd638dfde8ffa8b7f650d03ce7c49..3abe07a10a16b7b72ec144d6ddeb7ee32cd599b6 100644 --- a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/AuthenticationService.java +++ b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/AuthenticationService.java @@ -1,6 +1,5 @@ package at.tuwien.service; -import at.tuwien.api.user.UserPasswordDto; import at.tuwien.entities.user.User; import at.tuwien.exception.AuthServiceConnectionException; import at.tuwien.exception.AuthServiceException; @@ -23,8 +22,7 @@ public interface AuthenticationService { * Updates the password of a user with given id. * * @param user The user. - * @param data The new password. * @throws UserNotFoundException The user was not found after creation in the auth database. */ - void updatePassword(User user, UserPasswordDto data) throws UserNotFoundException; + void setupFinished(User user) throws UserNotFoundException, AuthServiceException; } diff --git a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/UserService.java b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/UserService.java index 581641a93ab95927d63c5b4a5f6b069eb6f270b7..c6ca0ff21eb02986f30d42ec0b8727b368d5bfe4 100644 --- a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/UserService.java +++ b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/UserService.java @@ -4,7 +4,6 @@ import at.tuwien.api.auth.CreateUserDto; import at.tuwien.api.user.UserPasswordDto; import at.tuwien.api.user.UserUpdateDto; import at.tuwien.entities.user.User; -import at.tuwien.exception.AuthServiceConnectionException; import at.tuwien.exception.AuthServiceException; import at.tuwien.exception.UserExistsException; import at.tuwien.exception.UserNotFoundException; @@ -47,7 +46,7 @@ public interface UserService { * @param data The user data. * @return The user, if successful. */ - User create(CreateUserDto data) throws UserNotFoundException, AuthServiceException; + User create(CreateUserDto data); /** * Updates the user information for a user with given id in the metadata database. diff --git a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/AuthenticationServiceImpl.java b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/AuthenticationServiceImpl.java index 1159913039ef59227758006f28d678db99329386..dec3577886de7e4e0b60b015f72f73099a441065 100644 --- a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/AuthenticationServiceImpl.java +++ b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/AuthenticationServiceImpl.java @@ -1,10 +1,7 @@ package at.tuwien.service.impl; -import at.tuwien.api.user.UserPasswordDto; import at.tuwien.entities.user.User; -import at.tuwien.exception.AuthServiceConnectionException; import at.tuwien.exception.AuthServiceException; -import at.tuwien.exception.CredentialsInvalidException; import at.tuwien.exception.UserNotFoundException; import at.tuwien.gateway.KeycloakGateway; import at.tuwien.service.AuthenticationService; @@ -29,8 +26,8 @@ public class AuthenticationServiceImpl implements AuthenticationService { } @Override - public void updatePassword(User user, UserPasswordDto data) throws UserNotFoundException { - keycloakGateway.updateUserCredentials(user.getKeycloakId(), data); + public void setupFinished(User user) throws AuthServiceException, UserNotFoundException { + keycloakGateway.setupFinished(user.getKeycloakId()); } } diff --git a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/UserServiceImpl.java b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/UserServiceImpl.java index 1d582bb9752a0cecd6885c72ba5de19d5cb78887..e79dd9bd842ac4cbb145f3b86f533962c23e2625 100644 --- a/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/UserServiceImpl.java +++ b/dbrepo-metadata-service/services/src/main/java/at/tuwien/service/impl/UserServiceImpl.java @@ -65,14 +65,14 @@ public class UserServiceImpl implements UserService { } @Override - public User create(CreateUserDto data) throws UserNotFoundException, AuthServiceException { + public User create(CreateUserDto data) { /* create at authentication service */ final User entity = User.builder() .id(data.getLdapId()) .keycloakId(data.getId()) .username(data.getUsername()) .theme("light") - .mariadbPassword(getMariaDbPassword(RandomStringUtils.randomAlphabetic(10))) /* user needs to set it later to access */ + .mariadbPassword(getMariaDbPassword(RandomStringUtils.randomAlphabetic(10))) .language("en") .firstname(data.getGivenName()) .lastname(data.getFamilyName()) 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 bccf903b8befe7ceec1915f10a49ff0fdb0e35c0..860eeb8253c18a58d19ac6c61d8017382399875a 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 @@ -5,6 +5,7 @@ import at.tuwien.api.amqp.CreateVirtualHostDto; import at.tuwien.api.amqp.ExchangeDto; import at.tuwien.api.amqp.GrantVirtualHostPermissionsDto; import at.tuwien.api.amqp.QueueDto; +import at.tuwien.api.auth.CreateUserDto; import at.tuwien.api.auth.LoginRequestDto; import at.tuwien.api.auth.RefreshTokenRequestDto; import at.tuwien.api.container.ContainerBriefDto; @@ -478,8 +479,6 @@ public abstract class BaseTest { @SuppressWarnings("java:S2068") public final static String USER_1_PASSWORD = "junit1"; @SuppressWarnings("java:S2068") - public final static String USER_1_PASSWORD_ENCODED = "$2a$10$0dtdedA/RLTrFbUsvpbUw.I73AXOKeQP3t5UXj96OvnDEaDb3d3M6"; - @SuppressWarnings("java:S2068") public final static String USER_1_DATABASE_PASSWORD = "*440BA4FD1A87A0999647DB67C0EE258198B247BA" /* junit1 */; public final static String USER_1_FIRSTNAME = "John"; public final static String USER_1_LASTNAME = "Doe"; @@ -487,18 +486,11 @@ public abstract class BaseTest { public final static String USER_1_NAME = "John Doe"; public final static String USER_1_AFFILIATION = "TU Graz"; public final static String USER_1_ORCID_URL = "https://orcid.org/0000-0003-4216-302X"; - public final static String USER_1_TITLES_BEFORE = "Dr."; - public final static String USER_1_TITLES_AFTER = "MSc BSc"; - public final static Boolean USER_1_VERIFIED = false; - public final static Boolean USER_1_TOTP = false; - public final static Long USER_1_NOT_BEFORE = 0L; public final static Boolean USER_1_ENABLED = true; public final static Boolean USER_1_IS_INTERNAL = false; public final static String USER_1_THEME = "light"; public final static String USER_1_LANGUAGE = "en"; public final static Instant USER_1_CREATED = Instant.ofEpochSecond(1677399441L) /* 2023-02-26 08:17:21 (UTC) */; - public final static Instant USER_1_LAST_MODIFIED = USER_1_CREATED; - public final static UUID USER_1_REALM_ID = REALM_DBREPO_ID; public final static UpdateUserPasswordDto USER_1_UPDATE_PASSWORD_DTO = UpdateUserPasswordDto.builder() .username(USER_1_USERNAME) @@ -568,6 +560,14 @@ public abstract class BaseTest { .qualifiedName(USER_1_QUALIFIED_NAME) .build(); + public final static CreateUserDto USER_1_CREATE_USER_DTO = CreateUserDto.builder() + .id(USER_1_KEYCLOAK_ID) + .ldapId(USER_1_ID) + .givenName(USER_1_FIRSTNAME) + .familyName(USER_1_LASTNAME) + .username(USER_1_USERNAME) + .build(); + public final static UserUpdateDto USER_1_UPDATE_DTO = UserUpdateDto.builder() .firstname(USER_1_FIRSTNAME) .lastname(USER_1_LASTNAME) @@ -608,7 +608,6 @@ public abstract class BaseTest { public final static UUID USER_2_ID = UUID.fromString("eeb9a51b-4cd8-4039-90bf-e24f17372f7c"); public final static UUID USER_2_KEYCLOAK_ID = UUID.fromString("eeb9a51b-4cd8-4039-90bf-e24f17372f7c"); - public final static String USER_2_EMAIL = "jane.doe@example.com"; public final static String USER_2_USERNAME = "junit2"; public final static String USER_2_FIRSTNAME = "Jane"; public final static String USER_2_LASTNAME = "Doe"; @@ -620,16 +619,9 @@ public abstract class BaseTest { @SuppressWarnings("java:S2068") public final static String USER_2_DATABASE_PASSWORD = "*9AA70A8B0EEFAFCB5BED5BDEF6EE264D5DA915AE" /* junit2 */; public final static String USER_2_QUALIFIED_NAME = USER_2_FIRSTNAME + " " + USER_2_LASTNAME + " — @" + USER_2_USERNAME; - public final static Boolean USER_2_VERIFIED = true; - public final static Boolean USER_2_TOTP = false; - public final static Long USER_2_NOT_BEFORE = 0L; - public final static Boolean USER_2_ENABLED = true; public final static Boolean USER_2_IS_INTERNAL = false; public final static String USER_2_THEME = "light"; public final static String USER_2_LANGUAGE = "de"; - public final static Instant USER_2_CREATED = Instant.ofEpochSecond(1677399528L) /* 2023-02-26 08:18:48 (UTC) */; - public final static Instant USER_2_LAST_MODIFIED = USER_1_CREATED; - public final static UUID USER_2_REALM_ID = REALM_DBREPO_ID; public final static UserAttributesDto USER_2_ATTRIBUTES_DTO = UserAttributesDto.builder() .theme(USER_2_THEME) @@ -697,20 +689,13 @@ public abstract class BaseTest { public final static String USER_3_AFFILIATION = "TU Wien"; public final static String USER_3_ORCID_URL = null; public final static String USER_3_ORCID_UNCOMPRESSED = null; - public final static String USER_3_EMAIL = "system@example.com"; @SuppressWarnings("java:S2068") public final static String USER_3_PASSWORD = "password"; @SuppressWarnings("java:S2068") public final static String USER_3_DATABASE_PASSWORD = "*D65FCA043964B63E849DD6334699ECB065905DA4" /* junit3 */; public final static String USER_3_QUALIFIED_NAME = USER_3_FIRSTNAME + " " + USER_3_LASTNAME + " — @" + USER_3_USERNAME; - public final static Boolean USER_3_VERIFIED = true; - public final static Boolean USER_3_TOTP = false; - public final static Long USER_3_NOT_BEFORE = 0L; - public final static Boolean USER_3_ENABLED = true; public final static Boolean USER_3_IS_INTERNAL = false; public final static String USER_3_THEME = "light"; - public final static Instant USER_3_CREATED = Instant.ofEpochSecond(1677399559L) /* 2023-02-26 08:19:19 (UTC) */; - public final static UUID USER_3_REALM_ID = REALM_DBREPO_ID; public final static UserAttributesDto USER_3_ATTRIBUTES_DTO = UserAttributesDto.builder() .theme(USER_3_THEME) @@ -779,12 +764,8 @@ public abstract class BaseTest { @SuppressWarnings("java:S2068") public final static String USER_4_DATABASE_PASSWORD = "*C20EF5C6875857DEFA9BE6E9B62DD76AAAE51882" /* junit4 */; public final static String USER_4_QUALIFIED_NAME = USER_4_FIRSTNAME + " " + USER_4_LASTNAME + " — @" + USER_4_USERNAME; - public final static Boolean USER_4_VERIFIED = true; - public final static Boolean USER_4_ENABLED = true; public final static Boolean USER_4_IS_INTERNAL = false; public final static String USER_4_THEME = "light"; - public final static Instant USER_4_CREATED = Instant.ofEpochSecond(1677399592L) /* 2023-02-26 08:19:52 (UTC) */; - public final static UUID USER_4_REALM_ID = REALM_DBREPO_ID; public final static UserAttributesDto USER_4_ATTRIBUTES_DTO = UserAttributesDto.builder() .theme(USER_4_THEME) @@ -842,18 +823,13 @@ public abstract class BaseTest { public final static String USER_5_LASTNAME = "Body"; public final static String USER_5_NAME = "No Body"; public final static String USER_5_AFFILIATION = "TU Wien"; - public final static String USER_5_ORCID = null; @SuppressWarnings("java:S2068") public final static String USER_5_PASSWORD = "junit5"; @SuppressWarnings("java:S2068") public final static String USER_5_DATABASE_PASSWORD = "*C20EF5C6875857DEFA9BE6E9B62DD76AAAE51882" /* junit5 */; public final static String USER_5_QUALIFIED_NAME = USER_5_FIRSTNAME + " " + USER_5_LASTNAME + " — @" + USER_5_USERNAME; - public final static Boolean USER_5_VERIFIED = true; - public final static Boolean USER_5_ENABLED = true; public final static Boolean USER_5_IS_INTERNAL = false; public final static String USER_5_THEME = "dark"; - public final static Instant USER_5_CREATED = Instant.ofEpochSecond(1677399592L) /* 2023-02-26 08:19:52 (UTC) */; - public final static UUID USER_5_REALM_ID = REALM_DBREPO_ID; public final static UserAttributesDto USER_5_ATTRIBUTES_DTO = UserAttributesDto.builder() .theme(USER_5_THEME) @@ -7680,6 +7656,21 @@ public abstract class BaseTest { .user(USER_3_BRIEF_DTO) .build(); + public final static DatabaseAccess DATABASE_1_USER_4_READ_ACCESS = DatabaseAccess.builder() + .type(AccessType.READ) + .hdbid(DATABASE_1_ID) + .database(DATABASE_1) + .huserid(USER_4_ID) + .user(USER_4) + .build(); + + public final static DatabaseAccessDto DATABASE_1_USER_4_READ_ACCESS_DTO = DatabaseAccessDto.builder() + .type(AccessTypeDto.READ) + .hdbid(DATABASE_1_ID) + .huserid(USER_4_ID) + .user(USER_4_BRIEF_DTO) + .build(); + public final static Database DATABASE_2 = Database.builder() .id(DATABASE_2_ID) .created(DATABASE_2_CREATED) diff --git a/dbrepo-ui/composables/authentication-service.ts b/dbrepo-ui/composables/authentication-service.ts deleted file mode 100644 index 39f6cc5a3fc843bd8db07c22b0f94a2ea2d7a93a..0000000000000000000000000000000000000000 --- a/dbrepo-ui/composables/authentication-service.ts +++ /dev/null @@ -1,24 +0,0 @@ -import {jwtDecode} from 'jwt-decode' - -export const useAuthenticationService = (): any => { - - function isExpiredToken(token: string): boolean { - if (!token) { - return false - } - return tokenToExpiryDate(token) < Date.now() - } - - function tokenToExpiryDate(token: string): number { - if (!token) { - return -1 - } - const exp: number = jwtDecode<Token>(token).exp - if (exp) { - return exp * 1000 - } - return -1 - } - - return {isExpiredToken, tokenToExpiryDate} -} diff --git a/dbrepo-ui/composables/axios-instance.ts b/dbrepo-ui/composables/axios-instance.ts index ca7a7b111cf70cf92524f5459808277e4b14144e..cd3737f2bfad4bbd40d15f6cb5cf334a73d0c334 100644 --- a/dbrepo-ui/composables/axios-instance.ts +++ b/dbrepo-ui/composables/axios-instance.ts @@ -16,7 +16,7 @@ export const useAxiosInstance = () => { baseURL: config.public.api.client }); instance.interceptors.request.use((config) => { - const { loggedIn, user, login, logout } = useOidcAuth() + const { loggedIn, user } = useOidcAuth() if (!loggedIn) { return config } diff --git a/dbrepo-ui/composables/user-service.ts b/dbrepo-ui/composables/user-service.ts index 3425dbaa5c7d77d927798914b8a4fdac6123c166..b90ee34033fb773418a16b1f9ffc9afdbfcc1f4c 100644 --- a/dbrepo-ui/composables/user-service.ts +++ b/dbrepo-ui/composables/user-service.ts @@ -1,5 +1,3 @@ -import {jwtDecode} from 'jwt-decode' -import axios from 'axios' import {axiosErrorToApiError} from '@/utils' export const useUserService = (): any => { @@ -80,36 +78,6 @@ export const useUserService = (): any => { }) } - async function refreshToken(refreshToken: string): Promise<KeycloakOpenIdTokenDto> { - console.debug('refresh user token') - return new Promise<KeycloakOpenIdTokenDto>((resolve, reject) => { - const config = useRuntimeConfig() - const instance = axios.create({ - timeout: 90_000, - params: {}, - baseURL: config.public.api.client - }) - instance.put<KeycloakOpenIdTokenDto>('/api/user/token', {refresh_token: refreshToken}) - .then((response) => { - console.info('Refreshed user token') - const userStore = useUserStore() - // eslint-disable-next-line camelcase - const {access_token, refresh_token} = response.data - userStore.setToken(access_token) - userStore.setRefreshToken(refresh_token) - resolve(response.data) - }).catch((error) => { - console.error('Failed to refresh user token', error) - reject(axiosErrorToApiError(error)) - }) - }) - } - - function tokenToRoles(token: string): string[] { - const data: Token = jwtDecode<Token>(token) - return data.realm_access.roles || [] - } - function nameIdentifierToNameIdentifierScheme(nameIdentifier: string) { if (nameIdentifier.includes('orcid.org')) { return 'ORCID' diff --git a/dbrepo-ui/layouts/default.vue b/dbrepo-ui/layouts/default.vue index a26c6d2539c86a240a3f4f45818e2780915aff9f..85d530e74daf40dcf3deb47bae5adca762ef99c2 100644 --- a/dbrepo-ui/layouts/default.vue +++ b/dbrepo-ui/layouts/default.vue @@ -141,12 +141,30 @@ </v-form> <v-main> <v-container> - <slot /> + <div + v-cloak> + <v-alert + v-if="isNotFinishedAccountSetup" + border="start" + color="info" + class="mb-4"> + {{ $t('pages.settings.subpages.authentication.setup.text') }} + <v-btn + variant="flat" + size="small" + to="/user/authentication"> + {{ $t('pages.settings.subpages.authentication.setup.action') }} + </v-btn> + . + </v-alert> + </div> <JumboBox v-if="error" :title="$t(errorCodeKey(error).title, { resource })" :subtitle="$t(errorCodeKey(error).subtitle)" :text="$t(errorCodeKey(error).text, { resource })" /> + <slot + v-else /> </v-container> </v-main> </v-app> @@ -251,6 +269,15 @@ export default { commitShort () { return this.$config.public.commit.substr(0, 8) }, + isNotFinishedAccountSetup () { + if (!this.cacheUser) { + return false + } + if (!('setup_finished' in this.cacheUser)) { + return true + } + return this.cacheUser.setup_finished === false + }, error () { if (this.identifier) { return null diff --git a/dbrepo-ui/locales/en-US.json b/dbrepo-ui/locales/en-US.json index 07ac0163ef086dd1915d90daf5cf7aabc59695d4..d17da2341db70b270a9cf780773ea7900afa4c03 100644 --- a/dbrepo-ui/locales/en-US.json +++ b/dbrepo-ui/locales/en-US.json @@ -794,11 +794,11 @@ }, "firstname": { "label": "Given Name", - "hint": "" + "hint": "Managed by your identity provider: {provider}" }, "lastname": { "label": "Family Name", - "hint": "" + "hint": "Managed by your identity provider: {provider}" }, "affiliation": { "label": "Affiliation Identifier", @@ -834,8 +834,12 @@ "settings": { "subpages": { "authentication": { - "title": "User Password", - "subtitle": "Update the user password used for basic authentication with all interfaces", + "title": "API Password", + "subtitle": "Update the user password used for authentication with all interfaces (e.g. HTTP API, AMQP API, MQTT API)", + "setup": { + "text": "Finish your account setup by setting the", + "action": "API Password" + }, "password": { "label": "Password", "hint": "Required" diff --git a/dbrepo-ui/pages/user/authentication.vue b/dbrepo-ui/pages/user/authentication.vue index 50008d3c5dab56111fb15ae7a6164e1a591af1f9..912c1878c4940556e1dae3e890e4087ca7af5984 100644 --- a/dbrepo-ui/pages/user/authentication.vue +++ b/dbrepo-ui/pages/user/authentication.vue @@ -61,7 +61,7 @@ </template> <script setup> -const { loggedIn } = useOidcAuth() +const { loggedIn, user } = useOidcAuth() </script> <script> import UserToolbar from '@/components/user/UserToolbar.vue' @@ -113,11 +113,20 @@ export default { const userService = useUserService() userService.updatePassword(this.cacheUser.uid, {'password': this.password}) .then(() => { + const user = Object.assign({}, this.cacheUser) + user.setup_finished = true + this.cacheStore.setUser(user) + // fixme [mweise]: currently nuxt-oidc-auth cannot refresh the session correctly const toast = useToastInstance() toast.success(this.$t('success.user.password')) this.loadingUpdate = false }) - .catch(() => { + .catch(({code, message}) => { + const toast = useToastInstance() + if (typeof code !== 'string') { + return + } + toast.error(message) this.loadingUpdate = false }) .finally(() => { diff --git a/dbrepo-ui/pages/user/info.vue b/dbrepo-ui/pages/user/info.vue index 8674c57e2d15abb0be7a50182f2681d5e0f6526e..9c8dbf873d5b7ec4c13a4cde7a42a61790fdc4a3 100644 --- a/dbrepo-ui/pages/user/info.vue +++ b/dbrepo-ui/pages/user/info.vue @@ -74,24 +74,24 @@ <v-col md="6"> <v-text-field v-model="model.firstname" - :disabled="!canModifyInformation" + :disabled="!canModifyInformation || identityProvider" clearable persistent-hint :variant="inputVariant" :label="$t('pages.user.subpages.info.firstname.label')" - :hint="$t('pages.user.subpages.info.firstname.hint')" /> + :hint="identityProvider ? $t('pages.user.subpages.info.firstname.hint', { provider: identityProvider }) : ''" /> </v-col> </v-row> <v-row dense> <v-col md="6"> <v-text-field v-model="model.lastname" - :disabled="!canModifyInformation" + :disabled="!canModifyInformation || identityProvider" clearable persistent-hint :variant="inputVariant" :label="$t('pages.user.subpages.info.lastname.label')" - :hint="$t('pages.user.subpages.info.lastname.hint')" /> + :hint="identityProvider ? $t('pages.user.subpages.info.lastname.hint', { provider: identityProvider }) : ''" /> </v-col> </v-row> <v-row dense> @@ -191,6 +191,12 @@ export default { cacheUser () { return this.cacheStore.getUser }, + identityProvider () { + if (!this.cacheUser || !('identity_provider' in this.cacheUser)) { + return false + } + return this.cacheUser.identity_provider + }, canModifyInformation () { if (!this.roles) { return false diff --git a/docker-compose.yml b/docker-compose.yml index 9176f6404a06df5e30269f4a0dde4d8e4483ebd8..94b5fc30a61c1081e194fd4eb98c45d7af9b261c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -96,7 +96,7 @@ services: KEYCLOAK_DATABASE_NAME: "${AUTH_DB_NAME:-keycloak}" KEYCLOAK_DATABASE_USER: "${AUTH_DB_USERNAME:-keycloak}" KEYCLOAK_DATABASE_PASSWORD: "${AUTH_DB_PASSWORD:-dbrepo}" - METADATA_SERVICE_ENDPOINT: "${METADATA_SERVICE_ENDPOINT:-http://metadata-service:8080}/api/user" + METADATA_SERVICE_ENDPOINT: "${METADATA_SERVICE_ENDPOINT:-http://metadata-service:8080}" SYSTEM_USERNAME: "${SYSTEM_USERNAME:-admin}" SYSTEM_PASSWORD: "${SYSTEM_PASSWORD:-admin}" healthcheck: diff --git a/helm/dbrepo/files/create-event-listener.jar b/helm/dbrepo/files/create-event-listener.jar index a23243d39509ec3821219e5799a25740c93e2ca1..a970096eeaa5015d9e95064f6b341fffdf8aae5f 100644 Binary files a/helm/dbrepo/files/create-event-listener.jar and b/helm/dbrepo/files/create-event-listener.jar differ