-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathregion.test.ts
More file actions
381 lines (344 loc) · 16.4 KB
/
region.test.ts
File metadata and controls
381 lines (344 loc) · 16.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
import { expect } from 'chai';
import * as sinon from 'sinon';
import { configHandler, log, cliux } from '@contentstack/cli-utilities';
import GetRegionCommand from '../../../src/commands/config/get/region';
import { Region } from '../../../src/interfaces';
import UserConfig from '../../../src/utils/region-handler';
import { askCustomRegion, askRegions } from '../../../src/utils/interactive';
const config = configHandler;
describe('Region command', function () {
const region: Region = {
name: 'test',
cma: 'https://api.contentstack.com',
cda: 'https://cda.contentstack.com',
uiHost: '',
developerHubUrl: 'https://developerhub-api.contentstack.com',
launchHubUrl: 'https://launch-api.contentstack.com',
personalizeUrl: 'https://personalization-api.contentstack.com',
composableStudioUrl: 'https://composable-studio-api.contentstack.com',
assetManagementUrl: 'https://am-api.contentstack.com',
};
let cliuxPrintStub: sinon.SinonStub;
let configGetStub: sinon.SinonStub;
let configSetStub: sinon.SinonStub;
beforeEach(function () {
configGetStub = sinon.stub(config, 'get').callsFake((key) => {
if (key === 'region') return region.name;
return undefined;
});
configSetStub = sinon.stub(config, 'set').callsFake(function (key, value) {
this[key] = value;
return this;
});
cliuxPrintStub = sinon.stub(cliux, 'print').callsFake(function () {});
});
afterEach(function () {
cliuxPrintStub.restore();
configGetStub.restore();
configSetStub.restore();
});
it.skip('Get region, should print region', async function () {
await GetRegionCommand.run([]);
expect(cliuxPrintStub.callCount).to.equal(7);
});
it('should log an error and exit when the region is not set', async function () {
<<<<<<< HEAD
const command = new GetRegionCommand([], {} as any);
// Stub the region property to return undefined
sinon.stub(command, 'region').get(() => undefined);
// Stub the exit method to throw to stop execution
const exitStub = sinon.stub(command, 'exit').throws(new Error('EXIT_CALLED'));
// Stub cliux.error to capture error calls
const errorStub = sinon.stub(cliux, 'error');
// Reset cliuxPrintStub to capture new calls
cliuxPrintStub.reset();
// Call the run method directly, expect it to throw
=======
configGetStub.callsFake((key) => {
if (key === 'region') return undefined;
return undefined;
});
const exitStub = sinon.stub(process, 'exit').callsFake((code) => {
throw new Error(`CLI_CONFIG_GET_REGION_NOT_FOUND EEXIT: ${code}`);
});
let result;
>>>>>>> main
try {
await command.run();
} catch (error) {
// Expected to throw due to exit stub
}
<<<<<<< HEAD
// Verify that cliux.error was called with the correct message
expect(errorStub.calledWith('CLI_CONFIG_GET_REGION_NOT_FOUND')).to.be.true;
// Verify exit was called
expect(exitStub.called).to.be.true;
errorStub.restore();
=======
exitStub.restore();
expect(result.message).to.include('CLI_CONFIG_GET_REGION_NOT_FOUND EEXIT: 1');
>>>>>>> main
});
// Test cases for predefined regions
describe('Predefined Regions', function () {
it('should set NA region', function () {
const result = UserConfig.setRegion('NA');
expect(result).to.have.property('name', 'NA');
expect(result.cma).to.equal('https://api.contentstack.io');
expect(result.cda).to.equal('https://cdn.contentstack.io');
expect(result.uiHost).to.equal('https://app.contentstack.com');
expect(result.developerHubUrl).to.equal('https://developerhub-api.contentstack.com');
expect(result.personalizeUrl).to.equal('https://personalize-api.contentstack.com');
expect(result.launchHubUrl).to.equal('https://launch-api.contentstack.com');
expect(result.composableStudioUrl).to.equal('https://composable-studio-api.contentstack.com');
});
it('should set EU region', function () {
const result = UserConfig.setRegion('EU');
expect(result).to.have.property('name', 'EU');
expect(result.cma).to.equal('https://eu-api.contentstack.com');
expect(result.cda).to.equal('https://eu-cdn.contentstack.com');
expect(result.uiHost).to.equal('https://eu-app.contentstack.com');
expect(result.developerHubUrl).to.equal('https://eu-developerhub-api.contentstack.com');
expect(result.personalizeUrl).to.equal('https://eu-personalize-api.contentstack.com');
expect(result.launchHubUrl).to.equal('https://eu-launch-api.contentstack.com');
expect(result.composableStudioUrl).to.equal('https://eu-composable-studio-api.contentstack.com');
});
it('should set AU region', function () {
const result = UserConfig.setRegion('AU');
expect(result).to.have.property('name', 'AU');
expect(result.cma).to.equal('https://au-api.contentstack.com');
expect(result.cda).to.equal('https://au-cdn.contentstack.com');
expect(result.uiHost).to.equal('https://au-app.contentstack.com');
expect(result.developerHubUrl).to.equal('https://au-developerhub-api.contentstack.com');
expect(result.personalizeUrl).to.equal('https://au-personalize-api.contentstack.com');
expect(result.launchHubUrl).to.equal('https://au-launch-api.contentstack.com');
expect(result.composableStudioUrl).to.equal('https://au-composable-studio-api.contentstack.com');
});
it('should set AWS-NA region', function () {
const result = UserConfig.setRegion('AWS-NA');
expect(result).to.have.property('name', 'AWS-NA');
expect(result.cma).to.equal('https://api.contentstack.io');
expect(result.cda).to.equal('https://cdn.contentstack.io');
expect(result.uiHost).to.equal('https://app.contentstack.com');
expect(result.developerHubUrl).to.equal('https://developerhub-api.contentstack.com');
expect(result.personalizeUrl).to.equal('https://personalize-api.contentstack.com');
expect(result.launchHubUrl).to.equal('https://launch-api.contentstack.com');
expect(result.composableStudioUrl).to.equal('https://composable-studio-api.contentstack.com');
});
it('should set AWS-EU region', function () {
const result = UserConfig.setRegion('AWS-EU');
expect(result).to.have.property('name', 'AWS-EU');
expect(result.cma).to.equal('https://eu-api.contentstack.com');
expect(result.cda).to.equal('https://eu-cdn.contentstack.com');
expect(result.uiHost).to.equal('https://eu-app.contentstack.com');
expect(result.developerHubUrl).to.equal('https://eu-developerhub-api.contentstack.com');
expect(result.personalizeUrl).to.equal('https://eu-personalize-api.contentstack.com');
expect(result.launchHubUrl).to.equal('https://eu-launch-api.contentstack.com');
expect(result.composableStudioUrl).to.equal('https://eu-composable-studio-api.contentstack.com');
});
it('should set AWS-AU region', function () {
const result = UserConfig.setRegion('AWS-AU');
expect(result).to.have.property('name', 'AWS-AU');
expect(result.cma).to.equal('https://au-api.contentstack.com');
expect(result.cda).to.equal('https://au-cdn.contentstack.com');
expect(result.uiHost).to.equal('https://au-app.contentstack.com');
expect(result.developerHubUrl).to.equal('https://au-developerhub-api.contentstack.com');
expect(result.personalizeUrl).to.equal('https://au-personalize-api.contentstack.com');
expect(result.launchHubUrl).to.equal('https://au-launch-api.contentstack.com');
expect(result.composableStudioUrl).to.equal('https://au-composable-studio-api.contentstack.com');
});
it('should set AZURE-NA region', function () {
const result = UserConfig.setRegion('AZURE-NA');
expect(result).to.have.property('name', 'AZURE-NA');
expect(result.cma).to.equal('https://azure-na-api.contentstack.com');
expect(result.cda).to.equal('https://azure-na-cdn.contentstack.com');
expect(result.uiHost).to.equal('https://azure-na-app.contentstack.com');
expect(result.developerHubUrl).to.equal('https://azure-na-developerhub-api.contentstack.com');
expect(result.personalizeUrl).to.equal('https://azure-na-personalize-api.contentstack.com');
expect(result.launchHubUrl).to.equal('https://azure-na-launch-api.contentstack.com');
expect(result.composableStudioUrl).to.equal('https://azure-na-composable-studio-api.contentstack.com');
});
it('should set AZURE-EU region', function () {
const result = UserConfig.setRegion('AZURE-EU');
expect(result).to.have.property('name', 'AZURE-EU');
expect(result.cma).to.equal('https://azure-eu-api.contentstack.com');
expect(result.cda).to.equal('https://azure-eu-cdn.contentstack.com');
expect(result.uiHost).to.equal('https://azure-eu-app.contentstack.com');
expect(result.developerHubUrl).to.equal('https://azure-eu-developerhub-api.contentstack.com');
expect(result.personalizeUrl).to.equal('https://azure-eu-personalize-api.contentstack.com');
expect(result.launchHubUrl).to.equal('https://azure-eu-launch-api.contentstack.com');
expect(result.composableStudioUrl).to.equal('https://azure-eu-composable-studio-api.contentstack.com');
});
it('should set GCP-NA region', function () {
const result = UserConfig.setRegion('GCP-NA');
expect(result).to.have.property('name', 'GCP-NA');
expect(result.cma).to.equal('https://gcp-na-api.contentstack.com');
expect(result.cda).to.equal('https://gcp-na-cdn.contentstack.com');
expect(result.uiHost).to.equal('https://gcp-na-app.contentstack.com');
expect(result.developerHubUrl).to.equal('https://gcp-na-developerhub-api.contentstack.com');
expect(result.personalizeUrl).to.equal('https://gcp-na-personalize-api.contentstack.com');
expect(result.launchHubUrl).to.equal('https://gcp-na-launch-api.contentstack.com');
expect(result.composableStudioUrl).to.equal('https://gcp-na-composable-studio-api.contentstack.com');
});
it('should set GCP-EU region', function () {
const result = UserConfig.setRegion('GCP-EU');
expect(result).to.have.property('name', 'GCP-EU');
expect(result.cma).to.equal('https://gcp-eu-api.contentstack.com');
expect(result.cda).to.equal('https://gcp-eu-cdn.contentstack.com');
expect(result.uiHost).to.equal('https://gcp-eu-app.contentstack.com');
expect(result.developerHubUrl).to.equal('https://gcp-eu-developerhub-api.contentstack.com');
expect(result.personalizeUrl).to.equal('https://gcp-eu-personalize-api.contentstack.com');
expect(result.launchHubUrl).to.equal('https://gcp-eu-launch-api.contentstack.com');
expect(result.composableStudioUrl).to.equal('https://gcp-eu-composable-studio-api.contentstack.com');
});
it('should return undefined for invalid region', function () {
const result = UserConfig.setRegion('INVALID-REGION');
expect(result).to.be.undefined;
});
});
it('should get the default region if none is set', function () {
configGetStub.callsFake((key) => {
if (key === 'region') return undefined;
return undefined;
});
const region = UserConfig.getRegion();
expect(region).to.have.property('name', 'AWS-NA');
});
it('should set a custom region with valid data', function () {
const customRegion = {
cma: 'https://custom-cma.com',
cda: 'https://custom-cda.com',
uiHost: 'https://custom-ui.com',
name: 'Custom Region',
};
const result = UserConfig.setCustomRegion(customRegion);
expect(result.cma).to.equal(customRegion.cma);
expect(result.cda).to.equal(customRegion.cda);
expect(result.uiHost).to.equal(customRegion.uiHost);
expect(result.name).to.equal(customRegion.name);
});
it('should throw an error for invalid custom region', function () {
const invalidRegion = {
name: 'Invalid Region',
cma: 'invalid-url',
cda: 'invalid-url',
};
expect(() => UserConfig.setCustomRegion(invalidRegion)).to.throw(TypeError, /valid cma/);
});
it('should validate a region object', function () {
const validRegion = {
name: 'Valid Region',
cma: 'https://valid-cma.com',
cda: 'https://valid-cda.com',
uiHost: 'https://valid-ui.com',
};
const invalidRegion = {
name: 'Invalid Region',
cma: 'invalid-url',
cda: 'invalid-url',
};
expect(UserConfig.validateRegion(validRegion)).to.be.true;
expect(UserConfig.validateRegion(invalidRegion)).to.be.false;
});
it('should set a custom region with developer hub URL', function () {
const customRegion = {
cma: 'https://custom-cma.com',
cda: 'https://custom-cda.com',
uiHost: 'https://custom-ui.com',
name: 'Custom Region',
developerHubUrl: 'https://custom-developer-hub.com',
};
const result = UserConfig.setCustomRegion(customRegion);
expect(result.developerHubUrl).to.equal(customRegion.developerHubUrl);
});
it('should set a custom region with personalize URL', function () {
const customRegion = {
cma: 'https://custom-cma.com',
cda: 'https://custom-cda.com',
uiHost: 'https://custom-ui.com',
name: 'Custom Region',
personalizeUrl: 'https://custom-personalize.com',
};
const result = UserConfig.setCustomRegion(customRegion);
expect(result.personalizeUrl).to.equal(customRegion.personalizeUrl);
});
it('should set a custom region with launch URL', function () {
const customRegion = {
cma: 'https://custom-cma.com',
cda: 'https://custom-cda.com',
uiHost: 'https://custom-ui.com',
name: 'Custom Region',
launchHubUrl: 'https://custom-launch.com',
};
const result = UserConfig.setCustomRegion(customRegion);
expect(result.launchHubUrl).to.equal(customRegion.launchHubUrl);
});
it('should set a custom region with studio URL', function () {
const customRegion = {
cma: 'https://custom-cma.com',
cda: 'https://custom-cda.com',
uiHost: 'https://custom-ui.com',
name: 'Custom Region',
composableStudioUrl: 'https://custom-composable-studio.com',
};
const result = UserConfig.setCustomRegion(customRegion);
expect(result.composableStudioUrl).to.equal(customRegion.composableStudioUrl);
});
it('should set a custom region with all optional URLs', function () {
const customRegion = {
cma: 'https://custom-cma.com',
cda: 'https://custom-cda.com',
uiHost: 'https://custom-ui.com',
name: 'Custom Region',
developerHubUrl: 'https://custom-developer-hub.com',
personalizeUrl: 'https://custom-personalize.com',
launchHubUrl: 'https://custom-launch.com',
composableStudioUrl: 'https://custom-composable-studio.com',
assetManagementUrl: 'https://custom-asset-management.com',
};
const result = UserConfig.setCustomRegion(customRegion);
expect(result).to.deep.equal(customRegion);
});
it('should sanitize region object to only include valid properties', function () {
const customRegion = {
cma: 'https://custom-cma.com',
cda: 'https://custom-cda.com',
uiHost: 'https://custom-ui.com',
name: 'Custom Region',
developerHubUrl: 'https://custom-developer-hub.com',
personalizeUrl: 'https://custom-personalize.com',
launchHubUrl: 'https://custom-launch.com',
composableStudioUrl: 'https://custom-composable-studio.com',
invalidProperty: 'should be removed',
};
const result = UserConfig.setCustomRegion(customRegion);
expect(result).to.not.have.property('invalidProperty');
});
});
describe('Region Handler', function () {
let inquireStub;
beforeEach(function () {
inquireStub = sinon.stub(cliux, 'inquire');
});
afterEach(function () {
inquireStub.restore();
});
it('askRegions should return selected region', async function () {
inquireStub.returns(Promise.resolve('AWS-NA'));
const result = await askRegions();
expect(result).to.equal('AWS-NA');
});
it('askCustomRegion should return custom region details', async function () {
inquireStub.returns(Promise.resolve('Custom Region'));
inquireStub.onCall(1).returns(Promise.resolve('https://custom-cma.com'));
inquireStub.onCall(2).returns(Promise.resolve('https://custom-cda.com'));
inquireStub.onCall(3).returns(Promise.resolve('https://custom-ui.com'));
const result = await askCustomRegion();
expect(result).to.deep.equal({
name: 'Custom Region',
cma: 'https://custom-cma.com',
cda: 'https://custom-cda.com',
uiHost: 'https://custom-ui.com',
});
});
});