From 42bc8fa21e8918c63411e512ff77c694891b69ff Mon Sep 17 00:00:00 2001 From: Simon Dumas Date: Fri, 4 Aug 2023 14:16:06 +0200 Subject: [PATCH 1/2] Add integration tests for automatic project deletion --- tests/docker/config/delta-postgres.conf | 7 +++ .../nexus/tests/admin/AdminDsl.scala | 38 +++++++++------- .../tests/kg/AutoProjectDeletionSpec.scala | 44 +++++++++++++++++++ 3 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/AutoProjectDeletionSpec.scala diff --git a/tests/docker/config/delta-postgres.conf b/tests/docker/config/delta-postgres.conf index b27dded274..6c9776faa4 100644 --- a/tests/docker/config/delta-postgres.conf +++ b/tests/docker/config/delta-postgres.conf @@ -108,5 +108,12 @@ plugins { project-deletion { enabled = true + included-projects = [ + "autodeletion.+" + ] + excluded-projects = [] + idle-interval = 5s + idle-check-period = 1s + delete-deprecated-projects = true } } \ No newline at end of file diff --git a/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/admin/AdminDsl.scala b/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/admin/AdminDsl.scala index 6383d9e4e9..630309ba34 100644 --- a/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/admin/AdminDsl.scala +++ b/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/admin/AdminDsl.scala @@ -82,16 +82,18 @@ class AdminDsl(cl: HttpClient, config: TestsConfig) extends TestHelpers with Cir id: String, description: String, authenticated: Authenticated, - expectedResponse: Option[ExpectedResponse] = None + expectedResponse: Option[ExpectedResponse] = None, + ignoreConflict: Boolean = false ): Task[Assertion] = - updateOrganization(id, description, authenticated, 0, expectedResponse) + updateOrganization(id, description, authenticated, 0, expectedResponse, ignoreConflict) def updateOrganization( id: String, description: String, authenticated: Authenticated, rev: Int, - expectedResponse: Option[ExpectedResponse] = None + expectedResponse: Option[ExpectedResponse] = None, + ignoreConflict: Boolean = false ): Task[Assertion] = { cl.put[Json](s"/orgs/$id${queryParams(rev)}", orgPayload(description), authenticated) { (json, response) => expectedResponse match { @@ -99,19 +101,23 @@ class AdminDsl(cl: HttpClient, config: TestsConfig) extends TestHelpers with Cir response.status shouldEqual e.statusCode json shouldEqual e.json case None => - if (rev == 0L) - response.status shouldEqual StatusCodes.Created - else - response.status shouldEqual StatusCodes.OK - - filterMetadataKeys(json) shouldEqual createOrgRespJson( - id, - rev + 1, - "orgs", - "Organization", - authenticated, - "organizations" - ) + if (ignoreConflict && response.status == StatusCodes.Conflict) + succeed + else { + if (rev == 0L) + response.status shouldEqual StatusCodes.Created + else + response.status shouldEqual StatusCodes.OK + + filterMetadataKeys(json) shouldEqual createOrgRespJson( + id, + rev + 1, + "orgs", + "Organization", + authenticated, + "organizations" + ) + } } } } diff --git a/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/AutoProjectDeletionSpec.scala b/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/AutoProjectDeletionSpec.scala new file mode 100644 index 0000000000..705d1d58b7 --- /dev/null +++ b/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/AutoProjectDeletionSpec.scala @@ -0,0 +1,44 @@ +package ch.epfl.bluebrain.nexus.tests.kg + +import akka.http.scaladsl.model.StatusCodes +import ch.epfl.bluebrain.nexus.tests.BaseSpec +import ch.epfl.bluebrain.nexus.tests.Identity.projects.Bojack +import ch.epfl.bluebrain.nexus.tests.iam.types.Permission.{Events, Organizations, Projects, Resources} +import io.circe.Json + +import scala.concurrent.duration._ + +/** + * Test related to automatic project deletion Only checks that the project deletion has been triggered and that the + * project itself has been deleted. All the additional checks on the deletion of resources/views contained in this + * project are done in [[ProjectsDeletionSpec]] + * + * @see + * ProjectsDeletionSpec + */ +class AutoProjectDeletionSpec extends BaseSpec { + + // We double the default patience in order to make sure that the automatic deletion has time to process the project + implicit override def patienceConfig: PatienceConfig = PatienceConfig(config.patience * 2, 300.millis) + + private val org = "autodeletion" + private val proj1 = genId() + private val ref1 = s"$org/$proj1" + + "Setting up" should { + "succeed in setting up org and project" in { + for { + _ <- aclDsl.addPermissions("/", Bojack, Set(Organizations.Create, Projects.Delete, Resources.Read, Events.Read)) + // First org and projects + _ <- adminDsl.createOrganization(org, org, Bojack, ignoreConflict = true) + _ <- adminDsl.createProject(org, proj1, kgDsl.projectJson(name = proj1), Bojack) + _ <- deltaClient.get[Json](s"/projects/$ref1", Bojack)(expect(StatusCodes.OK)) + } yield succeed + } + } + + "eventually return a not found when attempting to fetch the project" in eventually { + deltaClient.get[Json](s"/projects/$ref1", Bojack)(expect(StatusCodes.NotFound)) + } + +} From 8e84341fb236109ba440128ee97eef34a91f2dcd Mon Sep 17 00:00:00 2001 From: Simon Dumas Date: Mon, 7 Aug 2023 13:53:32 +0200 Subject: [PATCH 2/2] Address feedback --- .../tests/kg/AutoProjectDeletionSpec.scala | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/AutoProjectDeletionSpec.scala b/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/AutoProjectDeletionSpec.scala index 705d1d58b7..21928414d7 100644 --- a/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/AutoProjectDeletionSpec.scala +++ b/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/AutoProjectDeletionSpec.scala @@ -4,14 +4,19 @@ import akka.http.scaladsl.model.StatusCodes import ch.epfl.bluebrain.nexus.tests.BaseSpec import ch.epfl.bluebrain.nexus.tests.Identity.projects.Bojack import ch.epfl.bluebrain.nexus.tests.iam.types.Permission.{Events, Organizations, Projects, Resources} +import monix.execution.Scheduler.Implicits.global import io.circe.Json import scala.concurrent.duration._ /** - * Test related to automatic project deletion Only checks that the project deletion has been triggered and that the - * project itself has been deleted. All the additional checks on the deletion of resources/views contained in this - * project are done in [[ProjectsDeletionSpec]] + * Tests related to automatic project deletion + * + * Automatic deletion is configured to look up on projects in the `autodeletion` organization and deletes them after 5 + * seconds without activity + * + * Only checks that the project deletion has been triggered and that the project itself has been deleted. All the + * additional checks on the deletion of resources/views contained in this project are done in [[ProjectsDeletionSpec]] * * @see * ProjectsDeletionSpec @@ -25,16 +30,17 @@ class AutoProjectDeletionSpec extends BaseSpec { private val proj1 = genId() private val ref1 = s"$org/$proj1" - "Setting up" should { - "succeed in setting up org and project" in { - for { - _ <- aclDsl.addPermissions("/", Bojack, Set(Organizations.Create, Projects.Delete, Resources.Read, Events.Read)) - // First org and projects - _ <- adminDsl.createOrganization(org, org, Bojack, ignoreConflict = true) - _ <- adminDsl.createProject(org, proj1, kgDsl.projectJson(name = proj1), Bojack) - _ <- deltaClient.get[Json](s"/projects/$ref1", Bojack)(expect(StatusCodes.OK)) - } yield succeed - } + override def beforeAll(): Unit = { + super.beforeAll() + val setup = for { + _ <- aclDsl.addPermissions("/", Bojack, Set(Organizations.Create, Projects.Delete, Resources.Read, Events.Read)) + // First org and projects + _ <- adminDsl.createOrganization(org, org, Bojack, ignoreConflict = true) + _ <- adminDsl.createProject(org, proj1, kgDsl.projectJson(name = proj1), Bojack) + _ <- deltaClient.get[Json](s"/projects/$ref1", Bojack)(expect(StatusCodes.OK)) + } yield succeed + + setup.void.runSyncUnsafe() } "eventually return a not found when attempting to fetch the project" in eventually {