Skip to content

Commit

Permalink
test: add test for organization deletion api service
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddseL committed Dec 19, 2024
1 parent 86c0c7d commit a3dc440
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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", "[email protected]")

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", "[email protected]")

val dapsClient = mock<DapsClient>()

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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ class UserDeletionApiServiceTest {
fun `checkUserDeletion as last participant admin and organization creator`() {
// arrange
val lastParticipantAdmin = KeycloakUserDto(dummyDevUserUuid(0), "Test", "User", "[email protected]")
val lastAuthorityUser = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "User", "[email protected]")
val lastAuthorityAdmin = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "Admin", "[email protected]")

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 {
Expand Down Expand Up @@ -132,11 +132,11 @@ class UserDeletionApiServiceTest {
// arrange
val keycloakUser = KeycloakUserDto(dummyDevUserUuid(0), "Test", "User", "[email protected]")
val possibleSuccessor = KeycloakUserDto(dummyDevUserUuid(1), "Max", "Mustermann", "[email protected]")
val lastAuthorityUser = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "User", "[email protected]")
val lastAuthorityAdmin = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "Admin", "[email protected]")

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 {
Expand Down Expand Up @@ -178,12 +178,12 @@ class UserDeletionApiServiceTest {

val keycloakUser = KeycloakUserDto(dummyDevUserUuid(0), "Test", "User", "[email protected]")
val possibleSuccessor = KeycloakUserDto(dummyDevUserUuid(1), "Max", "Mustermann", "[email protected]")
val lastAuthorityUser = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "User", "[email protected]")
val lastAuthorityAdmin = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "Admin", "[email protected]")

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)))

Expand Down Expand Up @@ -220,14 +220,14 @@ class UserDeletionApiServiceTest {
val now = OffsetDateTime.now()

val keycloakUser = KeycloakUserDto(dummyDevUserUuid(0), "Test", "User", "[email protected]")
val lastAuthorityUser = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "User", "[email protected]")
val lastAuthorityAdmin = KeycloakUserDto(dummyDevUserUuid(99), "Authority", "Admin", "[email protected]")

val dapsClient = mock<DapsClient>()

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)))
Expand Down

0 comments on commit a3dc440

Please sign in to comment.