From 1b44b0e5c6b304bfa8f13b1c5612ece4fec3cc86 Mon Sep 17 00:00:00 2001 From: Maya Karabula-Stysiak Date: Thu, 2 Nov 2023 15:44:41 +0100 Subject: [PATCH 1/4] chore: integration test keys --- test/integration/commands/apply/index.test.ts | 9 ++-- test/integration/commands/bootstrap.ts | 50 +++++++++++++++++-- .../integration/commands/create/index.test.ts | 10 ++-- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/test/integration/commands/apply/index.test.ts b/test/integration/commands/apply/index.test.ts index 2c43e3e4..84fb6bea 100644 --- a/test/integration/commands/apply/index.test.ts +++ b/test/integration/commands/apply/index.test.ts @@ -1,7 +1,7 @@ import { expect } from '@oclif/test' import { ApiKey, Space, createClient } from 'contentful-management' import fs from 'fs' -import { ApplyTestContext, createCdaToken, createEnvironments } from './../bootstrap' +import { ApplyTestContext, INTEGRATION_TEST_KEY_MASTER, createCdaToken, createEnvironments } from './../bootstrap' import fancy from './../register-plugins' import { createChangeset } from '../../../../src/engine/utils/create-changeset' import { createAddTwoItemsChangeset } from '../fixtures/add-two-items-changeset' @@ -69,13 +69,16 @@ describe('create command', () => { fs.writeFileSync(changesetPath, JSON.stringify(changeset, null, 2)) fs.writeFileSync(changesetPathAddItems, JSON.stringify(addTwoItemsChangeset, null, 2)) - cdaTokenWithOnlyMasterAccess = await createCdaToken(testSpace, ['master']) + try { + cdaTokenWithOnlyMasterAccess = await testSpace.getApiKey(INTEGRATION_TEST_KEY_MASTER) + } catch (e) { + cdaTokenWithOnlyMasterAccess = await createCdaToken(testSpace, ['master']) + } }) after(async () => { await Promise.all([ testContext.teardown(), - cdaTokenWithOnlyMasterAccess.delete(), fs.promises.rm(changesetPath, { force: true }), fs.promises.rm(changesetPathAddItems, { force: true }), ]) diff --git a/test/integration/commands/bootstrap.ts b/test/integration/commands/bootstrap.ts index a36ff220..72188955 100644 --- a/test/integration/commands/bootstrap.ts +++ b/test/integration/commands/bootstrap.ts @@ -3,6 +3,9 @@ import { createClient } from 'contentful' import { ApiKey, Environment } from 'contentful-management' import { CreateApiKeyProps, Space } from 'contentful-management/types' +export const INTEGRATION_TEST_KEY = '5HAbGYZ6iZWiDXGxMtvsrW' +export const INTEGRATION_TEST_KEY_MASTER = '5DH7JdIDD4k6sF05Z3VPe2' + export type TestContext = { sourceEnvironment: Environment targetEnvironment: Environment @@ -60,8 +63,46 @@ export const createCdaToken = async (space: Space, environmentIds: string[]): Pr return apiKey } +export const updateCdaToken = async (cdaTokenId: string, space: Space, environmentIds: string[]): Promise => { + console.log('fetching api key...') + let apiKey: ApiKey + + try { + apiKey = await space.getApiKey(cdaTokenId) + + environmentIds.forEach((envId) => { + apiKey.environments.push({ + sys: { + type: 'Link', + linkType: 'Environment', + id: envId, + }, + }) + }) + + await apiKey.update() + } catch (e) { + console.log('failed to fetch key', e) + apiKey = await createCdaToken(space, environmentIds) + } + + return apiKey +} + +const removeEnvironmentsFromKey = async (apiKey: ApiKey, environments: Environment[]) => { + if (apiKey.sys.id !== INTEGRATION_TEST_KEY) { + await apiKey.delete() + } + + apiKey.environments = apiKey.environments.filter((env) => !environments.map((e) => e.sys.id).includes(env.sys.id)) + await apiKey.update() +} + const teardown = async ({ apiKey, environments }: { apiKey?: ApiKey; environments: Environment[] }): Promise => { - await Promise.allSettled([apiKey && apiKey.delete(), ...environments.map((env) => env.delete())]) + await Promise.allSettled([ + apiKey && removeEnvironmentsFromKey(apiKey, environments), + ...environments.map((env) => env.delete()), + ]) } export const createEnvironments = async (testSpace: Space): Promise => { @@ -70,8 +111,11 @@ export const createEnvironments = async (testSpace: Space): Promise { @@ -27,11 +27,15 @@ describe('create command', () => { } testContext = environmentsContext - cdaTokenWithOnlyMasterAccess = await createCdaToken(testSpace, ['master']) + try { + cdaTokenWithOnlyMasterAccess = await testSpace.getApiKey(INTEGRATION_TEST_KEY_MASTER) + } catch (e) { + cdaTokenWithOnlyMasterAccess = await createCdaToken(testSpace, ['master']) + } }) after(async () => { - await Promise.all([testContext.teardown(), cdaTokenWithOnlyMasterAccess.delete()]) + await Promise.all([testContext.teardown()]) }) afterEach(() => fs.promises.rm(changesetPath, { force: true })) From 98beea08ba89b6ba1fc4fa113d67a57a19a99670 Mon Sep 17 00:00:00 2001 From: Maya Karabula-Stysiak Date: Thu, 2 Nov 2023 15:47:28 +0100 Subject: [PATCH 2/4] fix: package-lock.json --- package-lock.json | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 715be374..0e50ed5a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1071,9 +1071,9 @@ } }, "node_modules/@oclif/core": { - "version": "3.9.2", - "resolved": "https://registry.npmjs.org/@oclif/core/-/core-3.9.2.tgz", - "integrity": "sha512-fDygnFqh2KXn/IDquhypZSP7Q2V071WgRswq8cgli5uw6RzS4Uyj/7EkijboiAo7H+bRFxN0PdgGQHGyAoc+XQ==", + "version": "3.10.0", + "resolved": "https://registry.npmjs.org/@oclif/core/-/core-3.10.0.tgz", + "integrity": "sha512-gXats+3wTQRztZS2gy8rcZgkKqLY/FexCzcAkNe8Z4E+268hagAHMFxA8tsiUcOKaywgQAL55TCHD1bgni54GA==", "dependencies": { "ansi-escapes": "^4.3.2", "ansi-styles": "^4.3.0", @@ -1097,6 +1097,7 @@ "strip-ansi": "^6.0.1", "supports-color": "^8.1.1", "supports-hyperlinks": "^2.2.0", + "tsconfck": "^3.0.0", "widest-line": "^3.1.0", "wordwrap": "^1.0.0", "wrap-ansi": "^7.0.0" @@ -20246,6 +20247,25 @@ "node": ">=0.3.1" } }, + "node_modules/tsconfck": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/tsconfck/-/tsconfck-3.0.0.tgz", + "integrity": "sha512-w3wnsIrJNi7avf4Zb0VjOoodoO0woEqGgZGQm+LHH9przdUI+XDKsWAXwxHA1DaRTjeuZNcregSzr7RaA8zG9A==", + "bin": { + "tsconfck": "bin/tsconfck.js" + }, + "engines": { + "node": "^18 || >=20" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, "node_modules/tslib": { "version": "2.6.2", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz", From 28d9455a498248f61148611b37df93bada76a7d2 Mon Sep 17 00:00:00 2001 From: Maya Karabula-Stysiak Date: Thu, 2 Nov 2023 16:05:07 +0100 Subject: [PATCH 3/4] fix: conditional token removal --- test/integration/commands/apply/index.test.ts | 1 + test/integration/commands/create/index.test.ts | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/test/integration/commands/apply/index.test.ts b/test/integration/commands/apply/index.test.ts index 84fb6bea..255521ad 100644 --- a/test/integration/commands/apply/index.test.ts +++ b/test/integration/commands/apply/index.test.ts @@ -79,6 +79,7 @@ describe('create command', () => { after(async () => { await Promise.all([ testContext.teardown(), + cdaTokenWithOnlyMasterAccess.sys.id !== INTEGRATION_TEST_KEY_MASTER && cdaTokenWithOnlyMasterAccess.delete(), fs.promises.rm(changesetPath, { force: true }), fs.promises.rm(changesetPathAddItems, { force: true }), ]) diff --git a/test/integration/commands/create/index.test.ts b/test/integration/commands/create/index.test.ts index ce630a85..9149da8f 100644 --- a/test/integration/commands/create/index.test.ts +++ b/test/integration/commands/create/index.test.ts @@ -35,7 +35,10 @@ describe('create command', () => { }) after(async () => { - await Promise.all([testContext.teardown()]) + await Promise.all([ + cdaTokenWithOnlyMasterAccess.sys.id !== INTEGRATION_TEST_KEY_MASTER && cdaTokenWithOnlyMasterAccess.delete(), + testContext.teardown(), + ]) }) afterEach(() => fs.promises.rm(changesetPath, { force: true })) From 47452261d20d4b53ba5bb774866c80e240ddfca0 Mon Sep 17 00:00:00 2001 From: Maya Karabula-Stysiak Date: Fri, 17 Nov 2023 16:24:49 +0100 Subject: [PATCH 4/4] fix: update key names --- test/integration/commands/apply/index.test.ts | 6 +++--- test/integration/commands/bootstrap.ts | 8 ++++---- test/integration/commands/create/index.test.ts | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/integration/commands/apply/index.test.ts b/test/integration/commands/apply/index.test.ts index 255521ad..18e3b154 100644 --- a/test/integration/commands/apply/index.test.ts +++ b/test/integration/commands/apply/index.test.ts @@ -1,7 +1,7 @@ import { expect } from '@oclif/test' import { ApiKey, Space, createClient } from 'contentful-management' import fs from 'fs' -import { ApplyTestContext, INTEGRATION_TEST_KEY_MASTER, createCdaToken, createEnvironments } from './../bootstrap' +import { ApplyTestContext, CDA_ACCESS_TOKEN_MASTER_FOR_TEST, createCdaToken, createEnvironments } from './../bootstrap' import fancy from './../register-plugins' import { createChangeset } from '../../../../src/engine/utils/create-changeset' import { createAddTwoItemsChangeset } from '../fixtures/add-two-items-changeset' @@ -70,7 +70,7 @@ describe('create command', () => { fs.writeFileSync(changesetPathAddItems, JSON.stringify(addTwoItemsChangeset, null, 2)) try { - cdaTokenWithOnlyMasterAccess = await testSpace.getApiKey(INTEGRATION_TEST_KEY_MASTER) + cdaTokenWithOnlyMasterAccess = await testSpace.getApiKey(CDA_ACCESS_TOKEN_MASTER_FOR_TEST) } catch (e) { cdaTokenWithOnlyMasterAccess = await createCdaToken(testSpace, ['master']) } @@ -79,7 +79,7 @@ describe('create command', () => { after(async () => { await Promise.all([ testContext.teardown(), - cdaTokenWithOnlyMasterAccess.sys.id !== INTEGRATION_TEST_KEY_MASTER && cdaTokenWithOnlyMasterAccess.delete(), + cdaTokenWithOnlyMasterAccess.sys.id !== CDA_ACCESS_TOKEN_MASTER_FOR_TEST && cdaTokenWithOnlyMasterAccess.delete(), fs.promises.rm(changesetPath, { force: true }), fs.promises.rm(changesetPathAddItems, { force: true }), ]) diff --git a/test/integration/commands/bootstrap.ts b/test/integration/commands/bootstrap.ts index 72188955..d3a071b0 100644 --- a/test/integration/commands/bootstrap.ts +++ b/test/integration/commands/bootstrap.ts @@ -3,8 +3,8 @@ import { createClient } from 'contentful' import { ApiKey, Environment } from 'contentful-management' import { CreateApiKeyProps, Space } from 'contentful-management/types' -export const INTEGRATION_TEST_KEY = '5HAbGYZ6iZWiDXGxMtvsrW' -export const INTEGRATION_TEST_KEY_MASTER = '5DH7JdIDD4k6sF05Z3VPe2' +export const CDA_ACCESS_TOKEN_FOR_TEST = '5HAbGYZ6iZWiDXGxMtvsrW' +export const CDA_ACCESS_TOKEN_MASTER_FOR_TEST = '5DH7JdIDD4k6sF05Z3VPe2' export type TestContext = { sourceEnvironment: Environment @@ -90,7 +90,7 @@ export const updateCdaToken = async (cdaTokenId: string, space: Space, environme } const removeEnvironmentsFromKey = async (apiKey: ApiKey, environments: Environment[]) => { - if (apiKey.sys.id !== INTEGRATION_TEST_KEY) { + if (apiKey.sys.id !== CDA_ACCESS_TOKEN_FOR_TEST) { await apiKey.delete() } @@ -112,7 +112,7 @@ export const createEnvironments = async (testSpace: Space): Promise { @@ -28,7 +28,7 @@ describe('create command', () => { testContext = environmentsContext try { - cdaTokenWithOnlyMasterAccess = await testSpace.getApiKey(INTEGRATION_TEST_KEY_MASTER) + cdaTokenWithOnlyMasterAccess = await testSpace.getApiKey(CDA_ACCESS_TOKEN_MASTER_FOR_TEST) } catch (e) { cdaTokenWithOnlyMasterAccess = await createCdaToken(testSpace, ['master']) } @@ -36,7 +36,7 @@ describe('create command', () => { after(async () => { await Promise.all([ - cdaTokenWithOnlyMasterAccess.sys.id !== INTEGRATION_TEST_KEY_MASTER && cdaTokenWithOnlyMasterAccess.delete(), + cdaTokenWithOnlyMasterAccess.sys.id !== CDA_ACCESS_TOKEN_MASTER_FOR_TEST && cdaTokenWithOnlyMasterAccess.delete(), testContext.teardown(), ]) })