Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, 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'
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(INTEGRATION_TEST_KEY_MASTER)
} 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
Contributor 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 !== INTEGRATION_TEST_KEY_MASTER && 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 INTEGRATION_TEST_KEY = '5HAbGYZ6iZWiDXGxMtvsrW'
Copy link
Member

Choose a reason for hiding this comment

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

Can we name them something along the line of "access key id"?

export const INTEGRATION_TEST_KEY_MASTER = '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 !== 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<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(INTEGRATION_TEST_KEY, 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 { INTEGRATION_TEST_KEY_MASTER, 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(INTEGRATION_TEST_KEY_MASTER)
} catch (e) {
cdaTokenWithOnlyMasterAccess = await createCdaToken(testSpace, ['master'])
}
})

after(async () => {
await Promise.all([testContext.teardown(), cdaTokenWithOnlyMasterAccess.delete()])
await Promise.all([
cdaTokenWithOnlyMasterAccess.sys.id !== INTEGRATION_TEST_KEY_MASTER && cdaTokenWithOnlyMasterAccess.delete(),
testContext.teardown(),
])
})

afterEach(() => fs.promises.rm(changesetPath, { force: true }))
Expand Down