Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: integration test keys #495

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
10 changes: 7 additions & 3 deletions test/integration/commands/apply/index.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what case would we end up in the catch clause?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the key is not found, for example, if it was removed

}
})

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 }),
])
Expand Down
50 changes: 47 additions & 3 deletions test/integration/commands/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ApiKey> => {
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<void> => {
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<TestContext | undefined> => {
Expand All @@ -70,8 +111,11 @@ export const createEnvironments = async (testSpace: Space): Promise<TestContext
const sourceEnvironment = await testUtils.createTestEnvironment(testSpace, randomId + '_source_environment')
console.log('creating target environment...')
const targetEnvironment = await testUtils.createTestEnvironment(testSpace, randomId + '_target_environment')
console.log('creating API keys...')
const apiKey = await createCdaToken(testSpace, [targetEnvironment.sys.id, sourceEnvironment.sys.id])
console.log('obtaining API keys...')
const apiKey = await updateCdaToken(CDA_ACCESS_TOKEN_FOR_TEST, testSpace, [
targetEnvironment.sys.id,
sourceEnvironment.sys.id,
])
console.log('setup finished\n')

return {
Expand Down
13 changes: 10 additions & 3 deletions test/integration/commands/create/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from '@oclif/test'
import { ApiKey, Space, createClient } from 'contentful-management'
import fs from 'fs'
import { TestContext, createCdaToken, createEnvironments } from './../bootstrap'
import { CDA_ACCESS_TOKEN_MASTER_FOR_TEST, TestContext, createCdaToken, createEnvironments } from './../bootstrap'
import fancy from './../register-plugins'

describe('create command', () => {
Expand All @@ -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 }))
Expand Down