diff --git a/test/integration/commands/apply/index.test.ts b/test/integration/commands/apply/index.test.ts index 2c43e3e4..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, 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' @@ -69,13 +69,17 @@ 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(CDA_ACCESS_TOKEN_MASTER_FOR_TEST) + } catch (e) { + cdaTokenWithOnlyMasterAccess = await createCdaToken(testSpace, ['master']) + } }) after(async () => { await Promise.all([ testContext.teardown(), - 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 a36ff220..d3a071b0 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 CDA_ACCESS_TOKEN_FOR_TEST = '5HAbGYZ6iZWiDXGxMtvsrW' +export const CDA_ACCESS_TOKEN_MASTER_FOR_TEST = '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 !== CDA_ACCESS_TOKEN_FOR_TEST) { + 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,18 @@ describe('create command', () => { } testContext = environmentsContext - cdaTokenWithOnlyMasterAccess = await createCdaToken(testSpace, ['master']) + try { + cdaTokenWithOnlyMasterAccess = await testSpace.getApiKey(CDA_ACCESS_TOKEN_MASTER_FOR_TEST) + } catch (e) { + cdaTokenWithOnlyMasterAccess = await createCdaToken(testSpace, ['master']) + } }) after(async () => { - await Promise.all([testContext.teardown(), cdaTokenWithOnlyMasterAccess.delete()]) + await Promise.all([ + cdaTokenWithOnlyMasterAccess.sys.id !== CDA_ACCESS_TOKEN_MASTER_FOR_TEST && cdaTokenWithOnlyMasterAccess.delete(), + testContext.teardown(), + ]) }) afterEach(() => fs.promises.rm(changesetPath, { force: true }))