From b464fdf01616c71c2ebce249bdacf13c55ddb08b Mon Sep 17 00:00:00 2001 From: Karthick Vinod Date: Sat, 13 Apr 2024 12:11:02 +0530 Subject: [PATCH] Attempt to cache postgres docker image --- .github/workflows/build.yml | 21 ++- docker-compose.yml | 2 +- .../hypto/iam/server/apis/ActionApiTest.kt | 16 +- .../iam/server/apis/CredentialApiKtTest.kt | 24 +-- .../com/hypto/iam/server/apis/KeyApiTest.kt | 3 + .../iam/server/apis/OrganizationApiKtTest.kt | 16 +- .../hypto/iam/server/apis/PolicyApiTest.kt | 11 +- .../hypto/iam/server/apis/ResourceApiTest.kt | 16 +- .../server/apis/SubOrganizationApiKtTest.kt | 102 +++++------ .../iam/server/helpers/DataSetupHelper.kt | 159 ------------------ src/test/resources/logback-test.xml | 35 +--- 11 files changed, 108 insertions(+), 297 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 736c4483..20127d83 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,11 +11,30 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - name: Set up JDK 11 + - name: Set up JDK 17 uses: actions/setup-java@v3 with: java-version: '17' distribution: 'temurin' + + - run: mkdir -p ~/image-cache + - id: image-cache + uses: actions/cache@v4 + with: + path: ~/image-cache + key: image-cache-${{ runner.os }} + - id: Pull on cache miss + if: steps.image-cache.outputs.cache-hit != 'true' + run: | + echo "Cache miss!" + docker pull postgres:14.1-alpine + docker save -o ~/image-cache/postgres.tar postgres:14.1-alpine + - id: Load on cache hit + if: steps.image-cache.outputs.cache-hit == 'true' + run: | + echo "Cache hit!" + docker load -i ~/image-cache/postgres.tar + - name: Run Postgres run: | docker-compose -f docker-compose.yml up -d pg diff --git a/docker-compose.yml b/docker-compose.yml index c99c8195..b7573e6b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,7 +18,7 @@ services: env_file: - ./docker/iam-web/dev.env pg: - image: postgres:14.1 + image: postgres:14.1-alpine ports: - "4921:5432" environment: diff --git a/src/test/kotlin/com/hypto/iam/server/apis/ActionApiTest.kt b/src/test/kotlin/com/hypto/iam/server/apis/ActionApiTest.kt index 112e75df..528dd473 100644 --- a/src/test/kotlin/com/hypto/iam/server/apis/ActionApiTest.kt +++ b/src/test/kotlin/com/hypto/iam/server/apis/ActionApiTest.kt @@ -2,10 +2,10 @@ package com.hypto.iam.server.apis import com.hypto.iam.server.Constants import com.hypto.iam.server.helpers.BaseSingleAppTest -import com.hypto.iam.server.helpers.DataSetupHelper.deleteOrganization import com.hypto.iam.server.helpers.DataSetupHelperV3.createAction import com.hypto.iam.server.helpers.DataSetupHelperV3.createOrganization import com.hypto.iam.server.helpers.DataSetupHelperV3.createResource +import com.hypto.iam.server.helpers.DataSetupHelperV3.deleteOrganization import com.hypto.iam.server.models.Action import com.hypto.iam.server.models.ActionPaginatedResponse import com.hypto.iam.server.models.BaseSuccessResponse @@ -55,7 +55,7 @@ class ActionApiTest : BaseSingleAppTest() { assertEquals(organization.id, responseBody.organizationId) assertEquals("", responseBody.description) - deleteOrganization(organization.id) + testApp.deleteOrganization(organization.id) } } @@ -85,7 +85,7 @@ class ActionApiTest : BaseSingleAppTest() { assertEquals(organization.id, responseBody.organizationId) assertEquals(resource.name, responseBody.resourceName) - deleteOrganization(organization.id) + testApp.deleteOrganization(organization.id) } } @@ -114,8 +114,8 @@ class ActionApiTest : BaseSingleAppTest() { response.contentType(), ) - deleteOrganization(organization1.id) - deleteOrganization(organization2.id) + testApp.deleteOrganization(organization1.id) + testApp.deleteOrganization(organization2.id) } } @@ -147,7 +147,7 @@ class ActionApiTest : BaseSingleAppTest() { } assertEquals(HttpStatusCode.NotFound, response.status) - deleteOrganization(organization.id) + testApp.deleteOrganization(organization.id) } } @@ -177,7 +177,7 @@ class ActionApiTest : BaseSingleAppTest() { assert(responseBody.data!!.contains(action1)) assert(responseBody.data!!.contains(action2)) - deleteOrganization(organization.id) + testApp.deleteOrganization(organization.id) } } @@ -209,7 +209,7 @@ class ActionApiTest : BaseSingleAppTest() { assertEquals(resource.name, responseBody.resourceName) assertEquals(newDescription, responseBody.description) - deleteOrganization(organization.id) + testApp.deleteOrganization(organization.id) } } } diff --git a/src/test/kotlin/com/hypto/iam/server/apis/CredentialApiKtTest.kt b/src/test/kotlin/com/hypto/iam/server/apis/CredentialApiKtTest.kt index 779a3fb1..e470adda 100644 --- a/src/test/kotlin/com/hypto/iam/server/apis/CredentialApiKtTest.kt +++ b/src/test/kotlin/com/hypto/iam/server/apis/CredentialApiKtTest.kt @@ -2,9 +2,9 @@ package com.hypto.iam.server.apis import com.hypto.iam.server.Constants import com.hypto.iam.server.helpers.BaseSingleAppTest -import com.hypto.iam.server.helpers.DataSetupHelper.deleteOrganization import com.hypto.iam.server.helpers.DataSetupHelperV3.createCredential import com.hypto.iam.server.helpers.DataSetupHelperV3.createOrganization +import com.hypto.iam.server.helpers.DataSetupHelperV3.deleteOrganization import com.hypto.iam.server.models.CreateCredentialRequest import com.hypto.iam.server.models.Credential import com.hypto.iam.server.models.ListCredentialResponse @@ -66,7 +66,7 @@ internal class CredentialApiKtTest : BaseSingleAppTest() { Assertions.assertEquals(Credential.Status.active, responseBody.status) Assertions.assertNotNull(responseBody.secret) - deleteOrganization(createdOrganization.id) + testApp.deleteOrganization(createdOrganization.id) } } @@ -105,7 +105,7 @@ internal class CredentialApiKtTest : BaseSingleAppTest() { Assertions.assertEquals(Credential.Status.active, responseBody.status) Assertions.assertNotNull(responseBody.secret) - deleteOrganization(createdOrganization.id) + testApp.deleteOrganization(createdOrganization.id) } } @@ -134,7 +134,7 @@ internal class CredentialApiKtTest : BaseSingleAppTest() { Json, response.contentType(), ) - deleteOrganization(createdOrganization.id) + testApp.deleteOrganization(createdOrganization.id) } } @@ -171,7 +171,7 @@ internal class CredentialApiKtTest : BaseSingleAppTest() { Json, response.contentType(), ) - deleteOrganization(createdOrganization.id) + testApp.deleteOrganization(createdOrganization.id) } } } @@ -233,7 +233,7 @@ internal class CredentialApiKtTest : BaseSingleAppTest() { } Assertions.assertEquals(HttpStatusCode.Unauthorized, response.status) - deleteOrganization(createdOrganization.id) + testApp.deleteOrganization(createdOrganization.id) } } @@ -258,7 +258,7 @@ internal class CredentialApiKtTest : BaseSingleAppTest() { Assertions.assertEquals(HttpStatusCode.NotFound, response.status) Assertions.assertEquals(Json, response.contentType()) - deleteOrganization(createdOrganization.id) + testApp.deleteOrganization(createdOrganization.id) } } @@ -285,8 +285,8 @@ internal class CredentialApiKtTest : BaseSingleAppTest() { } Assertions.assertEquals(HttpStatusCode.Forbidden, response.status) Assertions.assertEquals(Json, response.contentType()) - deleteOrganization(organization1.id) - deleteOrganization(organizationResponse2.organization.id) + testApp.deleteOrganization(organization1.id) + testApp.deleteOrganization(organizationResponse2.organization.id) } } } @@ -319,7 +319,7 @@ internal class CredentialApiKtTest : BaseSingleAppTest() { Assertions.assertNull(responseBody.secret) Assertions.assertEquals(Credential.Status.active, responseBody.status) - deleteOrganization(createdOrganization.id) + testApp.deleteOrganization(createdOrganization.id) } } @@ -342,7 +342,7 @@ internal class CredentialApiKtTest : BaseSingleAppTest() { Assertions.assertEquals(HttpStatusCode.NotFound, response.status) Assertions.assertEquals(Json, response.contentType()) - deleteOrganization(createdOrganization.id) + testApp.deleteOrganization(createdOrganization.id) } } @@ -363,7 +363,7 @@ internal class CredentialApiKtTest : BaseSingleAppTest() { } Assertions.assertEquals(HttpStatusCode.BadRequest, response.status) Assertions.assertEquals(Json, response.contentType()) - deleteOrganization(createdOrganization.id) + testApp.deleteOrganization(createdOrganization.id) } } } diff --git a/src/test/kotlin/com/hypto/iam/server/apis/KeyApiTest.kt b/src/test/kotlin/com/hypto/iam/server/apis/KeyApiTest.kt index 21c8b5ba..3445865b 100644 --- a/src/test/kotlin/com/hypto/iam/server/apis/KeyApiTest.kt +++ b/src/test/kotlin/com/hypto/iam/server/apis/KeyApiTest.kt @@ -2,6 +2,7 @@ package com.hypto.iam.server.apis import com.hypto.iam.server.helpers.BaseSingleAppTest import com.hypto.iam.server.helpers.DataSetupHelperV3.createOrganization +import com.hypto.iam.server.helpers.DataSetupHelperV3.deleteOrganization import com.hypto.iam.server.models.KeyResponse import com.hypto.iam.server.models.TokenResponse import com.hypto.iam.server.service.TokenServiceImpl @@ -70,6 +71,8 @@ class KeyApiTest : BaseSingleAppTest() { assertDoesNotThrow { Jwts.parserBuilder().setSigningKey(publicKey).build().parseClaimsJws(token) } + + testApp.deleteOrganization(organizationResponse.organization.id) } } } diff --git a/src/test/kotlin/com/hypto/iam/server/apis/OrganizationApiKtTest.kt b/src/test/kotlin/com/hypto/iam/server/apis/OrganizationApiKtTest.kt index 87ec69a6..27ef1fee 100644 --- a/src/test/kotlin/com/hypto/iam/server/apis/OrganizationApiKtTest.kt +++ b/src/test/kotlin/com/hypto/iam/server/apis/OrganizationApiKtTest.kt @@ -9,7 +9,7 @@ import com.hypto.iam.server.db.repositories.PasscodeRepo import com.hypto.iam.server.db.tables.pojos.Organizations import com.hypto.iam.server.db.tables.records.PasscodesRecord import com.hypto.iam.server.helpers.BaseSingleAppTest -import com.hypto.iam.server.helpers.DataSetupHelper.deleteOrganization +import com.hypto.iam.server.helpers.DataSetupHelperV3.deleteOrganization import com.hypto.iam.server.models.CreateOrganizationRequest import com.hypto.iam.server.models.CreateOrganizationResponse import com.hypto.iam.server.models.Organization @@ -145,7 +145,7 @@ internal class OrganizationApiKtTest : BaseSingleAppTest() { Assertions.assertEquals(1, policies.data?.size) Assertions.assertEquals("admin", policies.data!![0].name) - deleteOrganization(orgId) + testApp.deleteOrganization(orgId) } } @@ -244,7 +244,7 @@ internal class OrganizationApiKtTest : BaseSingleAppTest() { assertEquals(requestBody.name, responseBody.organization.name) assertEquals(10, responseBody.organization.id.length) - deleteOrganization(orgId) + testApp.deleteOrganization(orgId) } } @@ -310,7 +310,7 @@ internal class OrganizationApiKtTest : BaseSingleAppTest() { assertEquals(orgName, responseBody.organization.name) assertEquals(10, responseBody.organization.id.length) - deleteOrganization(orgId) + testApp.deleteOrganization(orgId) } } @@ -358,7 +358,7 @@ internal class OrganizationApiKtTest : BaseSingleAppTest() { // Cleanup val orgId = responseBody.organization.id - deleteOrganization(orgId) + testApp.deleteOrganization(orgId) } } @@ -478,7 +478,7 @@ internal class OrganizationApiKtTest : BaseSingleAppTest() { assertFalse(response.headers.contains(HttpHeaders.ContentType)) assertNull(response.headers[Constants.X_ORGANIZATION_HEADER]) - deleteOrganization(createdOrganization.organization.id) + testApp.deleteOrganization(createdOrganization.organization.id) } } @@ -525,7 +525,7 @@ internal class OrganizationApiKtTest : BaseSingleAppTest() { val fetchedOrganization = gson.fromJson(response.bodyAsText(), Organization::class.java) assertEquals(createdOrganization.organization, fetchedOrganization) - deleteOrganization(createdOrganization.organization.id) + testApp.deleteOrganization(createdOrganization.organization.id) } } @@ -572,7 +572,7 @@ internal class OrganizationApiKtTest : BaseSingleAppTest() { } // These assertions assertEquals(HttpStatusCode.Forbidden, response.status) - deleteOrganization(createdOrganization.organization.id) + testApp.deleteOrganization(createdOrganization.organization.id) } } diff --git a/src/test/kotlin/com/hypto/iam/server/apis/PolicyApiTest.kt b/src/test/kotlin/com/hypto/iam/server/apis/PolicyApiTest.kt index 9207d1c4..6c29b10a 100644 --- a/src/test/kotlin/com/hypto/iam/server/apis/PolicyApiTest.kt +++ b/src/test/kotlin/com/hypto/iam/server/apis/PolicyApiTest.kt @@ -9,6 +9,7 @@ import com.hypto.iam.server.helpers.DataSetupHelperV3.createAndAttachPolicy import com.hypto.iam.server.helpers.DataSetupHelperV3.createOrganization import com.hypto.iam.server.helpers.DataSetupHelperV3.createResourceActionHrn import com.hypto.iam.server.helpers.DataSetupHelperV3.createUser +import com.hypto.iam.server.helpers.DataSetupHelperV3.deleteOrganization import com.hypto.iam.server.models.CreatePolicyFromTemplateRequest import com.hypto.iam.server.models.CreatePolicyRequest import com.hypto.iam.server.models.CreateUserRequest @@ -100,7 +101,7 @@ internal class PolicyApiTest : BaseSingleAppTest() { Assertions.assertEquals(1, responseBody.version) Assertions.assertEquals(policyStatements, responseBody.statements) - deleteOrganization(createdOrganization.id) + testApp.deleteOrganization(createdOrganization.id) } } @@ -252,7 +253,7 @@ internal class PolicyApiTest : BaseSingleAppTest() { response.contentType(), ) - deleteOrganization(createdOrganization.id) + testApp.deleteOrganization(createdOrganization.id) } } @@ -334,7 +335,7 @@ internal class PolicyApiTest : BaseSingleAppTest() { // cleanup policyTempRecord.delete() - deleteOrganization(createdOrganization.id) + testApp.deleteOrganization(createdOrganization.id) } } } @@ -1037,7 +1038,7 @@ internal class PolicyApiTest : BaseSingleAppTest() { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) } Assertions.assertEquals(HttpStatusCode.Forbidden, response.status) - deleteOrganization(organization.id) + testApp.deleteOrganization(organization.id) } } @@ -1108,7 +1109,7 @@ internal class PolicyApiTest : BaseSingleAppTest() { val responseBody = gson.fromJson(response.bodyAsText(), PolicyPaginatedResponse::class.java) Assertions.assertEquals(0, responseBody.data!!.size) - deleteOrganization(organization.id) + testApp.deleteOrganization(organization.id) } } } diff --git a/src/test/kotlin/com/hypto/iam/server/apis/ResourceApiTest.kt b/src/test/kotlin/com/hypto/iam/server/apis/ResourceApiTest.kt index 3a7ab227..51fbf0c1 100644 --- a/src/test/kotlin/com/hypto/iam/server/apis/ResourceApiTest.kt +++ b/src/test/kotlin/com/hypto/iam/server/apis/ResourceApiTest.kt @@ -1,9 +1,9 @@ package com.hypto.iam.server.apis import com.hypto.iam.server.helpers.BaseSingleAppTest -import com.hypto.iam.server.helpers.DataSetupHelper.deleteOrganization import com.hypto.iam.server.helpers.DataSetupHelperV3.createOrganization import com.hypto.iam.server.helpers.DataSetupHelperV3.createResource +import com.hypto.iam.server.helpers.DataSetupHelperV3.deleteOrganization import com.hypto.iam.server.models.BaseSuccessResponse import com.hypto.iam.server.models.CreateResourceRequest import com.hypto.iam.server.models.Resource @@ -50,7 +50,7 @@ internal class ResourceApiTest : BaseSingleAppTest() { assertEquals(organization.id, responseBody.organizationId) assertEquals("", responseBody.description) - deleteOrganization(organization.id) + testApp.deleteOrganization(organization.id) } } @@ -74,7 +74,7 @@ internal class ResourceApiTest : BaseSingleAppTest() { val responseBody = gson.fromJson(response.bodyAsText(), Resource::class.java) assertEquals(resourceName, responseBody.name) - deleteOrganization(organization.id) + testApp.deleteOrganization(organization.id) } } @@ -98,8 +98,8 @@ internal class ResourceApiTest : BaseSingleAppTest() { assertEquals(HttpStatusCode.Forbidden, response.status) assertEquals(ContentType.Application.Json, response.contentType()) - deleteOrganization(organization1.id) - deleteOrganization(organization2.id) + testApp.deleteOrganization(organization1.id) + testApp.deleteOrganization(organization2.id) } } @@ -128,7 +128,7 @@ internal class ResourceApiTest : BaseSingleAppTest() { } assertEquals(HttpStatusCode.NotFound, getResourceResponse.status) - deleteOrganization(organization.id) + testApp.deleteOrganization(organization.id) } } @@ -155,7 +155,7 @@ internal class ResourceApiTest : BaseSingleAppTest() { assert(responseBody.data!!.contains(resource1)) assert(responseBody.data!!.contains(resource2)) - deleteOrganization(organization.id) + testApp.deleteOrganization(organization.id) } } @@ -182,7 +182,7 @@ internal class ResourceApiTest : BaseSingleAppTest() { assertEquals(resource.name, responseBody.name) assertEquals(newDescription, responseBody.description) - deleteOrganization(organization.id) + testApp.deleteOrganization(organization.id) } } } diff --git a/src/test/kotlin/com/hypto/iam/server/apis/SubOrganizationApiKtTest.kt b/src/test/kotlin/com/hypto/iam/server/apis/SubOrganizationApiKtTest.kt index 0b1e885a..c37f4054 100644 --- a/src/test/kotlin/com/hypto/iam/server/apis/SubOrganizationApiKtTest.kt +++ b/src/test/kotlin/com/hypto/iam/server/apis/SubOrganizationApiKtTest.kt @@ -1,10 +1,9 @@ package com.hypto.iam.server.apis -import com.google.gson.Gson import com.hypto.iam.server.Constants -import com.hypto.iam.server.helpers.AbstractContainerBaseTest -import com.hypto.iam.server.helpers.DataSetupHelperV2.createOrganization -import com.hypto.iam.server.helpers.DataSetupHelperV2.deleteOrganization +import com.hypto.iam.server.helpers.BaseSingleAppTest +import com.hypto.iam.server.helpers.DataSetupHelperV3.createOrganization +import com.hypto.iam.server.helpers.DataSetupHelperV3.deleteOrganization import com.hypto.iam.server.models.CreateSubOrganizationRequest import com.hypto.iam.server.models.CreateSubOrganizationResponse import com.hypto.iam.server.models.SubOrganization @@ -22,27 +21,20 @@ import io.ktor.http.ContentType import io.ktor.http.HttpHeaders import io.ktor.http.HttpStatusCode import io.ktor.http.contentType -import io.ktor.server.config.ApplicationConfig -import io.ktor.server.testing.testApplication +import io.ktor.test.dispatcher.testSuspend import org.junit.jupiter.api.Test -import org.koin.test.inject import org.testcontainers.junit.jupiter.Testcontainers import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNull @Testcontainers -internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { - private val gson: Gson by inject() - +internal class SubOrganizationApiKtTest : BaseSingleAppTest() { @Test fun `create a sub organization under an organization`() { - testApplication { - environment { - config = ApplicationConfig("application-custom.conf") - } + testSuspend { val subOrgName = "test-sub-org" + IdGenerator.randomId() - val (organizationResponse, _) = createOrganization() + val (organizationResponse, _) = testApp.createOrganization() val orgId = organizationResponse.organization.id val token = organizationResponse.rootUserToken @@ -51,7 +43,7 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { subOrgName, ) val response = - client.post("/organizations/$orgId/sub_organizations") { + testApp.client.post("/organizations/$orgId/sub_organizations") { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) header("Authorization", "Bearer $token") setBody(gson.toJson(requestBody)) @@ -70,18 +62,15 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { "match", ) - deleteOrganization(orgId) + testApp.deleteOrganization(orgId) } } @Test fun `create sub organization with invalid credentials`() { - testApplication { - environment { - config = ApplicationConfig("application-custom.conf") - } + testSuspend { val subOrgName = "test-sub-org" + IdGenerator.randomId() - val (organizationResponse, _) = createOrganization() + val (organizationResponse, _) = testApp.createOrganization() val orgId = organizationResponse.organization.id val requestBody = @@ -89,7 +78,7 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { subOrgName, ) val response = - client.post("/organizations/$orgId/sub_organizations") { + testApp.client.post("/organizations/$orgId/sub_organizations") { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) header("Authorization", "Bearer test-bearer-token") setBody(gson.toJson(requestBody)) @@ -98,18 +87,15 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { assertEquals(HttpStatusCode.Unauthorized, response.status) assertFalse(response.headers.contains(HttpHeaders.ContentType)) assertNull(response.headers[Constants.X_ORGANIZATION_HEADER]) - deleteOrganization(orgId) + testApp.deleteOrganization(orgId) } } @Test fun `get sub organization success`() { - testApplication { - environment { - config = ApplicationConfig("application-custom.conf") - } + testSuspend { val subOrgName = "test-sub-org" + IdGenerator.randomId() - val (organizationResponse, _) = createOrganization() + val (organizationResponse, _) = testApp.createOrganization() val orgId = organizationResponse.organization.id val token = organizationResponse.rootUserToken val subOrgDescription = "test-sub-org-desc" @@ -120,7 +106,7 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { subOrgDescription, ) val response = - client.post("/organizations/$orgId/sub_organizations") { + testApp.client.post("/organizations/$orgId/sub_organizations") { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) header("Authorization", "Bearer $token") setBody(gson.toJson(requestBody)) @@ -141,7 +127,7 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { // Get sub organization val getSubOrganizationResponse = - client.get("/organizations/$orgId/sub_organizations/$subOrgName") { + testApp.client.get("/organizations/$orgId/sub_organizations/$subOrgName") { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) header("Authorization", "Bearer $token") } @@ -150,41 +136,35 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { gson.fromJson(getSubOrganizationResponse.bodyAsText(), SubOrganization::class.java) assertEquals(subOrgName, subOrganization.name, "Sub organization name should match") assertEquals(subOrgDescription, subOrganization.description, "Sub organization description should match") - deleteOrganization(orgId) + testApp.deleteOrganization(orgId) } } @Test fun `get organization not found`() { - testApplication { - environment { - config = ApplicationConfig("application-custom.conf") - } + testSuspend { val subOrgName = "test-sub-org" + IdGenerator.randomId() - val (organizationResponse, _) = createOrganization() + val (organizationResponse, _) = testApp.createOrganization() val orgId = organizationResponse.organization.id val token = organizationResponse.rootUserToken val subOrgDescription = "test-sub-org-desc" // Get sub organization val getSubOrganizationResponse = - client.get("/organizations/$orgId/sub_organizations/dummy_sub_org") { + testApp.client.get("/organizations/$orgId/sub_organizations/dummy_sub_org") { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) header("Authorization", "Bearer $token") } assertEquals(HttpStatusCode.NotFound, getSubOrganizationResponse.status) - deleteOrganization(orgId) + testApp.deleteOrganization(orgId) } } @Test fun `update sub organization desc success`() { - testApplication { - environment { - config = ApplicationConfig("application-custom.conf") - } + testSuspend { val subOrgName = "test-sub-org" + IdGenerator.randomId() - val (organizationResponse, _) = createOrganization() + val (organizationResponse, _) = testApp.createOrganization() val orgId = organizationResponse.organization.id val token = organizationResponse.rootUserToken val subOrgDescription = "test-sub-org-desc" @@ -195,7 +175,7 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { subOrgDescription, ) val response = - client.post("/organizations/$orgId/sub_organizations") { + testApp.client.post("/organizations/$orgId/sub_organizations") { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) header("Authorization", "Bearer $token") setBody(gson.toJson(requestBody)) @@ -220,7 +200,7 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { "updated-sub-org-desc", ) val updateSubOrganizationNameResponse = - client + testApp.client .patch("/organizations/$orgId/sub_organizations/$subOrgName") { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) header("Authorization", "Bearer $token") @@ -234,18 +214,15 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { updatedSubOrganization.description, "Sub organization description should match", ) - deleteOrganization(orgId) + testApp.deleteOrganization(orgId) } } @Test fun `delete sub organization success`() { - testApplication { - environment { - config = ApplicationConfig("application-custom.conf") - } + testSuspend { val subOrgName = "test-sub-org" + IdGenerator.randomId() - val (organizationResponse, _) = createOrganization() + val (organizationResponse, _) = testApp.createOrganization() val orgId = organizationResponse.organization.id val token = organizationResponse.rootUserToken val subOrgDescription = "test-sub-org-desc" @@ -256,7 +233,7 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { subOrgDescription, ) val response = - client.post("/organizations/$orgId/sub_organizations") { + testApp.client.post("/organizations/$orgId/sub_organizations") { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) header("Authorization", "Bearer $token") setBody(gson.toJson(requestBody)) @@ -277,7 +254,7 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { // Delete sub organization val deleteSubOrganizationResponse = - client.delete("/organizations/$orgId/sub_organizations/$subOrgName") { + testApp.client.delete("/organizations/$orgId/sub_organizations/$subOrgName") { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) header("Authorization", "Bearer $token") } @@ -285,24 +262,21 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { // Get sub organization val getSubOrganizationResponse = - client.get("/organizations/$orgId/sub_organizations/$subOrgName") { + testApp.client.get("/organizations/$orgId/sub_organizations/$subOrgName") { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) header("Authorization", "Bearer $token") } assertEquals(HttpStatusCode.NotFound, getSubOrganizationResponse.status) - deleteOrganization(orgId) + testApp.deleteOrganization(orgId) } } @Test fun `list sub organization success`() { - testApplication { - environment { - config = ApplicationConfig("application-custom.conf") - } + testSuspend { val subOrgCount = 50 - val (organizationResponse, _) = createOrganization() + val (organizationResponse, _) = testApp.createOrganization() val orgId = organizationResponse.organization.id val token = organizationResponse.rootUserToken val originalSubOrgList = mutableListOf() @@ -315,7 +289,7 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { subOrgDescription, ) val response = - client.post("/organizations/$orgId/sub_organizations") { + testApp.client.post("/organizations/$orgId/sub_organizations") { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) header("Authorization", "Bearer $token") setBody(gson.toJson(requestBody)) @@ -348,7 +322,7 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { } val listSubOrganizationResponse = - client.get(url) { + testApp.client.get(url) { header(HttpHeaders.ContentType, ContentType.Application.Json.toString()) header("Authorization", "Bearer $token") } @@ -364,7 +338,7 @@ internal class SubOrganizationApiKtTest : AbstractContainerBaseTest() { assertEquals(50, subOrganizationList.size, "Sub organization list size should match") assertEquals(originalSubOrgList.toSet(), subOrganizationList.toSet(), "Sub organization list should match") - deleteOrganization(orgId) + testApp.deleteOrganization(orgId) } } } diff --git a/src/test/kotlin/com/hypto/iam/server/helpers/DataSetupHelper.kt b/src/test/kotlin/com/hypto/iam/server/helpers/DataSetupHelper.kt index 75f2123b..57504572 100644 --- a/src/test/kotlin/com/hypto/iam/server/helpers/DataSetupHelper.kt +++ b/src/test/kotlin/com/hypto/iam/server/helpers/DataSetupHelper.kt @@ -12,22 +12,9 @@ import com.hypto.iam.server.db.repositories.PoliciesRepo import com.hypto.iam.server.db.repositories.PrincipalPoliciesRepo import com.hypto.iam.server.db.repositories.ResourceRepo import com.hypto.iam.server.helpers.DataSetupHelperV2.toStringEscape -import com.hypto.iam.server.models.Action -import com.hypto.iam.server.models.CreateActionRequest -import com.hypto.iam.server.models.CreateCredentialRequest import com.hypto.iam.server.models.CreateOrganizationRequest import com.hypto.iam.server.models.CreateOrganizationResponse -import com.hypto.iam.server.models.CreatePolicyRequest -import com.hypto.iam.server.models.CreateResourceRequest -import com.hypto.iam.server.models.CreateUserRequest -import com.hypto.iam.server.models.CreateUserResponse -import com.hypto.iam.server.models.Credential -import com.hypto.iam.server.models.Policy -import com.hypto.iam.server.models.PolicyAssociationRequest -import com.hypto.iam.server.models.PolicyStatement -import com.hypto.iam.server.models.Resource import com.hypto.iam.server.models.RootUser -import com.hypto.iam.server.models.User import com.hypto.iam.server.utils.ActionHrn import com.hypto.iam.server.utils.IdGenerator import com.hypto.iam.server.utils.ResourceHrn @@ -95,152 +82,6 @@ object DataSetupHelper : AutoCloseKoinTest() { } } - fun createCredential( - orgId: String, - userName: String, - jwtToken: String, - engine: TestApplicationEngine, - ): Credential { - with(engine) { - val createCredentialCall = - handleRequest( - HttpMethod.Post, - "/organizations/$orgId/users/$userName/credentials", - ) { - addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString()) - addHeader(HttpHeaders.Authorization, "Bearer $jwtToken") - setBody(gson.toJson(CreateCredentialRequest())) - } - - return gson - .fromJson(createCredentialCall.response.content, Credential::class.java) - } - } - - fun createUser( - orgId: String, - bearerToken: String, - createUserRequest: CreateUserRequest, - engine: TestApplicationEngine, - ): Pair { - with(engine) { - val createUserCall = - handleRequest(HttpMethod.Post, "/organizations/$orgId/users") { - addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString()) - addHeader(HttpHeaders.Authorization, "Bearer $bearerToken") - setBody(gson.toJson(createUserRequest)) - } - val createdUser = - gson - .fromJson(createUserCall.response.content, CreateUserResponse::class.java) - val credential = - createCredential(orgId, createdUser.user.username, bearerToken, engine) - - return Pair(createdUser.user, credential) - } - } - - fun createAndAttachPolicy( - orgId: String, - username: String?, - bearerToken: String, - policyName: String, - accountId: String?, - resourceName: String, - actionName: String, - resourceInstance: String, - engine: TestApplicationEngine, - effect: PolicyStatement.Effect = PolicyStatement.Effect.allow, - ): Policy { - with(engine) { - val (resourceHrn, actionHrn) = - createResourceActionHrn( - orgId, - accountId, - resourceName, - actionName, - resourceInstance, - ) - val policyStatements = listOf(PolicyStatement(resourceHrn, actionHrn, effect)) - val requestBody = CreatePolicyRequest(policyName, policyStatements) - - val createPolicyCall = - handleRequest( - HttpMethod.Post, - "/organizations/$orgId/policies", - ) { - addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString()) - addHeader(HttpHeaders.Authorization, "Bearer $bearerToken") - setBody(gson.toJson(requestBody)) - } - - val policy = gson.fromJson(createPolicyCall.response.content, Policy::class.java) - - if (username != null) { - handleRequest( - HttpMethod.Patch, - "/organizations/$orgId/users/" + - "$username/attach_policies", - ) { - val createAssociationRequest = PolicyAssociationRequest(listOf(policy.hrn)) - addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString()) - addHeader( - HttpHeaders.Authorization, - "Bearer $bearerToken", - ) - setBody(gson.toJson(createAssociationRequest)) - } - } - return policy - } - } - - fun createResource( - orgId: String, - jwtToken: String, - engine: TestApplicationEngine, - resourceName: String? = null, - ): Resource { - with(engine) { - val name = resourceName ?: ("test-resource" + IdGenerator.randomId()) - - val createResourceCall = - handleRequest(HttpMethod.Post, "/organizations/$orgId/resources") { - addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString()) - addHeader(HttpHeaders.Authorization, "Bearer $jwtToken") - setBody(gson.toJson(CreateResourceRequest(name = name))) - } - - return gson - .fromJson(createResourceCall.response.content, Resource::class.java) - } - } - - fun createAction( - orgId: String, - resource: Resource? = null, - jwtToken: String, - engine: TestApplicationEngine, - ): Pair { - with(engine) { - val createdResource = resource ?: createResource(orgId, jwtToken, engine) - val actionName = "test-action" + IdGenerator.randomId() - - val createActionCall = - handleRequest(HttpMethod.Post, "/organizations/$orgId/resources/${createdResource.name}/actions") { - addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString()) - addHeader(HttpHeaders.Authorization, "Bearer $jwtToken") - setBody(gson.toJson(CreateActionRequest(name = actionName))) - } - - val createdAction = - gson - .fromJson(createActionCall.response.content, Action::class.java) - - return Pair(createdAction, createdResource) - } - } - fun createResourceActionHrn( orgId: String, accountId: String?, diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index 71b0e686..677866ae 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -1,32 +1,5 @@ - - - - %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - ${TEST_LOG_DEST:-build/output/logs}/iam_application.log - - - ${TEST_LOG_DEST:-build/test/output/logs}/iam_application.%d{yyyy-MM-dd}.log + - - ${TEST_LOG_MAX_HISTORY:-1} - 100MB - - - - - %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - - - - - + + + \ No newline at end of file