/
index.test.ts
628 lines (533 loc) · 22.1 KB
/
index.test.ts
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import Globals from "../../src/globals";
import { chaiSpy, consoleOutput, constructPlugin, expect, getDomainConfig, getV3Utils } from "./base";
import Logging from "../../src/logging";
import DomainConfig = require("../../src/models/domain-config");
import APIGatewayV1Wrapper = require("../../src/aws/api-gateway-v1-wrapper");
import APIGatewayV2Wrapper = require("../../src/aws/api-gateway-v2-wrapper");
import { mockClient } from "aws-sdk-client-mock";
import {
APIGatewayClient, CreateBasePathMappingCommand, CreateDomainNameCommand, DeleteBasePathMappingCommand,
DeleteDomainNameCommand,
GetBasePathMappingsCommand,
GetDomainNameCommand, UpdateBasePathMappingCommand
} from "@aws-sdk/client-api-gateway";
import { ACMClient, ListCertificatesCommand } from "@aws-sdk/client-acm";
import { ChangeResourceRecordSetsCommand, ListHostedZonesCommand, Route53Client } from "@aws-sdk/client-route-53";
import { CloudFormationClient, DescribeStackResourceCommand, ResourceStatus } from "@aws-sdk/client-cloudformation";
describe("Custom Domain Plugin", () => {
beforeEach(() => {
consoleOutput.length = 0;
Globals.v3Utils = null;
});
describe("Initialization", () => {
it("with v3Utils", () => {
const testMessage = "test message";
const v3Utils = getV3Utils();
const domainConfig = getDomainConfig({});
constructPlugin(domainConfig, null, v3Utils);
Logging.logInfo(testMessage);
expect(consoleOutput[0]).to.equal("V3 [Info] " + testMessage);
});
it("init AWS resources", () => {
const domainConfig = getDomainConfig({});
const plugin = constructPlugin(domainConfig);
let errored = false;
try {
plugin.initializeVariables();
plugin.validateDomainConfigs();
plugin.initAWSResources();
} catch (err) {
errored = true;
}
expect(errored).to.equal(false);
});
it("Unsupported endpoint types throw exception", () => {
const domainConfig = getDomainConfig({ endpointType: "notSupported" });
const plugin = constructPlugin(domainConfig);
let errored = false;
try {
plugin.initializeVariables();
} catch (err) {
errored = true;
expect(err.message).to.equal("notSupported is not supported endpointType, use EDGE or REGIONAL.");
}
expect(errored).to.equal(true);
});
it("Unsupported api type throw exception", () => {
const domainConfig = getDomainConfig({ apiType: "notSupported" });
const plugin = constructPlugin(domainConfig);
let errored = false;
try {
plugin.initializeVariables();
} catch (err) {
errored = true;
expect(err.message).to.equal("notSupported is not supported api type, use REST, HTTP or WEBSOCKET.");
}
expect(errored).to.equal(true);
});
it("Get ApiGateway V1", () => {
const plugin = constructPlugin({});
plugin.initAWSResources();
const dc = new DomainConfig(getDomainConfig({
apiType: Globals.apiTypes.rest
}));
const apiGateway = plugin.getApiGateway(dc);
expect(apiGateway instanceof APIGatewayV1Wrapper).to.equal(true);
});
it("Get ApiGateway V2", () => {
const plugin = constructPlugin({});
plugin.initAWSResources();
// for the http API type should be APIGatewayV2Wrapper
let dc = new DomainConfig(getDomainConfig({
apiType: Globals.apiTypes.http
}));
expect(plugin.getApiGateway(dc) instanceof APIGatewayV2Wrapper).to.equal(true);
// for the websocket API type should be APIGatewayV2Wrapper
dc = new DomainConfig(getDomainConfig({
apiType: Globals.apiTypes.websocket
}));
expect(plugin.getApiGateway(dc) instanceof APIGatewayV2Wrapper).to.equal(true);
// for the multi-level base path and rest API type should be APIGatewayV2Wrapper
dc = new DomainConfig(getDomainConfig({
basePath: "api/test",
apiType: Globals.apiTypes.rest
}));
expect(plugin.getApiGateway(dc) instanceof APIGatewayV2Wrapper).to.equal(true);
});
});
describe("Validate plugin configuration", () => {
it("Should thrown an Error when plugin customDomain configuration object is missing", () => {
const plugin = constructPlugin(getDomainConfig(getDomainConfig({})));
delete plugin.serverless.service.custom.customDomain;
let errored = false;
try {
plugin.validateConfigExists();
} catch (err) {
errored = true;
expect(err.message).to.equal(`${Globals.pluginName}: Plugin configuration is missing.`);
}
expect(errored).to.equal(true);
});
it("Should thrown an Error when Serverless custom configuration object is missing for multiple domains", () => {
const plugin = constructPlugin(getDomainConfig({}), null, null);
delete plugin.serverless.service.custom.customDomain;
delete plugin.serverless.service.custom.customDomains;
let errored = false;
try {
plugin.validateConfigExists();
} catch (err) {
errored = true;
expect(err.message).to.equal(`${Globals.pluginName}: Plugin configuration is missing.`);
}
expect(errored).to.equal(true);
});
it("Should thrown an Error when Serverless custom configuration object is missing", () => {
const plugin = constructPlugin(getDomainConfig({}));
delete plugin.serverless.service.custom;
let errored = false;
try {
plugin.validateConfigExists();
} catch (err) {
errored = true;
expect(err.message).to.equal(`${Globals.pluginName}: Plugin configuration is missing.`);
}
expect(errored).to.equal(true);
});
it("Unsupported HTTP EDGE endpoint configuration", () => {
const domainOptions = getDomainConfig({ apiType: "http" });
const plugin = constructPlugin(domainOptions);
let errored = false;
try {
plugin.initializeVariables();
plugin.validateDomainConfigs();
} catch (err) {
errored = true;
expect(err.message).to.contains("'EDGE' endpointType is not compatible with HTTP APIs");
}
expect(errored).to.equal(true);
});
it("Unsupported WS EDGE endpoint configuration", () => {
const domainOptions = getDomainConfig({ apiType: "websocket" });
const plugin = constructPlugin(domainOptions);
let errored = false;
try {
plugin.initializeVariables();
plugin.validateDomainConfigs();
} catch (err) {
errored = true;
expect(err.message).to.equal("'EDGE' endpointType is not compatible with WebSocket APIs");
}
expect(errored).to.equal(true);
});
it("Lowercase edge endpoint type without errors", () => {
const domainOptions = getDomainConfig({ endpointType: "edge" });
const plugin = constructPlugin(domainOptions);
let errored = false;
try {
plugin.initializeVariables();
plugin.validateDomainConfigs();
} catch (err) {
errored = true;
}
expect(errored).to.equal(false);
});
it("Nested api type configuration", () => {
const domainOptions = getDomainConfig({});
const plugin = constructPlugin({ rest: domainOptions });
plugin.initializeVariables();
plugin.validateDomainConfigs();
expect(plugin.domains.length).to.equal(1);
});
it("Should enable the plugin by default", () => {
const plugin = constructPlugin(getDomainConfig({}));
plugin.initializeVariables();
plugin.validateDomainConfigs();
expect(plugin.domains).length.to.be.greaterThan(0);
for (const domain of plugin.domains) {
expect(domain.enabled).to.equal(true);
}
});
it("Should enable the plugin when passing a true parameter with type boolean", () => {
const plugin = constructPlugin(getDomainConfig({ enabled: true }));
plugin.initializeVariables();
plugin.validateDomainConfigs();
expect(plugin.domains).length.to.be.greaterThan(0);
for (const domain of plugin.domains) {
expect(domain.enabled).to.equal(true);
}
});
it("Should enable the plugin when passing a true parameter with type string", () => {
const plugin = constructPlugin(getDomainConfig({ enabled: "true" }));
plugin.initializeVariables();
plugin.validateDomainConfigs();
expect(plugin.domains).length.to.be.greaterThan(0);
for (const domain of plugin.domains) {
expect(domain.enabled).to.equal(true);
}
});
it("Should disable the plugin when passing a false parameter with type boolean", () => {
const plugin = constructPlugin(getDomainConfig({ enabled: false }));
plugin.initializeVariables();
plugin.validateDomainConfigs();
expect(plugin.domains.length).to.equal(0);
});
it("Should disable the plugin when passing a false parameter with type string", () => {
const plugin = constructPlugin(getDomainConfig({ enabled: "false" }));
plugin.initializeVariables();
plugin.validateDomainConfigs();
expect(plugin.domains.length).to.equal(0);
});
it("Should throw an Error when passing a parameter that is not boolean", async () => {
const plugin = constructPlugin(getDomainConfig({ enabled: "11" }));
let errored = false;
try {
await plugin.hookWrapper(null);
} catch (err) {
errored = true;
expect(err.message).to.equal(`${Globals.pluginName}: Ambiguous boolean config: "11"`);
}
expect(errored).to.equal(true);
});
it("Should throw an Error when passing a parameter that cannot be converted to boolean", async () => {
const plugin = constructPlugin(getDomainConfig({ enabled: "yes" }));
let errored = false;
try {
await plugin.hookWrapper(null);
} catch (err) {
errored = true;
expect(err.message).to.equal(`${Globals.pluginName}: Ambiguous boolean config: "yes"`);
}
expect(errored).to.equal(true);
});
it("Should throw an Error when mutual TLS is enabled for edge APIs", async () => {
const plugin = constructPlugin(getDomainConfig({
endpointType: "edge",
tlsTruststoreUri: "s3://bucket-name/key-name"
}));
let errored = false;
try {
await plugin.hookWrapper(null);
} catch (err) {
errored = true;
expect(err.message).to.equal("EDGE APIs do not support mutual TLS, remove tlsTruststoreUri or change to a regional API.");
}
expect(errored).to.equal(true);
});
it("Should throw an Error when mutual TLS uri is not an S3 uri", async () => {
const plugin = constructPlugin(getDomainConfig({
endpointType: "regional",
tlsTruststoreUri: "https://example.com"
}));
let errored = false;
try {
await plugin.hookWrapper(null);
} catch (err) {
errored = true;
expect(err.message).to.equal("https://example.com is not a valid s3 uri, try something like s3://bucket-name/key-name.");
}
expect(errored).to.equal(true);
});
});
describe("Hooks checks", () => {
it("after:deploy:deploy with the createBasePathMapping", async () => {
const APIGatewayMock = mockClient(APIGatewayClient);
APIGatewayMock.on(GetDomainNameCommand).resolves({
domainName: "dummy_domain",
regionalHostedZoneId: "test_id"
});
APIGatewayMock.on(GetBasePathMappingsCommand).resolves({ items: [] });
APIGatewayMock.on(CreateBasePathMappingCommand).resolves(null);
const CloudFormationMock = mockClient(CloudFormationClient);
CloudFormationMock.on(DescribeStackResourceCommand).resolves({
StackResourceDetail: {
LogicalResourceId: Globals.CFResourceIds[Globals.apiTypes.rest],
PhysicalResourceId: "test_rest_api_id",
ResourceType: "",
LastUpdatedTimestamp: null,
ResourceStatus: ResourceStatus.CREATE_COMPLETE
}
});
const domainOptions = getDomainConfig({ domainName: "test_domain" });
const plugin = constructPlugin(domainOptions);
plugin.initAWSRegion = async () => null;
await plugin.hooks["after:deploy:deploy"]();
const commandCalls = APIGatewayMock.commandCalls(CreateBasePathMappingCommand);
expect(commandCalls.length).to.equal(1);
});
it("after:deploy:deploy with the updateBasePathMapping", async () => {
const APIGatewayMock = mockClient(APIGatewayClient);
APIGatewayMock.on(GetDomainNameCommand).resolves({
domainName: "dummy_domain",
regionalHostedZoneId: "test_id"
});
APIGatewayMock.on(GetBasePathMappingsCommand).resolves({
items: [{
restApiId: "test_rest_api_id",
basePath: "test",
stage: "test"
}]
});
APIGatewayMock.on(UpdateBasePathMappingCommand).resolves(null);
const CloudFormationMock = mockClient(CloudFormationClient);
CloudFormationMock.on(DescribeStackResourceCommand).resolves({
StackResourceDetail: {
LogicalResourceId: Globals.CFResourceIds[Globals.apiTypes.rest],
PhysicalResourceId: "test_rest_api_id",
ResourceType: "",
LastUpdatedTimestamp: null,
ResourceStatus: ResourceStatus.CREATE_COMPLETE
}
});
const domainOptions = getDomainConfig({ domainName: "test_domain" });
const plugin = constructPlugin(domainOptions);
plugin.initAWSRegion = async () => null;
await plugin.hooks["after:deploy:deploy"]();
const commandCalls = APIGatewayMock.commandCalls(UpdateBasePathMappingCommand);
expect(commandCalls.length).to.equal(1);
});
it("after:info:info", async () => {
const APIGatewayMock = mockClient(APIGatewayClient);
APIGatewayMock.on(GetDomainNameCommand).resolves({
domainName: "dummy_domain",
regionalHostedZoneId: "test_id"
});
const plugin = constructPlugin(getDomainConfig({ domainName: "test_domain" }));
plugin.initAWSRegion = async () => null;
await plugin.hooks["after:info:info"]();
expect(consoleOutput[0]).to.equal("[Summary] Distribution Domain Name");
expect(consoleOutput[1]).to.equal(" Domain Name: test_domain");
expect(consoleOutput[2]).to.equal(" Target Domain: dummy_domain");
expect(consoleOutput[3]).to.equal(" Hosted Zone Id: test_id");
});
it("before:deploy:deploy with autoDomain false", async () => {
const APIGatewayMock = mockClient(APIGatewayClient);
APIGatewayMock.on(GetDomainNameCommand).resolvesOnce({})
.resolves({
domainName: "dummy_domain",
regionalHostedZoneId: "test_id"
});
const domainOptions = getDomainConfig({ domainName: "test_domain" });
const plugin = constructPlugin(domainOptions);
plugin.initAWSRegion = async () => null;
const createDomainSpy = chaiSpy.on(plugin, "createDomain");
await plugin.hooks["before:deploy:deploy"]();
expect(createDomainSpy).to.not.have.been.called();
});
it("before:deploy:deploy with autoDomain true", async () => {
const APIGatewayMock = mockClient(APIGatewayClient);
APIGatewayMock.on(GetDomainNameCommand)
.rejectsOnce({ $metadata: { httpStatusCode: 404 } })
.resolves({
domainName: "dummy_domain",
regionalHostedZoneId: "test_id"
});
const plugin = constructPlugin(getDomainConfig({
domainName: "test_domain",
autoDomain: true
}));
plugin.createDomain = async () => null;
plugin.initAWSRegion = async () => null;
const createDomainSpy = chaiSpy.on(plugin, "createDomain");
await plugin.hooks["before:deploy:deploy"]();
expect(createDomainSpy).to.have.been.called();
});
it("before:remove:remove with autoDomain true", async () => {
const APIGatewayMock = mockClient(APIGatewayClient);
APIGatewayMock.on(GetDomainNameCommand).resolves({
domainName: "dummy_domain",
regionalHostedZoneId: "test_id"
});
APIGatewayMock.on(GetBasePathMappingsCommand).resolves({
items: [{
restApiId: "test_rest_api_id",
basePath: "test",
stage: "test"
}]
});
APIGatewayMock.on(DeleteBasePathMappingCommand).resolves(null);
const CloudFormationMock = mockClient(CloudFormationClient);
CloudFormationMock.on(DescribeStackResourceCommand).resolves({
StackResourceDetail: {
LogicalResourceId: Globals.CFResourceIds[Globals.apiTypes.rest],
PhysicalResourceId: "test_rest_api_id",
ResourceType: "",
LastUpdatedTimestamp: null,
ResourceStatus: ResourceStatus.CREATE_COMPLETE
}
});
const domainOptions = getDomainConfig({ domainName: "test_domain" });
const plugin = constructPlugin(domainOptions);
plugin.initAWSRegion = async () => null;
await plugin.hooks["before:remove:remove"]();
const commandCalls = APIGatewayMock.commandCalls(DeleteBasePathMappingCommand);
expect(commandCalls.length).to.equal(1);
});
it("before:remove:remove with autoDomain false", async () => {
const APIGatewayMock = mockClient(APIGatewayClient);
APIGatewayMock.on(GetDomainNameCommand).resolves({
domainName: "dummy_domain",
regionalHostedZoneId: "test_id"
});
APIGatewayMock.on(GetBasePathMappingsCommand).resolves({
items: [{
restApiId: "test_rest_api_id",
basePath: "test2",
stage: "dummy"
}, {
restApiId: "test_rest_api_id",
basePath: "test",
stage: "test"
}]
});
APIGatewayMock.on(DeleteBasePathMappingCommand).resolves(null);
const CloudFormationMock = mockClient(CloudFormationClient);
CloudFormationMock.on(DescribeStackResourceCommand).resolves({
StackResourceDetail: {
LogicalResourceId: Globals.CFResourceIds[Globals.apiTypes.rest],
PhysicalResourceId: "test_rest_api_id",
ResourceType: "",
LastUpdatedTimestamp: null,
ResourceStatus: ResourceStatus.CREATE_COMPLETE
}
});
const domainOptions = getDomainConfig({
domainName: "test_domain",
autoDomain: true
});
const plugin = constructPlugin(domainOptions);
plugin.initAWSRegion = async () => null;
plugin.deleteDomain = async () => null;
const deleteDomainSpy = chaiSpy.on(plugin, "deleteDomain");
await plugin.hooks["before:remove:remove"]();
expect(deleteDomainSpy).to.have.been.called();
});
it("create_domain:create", async () => {
const APIGatewayMock = mockClient(APIGatewayClient);
APIGatewayMock.on(GetDomainNameCommand).resolves({
domainName: "dummy_domain",
regionalHostedZoneId: "test_id"
});
const Route53Mock = mockClient(Route53Client);
Route53Mock.on(ListHostedZonesCommand).resolves({
HostedZones: [{
CallerReference: "1",
Config: { PrivateZone: true },
Id: "public_host_id",
Name: "test_domain"
}]
});
Route53Mock.on(ChangeResourceRecordSetsCommand).resolves(null);
const plugin = constructPlugin(getDomainConfig({ domainName: "test_domain" }));
plugin.initAWSRegion = async () => null;
plugin.deleteDomain = async () => null;
const deleteDomainSpy = chaiSpy.on(plugin, "deleteDomain");
await plugin.hooks["create_domain:create"]();
const commandCalls = Route53Mock.commandCalls(ChangeResourceRecordSetsCommand);
expect(commandCalls.length).to.equal(1);
expect(deleteDomainSpy).to.not.have.been.called();
});
it("create_domain:create with no domain info", async () => {
const APIGatewayMock = mockClient(APIGatewayClient);
APIGatewayMock.on(GetDomainNameCommand).rejects({ $metadata: { httpStatusCode: 404 } });
APIGatewayMock.on(CreateDomainNameCommand).resolves({
distributionDomainName: "foo",
securityPolicy: "TLS_1_0"
});
const ACMCMock = mockClient(ACMClient);
ACMCMock.on(ListCertificatesCommand).resolves({
CertificateSummaryList: [{
CertificateArn: "test_certificate_arn",
DomainName: "test_domain"
}]
});
const Route53Mock = mockClient(Route53Client);
Route53Mock.on(ListHostedZonesCommand).resolves({
HostedZones: [{
CallerReference: "1",
Config: { PrivateZone: true },
Id: "public_host_id",
Name: "test_domain"
}]
});
Route53Mock.on(ChangeResourceRecordSetsCommand).resolves(null);
const plugin = constructPlugin(getDomainConfig({ domainName: "test_domain" }));
plugin.initAWSRegion = async () => null;
plugin.deleteDomain = async () => null;
const deleteDomainSpy = chaiSpy.on(plugin, "deleteDomain");
await plugin.hooks["create_domain:create"]();
const commandCalls = Route53Mock.commandCalls(ChangeResourceRecordSetsCommand);
expect(commandCalls.length).to.equal(1);
expect(deleteDomainSpy).to.not.have.been.called();
});
it("delete_domain:delete", async () => {
const APIGatewayMock = mockClient(APIGatewayClient);
APIGatewayMock.on(GetDomainNameCommand).resolves({
domainName: "dummy_domain",
regionalHostedZoneId: "test_id"
});
APIGatewayMock.on(DeleteDomainNameCommand).resolves(null);
const ACMCMock = mockClient(ACMClient);
ACMCMock.on(ListCertificatesCommand).resolves({
CertificateSummaryList: [{
CertificateArn: "test_certificate_arn",
DomainName: "test_domain"
}]
});
const Route53Mock = mockClient(Route53Client);
Route53Mock.on(ListHostedZonesCommand).resolves({
HostedZones: [{
CallerReference: "1",
Config: { PrivateZone: true },
Id: "public_host_id",
Name: "test_domain"
}]
});
Route53Mock.on(ChangeResourceRecordSetsCommand).resolves(null);
const plugin = constructPlugin(getDomainConfig({ domainName: "test_domain" }));
plugin.initAWSRegion = async () => null;
await plugin.hooks["delete_domain:delete"]();
const commandCalls = Route53Mock.commandCalls(ChangeResourceRecordSetsCommand);
expect(commandCalls.length).to.equal(1);
});
});
});