Skip to content

Commit c2d84d5

Browse files
author
naman-contentstack
committed
chore: add test cases for envs, extensions, locales,stacks, taxonomies and webhooks
1 parent a42eb01 commit c2d84d5

File tree

7 files changed

+1765
-0
lines changed

7 files changed

+1765
-0
lines changed

.talismanrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,16 @@ fileignoreconfig:
131131
checksum: 58165d06d92f55be8abb04c4ecc47df775a1c47f1cee529f1be5277187700f97
132132
- filename: packages/contentstack-import/test/unit/import/modules/locales.test.ts
133133
checksum: 011ec3efd7a29ed274f073c8678229eaef46f33e272e7e1db1206fa1a20383f0
134+
- filename: packages/contentstack-export/test/unit/export/modules/environments.test.ts
135+
checksum: 530573c4c92387b755ca1b4eef88ae8bb2ae076be9a726bba7b67a525cba23e9
136+
- filename: packages/contentstack-export/test/unit/export/modules/extensions.test.ts
137+
checksum: 857978a21ea981183254245f6b3cb5f51778d68fc726ddb26005ac96c706650f
138+
- filename: packages/contentstack-export/test/unit/export/modules/webhooks.test.ts
139+
checksum: 2e2d75281a57f873fb7f5fff0e5a9e863b631efd2fd92c4d2c81d9c8aeb3e252
140+
- filename: packages/contentstack-export/test/unit/export/modules/locales.test.ts
141+
checksum: 93bdd99ee566fd38545b38a8b528947af1d42a31908aca85e2cb221e39a5b6cc
142+
- filename: packages/contentstack-export/test/unit/export/modules/stack.test.ts
143+
checksum: bb0f20845d85fd56197f1a8c67b8f71c57dcd1836ed9cfd86d1f49f41e84d3a0
144+
- filename: packages/contentstack-export/test/unit/export/modules/taxonomies.test.ts
145+
checksum: 621c1de129488b6a0372a91056ebb84353bcc642ce06de59e3852cfee8d0ce49
134146
version: "1.0"
Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
import { expect } from 'chai';
2+
import sinon from 'sinon';
3+
import { FsUtility } from '@contentstack/cli-utilities';
4+
import ExportEnvironments from '../../../../src/export/modules/environments';
5+
import ExportConfig from '../../../../src/types/export-config';
6+
7+
describe('ExportEnvironments', () => {
8+
let exportEnvironments: any;
9+
let mockStackClient: any;
10+
let mockExportConfig: ExportConfig;
11+
12+
beforeEach(() => {
13+
mockStackClient = {
14+
environment: sinon.stub().returns({
15+
query: sinon.stub().returns({
16+
find: sinon.stub().resolves({
17+
items: [
18+
{ uid: 'env-1', name: 'Production' },
19+
{ uid: 'env-2', name: 'Development' }
20+
],
21+
count: 2
22+
})
23+
})
24+
})
25+
};
26+
27+
mockExportConfig = {
28+
contentVersion: 1,
29+
versioning: false,
30+
host: 'https://api.contentstack.io',
31+
developerHubUrls: {},
32+
apiKey: 'test-api-key',
33+
exportDir: '/test/export',
34+
data: '/test/data',
35+
branchName: '',
36+
context: {
37+
command: 'cm:stacks:export',
38+
module: 'environments',
39+
userId: 'user-123',
40+
41+
sessionId: 'session-123',
42+
apiKey: 'test-api-key',
43+
orgId: 'org-123',
44+
authenticationMethod: 'Basic Auth'
45+
},
46+
cliLogsPath: '/test/logs',
47+
forceStopMarketplaceAppsPrompt: false,
48+
master_locale: { code: 'en-us' },
49+
region: {
50+
name: 'us',
51+
cma: 'https://api.contentstack.io',
52+
cda: 'https://cdn.contentstack.io',
53+
uiHost: 'https://app.contentstack.com'
54+
},
55+
skipStackSettings: false,
56+
skipDependencies: false,
57+
languagesCode: ['en'],
58+
apis: {},
59+
preserveStackVersion: false,
60+
personalizationEnabled: false,
61+
fetchConcurrency: 5,
62+
writeConcurrency: 5,
63+
developerHubBaseUrl: '',
64+
marketplaceAppEncryptionKey: '',
65+
onlyTSModules: [],
66+
modules: {
67+
types: ['environments'],
68+
environments: {
69+
dirName: 'environments',
70+
fileName: 'environments.json',
71+
limit: 100,
72+
invalidKeys: []
73+
}
74+
}
75+
} as any;
76+
77+
exportEnvironments = new ExportEnvironments({
78+
exportConfig: mockExportConfig,
79+
stackAPIClient: mockStackClient,
80+
moduleName: 'environments'
81+
});
82+
83+
sinon.stub(FsUtility.prototype, 'writeFile').resolves();
84+
sinon.stub(FsUtility.prototype, 'makeDirectory').resolves();
85+
});
86+
87+
afterEach(() => {
88+
sinon.restore();
89+
});
90+
91+
describe('Constructor', () => {
92+
it('should initialize with correct parameters', () => {
93+
expect(exportEnvironments).to.be.instanceOf(ExportEnvironments);
94+
});
95+
96+
it('should initialize environments object', () => {
97+
expect(exportEnvironments.environments).to.be.an('object');
98+
});
99+
100+
it('should set context module to environments', () => {
101+
expect(exportEnvironments.exportConfig.context.module).to.equal('environments');
102+
});
103+
});
104+
105+
describe('getEnvironments() method', () => {
106+
it('should fetch and process environments correctly', async () => {
107+
const environments = [
108+
{ uid: 'env-1', name: 'Production', ACL: 'test' },
109+
{ uid: 'env-2', name: 'Development', ACL: 'test' }
110+
];
111+
112+
mockStackClient.environment.returns({
113+
query: sinon.stub().returns({
114+
find: sinon.stub().resolves({
115+
items: environments,
116+
count: 2
117+
})
118+
})
119+
});
120+
121+
await exportEnvironments.getEnvironments();
122+
123+
// Verify environments were processed
124+
expect(Object.keys(exportEnvironments.environments).length).to.equal(2);
125+
expect(exportEnvironments.environments['env-1']).to.exist;
126+
expect(exportEnvironments.environments['env-1'].name).to.equal('Production');
127+
// Verify ACL was removed
128+
expect(exportEnvironments.environments['env-1'].ACL).to.be.undefined;
129+
});
130+
131+
it('should call getEnvironments recursively when more environments exist', async () => {
132+
let callCount = 0;
133+
mockStackClient.environment.returns({
134+
query: sinon.stub().returns({
135+
find: sinon.stub().callsFake(() => {
136+
callCount++;
137+
if (callCount === 1) {
138+
return Promise.resolve({
139+
items: Array(100).fill({ uid: 'test', name: 'Test' }),
140+
count: 150
141+
});
142+
} else {
143+
return Promise.resolve({
144+
items: Array(50).fill({ uid: 'test2', name: 'Test2' }),
145+
count: 150
146+
});
147+
}
148+
})
149+
})
150+
});
151+
152+
await exportEnvironments.getEnvironments();
153+
154+
// Verify multiple calls were made for recursive fetching
155+
expect(callCount).to.be.greaterThan(1);
156+
});
157+
158+
it('should handle API errors gracefully', async () => {
159+
mockStackClient.environment.returns({
160+
query: sinon.stub().returns({
161+
find: sinon.stub().rejects(new Error('API Error'))
162+
})
163+
});
164+
165+
await exportEnvironments.getEnvironments();
166+
167+
// Verify method completes without throwing
168+
expect(exportEnvironments.environments).to.exist;
169+
});
170+
171+
it('should handle no items response and not process environments', async () => {
172+
mockStackClient.environment.returns({
173+
query: sinon.stub().returns({
174+
find: sinon.stub().resolves({
175+
items: [],
176+
count: 0
177+
})
178+
})
179+
});
180+
181+
const initialCount = Object.keys(exportEnvironments.environments).length;
182+
await exportEnvironments.getEnvironments();
183+
184+
// Verify no new environments were added
185+
expect(Object.keys(exportEnvironments.environments).length).to.equal(initialCount);
186+
});
187+
188+
it('should handle empty environments array gracefully', async () => {
189+
mockStackClient.environment.returns({
190+
query: sinon.stub().returns({
191+
find: sinon.stub().resolves({
192+
items: null,
193+
count: 0
194+
})
195+
})
196+
});
197+
198+
const initialCount = Object.keys(exportEnvironments.environments).length;
199+
await exportEnvironments.getEnvironments();
200+
201+
// Verify no processing occurred with null items
202+
expect(Object.keys(exportEnvironments.environments).length).to.equal(initialCount);
203+
});
204+
});
205+
206+
describe('start() method', () => {
207+
it('should complete full export flow and write files', async () => {
208+
const writeFileStub = FsUtility.prototype.writeFile as sinon.SinonStub;
209+
210+
const environments = [
211+
{ uid: 'env-1', name: 'Production' },
212+
{ uid: 'env-2', name: 'Development' }
213+
];
214+
215+
mockStackClient.environment.returns({
216+
query: sinon.stub().returns({
217+
find: sinon.stub().resolves({
218+
items: environments,
219+
count: 2
220+
})
221+
})
222+
});
223+
224+
await exportEnvironments.start();
225+
226+
// Verify environments were processed
227+
expect(Object.keys(exportEnvironments.environments).length).to.equal(2);
228+
expect(exportEnvironments.environments['env-1']).to.exist;
229+
expect(exportEnvironments.environments['env-2']).to.exist;
230+
// Verify file was written
231+
expect(writeFileStub.called).to.be.true;
232+
});
233+
234+
it('should handle empty environments and log NOT_FOUND', async () => {
235+
const writeFileStub = FsUtility.prototype.writeFile as sinon.SinonStub;
236+
237+
mockStackClient.environment.returns({
238+
query: sinon.stub().returns({
239+
find: sinon.stub().resolves({
240+
items: [],
241+
count: 0
242+
})
243+
})
244+
});
245+
246+
exportEnvironments.environments = {};
247+
await exportEnvironments.start();
248+
249+
// Verify writeFile was NOT called when environments are empty
250+
expect(writeFileStub.called).to.be.false;
251+
});
252+
});
253+
254+
describe('sanitizeAttribs() method', () => {
255+
it('should sanitize environment attributes and remove ACL', () => {
256+
const environments = [
257+
{ uid: 'env-1', name: 'Production', ACL: 'remove' },
258+
{ uid: 'env-2', name: 'Development', ACL: 'remove' }
259+
];
260+
261+
exportEnvironments.sanitizeAttribs(environments);
262+
263+
expect(exportEnvironments.environments['env-1'].ACL).to.be.undefined;
264+
expect(exportEnvironments.environments['env-1'].name).to.equal('Production');
265+
});
266+
267+
it('should handle environments without name field', () => {
268+
const environments = [
269+
{ uid: 'env-1', ACL: 'remove' }
270+
];
271+
272+
exportEnvironments.sanitizeAttribs(environments);
273+
274+
expect(exportEnvironments.environments['env-1']).to.exist;
275+
expect(exportEnvironments.environments['env-1'].ACL).to.be.undefined;
276+
});
277+
278+
it('should handle empty environments array', () => {
279+
const environments: any[] = [];
280+
281+
exportEnvironments.sanitizeAttribs(environments);
282+
283+
expect(Object.keys(exportEnvironments.environments).length).to.equal(0);
284+
});
285+
});
286+
});
287+

0 commit comments

Comments
 (0)