From a3dc440f42e710b93b362c637f85658b5be71709 Mon Sep 17 00:00:00 2001 From: Patrick Schauer Date: Thu, 19 Dec 2024 13:28:47 +0100 Subject: [PATCH] test: add test for organization deletion api service --- .../OrganizationDeletionApiServiceTest.kt | 137 ++++++++++++++++++ .../user/UserDeletionApiServiceTest.kt | 16 +- 2 files changed, 145 insertions(+), 8 deletions(-) create mode 100644 authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/organization/OrganizationDeletionApiServiceTest.kt diff --git a/authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/organization/OrganizationDeletionApiServiceTest.kt b/authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/organization/OrganizationDeletionApiServiceTest.kt new file mode 100644 index 00000000..3ca4a6e2 --- /dev/null +++ b/authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/organization/OrganizationDeletionApiServiceTest.kt @@ -0,0 +1,137 @@ +package de.sovity.authorityportal.web.tests.services.organization + +import de.sovity.authorityportal.api.UiResource +import de.sovity.authorityportal.db.jooq.Tables +import de.sovity.authorityportal.seeds.utils.ScenarioData +import de.sovity.authorityportal.seeds.utils.ScenarioInstaller +import de.sovity.authorityportal.seeds.utils.dummyDevOrganizationId +import de.sovity.authorityportal.seeds.utils.dummyDevUserUuid +import de.sovity.authorityportal.web.Roles +import de.sovity.authorityportal.web.tests.useDevUser +import de.sovity.authorityportal.web.thirdparty.daps.DapsClient +import de.sovity.authorityportal.web.thirdparty.daps.DapsClientService +import de.sovity.authorityportal.web.thirdparty.keycloak.KeycloakService +import de.sovity.authorityportal.web.thirdparty.keycloak.model.KeycloakUserDto +import io.quarkus.test.InjectMock +import io.quarkus.test.TestTransaction +import io.quarkus.test.junit.QuarkusTest +import jakarta.inject.Inject +import jakarta.ws.rs.NotAuthorizedException +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.jooq.DSLContext +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.mockito.junit.jupiter.MockitoExtension +import org.mockito.kotlin.any +import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever + +@QuarkusTest +@ExtendWith(MockitoExtension::class) +class OrganizationDeletionApiServiceTest { + + @Inject + lateinit var uiResource: UiResource + + @Inject + lateinit var scenarioInstaller: ScenarioInstaller + + @Inject + lateinit var dsl: DSLContext + + @InjectMock + lateinit var keycloakService: KeycloakService + + @InjectMock + lateinit var dapsClientService: DapsClientService + + @Test + fun `deleteOrganization fails because user is not admin`() { + // arrange + useDevUser(0, 0, setOf(Roles.UserRoles.PARTICIPANT_USER)) + + // act & assert + assertThatThrownBy { + uiResource.deleteOrganization(dummyDevOrganizationId(1)) + }.isInstanceOf(NotAuthorizedException::class.java) + } + + @Test + @TestTransaction + fun `deleteOrganization fails because all remaining authority admins are part of it`() { + // arrange + val lastAuthorityAdmin = KeycloakUserDto(dummyDevUserUuid(0), "Authority", "Admin", "authority@test.sovity.io") + + useDevUser(0, 0) + + ScenarioData().apply { + organization(0, 0) + user(0, 0) + scenarioInstaller.install(this) + } + + whenever(keycloakService.getAuthorityAdmins()).thenReturn(listOf(lastAuthorityAdmin)) + + // act & assert + assertThatThrownBy { + uiResource.deleteOrganization(dummyDevOrganizationId(0)) + }.isInstanceOf(IllegalStateException::class.java).message() + .contains("Organization can not be deleted. The last Authority Admins are part of this organization.") + } + + @Test + @TestTransaction + fun deleteOrganization() { + // arrange + val lastAuthorityAdmin = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "Admin", "authority@test.sovity.io") + + val dapsClient = mock() + + useDevUser(0, 0) + + ScenarioData().apply { + organization(0, 0) + organization(1, 99) + user(0, 0) + user(99, 1) + connector(0, 0, 0) + component(0, 0, 0) + scenarioInstaller.install(this) + } + + whenever(keycloakService.getAuthorityAdmins()).thenReturn(listOf(lastAuthorityAdmin)) + whenever(dapsClientService.forEnvironment(any())).thenReturn(dapsClient) + + // act + val result = uiResource.deleteOrganization(dummyDevOrganizationId(0)) + + // assert + assertThat(result).isNotNull + assertThat(result.id).isEqualTo(dummyDevOrganizationId(0)) + + val organizationDbQuery = dsl.selectOne() + .from(Tables.ORGANIZATION) + .where(Tables.ORGANIZATION.ID.eq(dummyDevOrganizationId(0))) + + assertThat(dsl.fetchExists(organizationDbQuery)).isEqualTo(false) + + val userDbQuery = dsl.selectOne() + .from(Tables.USER) + .where(Tables.USER.ORGANIZATION_ID.eq(dummyDevOrganizationId(0))) + + assertThat(dsl.fetchExists(userDbQuery)).isEqualTo(false) + + val connectorDbQuery = dsl.selectOne() + .from(Tables.CONNECTOR) + .where(Tables.CONNECTOR.ORGANIZATION_ID.eq(dummyDevOrganizationId(0))) + + assertThat(dsl.fetchExists(connectorDbQuery)).isEqualTo(false) + + val componentDbQuery = dsl.selectOne() + .from(Tables.COMPONENT) + .where(Tables.COMPONENT.ORGANIZATION_ID.eq(dummyDevOrganizationId(0))) + + assertThat(dsl.fetchExists(componentDbQuery)).isEqualTo(false) + } +} diff --git a/authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/user/UserDeletionApiServiceTest.kt b/authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/user/UserDeletionApiServiceTest.kt index 01aab490..441af1a0 100644 --- a/authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/user/UserDeletionApiServiceTest.kt +++ b/authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/user/UserDeletionApiServiceTest.kt @@ -100,11 +100,11 @@ class UserDeletionApiServiceTest { fun `checkUserDeletion as last participant admin and organization creator`() { // arrange val lastParticipantAdmin = KeycloakUserDto(dummyDevUserUuid(0), "Test", "User", "test.user@test.sovity.io") - val lastAuthorityUser = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "User", "authority@test.sovity.io") + val lastAuthorityAdmin = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "Admin", "authority@test.sovity.io") useDevUser(0, 0, setOf(Roles.UserRoles.PARTICIPANT_ADMIN)) - whenever(keycloakService.getAuthorityAdmins()).thenReturn(listOf(lastAuthorityUser)) + whenever(keycloakService.getAuthorityAdmins()).thenReturn(listOf(lastAuthorityAdmin)) whenever(keycloakService.getParticipantAdmins(eq(dummyDevOrganizationId(0)))).thenReturn(listOf(lastParticipantAdmin)) ScenarioData().apply { @@ -132,11 +132,11 @@ class UserDeletionApiServiceTest { // arrange val keycloakUser = KeycloakUserDto(dummyDevUserUuid(0), "Test", "User", "test.user@test.sovity.io") val possibleSuccessor = KeycloakUserDto(dummyDevUserUuid(1), "Max", "Mustermann", "max.mustermann@test.sovity.io") - val lastAuthorityUser = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "User", "authority@test.sovity.io") + val lastAuthorityAdmin = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "Admin", "authority@test.sovity.io") useDevUser(0, 0, setOf(Roles.UserRoles.PARTICIPANT_ADMIN)) - whenever(keycloakService.getAuthorityAdmins()).thenReturn(listOf(lastAuthorityUser)) + whenever(keycloakService.getAuthorityAdmins()).thenReturn(listOf(lastAuthorityAdmin)) whenever(keycloakService.getParticipantAdmins(eq(dummyDevOrganizationId(0)))).thenReturn(listOf(keycloakUser, possibleSuccessor)) ScenarioData().apply { @@ -178,12 +178,12 @@ class UserDeletionApiServiceTest { val keycloakUser = KeycloakUserDto(dummyDevUserUuid(0), "Test", "User", "test.user@test.sovity.io") val possibleSuccessor = KeycloakUserDto(dummyDevUserUuid(1), "Max", "Mustermann", "max.mustermann@test.sovity.io") - val lastAuthorityUser = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "User", "authority@test.sovity.io") + val lastAuthorityAdmin = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "Admin", "authority@test.sovity.io") useDevUser(0, 0, setOf(Roles.UserRoles.PARTICIPANT_ADMIN)) useMockNow(now) - whenever(keycloakService.getAuthorityAdmins()).thenReturn(listOf(lastAuthorityUser)) + whenever(keycloakService.getAuthorityAdmins()).thenReturn(listOf(lastAuthorityAdmin)) whenever(keycloakService.getParticipantAdmins(eq(dummyDevOrganizationId(0)))).thenReturn(listOf(keycloakUser, possibleSuccessor)) doNothing().whenever(keycloakService).deleteUser(eq(dummyDevUserUuid(0))) @@ -220,14 +220,14 @@ class UserDeletionApiServiceTest { val now = OffsetDateTime.now() val keycloakUser = KeycloakUserDto(dummyDevUserUuid(0), "Test", "User", "test.user@test.sovity.io") - val lastAuthorityUser = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "User", "authority@test.sovity.io") + val lastAuthorityAdmin = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "Admin", "authority@test.sovity.io") val dapsClient = mock() useDevUser(0, 0, setOf(Roles.UserRoles.PARTICIPANT_ADMIN)) useMockNow(now) - whenever(keycloakService.getAuthorityAdmins()).thenReturn(listOf(lastAuthorityUser)) + whenever(keycloakService.getAuthorityAdmins()).thenReturn(listOf(lastAuthorityAdmin)) whenever(keycloakService.getParticipantAdmins(eq(dummyDevOrganizationId(0)))).thenReturn(listOf(keycloakUser)) whenever(dapsClientService.forEnvironment(any())).thenReturn(dapsClient) doNothing().whenever(keycloakService).deleteUser(eq(dummyDevUserUuid(0)))