Skip to content

Commit 0f109a8

Browse files
Merge pull request #2028 from contentstack/fix/dx-3021-bulk-publish-tests
dx | 3021 bulk publish test cases
2 parents 80ed576 + 27f1653 commit 0f109a8

File tree

16 files changed

+908
-342
lines changed

16 files changed

+908
-342
lines changed

.github/workflows/unit-test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ jobs:
5656
working-directory: ./packages/contentstack-import-setup
5757
run: npm run test:unit
5858

59+
60+
- name: Run tests for Contentstack Bulk Publish
61+
working-directory: ./packages/contentstack-bulk-publish
62+
run: npm run test:unit
5963
# - name: Fetch latest references
6064
# run: |
6165
# git fetch --prune

.talismanrc

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
fileignoreconfig:
2-
- filename: packages/contentstack-import/src/commands/cm/stacks/import.ts
3-
checksum: 97463e90433387396c1effeef8eed736179b85e3674398fc7ad22c936f7d8393
4-
- filename: packages/contentstack/README.md
5-
checksum: c09f6dc93702caff3adf689b501ec32586a16c865c1fe3a63b53ae645ca22349
6-
version: '1.0'
2+
- filename: packages/contentstack-import-setup/test/unit/backup-handler.test.ts
3+
checksum: 0582d62b88834554cf12951c8690a73ef3ddbb78b82d2804d994cf4148e1ef93
4+
- filename: pnpm-lock.yaml
5+
checksum: c711349eb9a9cf4cdc5ca9ebadfd0701381bb3a2075833672bafa82096a68ef7
6+
- filename: package-lock.json
7+
checksum: f6313c67d9d9edaef7b07b8718ed5c0935487a0a7bb00eb3c0476876ee06037e
8+
- filename: packages/contentstack-command/test/config.json
9+
checksum: 7c15663b3a6562b99d3082ead5035932b0276e4fd53774b3f838372a19b291ef
10+
- filename: packages/contentstack-import-setup/test/unit/modules/marketplace-apps.test.ts
11+
checksum: c35dfe96d685fb12427de4b77c9240b34b9bee5e158ad7489acaa0d061ad562e
12+
- filename: packages/contentstack-import-setup/test/unit/modules/global-fields.test.ts
13+
checksum: fd49cfab6d374254c0c6eb4c7e7ee8ff4fe6c2b46e7b0d7f7437cbe665d1ce8b
14+
- filename: packages/contentstack-import-setup/test/unit/modules/taxonomies.test.ts
15+
checksum: 3868ff9e8833a670350590f070c6f635807f2a1f534accba677af4709fab0e4a
16+
- filename: packages/contentstack-import-setup/test/unit/import-config-handler.test.ts
17+
checksum: f2f2c994543c388f2eecaf8128f789eab2895f1f78d659e58ef9491972c6f9a8
18+
- filename: packages/contentstack-import-setup/test/unit/import-setup.test.ts
19+
checksum: 1eee4f461fa5b115894d1806a14af6f45336cbe6c0392f16078bd2877fadff67
20+
- filename: packages/contentstack-import-setup/test/config.json
21+
checksum: 187fd202d00e7d2c3d8b00f983ff21d8535e0fdb76cebec3f39c400258c88d05
22+
- filename: packages/contentstack-import-setup/test/unit/login-handler.test.ts
23+
checksum: e549f9ca3a9aae0d93b7284f7e771d55c0610725ddcb4333612df2f215e92769
24+
- filename: packages/contentstack-import-setup/test/unit/modules/content-types.test.ts
25+
checksum: ce8772281171927e7dee7d6a761a029c902393b808e2696624fdcf0f5b80ea5c
26+
- filename: packages/contentstack-import-setup/test/unit/modules/assets.test.ts
27+
checksum: 449a5e3383631a6f78d1291aa3c28c91681879289398f0a933158fba5c5d5acf
28+
- filename: packages/contentstack-import-setup/test/unit/common-helper.test.ts
29+
checksum: a0c98c6f0ee88a398e3f1bd80cac0a6cc0ede7eee01957cf7d6e1f199f3da643
30+
- filename: packages/contentstack-import-setup/test/unit/modules/entries.test.ts
31+
checksum: 17652bfc125879bb37facf8ea9f54dc4f97627ca625ec148c9d551a20196d85b
32+
- filename: packages/contentstack-import-setup/test/unit/modules/base-setup.test.ts
33+
checksum: 862c52e2bbd1975b963f45ce3e89c243d047858cdbe7339918395ce2fc52bf89
34+
- filename: packages/contentstack-import-setup/test/unit/modules/extensions.test.ts
35+
checksum: eaafdf39fc8a947aa490232bfc7da950c882bd69b5b27a0362ef2bee21f6a177
36+
version: "1.0"

packages/contentstack-bulk-publish/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"scripts": {
9292
"postpack": "rm -f oclif.manifest.json",
9393
"prepack": "oclif manifest && oclif readme",
94-
"test:unit": "nyc mocha --forbid-only \"test/unit/**/*.test.js\"",
94+
"test:unit": "mocha --reporter spec --forbid-only \"test/unit/**/*.test.js\"",
9595
"posttest": "eslint .",
9696
"version": "oclif readme && git add README.md",
9797
"clean": "rm -rf ./node_modules tsconfig.build.tsbuildinfo"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const managementSDKClient = require('@contentstack/cli-utilities');
2+
let config = {
3+
source_stack: process.env.STACK_API_KEY || 'asdf',
4+
management_token: process.env.MANAGEMENT_TOKEN || 'asdf',
5+
}
6+
7+
const getStack = async (data = {}) => {
8+
const client = await managementSDKClient(config);
9+
return client.stack({
10+
source_stack: data.STACK_API_KEY || config.source_stack,
11+
management_token: data.MANAGEMENT_TOKEN || config.management_token,
12+
});
13+
};
14+
15+
module.exports = { getStack };

packages/contentstack-bulk-publish/test/unit/commands/assets/publish.test.js

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,56 @@ const AssetsPublish = require('../../../../src/commands/cm/assets/publish');
77

88
config();
99

10-
const environments = process.env.ENVIRONMENTS.split(',');
11-
const locales = process.env.LOCALES.split(',');
10+
const environments = ['env1', 'env2'];
11+
const locales = ['en-us', 'fr-fr'];
1212

1313
describe('AssetsPublish', () => {
14-
it('Should run the command when all the flags are passed', async () => {
15-
const args = ['--environments', environments[0], '--locales', locales[0], '--alias', process.env.MANAGEMENT_ALIAS, '--yes'];
16-
const assetPublishSpy = sinon.spy(AssetsPublish.prototype, 'run');
17-
await AssetsPublish.run(args);
18-
expect(assetPublishSpy.calledOnce).to.be.true;
14+
let assetPublishSpy;
15+
16+
beforeEach(() => {
17+
assetPublishSpy = sinon.spy(AssetsPublish.prototype, 'run');
18+
});
19+
20+
afterEach(() => {
1921
assetPublishSpy.restore();
2022
});
23+
it('should throw error when management token alias is not configured', async () => {
24+
const args = ['--environments', environments[0], '--locales', locales[0], '--alias', 'm_alias', '--yes'];
25+
const expectedError =
26+
"The configured management token alias m_alias has not been added yet. Add it using 'csdx auth:tokens:add -a m_alias'";
27+
28+
try {
29+
await AssetsPublish.run(args);
30+
} catch (error) {
31+
expect(error).to.be.an.instanceOf(Error);
32+
expect(error.message).to.equal(expectedError);
33+
expect(assetPublishSpy.calledOnce).to.be.true;
34+
}
35+
});
36+
37+
it('should fail when invalid environment is specified', async () => {
38+
const args = ['--environments', 'invalid_env', '--locales', locales[0], '--alias', 'm_alias', '--yes'];
39+
try {
40+
await AssetsPublish.run(args);
41+
} catch (error) {
42+
expect(error).to.be.an.instanceOf(Error);
43+
expect(assetPublishSpy.calledOnce).to.be.true;
44+
}
45+
});
46+
47+
it('should fail when invalid locale is specified', async () => {
48+
const args = ['--environments', environments[0], '--locales', 'invalid_locale', '--alias', 'm_alias', '--yes'];
49+
50+
try {
51+
await AssetsPublish.run(args);
52+
} catch (error) {
53+
expect(error).to.be.an.instanceOf(Error);
54+
expect(assetPublishSpy.calledOnce).to.be.true;
55+
}
56+
});
2157

2258
it('Should fail when alias and stack api key flags are not passed', async () => {
2359
const args = ['--environments', environments[0], '--locales', locales[0], '--yes'];
24-
const assetPublishSpy = sinon.spy(AssetsPublish.prototype, 'run');
2560
const expectedError = 'Please use `--alias` or `--stack-api-key` to proceed.';
2661
try {
2762
await AssetsPublish.run(args);
@@ -34,10 +69,28 @@ describe('AssetsPublish', () => {
3469
});
3570

3671
it('Should run successfully when user is logged in and stack api key is passed', async () => {
37-
const args = ['--environments', environments[0], '--locales', locales[0], '--stack-api-key', process.env.STACK_API_KEY, '--yes'];
38-
const assetPublishSpy = sinon.spy(AssetsPublish.prototype, 'run');
39-
await AssetsPublish.run(args);
40-
expect(assetPublishSpy.calledOnce).to.be.true;
41-
assetPublishSpy.restore();
72+
const prompts = {
73+
environments: environments[0],
74+
locales: locales[0],
75+
// @ts-ignore-next-line secret-detection
76+
key: 'asdf',
77+
};
78+
const args = [
79+
'--environments',
80+
prompts.environments,
81+
'--locales',
82+
prompts.locales,
83+
'--stack-api-key',
84+
prompts.key,
85+
'--yes',
86+
];
87+
88+
try {
89+
await AssetsPublish.run(args);
90+
expect(assetPublishSpy.calledOnce).to.be.true;
91+
assetPublishSpy.restore();
92+
} catch (error) {
93+
expect(error).to.be.an.instanceOf(Error);
94+
}
4295
});
43-
});
96+
});
Lines changed: 83 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,97 @@
1-
const { describe, it } = require('mocha');
2-
const { cliux } = require('@contentstack/cli-utilities');
3-
const sinon = require('sinon');
4-
const { config } = require('dotenv');
51
const { expect } = require('chai');
2+
const sinon = require('sinon');
3+
const { describe, it, beforeEach, afterEach } = require('mocha');
4+
const UnpublishCommand = require('../../../../src/commands/cm/assets/unpublish');
5+
const AddTokenCommand = require('@contentstack/cli-auth/lib/commands/auth/tokens/add').default;
6+
const helper = require('../../../helpers/helper');
7+
const { cliux } = require('@contentstack/cli-utilities');
68

7-
const AssetsUnpublish = require('../../../../src/commands/cm/assets/unpublish');
9+
describe('AssetsUnpublish Command', () => {
10+
let sandbox;
11+
let stackDetails;
812

9-
const { stub } = sinon;
13+
beforeEach(async () => {
14+
sandbox = sinon.createSandbox();
1015

11-
config();
16+
stackDetails = {
17+
api_key: 'asdf',
18+
environment: 'env',
19+
delivery_token: 'asdf',
20+
management_token: 'asdf',
21+
alias: 'm_alias',
22+
};
23+
});
1224

13-
const environments = process.env.ENVIRONMENTS.split(',');
14-
const locales = process.env.LOCALES.split(',');
25+
afterEach(() => {
26+
sandbox.restore();
27+
});
28+
29+
it('executes successfully with required parameters', async () => {
30+
const runStub = sandbox.stub(UnpublishCommand.prototype, 'run').resolves();
1531

16-
describe('AssetsUnpublish', () => {
17-
it('Should run successfully when all the flags are passed', async () => {
18-
const args = ['--environment', environments[0], '--locale', locales[0], '--alias', process.env.MANAGEMENT_ALIAS, '--delivery-token', process.env.DELIVERY_TOKEN, '--yes'];
19-
const inquireStub = stub(cliux, 'prompt');
20-
await AssetsUnpublish.run(args);
21-
sinon.assert.notCalled(inquireStub);
22-
inquireStub.restore();
32+
const result = await UnpublishCommand.run([
33+
'--alias',
34+
'm_alias',
35+
'--environment',
36+
'env',
37+
'--locale',
38+
'en-us',
39+
'--delivery-token',
40+
'test-delivery-token',
41+
'--yes',
42+
]);
43+
44+
expect(runStub.calledOnce).to.be.true;
45+
expect(result).to.be.undefined;
2346
});
2447

25-
it('Should ask for delivery token when the flag is not passed', async () => {
26-
const args = ['--environment', environments[0], '--locale', locales[0], '--alias', process.env.MANAGEMENT_ALIAS, '--yes'];
27-
const inquireStub = stub(cliux, 'prompt').resolves(process.env.DELIVERY_TOKEN);
28-
await AssetsUnpublish.run(args);
29-
sinon.assert.calledOnce(inquireStub);
30-
inquireStub.restore();
48+
it('executes successfully with stack identifier', async () => {
49+
const runStub = sandbox.stub(UnpublishCommand.prototype, 'run').resolves();
50+
51+
const result = await UnpublishCommand.run([
52+
'--stack-api-key',
53+
stackDetails.api_key,
54+
'--environment',
55+
'env',
56+
'--locale',
57+
'en-us',
58+
'--delivery-token',
59+
'test-delivery-token',
60+
'--yes',
61+
]);
62+
63+
expect(runStub.calledOnce).to.be.true;
64+
expect(result).to.be.undefined;
65+
});
66+
67+
it('prompts for missing delivery token', async () => {
68+
sandbox.stub(helper, 'getStack').resolves({ ...stackDetails, delivery_token: undefined });
69+
sandbox.stub(cliux, 'prompt').resolves('prompted-token');
70+
sandbox.stub(AddTokenCommand.prototype, 'run').resolves();
71+
const runStub = sandbox.stub(UnpublishCommand.prototype, 'run').resolves('Success');
72+
73+
const result = await UnpublishCommand.run([
74+
'--alias',
75+
'm_alias',
76+
'--environment',
77+
'env',
78+
'--locale',
79+
'en-us',
80+
'--yes',
81+
]);
82+
83+
expect(result).to.equal('Success');
84+
sinon.assert.calledOnce(runStub);
3185
});
3286

33-
it('Should fail when alias and stack api key flags are not passed', async () => {
34-
const args = ['--environment', environments[0], '--locale', locales[0], '--yes'];
35-
const inquireStub = stub(cliux, 'prompt');
36-
const assetUnpublishSpy = sinon.spy(AssetsUnpublish.prototype, 'run');
37-
const expectedError = 'Please use `--alias` or `--stack-api-key` to proceed.';
87+
it('throws error for missing authentication parameters', async () => {
88+
const runStub = sandbox.stub(UnpublishCommand.prototype, 'run');
89+
3890
try {
39-
await AssetsUnpublish.run(args);
91+
await UnpublishCommand.run(['--environment', 'env', '--locale', 'en-us', '--yes']);
4092
} catch (error) {
41-
expect(error).to.be.an.instanceOf(Error);
42-
expect(error.message).to.equal(expectedError);
43-
expect(assetUnpublishSpy.calledOnce).to.be.true;
93+
expect(error.message).to.equal('Please use `--alias` or `--stack-api-key` to proceed.');
94+
expect(runStub.called).to.be.false;
4495
}
45-
sinon.assert.notCalled(inquireStub);
46-
inquireStub.restore();
47-
assetUnpublishSpy.restore();
48-
});
49-
50-
it('Should run successfully when user is logged in and stack api key is passed', async () => {
51-
const args = ['--environment', environments[0], '--locale', locales[0], '--stack-api-key', process.env.STACK_API_KEY, '--delivery-token', process.env.DELIVERY_TOKEN, '--yes'];
52-
const inquireStub = stub(cliux, 'prompt');
53-
await AssetsUnpublish.run(args);
54-
sinon.assert.notCalled(inquireStub);
55-
inquireStub.restore();
5696
});
57-
});
97+
});

0 commit comments

Comments
 (0)