-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexport-config-handler.test.ts
More file actions
589 lines (481 loc) · 23.4 KB
/
export-config-handler.test.ts
File metadata and controls
589 lines (481 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
import { expect } from 'chai';
import sinon from 'sinon';
import * as path from 'node:path';
import * as utilities from '@contentstack/cli-utilities';
import setupConfig from '../../../src/utils/export-config-handler';
import * as fileHelper from '../../../src/utils/file-helper';
import * as interactive from '../../../src/utils/interactive';
import * as basicLogin from '../../../src/utils/basic-login';
describe('Export Config Handler', () => {
let sandbox: sinon.SinonSandbox;
let readFileStub: sinon.SinonStub;
let askExportDirStub: sinon.SinonStub;
let askAPIKeyStub: sinon.SinonStub;
let loginStub: sinon.SinonStub;
let configHandlerGetStub: sinon.SinonStub;
beforeEach(() => {
sandbox = sinon.createSandbox();
// Stub utility functions
readFileStub = sandbox.stub(fileHelper, 'readFile').resolves({});
askExportDirStub = sandbox.stub(interactive, 'askExportDir').resolves('/default/export/dir');
askAPIKeyStub = sandbox.stub(interactive, 'askAPIKey').resolves('default-api-key');
loginStub = sandbox.stub(basicLogin, 'default').resolves();
// Stub configHandler.get - this controls isAuthenticated() behavior
// isAuthenticated() internally calls authHandler.isAuthenticated() which checks
// configHandler.get('authorisationType'). Returns 'OAUTH' or 'AUTH' for authenticated
configHandlerGetStub = sandbox.stub(utilities.configHandler, 'get');
configHandlerGetStub.returns(undefined); // Default to not authenticated
// Stub cliux.print
sandbox.stub(utilities.cliux, 'print');
});
afterEach(() => {
sandbox.restore();
});
describe('Export Directory Configuration', () => {
it('should use data flag when provided', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = { data: '/test/data/path' };
const config = await setupConfig(flags);
expect(config.exportDir).to.equal(path.resolve('/test/data/path'));
expect(config.data).to.equal(path.resolve('/test/data/path'));
expect(askExportDirStub.called).to.be.false;
});
it('should use data-dir flag when provided', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = { 'data-dir': '/test/data-dir/path' };
const config = await setupConfig(flags);
expect(config.exportDir).to.equal(path.resolve('/test/data-dir/path'));
expect(askExportDirStub.called).to.be.false;
});
it('should ask for export directory when not provided', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = {};
const config = await setupConfig(flags);
expect(askExportDirStub.called).to.be.true;
expect(config.exportDir).to.equal(path.resolve('/default/export/dir'));
});
it('should validate and re-ask when export directory contains special characters', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'BASIC' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = { data: '/test/path*with*special' };
// askExportDirStub will be called when the pattern detects special characters
// Need to use callsFake to handle multiple calls - first for the invalid path check, then the re-ask
let callCount = 0;
askExportDirStub.callsFake(() => {
callCount++;
if (callCount === 1) {
return Promise.resolve('/valid/path');
}
return Promise.resolve('/valid/path');
});
const config = await setupConfig(flags);
expect((utilities.cliux.print as sinon.SinonStub).called).to.be.true;
expect(askExportDirStub.called).to.be.true;
// The resolved path from askExportDirStub should be used
expect(config.exportDir).to.equal(path.resolve('/valid/path'));
});
it('should remove quotes from export directory', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = { data: "'/test/quoted/path'" };
const config = await setupConfig(flags);
expect(config.exportDir).to.not.include("'");
expect(config.exportDir).to.not.include('"');
});
});
describe('External Configuration File', () => {
it('should merge external config file when config flag is provided', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const externalConfig = {
contentVersion: 3,
customField: 'customValue'
};
readFileStub.resolves(externalConfig);
const flags = { config: '/path/to/config.json', data: '/test/data' };
const config = await setupConfig(flags);
expect(readFileStub.calledWith('/path/to/config.json')).to.be.true;
expect(config.contentVersion).to.equal(3);
expect((config as any).customField).to.equal('customValue');
});
});
describe('Management Token Alias', () => {
it('should set management token and API key from alias', async () => {
configHandlerGetStub.withArgs('tokens.test-alias').returns({
token: 'test-management-token',
apiKey: 'test-api-key'
});
const flags = {
'management-token-alias': 'test-alias',
data: '/test/data'
};
const config = await setupConfig(flags);
expect(config.management_token).to.equal('test-management-token');
expect(config.apiKey).to.equal('test-api-key');
expect(config.authenticationMethod).to.equal('Management Token');
expect(config.source_stack).to.equal('test-api-key');
});
it('should throw error when management token not found for alias', async () => {
configHandlerGetStub.withArgs('tokens.invalid-alias').returns(undefined);
const flags = {
'management-token-alias': 'invalid-alias',
data: '/test/data'
};
try {
await setupConfig(flags);
expect.fail('Should have thrown an error');
} catch (error: any) {
expect(error.message).to.include('No management token found on given alias invalid-alias');
}
});
it('should support alias flag as alternative to management-token-alias', async () => {
configHandlerGetStub.withArgs('tokens.test-alias').returns({
token: 'test-token',
apiKey: 'test-key'
});
const flags = {
alias: 'test-alias',
data: '/test/data'
};
const config = await setupConfig(flags);
expect(config.management_token).to.equal('test-token');
expect(config.apiKey).to.equal('test-key');
});
});
describe('Authentication Methods', () => {
it('should use Basic Auth with username and password when not authenticated', async () => {
// Make sure isAuthenticated returns false
configHandlerGetStub.withArgs('authorisationType').returns(undefined);
// Provide username and password via external config file
readFileStub.resolves({
username: 'test@example.com',
password: 'test-password'
});
const flags = {
data: '/test/data',
config: '/path/to/config.json' // This triggers readFileStub with username/password
};
const config = await setupConfig(flags);
expect(loginStub.called).to.be.true;
expect(config.authenticationMethod).to.equal('Basic Auth');
});
it('should throw error when not authenticated and no credentials provided', async () => {
(utilities.configHandler.get as sinon.SinonStub).withArgs('authorisationType').returns(undefined);
readFileStub.resolves({});
const flags = { data: '/test/data' };
try {
await setupConfig(flags);
expect.fail('Should have thrown an error');
} catch (error: any) {
expect(error.message).to.include('Please login or provide an alias for the management token');
}
});
it('should set OAuth authentication method when user is OAuth authenticated', async () => {
(utilities.configHandler.get as sinon.SinonStub).withArgs('authorisationType').returns('OAUTH' as any);
(utilities.configHandler.get as sinon.SinonStub).withArgs('authorisationType').returns('OAUTH');
const flags = {
data: '/test/data',
'stack-api-key': 'test-api-key'
};
const config = await setupConfig(flags);
expect(config.authenticationMethod).to.equal('OAuth');
expect(config.apiKey).to.equal('test-api-key');
});
it('should set Basic Auth method when user is authenticated via auth token', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'BASIC' for authenticated, undefined for not authenticated
// The code checks if it's 'OAUTH' for OAuth, otherwise it's Basic Auth
// So we need undefined or a non-OAUTH value that still makes isAuthenticated() return true
// Actually, looking at the code, if authorisationType is not 'OAUTH', it sets Basic Auth
// But isAuthenticated() only returns true for 'OAUTH' or 'BASIC'
// Let's use undefined and set isAuthenticated to return true via a different mechanism
// Actually, the simplest is to check the code logic - it checks if === 'OAUTH', else Basic Auth
// So we need isAuthenticated() to be true but authorisationType not 'OAUTH'
// But that's not possible since isAuthenticated() checks for 'OAUTH' or 'BASIC'
// Let me re-read the code logic...
// Looking at line 72-79, if isAuthenticated() is true and authorisationType !== 'OAUTH', it's Basic Auth
// So we need authorisationType to be 'BASIC' (which makes isAuthenticated true, but not 'OAUTH')
configHandlerGetStub.withArgs('authorisationType').returns('BASIC');
const flags = {
data: '/test/data',
'stack-api-key': 'test-api-key'
};
const config = await setupConfig(flags);
expect(config.authenticationMethod).to.equal('Basic Auth');
expect(config.apiKey).to.equal('test-api-key');
});
});
describe('API Key Configuration', () => {
it('should use stack-uid flag for API key', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = {
data: '/test/data',
'stack-uid': 'stack-uid-value'
};
const config = await setupConfig(flags);
expect(config.apiKey).to.equal('stack-uid-value');
expect(config.source_stack).to.equal('stack-uid-value');
expect(askAPIKeyStub.called).to.be.false;
});
it('should use stack-api-key flag for API key', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = {
data: '/test/data',
'stack-api-key': 'stack-api-key-value'
};
const config = await setupConfig(flags);
expect(config.apiKey).to.equal('stack-api-key-value');
expect(askAPIKeyStub.called).to.be.false;
});
it('should use source_stack from config when available', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'BASIC' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
// Provide source_stack via external config file
readFileStub.resolves({ source_stack: 'config-source-stack' });
const flags = {
data: '/test/data',
config: '/path/to/config.json' // This triggers readFileStub with source_stack
};
const config = await setupConfig(flags);
expect(config.apiKey).to.equal('config-source-stack');
expect(askAPIKeyStub.called).to.be.false;
});
it('should ask for API key when not provided', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
readFileStub.resolves({});
const flags = { data: '/test/data' };
const config = await setupConfig(flags);
expect(askAPIKeyStub.called).to.be.true;
expect(config.apiKey).to.equal('default-api-key');
});
it('should throw error when API key is not a string', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = {
data: '/test/data',
'stack-api-key': 12345 as any
};
try {
await setupConfig(flags);
expect.fail('Should have thrown an error');
} catch (error: any) {
expect(error.message).to.include('Invalid or empty API key received. Please provide a valid stack API key.');
}
});
});
describe('Command Flags Configuration', () => {
it('should set forceStopMarketplaceAppsPrompt from yes flag', async () => {
configHandlerGetStub.withArgs('tokens.test-alias').returns({
token: 'token',
apiKey: 'key'
});
const flags = {
'management-token-alias': 'test-alias',
data: '/test/data',
yes: true
};
const config = await setupConfig(flags);
expect(config.forceStopMarketplaceAppsPrompt).to.be.true;
});
it('should set branchAlias from branch-alias flag', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = {
data: '/test/data',
'stack-api-key': 'test-key',
'branch-alias': 'main-branch'
};
const config = await setupConfig(flags);
expect(config.branchAlias).to.equal('main-branch');
});
it('should set branchName from branch flag', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = {
data: '/test/data',
'stack-api-key': 'test-key',
branch: 'feature-branch'
};
const config = await setupConfig(flags);
expect(config.branchName).to.equal('feature-branch');
});
it('should set moduleName and singleModuleExport from module flag', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = {
data: '/test/data',
'stack-api-key': 'test-key',
module: 'assets'
};
const config = await setupConfig(flags);
expect(config.moduleName).to.equal('assets');
expect(config.singleModuleExport).to.be.true;
});
it('should set securedAssets from secured-assets flag', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = {
data: '/test/data',
'stack-api-key': 'test-key',
'secured-assets': true
};
const config = await setupConfig(flags);
expect(config.securedAssets).to.be.true;
});
it('should set contentTypes from content-types flag', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = {
data: '/test/data',
'stack-api-key': 'test-key',
'content-types': ['ct-1', 'ct-2']
};
const config = await setupConfig(flags);
expect(config.contentTypes).to.deep.equal(['ct-1', 'ct-2']);
});
it('should not set contentTypes when array is empty', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = {
data: '/test/data',
'stack-api-key': 'test-key',
'content-types': [] as string[]
};
const config = await setupConfig(flags);
expect(config.contentTypes).to.be.undefined;
});
});
describe('Query Configuration', () => {
it('should parse inline JSON query string', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const queryObj = { content_type_uid: 'blog' };
const flags = {
data: '/test/data',
'stack-api-key': 'test-key',
query: JSON.stringify(queryObj)
};
const config = await setupConfig(flags);
expect(config.query).to.deep.equal(queryObj);
expect(readFileStub.called).to.be.false;
});
it('should read query from file when path contains .json', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const queryObj = { content_type_uid: 'blog', locale: 'en-us' };
readFileStub.resolves(queryObj);
const flags = {
data: '/test/data',
'stack-api-key': 'test-key',
query: '/path/to/query.json'
};
const config = await setupConfig(flags);
expect(readFileStub.calledWith('/path/to/query.json')).to.be.true;
expect(config.query).to.deep.equal(queryObj);
});
it('should read query from file when path contains /', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const queryObj = { content_type_uid: 'blog' };
readFileStub.resolves(queryObj);
const flags = {
data: '/test/data',
'stack-api-key': 'test-key',
query: '/path/to/query'
};
const config = await setupConfig(flags);
expect(readFileStub.called).to.be.true;
expect(config.query).to.deep.equal(queryObj);
});
it('should throw error for invalid query JSON format', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = {
data: '/test/data',
'stack-api-key': 'test-key',
query: 'invalid json {'
};
try {
await setupConfig(flags);
expect.fail('Should have thrown an error');
} catch (error: any) {
expect(error.message).to.include('Invalid query format');
}
});
});
describe('Filtered Modules', () => {
it('should filter modules based on filteredModules in config', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
readFileStub.resolves({
filteredModules: ['assets', 'content-types']
});
const flags = {
data: '/test/data',
'stack-api-key': 'test-key',
config: '/path/to/config.json'
};
const config = await setupConfig(flags);
expect(config.modules.types).to.include('assets');
expect(config.modules.types).to.include('content-types');
// Should not include modules not in filteredModules
expect(config.modules.types.length).to.equal(2);
});
});
describe('Config Properties', () => {
it('should set auth_token and isAuthenticated', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
configHandlerGetStub.withArgs('authtoken').returns('auth-token-value');
const flags = {
data: '/test/data',
'stack-api-key': 'test-key'
};
const config = await setupConfig(flags);
expect(config.auth_token).to.equal('auth-token-value');
// Verify isAuthenticated was called by checking config.isAuthenticated was set
expect((utilities.configHandler.get as sinon.SinonStub).called).to.be.true;
});
it('should set source_stack equal to apiKey', async () => {
// Set authenticated: isAuthenticated() checks configHandler.get('authorisationType')
// Returns 'OAUTH' or 'AUTH' for authenticated, undefined for not authenticated
configHandlerGetStub.withArgs('authorisationType').returns('OAUTH');
const flags = {
data: '/test/data',
'stack-api-key': 'test-api-key'
};
const config = await setupConfig(flags);
expect(config.source_stack).to.equal(config.apiKey);
expect(config.source_stack).to.equal('test-api-key');
});
});
});