Skip to content

Commit 4bde38d

Browse files
authored
Merge pull request #2170 from contentstack/tests/custom-roles
Fix: Added test cases for Custom roles
2 parents 5749676 + 2f63a00 commit 4bde38d

File tree

8 files changed

+2164
-1073
lines changed

8 files changed

+2164
-1073
lines changed

.talismanrc

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
fileignoreconfig:
22
- filename: package-lock.json
3-
checksum: 39e71326fd3b883372e1153b8c2cf6b408fa6afe046cbe1bcf7e117a3d5f198d
3+
checksum: a4def28c6ccba29b6e3ac810ebd15bd391a1f8511e5456168b2511305973dacd
44
- filename: pnpm-lock.yaml
5-
checksum: 13c2f106f912e03bd2b25fc5305893d3f439416209d170f9846d254d98b5bcbb
5+
checksum: 75f25e1c8cd341aa77e375866e4d0c33dd3c38845db2fb3420eb535128268168
66
- filename: packages/contentstack-import-setup/test/unit/backup-handler.test.ts
77
checksum: 0582d62b88834554cf12951c8690a73ef3ddbb78b82d2804d994cf4148e1ef93
88
- filename: packages/contentstack-import-setup/test/config.json
@@ -109,16 +109,6 @@ fileignoreconfig:
109109
checksum: 34bdde6f85e8b60ebc73e627b315ec3886e5577102fca04c3e20c463c42eb681
110110
- filename: packages/contentstack-audit/src/modules/content-types.ts
111111
checksum: ddf7b08e6a80af09c6a7019a637c26089fb76572c7c3d079a8af244b02985f16
112-
- filename: packages/contentstack-import/test/unit/import/modules/base-class.test.ts
113-
checksum: 850383016629ed840bf12c8bea5b7640230a6e4f6af03d958d2bcbdcc740945d
114112
- filename: packages/contentstack-import/test/unit/commands/cm/stacks/import.test.ts
115-
checksum: 183feddf5ceee765a228c9c3d2759df459722fac20edce3c2fe957a7a28d790a
116-
- filename: packages/contentstack-import/test/unit/import/modules/content-types.test.ts
117-
checksum: 61fca8cda7873430d2a926c1b6d35d67f8707e0d68855e6c13c55e4be73a0ce3
118-
- filename: packages/contentstack-import/test/unit/import/modules/assets.test.ts
119-
checksum: 69d762d41c9b27f1563d2595549e581d3fcab723af3321d9db065468d0c60b79
120-
- filename: packages/contentstack-import/test/unit/import/modules/global-fields.test.ts
121-
checksum: 0de6c9c9f778d770a04da0c9927820090eb6478d9499f12a3329b580a1dc115b
122-
- filename: packages/contentstack-import/test/unit/import/modules/workflows.test.ts
123-
checksum: 42db46379556a837866dfa39bcd8cfc215a4be1b421c61a6508e8c3b958d39f9
113+
checksum: b11e57f1b824d405f86438e9e7c59183f8c59b66b42d8d16dbeaf76195a30548
124114
version: "1.0"

packages/contentstack-import/test/unit/commands/cm/stacks/import.test.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('ImportCommand', () => {
2828
};
2929

3030
mockFlags = {
31-
'stack-api-key': 'test-api-key',
31+
'stack-api-key': 'test',
3232
'data-dir': '/test/data',
3333
'alias': 'test-alias',
3434
'module': 'entries',
@@ -43,7 +43,8 @@ describe('ImportCommand', () => {
4343
};
4444

4545
mockImportConfig = {
46-
apiKey: 'test-api-key',
46+
// @ts-ignore-next-line secret-detection
47+
apiKey: 'test',
4748
contentDir: '/test/data',
4849
data: '/test/data',
4950
contentVersion: 1,
@@ -56,7 +57,8 @@ describe('ImportCommand', () => {
5657
userId: 'user-123',
5758
email: 'test@example.com',
5859
sessionId: 'test-session-123',
59-
apiKey: 'test-api-key',
60+
// @ts-ignore-next-line secret-detection
61+
apiKey: 'test',
6062
orgId: 'org-123',
6163
authenticationMethod: 'Basic Auth',
6264
},
@@ -210,20 +212,20 @@ describe('ImportCommand', () => {
210212
});
211213

212214
it('should create context with all required properties', () => {
213-
const context = command['createImportContext']('test-api-key', 'Basic Auth');
215+
const context = command['createImportContext']('test', 'Basic Auth');
214216

215217
expect(context).to.have.property('command', 'cm:stacks:import');
216218
expect(context).to.have.property('module', '');
217219
expect(context).to.have.property('userId', 'user-123');
218220
expect(context).to.have.property('email', 'test@example.com');
219221
expect(context).to.have.property('sessionId', 'test-session-123');
220-
expect(context).to.have.property('apiKey', 'test-api-key');
222+
expect(context).to.have.property('apiKey', 'test');
221223
expect(context).to.have.property('orgId', 'org-123');
222224
expect(context).to.have.property('authenticationMethod', 'Basic Auth');
223225
});
224226

225227
it('should use default authentication method when not provided', () => {
226-
const context = command['createImportContext']('test-api-key');
228+
const context = command['createImportContext']('test');
227229

228230
expect(context.authenticationMethod).to.equal('Basic Auth');
229231
});
@@ -232,7 +234,7 @@ describe('ImportCommand', () => {
232234
configHandlerStub.reset();
233235
configHandlerStub.returns(undefined);
234236

235-
const context = command['createImportContext']('test-api-key', 'Management Token');
237+
const context = command['createImportContext']('test', 'Management Token');
236238

237239
expect(context.userId).to.equal('');
238240
expect(context.email).to.equal('');
@@ -241,7 +243,7 @@ describe('ImportCommand', () => {
241243
});
242244

243245
it('should use context command when available', () => {
244-
const context = command['createImportContext']('test-api-key');
246+
const context = command['createImportContext']('test');
245247

246248
expect(context.command).to.equal('cm:stacks:import');
247249
});
@@ -270,7 +272,7 @@ describe('ImportCommand', () => {
270272

271273
// Mock the interactive functions
272274
sinon.stub(interactiveModule, 'askContentDir').resolves('/test/content');
273-
sinon.stub(interactiveModule, 'askAPIKey').resolves('test-api-key');
275+
sinon.stub(interactiveModule, 'askAPIKey').resolves('test');
274276

275277
// Mock log methods by replacing them on the log object
276278
logSuccessStub = sinon.stub().callsFake(() => {});
@@ -374,7 +376,7 @@ describe('ImportCommand', () => {
374376
ModuleImporterStub = sinon.stub().returns(mockModuleImporter);
375377

376378
sinon.stub(interactiveModule, 'askContentDir').resolves('/test/content');
377-
sinon.stub(interactiveModule, 'askAPIKey').resolves('test-api-key');
379+
sinon.stub(interactiveModule, 'askAPIKey').resolves('test');
378380

379381
logSuccessStub = sinon.stub().callsFake(() => {});
380382
logInfoStub = sinon.stub().callsFake(() => {});
@@ -504,23 +506,23 @@ describe('ImportCommand', () => {
504506
it('should handle undefined context', () => {
505507
(command as any).context = undefined;
506508

507-
const context = command['createImportContext']('test-api-key');
509+
const context = command['createImportContext']('test');
508510

509511
expect(context.command).to.equal('cm:stacks:import');
510512
});
511513

512514
it('should handle context without info', () => {
513515
(command as any).context = { sessionId: 'test-session' };
514516

515-
const context = command['createImportContext']('test-api-key');
517+
const context = command['createImportContext']('test');
516518

517519
expect(context.command).to.equal('cm:stacks:import');
518520
});
519521

520522
it('should handle context without sessionId', () => {
521523
(command as any).context = { info: { command: 'test' } };
522524

523-
const context = command['createImportContext']('test-api-key');
525+
const context = command['createImportContext']('test');
524526

525527
expect(context.sessionId).to.be.undefined;
526528
});
@@ -529,7 +531,7 @@ describe('ImportCommand', () => {
529531
configHandlerStub.reset();
530532
configHandlerStub.returns(undefined);
531533

532-
const context = command['createImportContext']('test-api-key');
534+
const context = command['createImportContext']('test');
533535

534536
expect(context.userId).to.equal('');
535537
expect(context.email).to.equal('');

packages/contentstack-import/test/unit/import/modules/assets.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('ImportAssets', () => {
3434
};
3535

3636
mockImportConfig = {
37-
apiKey: 'test-api-key',
37+
apiKey: 'test',
3838
contentDir: '/test/content',
3939
data: '/test/content',
4040
contentVersion: 1,
@@ -47,7 +47,7 @@ describe('ImportAssets', () => {
4747
userId: 'user-123',
4848
email: 'test@example.com',
4949
sessionId: 'session-123',
50-
apiKey: 'test-api-key',
50+
apiKey: 'test',
5151
orgId: 'org-123',
5252
authenticationMethod: 'Basic Auth'
5353
},

packages/contentstack-import/test/unit/import/modules/base-class.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('BaseClass', () => {
8686
};
8787

8888
mockImportConfig = {
89-
apiKey: 'test-api-key',
89+
apiKey: 'test',
9090
contentDir: '/test/content',
9191
data: '/test/content',
9292
contentVersion: 1,
@@ -99,7 +99,7 @@ describe('BaseClass', () => {
9999
userId: 'user-123',
100100
email: 'test@example.com',
101101
sessionId: 'session-123',
102-
apiKey: 'test-api-key',
102+
apiKey: 'test',
103103
orgId: 'org-123',
104104
authenticationMethod: 'Management Token'
105105
},

packages/contentstack-import/test/unit/import/modules/content-types.test.ts

Lines changed: 182 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('ImportContentTypes', () => {
4747
};
4848

4949
mockImportConfig = {
50-
apiKey: 'test-api-key',
50+
apiKey: 'test',
5151
contentDir: '/test/content',
5252
data: '/test/content',
5353
contentVersion: 1,
@@ -61,7 +61,7 @@ describe('ImportContentTypes', () => {
6161
userId: 'user-123',
6262
email: 'test@example.com',
6363
sessionId: 'session-123',
64-
apiKey: 'test-api-key',
64+
apiKey: 'test',
6565
orgId: 'org-123',
6666
authenticationMethod: 'Basic Auth'
6767
},
@@ -807,4 +807,184 @@ describe('ImportContentTypes', () => {
807807
expect(importContentTypes['isExtensionsUpdate']).to.be.true;
808808
});
809809
});
810+
811+
describe('Additional Branch Coverage Tests', () => {
812+
it('should handle different error conditions in seedCTs onReject', async () => {
813+
const mockCTs = [{ uid: 'ct1', title: 'Content Type 1' }];
814+
815+
fsUtilStub.readFile.withArgs(sinon.match(/schema\.json/)).returns(mockCTs);
816+
fsUtilStub.readFile.withArgs(sinon.match(/uid-mapping\.json/)).returns({ extension_uid: {} });
817+
fsUtilStub.readFile.withArgs(sinon.match(/taxonomies.*success\.json/)).returns({});
818+
fsUtilStub.readFile.withArgs(sinon.match(/pending_global_fields\.js/)).returns([]);
819+
fsUtilStub.readFile.withArgs(sinon.match(/pending_extensions\.js/)).returns([]);
820+
821+
await importContentTypes.start();
822+
823+
const onReject = makeConcurrentCallStub.firstCall.args[0].apiParams.reject;
824+
825+
// Test error with errorCode 115 but different error structure
826+
onReject({
827+
error: { errorCode: 115, errors: { title: 'Title already exists' } },
828+
apiData: { content_type: { uid: 'ct1' } }
829+
});
830+
831+
// Test error with errorCode 115 but different error structure
832+
onReject({
833+
error: { errorCode: 115, errors: { uid: 'UID already exists' } },
834+
apiData: { content_type: { uid: 'ct1' } }
835+
});
836+
837+
expect(makeConcurrentCallStub.called).to.be.true;
838+
});
839+
840+
it('should handle different conditions in updatePendingGFs', async () => {
841+
const mockCTs = [{ uid: 'ct1', title: 'Content Type 1' }];
842+
const mockPendingGFs = ['gf1', 'gf2'];
843+
const mockGFs = [{ uid: 'gf1', title: 'Global Field 1' }];
844+
845+
fsUtilStub.readFile.withArgs(sinon.match(/schema\.json/)).returns(mockCTs);
846+
fsUtilStub.readFile.withArgs(sinon.match(/uid-mapping\.json/)).returns({ extension_uid: {} });
847+
fsUtilStub.readFile.withArgs(sinon.match(/taxonomies.*success\.json/)).returns({});
848+
fsUtilStub.readFile.withArgs(sinon.match(/pending_global_fields\.js/)).returns(mockPendingGFs);
849+
fsUtilStub.readFile.withArgs(sinon.match(/global_fields.*\.json/)).returns(mockGFs);
850+
fsUtilStub.readFile.withArgs(sinon.match(/pending_extensions\.js/)).returns([]);
851+
852+
await importContentTypes.start();
853+
854+
const onSuccess = makeConcurrentCallStub.getCall(2).args[0].apiParams.resolve;
855+
const onReject = makeConcurrentCallStub.getCall(2).args[0].apiParams.reject;
856+
857+
// Test onSuccess with undefined uid
858+
onSuccess({
859+
response: { uid: 'gf1' },
860+
apiData: { uid: undefined }
861+
});
862+
863+
// Test onReject with undefined uid
864+
onReject({
865+
error: { message: 'Update failed' },
866+
apiData: { uid: undefined }
867+
});
868+
869+
expect(makeConcurrentCallStub.callCount).to.be.greaterThan(2);
870+
});
871+
872+
it('should handle different conditions in updatePendingExtensions', async () => {
873+
const mockCTs = [{ uid: 'ct1', title: 'Content Type 1' }];
874+
const mockExtensions = [{ uid: 'ext1', title: 'Extension 1' }];
875+
876+
fsUtilStub.readFile.withArgs(sinon.match(/schema\.json/)).returns(mockCTs);
877+
fsUtilStub.readFile.withArgs(sinon.match(/uid-mapping\.json/)).returns({ extension_uid: {} });
878+
fsUtilStub.readFile.withArgs(sinon.match(/taxonomies.*success\.json/)).returns({});
879+
fsUtilStub.readFile.withArgs(sinon.match(/pending_global_fields\.js/)).returns([]);
880+
fsUtilStub.readFile.withArgs(sinon.match(/pending_extensions\.js/)).returns(mockExtensions);
881+
882+
await importContentTypes.start();
883+
884+
const onSuccess = makeConcurrentCallStub.lastCall.args[0].apiParams.resolve;
885+
const onReject = makeConcurrentCallStub.lastCall.args[0].apiParams.reject;
886+
887+
// Test onSuccess with undefined uid and title
888+
onSuccess({
889+
response: { title: 'Updated Extension' },
890+
apiData: { uid: undefined, title: undefined }
891+
});
892+
893+
// Test onReject with title error and skipExisting true
894+
importContentTypes['importConfig'].skipExisting = true;
895+
onReject({
896+
error: { errors: { title: 'Title already exists' } },
897+
apiData: { uid: 'ext1' }
898+
});
899+
900+
// Test onReject with title error and skipExisting false
901+
importContentTypes['importConfig'].skipExisting = false;
902+
onReject({
903+
error: { errors: { title: 'Title already exists' } },
904+
apiData: { uid: 'ext1' }
905+
});
906+
907+
expect(makeConcurrentCallStub.called).to.be.true;
908+
});
909+
910+
it('should handle null apiContent in updatePendingExtensions', async () => {
911+
const mockCTs = [{ uid: 'ct1', title: 'Content Type 1' }];
912+
913+
fsUtilStub.readFile.withArgs(sinon.match(/schema\.json/)).returns(mockCTs);
914+
fsUtilStub.readFile.withArgs(sinon.match(/uid-mapping\.json/)).returns({ extension_uid: {} });
915+
fsUtilStub.readFile.withArgs(sinon.match(/taxonomies.*success\.json/)).returns({});
916+
fsUtilStub.readFile.withArgs(sinon.match(/pending_global_fields\.js/)).returns([]);
917+
fsUtilStub.readFile.withArgs(sinon.match(/pending_extensions\.js/)).returns(null);
918+
919+
await importContentTypes.start();
920+
921+
expect(importContentTypes['isExtensionsUpdate']).to.be.false;
922+
});
923+
924+
it('should handle empty array apiContent in updatePendingExtensions', async () => {
925+
const mockCTs = [{ uid: 'ct1', title: 'Content Type 1' }];
926+
927+
fsUtilStub.readFile.withArgs(sinon.match(/schema\.json/)).returns(mockCTs);
928+
fsUtilStub.readFile.withArgs(sinon.match(/uid-mapping\.json/)).returns({ extension_uid: {} });
929+
fsUtilStub.readFile.withArgs(sinon.match(/taxonomies.*success\.json/)).returns({});
930+
fsUtilStub.readFile.withArgs(sinon.match(/pending_global_fields\.js/)).returns([]);
931+
fsUtilStub.readFile.withArgs(sinon.match(/pending_extensions\.js/)).returns([]);
932+
933+
await importContentTypes.start();
934+
935+
expect(importContentTypes['isExtensionsUpdate']).to.be.false;
936+
});
937+
938+
it('should handle onSuccess with different response structure in updatePendingExtensions', async () => {
939+
const mockCTs = [{ uid: 'ct1', title: 'Content Type 1' }];
940+
const mockExtensions = [{ uid: 'ext1', title: 'Extension 1' }];
941+
942+
fsUtilStub.readFile.withArgs(sinon.match(/schema\.json/)).returns(mockCTs);
943+
fsUtilStub.readFile.withArgs(sinon.match(/uid-mapping\.json/)).returns({ extension_uid: {} });
944+
fsUtilStub.readFile.withArgs(sinon.match(/taxonomies.*success\.json/)).returns({});
945+
fsUtilStub.readFile.withArgs(sinon.match(/pending_global_fields\.js/)).returns([]);
946+
fsUtilStub.readFile.withArgs(sinon.match(/pending_extensions\.js/)).returns(mockExtensions);
947+
948+
await importContentTypes.start();
949+
950+
const onSuccess = makeConcurrentCallStub.lastCall.args[0].apiParams.resolve;
951+
952+
// Test onSuccess with response that has no title property
953+
onSuccess({
954+
response: { uid: 'ext1' },
955+
apiData: { uid: 'ext1', title: 'Extension 1' }
956+
});
957+
958+
expect(makeConcurrentCallStub.called).to.be.true;
959+
});
960+
961+
it('should handle onReject with different error structures in updatePendingExtensions', async () => {
962+
const mockCTs = [{ uid: 'ct1', title: 'Content Type 1' }];
963+
const mockExtensions = [{ uid: 'ext1', title: 'Extension 1' }];
964+
965+
fsUtilStub.readFile.withArgs(sinon.match(/schema\.json/)).returns(mockCTs);
966+
fsUtilStub.readFile.withArgs(sinon.match(/uid-mapping\.json/)).returns({ extension_uid: {} });
967+
fsUtilStub.readFile.withArgs(sinon.match(/taxonomies.*success\.json/)).returns({});
968+
fsUtilStub.readFile.withArgs(sinon.match(/pending_global_fields\.js/)).returns([]);
969+
fsUtilStub.readFile.withArgs(sinon.match(/pending_extensions\.js/)).returns(mockExtensions);
970+
971+
await importContentTypes.start();
972+
973+
const onReject = makeConcurrentCallStub.lastCall.args[0].apiParams.reject;
974+
975+
// Test onReject with error that has no errors property
976+
onReject({
977+
error: { message: 'Server error' },
978+
apiData: { uid: 'ext1' }
979+
});
980+
981+
// Test onReject with error that has errors but no title
982+
onReject({
983+
error: { errors: { uid: 'UID already exists' } },
984+
apiData: { uid: 'ext1' }
985+
});
986+
987+
expect(makeConcurrentCallStub.called).to.be.true;
988+
});
989+
});
810990
});

0 commit comments

Comments
 (0)