Skip to content

Commit a9eafb8

Browse files
refactor: dynamically create AM org stack instead of requiring static AM_API_KEY
The AM Org test suite now follows the same pattern as the main sanity suite: - Uses the existing authtoken from testSetup.testContext (already logged in) - Creates a fresh stack inside AM_ORG_UID via POST /v3/stacks at test start - Runs all 7 AM org asset scan tests against the dynamic stack - Deletes the AM stack in the after() hook No static AM_API_KEY needed in .env — removed from environment config. Fallback: skip the AM suite gracefully if AM_ORG_UID is not set. Verified locally: 30 passing, 2 pending (publish tests — expected, fresh stack has no environments; these will run in the full suite)
1 parent 7f7e44b commit a9eafb8

2 files changed

Lines changed: 81 additions & 10 deletions

File tree

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ fileignoreconfig:
44
- filename: test/sanity-check/sanity.js
55
checksum: f8b4b4b4492e04fd13338af9d99972807b4d61e2b0237a6c057d3f93d8c66d60
66
- filename: test/sanity-check/api/assetScanStatus-test.js
7-
checksum: a79aac966542050091b7a979235ada0c700296f91ba28a0ab2d9e8109f56d6c3
7+
checksum: c6e0485112f81ebf58ee4836a2dc1ed79e9c3395e9fa66968d0727ab4a9976db
88
version: "1.0"

test/sanity-check/api/assetScanStatus-test.js

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
* Uses process.env.API_KEY set at runtime.
1111
*
1212
* Part 2 – AM Org (AM_ORG_UID, DAM / Contentstack Assets + scan enabled)
13-
* Requires process.env.AM_API_KEY (a stack API key inside AM_ORG_UID).
14-
* All tests in Part 2 are skipped when AM_API_KEY is not set.
13+
* A stack is created dynamically inside AM_ORG_UID using the same authtoken
14+
* obtained during main setup. No static AM_API_KEY required.
15+
* All tests in Part 2 are skipped when AM_ORG_UID is not set.
1516
*
1617
* Bug surface these tests cover (per design doc):
1718
* § 3.1 - Scan status missing/leaking on fetch and list
@@ -714,35 +715,105 @@ describe('Asset Scan Status – api_version Header Isolation (§ 4.2)', () => {
714715

715716
// ============================================================================
716717
// Part 2 – AM Org (AM_ORG_UID – DAM / Contentstack Assets + scan enabled)
718+
//
719+
// The AM org stack is created dynamically using the same authtoken that the
720+
// main test suite already obtained — no static AM_API_KEY needed in .env.
721+
// The stack is deleted in the after() hook.
717722
// ============================================================================
718723

719724
describe('Asset Scan Status – AM Org (AM_ORG_UID, DAM + scan enabled)', function () {
720725
let amStack
721726
let amFreshAssetUid
722727
let amReplaceAssetUid
728+
let amStackApiKey = null
729+
let amStackName = null
723730

724-
before(function () {
725-
if (!process.env.AM_API_KEY) {
726-
console.log(' [scan-test] AM_API_KEY not set — skipping AM Org suite. ' +
727-
'Add AM_API_KEY=<stack key from AM_ORG_UID> to .env to enable.')
728-
this.skip()
731+
// Step 1: Create a stack dynamically inside AM_ORG_UID
732+
before(async function () {
733+
this.timeout(60000)
734+
735+
const amOrgUid = process.env.AM_ORG_UID
736+
if (!amOrgUid) {
737+
console.log(' [scan-test] AM_ORG_UID not set — skipping AM Org suite.')
738+
return this.skip()
739+
}
740+
741+
const authtoken = testSetup.testContext && testSetup.testContext.authtoken
742+
if (!authtoken) {
743+
console.log(' [scan-test] No authtoken in testContext — skipping AM Org suite.')
744+
return this.skip()
745+
}
746+
747+
const host = process.env.HOST || 'api.contentstack.io'
748+
const axios = (await import('axios')).default
749+
const stackName = `SDK_ScanAM_${Math.random().toString(36).substring(2, 7)}`
750+
751+
console.log(` [scan-test] Creating AM org test stack: ${stackName}...`)
752+
753+
try {
754+
const response = await axios.post(`https://${host}/v3/stacks`, {
755+
stack: {
756+
name: stackName,
757+
description: 'AM org asset scan status integration test stack',
758+
master_locale: 'en-us'
759+
}
760+
}, {
761+
headers: {
762+
authtoken,
763+
organization_uid: amOrgUid,
764+
'Content-Type': 'application/json'
765+
}
766+
})
767+
768+
amStackApiKey = response.data.stack.api_key
769+
amStackName = response.data.stack.name || stackName
770+
console.log(` [scan-test] AM stack created: ${amStackName} (${amStackApiKey})`)
771+
772+
// Wait for stack provisioning (same delay as main setup)
773+
await wait(5000)
774+
775+
amStack = buildStack(amStackApiKey)
776+
} catch (err) {
777+
const msg = (err.response && err.response.data && err.response.data.error_message) || err.message
778+
console.log(` [scan-test] AM stack creation failed: ${msg} — skipping AM Org suite.`)
779+
return this.skip()
729780
}
730-
amStack = buildStack(process.env.AM_API_KEY)
731781
})
732782

783+
// Step 2: Upload assets for tests (only runs if step 1 succeeded)
733784
before(async function () {
734785
this.timeout(60000)
786+
if (!amStack) return
735787
amFreshAssetUid = await uploadScanAsset(amStack, 'am-main')
736788
amReplaceAssetUid = await uploadScanAsset(amStack, 'am-replace')
737789
console.log(` [scan-test] AM freshAssetUid=${amFreshAssetUid} replaceAssetUid=${amReplaceAssetUid}`)
738790
})
739791

792+
// Cleanup: delete assets, then delete the dynamically created AM stack
740793
after(async function () {
794+
this.timeout(30000)
795+
741796
for (const uid of [amFreshAssetUid, amReplaceAssetUid]) {
742-
if (uid) {
797+
if (uid && amStack) {
743798
try { await amStack.asset(uid).delete() } catch (e) { /* ignore */ }
744799
}
745800
}
801+
802+
if (amStackApiKey) {
803+
try {
804+
const authtoken = testSetup.testContext && testSetup.testContext.authtoken
805+
if (authtoken) {
806+
const host = process.env.HOST || 'api.contentstack.io'
807+
const axios = (await import('axios')).default
808+
await axios.delete(`https://${host}/v3/stacks`, {
809+
headers: { api_key: amStackApiKey, authtoken }
810+
})
811+
console.log(` [scan-test] Deleted AM stack: ${amStackName}`)
812+
}
813+
} catch (e) {
814+
console.log(` [scan-test] AM stack deletion failed: ${e.message}`)
815+
}
816+
}
746817
})
747818

748819
// --------------------------------------------------------------------------

0 commit comments

Comments
 (0)