Skip to content

Commit

Permalink
CHANGE - moved tests to appropriate files
Browse files Browse the repository at this point in the history
  • Loading branch information
sromic committed Aug 14, 2023
1 parent 63ed97f commit 91c883a
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 254 deletions.
33 changes: 33 additions & 0 deletions test/unit-tests/aws/acm-wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ describe("ACM Wrapper checks", () => {
const actualResult = await acmWrapper.getCertArn(dc);
expect(actualResult).to.equal(testCertificateArnByDomain);
});

it("getCertArn by domain name by getting all paginated certificates from AWS", async () => {
const ACMCMock = mockClient(ACMClient);
ACMCMock.on(ListCertificatesCommand)
.resolvesOnce({
CertificateSummaryList: [
{
CertificateArn: "test_domain_arn",
DomainName: "test_domain",
Status: "ISSUED",
},
],
NextToken: 'NextToken',
})
.resolves({
CertificateSummaryList: [
{
CertificateArn: "test_domain_arn2",
DomainName: "test_domain2",
Status: "ISSUED",
},
],
});

const acmWrapper = new ACMWrapper(null, Globals.endpointTypes.regional);
const dc = new DomainConfig(getDomainConfig({
domainName: "test_domain",
}));

const actualResult = await acmWrapper.getCertArn(dc);
expect(actualResult).to.equal(testCertificateArnByDomain);
expect(ACMCMock.calls().length).to.equal(2);
});

it("empty getCertArn by certificate name", async () => {
const ACMCMock = mockClient(ACMClient);
Expand Down
38 changes: 38 additions & 0 deletions test/unit-tests/aws/api-gateway-v1-wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,44 @@ describe("API Gateway V1 wrapper checks", () => {

expect(commandCalls.length).to.equal(1);
});

it("get all base path mappings", async () => {
const APIGatewayCMock = mockClient(APIGatewayClient);
APIGatewayCMock.on(GetBasePathMappingsCommand)
.resolvesOnce({
items: [
{
restApiId: "test_rest_api_id",
basePath: "test",
stage: "test"
},
],
position: "position",
})
.resolves({
items: [
{
restApiId: "test_rest_api_id2",
basePath: "test2",
stage: "test",
},
],
});

const apiGatewayV1Wrapper = new APIGatewayV1Wrapper();
const dc = new DomainConfig(getDomainConfig({
domainName: "test_domain"
}));

const actualResult = await apiGatewayV1Wrapper.getBasePathMappings(dc);
const expectedResult = [
new ApiGatewayMap("test_rest_api_id", "test", "test", null),
new ApiGatewayMap("test_rest_api_id2", "test2", "test", null),
]

expect(actualResult).to.eql(expectedResult);
expect(APIGatewayCMock.calls().length).to.equal(2);
});

it("get base path mapping failure", async () => {
const APIGatewayMock = mockClient(APIGatewayClient);
Expand Down
35 changes: 35 additions & 0 deletions test/unit-tests/aws/api-gateway-v2-wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,41 @@ describe("API Gateway V2 wrapper checks", () => {

expect(commandCalls.length).to.equal(1);
});

it("get all base path mappings", async () => {
const APIGatewayMock = mockClient(ApiGatewayV2Client);
APIGatewayMock.on(GetApiMappingsCommand).resolvesOnce({
Items: [{
ApiId: "test_rest_api_id",
ApiMappingKey: "test",
Stage: "test",
ApiMappingId: "test_id"
}],
NextToken: "NextToken"
})
.resolves({
Items: [{
ApiId: "test_rest_api_id2",
ApiMappingKey: "test2",
Stage: "test",
ApiMappingId: "test_id2"
}]
});

const apiGatewayV2Wrapper = new APIGatewayV2Wrapper();
const dc = new DomainConfig(getDomainConfig({
domainName: "test_domain"
}));

const actualResult = await apiGatewayV2Wrapper.getBasePathMappings(dc);
const expectedResult = [
new ApiGatewayMap("test_rest_api_id", "test", "test", "test_id"),
new ApiGatewayMap("test_rest_api_id2", "test2", "test", "test_id2")
]

expect(actualResult).to.eql(expectedResult);
expect(APIGatewayMock.calls().length).to.equal(2);
});

it("get base path mapping failure", async () => {
const APIGatewayMock = mockClient(ApiGatewayV2Client);
Expand Down
98 changes: 98 additions & 0 deletions test/unit-tests/aws/cloud-formation-wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,38 @@ describe("Cloud Formation wrapper checks", () => {
const commandCalls = CloudFormationMock.commandCalls(ListExportsCommand, expectedParams, true);
expect(commandCalls.length).to.equal(1);
});

it("findApiId for the rest api type via Fn::ImportValue paginated", async () => {
const fnImportValue = "test-value";
const CloudFormationMock = mockClient(CloudFormationClient);
CloudFormationMock.on(ListExportsCommand).resolvesOnce({
Exports: [
{Name: "test-name", Value: fnImportValue},
{Name: "dummy-name", Value: "dummy-value"},
],
NextToken: "NextToken"
})
.resolves({
Exports: [
{Name: "test-name2", Value: "test-name2"},
{Name: "dummy-name2", Value: "dummy-value2"},
]
});

const cloudFormationWrapper = new CloudFormationWrapper();
Globals.serverless.service.provider.apiGateway.restApiId = {
[Globals.CFFuncNames.fnImport]: "test-name"
};

const actualResult = await cloudFormationWrapper.findApiId(Globals.apiTypes.rest)
expect(actualResult).to.equal(fnImportValue);

const expectedParams = {
NextToken: "NextToken"
};
const commandCalls = CloudFormationMock.commandCalls(ListExportsCommand, expectedParams, true);
expect(commandCalls.length).to.equal(2);
});

it("findApiId for the rest api type via Fn::ImportValue not found", async () => {
const fnImportValue = "test-value";
Expand Down Expand Up @@ -265,6 +297,72 @@ describe("Cloud Formation wrapper checks", () => {
const allCommandCalls = CloudFormationMock.commandCalls(DescribeStackResourceCommand);
expect(allCommandCalls.length).to.equal(2);
});

it("findApiId for the rest api type with paginated nested stacks", async () => {
const physicalResourceId = "test_rest_api_id";
const nestedStackName = "custom-stage-name-NestedStackTwo-U89W84TQIHJK";
const CloudFormationMock = mockClient(CloudFormationClient);
CloudFormationMock.on(DescribeStackResourceCommand).rejectsOnce()
.resolves({
StackResourceDetail: {
LogicalResourceId: Globals.CFResourceIds[Globals.apiTypes.rest],
PhysicalResourceId: physicalResourceId,
ResourceType: "",
LastUpdatedTimestamp: undefined,
ResourceStatus: ResourceStatus.CREATE_COMPLETE,
},
});
CloudFormationMock.on(DescribeStacksCommand).resolvesOnce({
Stacks: [
{
StackName: "custom-stage-name-NestedStackOne-U89W84TQIHJK",
RootId: "arn:aws:cloudformation:us-east-1:000000000000:stack/dummy-name/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
CreationTime: null,
StackStatus: StackStatus.CREATE_COMPLETE
},
{
StackName: nestedStackName,
RootId: `arn:aws:cloudformation:us-east-1:000000000000:stack/${Globals.serverless.service.provider.stackName}/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`,
CreationTime: null,
StackStatus: StackStatus.CREATE_COMPLETE
},
{
StackName: "outside-stack-NestedStackZERO-U89W84TQIHJK",
RootId: null,
CreationTime: null,
StackStatus: StackStatus.CREATE_COMPLETE
},
],
NextToken: "NextToken"
})
.resolves({
Stacks: [
{
StackName: "custom-stage-name-NestedStackOne-U89W84TQ1235",
RootId: "arn:aws:cloudformation:us-east-1:000000000000:stack/dummy-name2/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
CreationTime: null,
StackStatus: StackStatus.CREATE_COMPLETE
}
],
});

const actualResult = await new CloudFormationWrapper().findApiId(Globals.apiTypes.rest)
expect(actualResult).to.equal(physicalResourceId);

const expectedParams = {
LogicalResourceId: Globals.CFResourceIds[Globals.apiTypes.rest],
StackName: nestedStackName,
};

const commandCalls = CloudFormationMock.commandCalls(DescribeStackResourceCommand, expectedParams, true);
expect(commandCalls.length).to.equal(1);

const allCommandCalls = CloudFormationMock.commandCalls(DescribeStackResourceCommand);
expect(allCommandCalls.length).to.equal(2);

const describeStacksCommandCalls = CloudFormationMock.commandCalls(DescribeStacksCommand);
expect(describeStacksCommandCalls.length).to.equal(2);
});

it("findApiId for the rest api type with nested stacks failure", async () => {
const nestedStackName = "custom-stage-name-NestedStackTwo-U89W84TQIHJK";
Expand Down
83 changes: 82 additions & 1 deletion test/unit-tests/aws/route53-wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,87 @@ describe("Route53 wrapper checks", () => {
actualId = await new Route53Wrapper().getRoute53HostedZoneId(dc);
expect(actualId).to.equal(dc.hostedZoneId);
});

it("get route53 hosted zone id paginated", async () => {
const testId = "test_host_id"
const Route53Mock = mockClient(Route53Client);
Route53Mock.on(ListHostedZonesCommand).resolvesOnce({
HostedZones: [
{
CallerReference: "1",
Config: {PrivateZone: false},
Id: testId,
Name: "test_domain",
}, {
CallerReference: "2",
Config: {PrivateZone: false},
Id: testId,
Name: "dummy_test_domain",
}, {
CallerReference: "3",
Config: {PrivateZone: false},
Id: testId,
Name: "domain",
}
],
NextMarker: "NextMarker"
})
.resolvesOnce({
HostedZones: [
{
CallerReference: "4",
Config: {PrivateZone: false},
Id: testId,
Name: "test_domain2",
}, {
CallerReference: "5",
Config: {PrivateZone: false},
Id: testId,
Name: "dummy_test_domain2",
}, {
CallerReference: "6",
Config: {PrivateZone: false},
Id: testId,
Name: "domain2",
}
],
NextMarker: "NextMarker"
})
.resolves({
HostedZones: [
{
CallerReference: "7",
Config: {PrivateZone: false},
Id: testId,
Name: "test_domain3",
}, {
CallerReference: "8",
Config: {PrivateZone: false},
Id: testId,
Name: "dummy_test_domain3",
}, {
CallerReference: "9",
Config: {PrivateZone: false},
Id: testId,
Name: "domain3",
}
]
});

const dc = new DomainConfig(getDomainConfig({
domainName: "test_domain"
}));

let actualId = await new Route53Wrapper().getRoute53HostedZoneId(dc);
expect(actualId).to.equal(testId);

const commandCalls = Route53Mock.commandCalls(ListHostedZonesCommand, {});
expect(commandCalls.length).to.equal(3);

dc.hostedZoneId = "test_id"
actualId = await new Route53Wrapper().getRoute53HostedZoneId(dc);
expect(actualId).to.equal(dc.hostedZoneId);
});

it("get route53 hosted zone id public", async () => {
const testId = "test_host_id"
Expand Down Expand Up @@ -101,7 +182,7 @@ describe("Route53 wrapper checks", () => {
expect(commandCalls.length).to.equal(1);
});

it("get route53 hosted zone id privet", async () => {
it("get route53 hosted zone id private", async () => {
const testId = "test_host_id"
const Route53Mock = mockClient(Route53Client);
Route53Mock.on(ListHostedZonesCommand).resolves({
Expand Down
Loading

0 comments on commit 91c883a

Please sign in to comment.