Skip to content

Commit

Permalink
Add tests for assets mapped to s3 buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
robchett committed Jun 16, 2023
1 parent f5fb5fc commit 1858711
Showing 1 changed file with 125 additions and 5 deletions.
130 changes: 125 additions & 5 deletions test/unit/serverSideWebsite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,41 @@ describe("server-side website", () => {
type: "server-side-website",
assets: {
"/assets/*": "public",
"/s3-bucket/*": "s3://some-bucket",
"/s3-bucket-with-path/*": "s3://some-other-bucket/some-path",
"/s3-bucket-repeat/a/*": "s3://some-bucket-repeat",
"/s3-bucket-repeat/b/*": "s3://some-bucket-repeat",
},
},
},
}),
});
const bucketLogicalId = computeLogicalId("backend", "Assets");
const bucketPolicyLogicalId = computeLogicalId("backend", "Assets", "Policy");
const originAccessIdentityLogicalId = computeLogicalId("backend", "CDN", "Origin2", "S3Origin");
const originAccessIdentityLogicalId1 = computeLogicalId("backend", "CDN", "Origin2", "S3Origin");
const originAccessIdentityLogicalId2 = computeLogicalId("backend", "CDN", "Origin3", "S3Origin");
const originAccessIdentityLogicalId3 = computeLogicalId("backend", "CDN", "Origin4", "S3Origin");
const originAccessIdentityLogicalId4 = computeLogicalId("backend", "CDN", "Origin5", "S3Origin");
const originAccessIdentityLogicalId5 = computeLogicalId("backend", "CDN", "Origin6", "S3Origin");
const cfDistributionLogicalId = computeLogicalId("backend", "CDN");
const cfOriginId1 = computeLogicalId("backend", "CDN", "Origin1");
const cfOriginId2 = computeLogicalId("backend", "CDN", "Origin2");
const cfOriginId3 = computeLogicalId("backend", "CDN", "Origin3");
const cfOriginId4 = computeLogicalId("backend", "CDN", "Origin4");
const cfOriginId5 = computeLogicalId("backend", "CDN", "Origin5");
const cfOriginId6 = computeLogicalId("backend", "CDN", "Origin6");
const requestFunction = computeLogicalId("backend", "RequestFunction");
expect(Object.keys(cfTemplate.Resources)).toStrictEqual([
"ServerlessDeploymentBucket",
"ServerlessDeploymentBucketPolicy",
bucketLogicalId,
bucketPolicyLogicalId,
requestFunction,
originAccessIdentityLogicalId,
originAccessIdentityLogicalId1,
originAccessIdentityLogicalId2,
originAccessIdentityLogicalId3,
originAccessIdentityLogicalId4,
originAccessIdentityLogicalId5,
cfDistributionLogicalId,
]);
expect(cfTemplate.Resources[bucketLogicalId]).toMatchObject({
Expand All @@ -55,7 +71,7 @@ describe("server-side website", () => {
Action: "s3:GetObject",
Effect: "Allow",
Principal: {
CanonicalUser: { "Fn::GetAtt": [originAccessIdentityLogicalId, "S3CanonicalUserId"] },
CanonicalUser: { "Fn::GetAtt": [originAccessIdentityLogicalId1, "S3CanonicalUserId"] },
},
Resource: { "Fn::Join": ["", [{ "Fn::GetAtt": [bucketLogicalId, "Arn"] }, "/*"]] },
},
Expand All @@ -64,7 +80,7 @@ describe("server-side website", () => {
},
},
});
expect(cfTemplate.Resources[originAccessIdentityLogicalId]).toStrictEqual({
expect(cfTemplate.Resources[originAccessIdentityLogicalId1]).toStrictEqual({
Type: "AWS::CloudFront::CloudFrontOriginAccessIdentity",
Properties: {
CloudFrontOriginAccessIdentityConfig: {
Expand Down Expand Up @@ -106,6 +122,38 @@ describe("server-side website", () => {
TargetOriginId: cfOriginId2,
ViewerProtocolPolicy: "redirect-to-https",
},
{
AllowedMethods: ["GET", "HEAD", "OPTIONS"],
CachePolicyId: "658327ea-f89d-4fab-a63d-7e88639e58f6",
Compress: true,
PathPattern: "/s3-bucket/*",
TargetOriginId: cfOriginId3,
ViewerProtocolPolicy: "redirect-to-https",
},
{
AllowedMethods: ["GET", "HEAD", "OPTIONS"],
CachePolicyId: "658327ea-f89d-4fab-a63d-7e88639e58f6",
Compress: true,
PathPattern: "/s3-bucket-with-path/*",
TargetOriginId: cfOriginId4,
ViewerProtocolPolicy: "redirect-to-https",
},
{
AllowedMethods: ["GET", "HEAD", "OPTIONS"],
CachePolicyId: "658327ea-f89d-4fab-a63d-7e88639e58f6",
Compress: true,
PathPattern: "/s3-bucket-repeat/a/*",
TargetOriginId: cfOriginId5,
ViewerProtocolPolicy: "redirect-to-https",
},
{
AllowedMethods: ["GET", "HEAD", "OPTIONS"],
CachePolicyId: "658327ea-f89d-4fab-a63d-7e88639e58f6",
Compress: true,
PathPattern: "/s3-bucket-repeat/b/*",
TargetOriginId: cfOriginId6,
ViewerProtocolPolicy: "redirect-to-https",
},
],
Enabled: true,
HttpVersion: "http2and3",
Expand All @@ -128,7 +176,79 @@ describe("server-side website", () => {
OriginAccessIdentity: {
"Fn::Join": [
"",
["origin-access-identity/cloudfront/", { Ref: originAccessIdentityLogicalId }],
["origin-access-identity/cloudfront/", { Ref: originAccessIdentityLogicalId1 }],
],
},
},
},
{
DomainName: {
"Fn::Join": [
"",
["some-bucket.s3.", { Ref: "AWS::Region" }, ".", { Ref: "AWS::URLSuffix" }],
],
},
Id: cfOriginId3,
OriginPath: "",
S3OriginConfig: {
OriginAccessIdentity: {
"Fn::Join": [
"",
["origin-access-identity/cloudfront/", { Ref: originAccessIdentityLogicalId2 }],
],
},
},
},
{
DomainName: {
"Fn::Join": [
"",
["some-other-bucket.s3.", { Ref: "AWS::Region" }, ".", { Ref: "AWS::URLSuffix" }],
],
},
Id: cfOriginId4,
OriginPath: "/some-path",
S3OriginConfig: {
OriginAccessIdentity: {
"Fn::Join": [
"",
["origin-access-identity/cloudfront/", { Ref: originAccessIdentityLogicalId3 }],
],
},
},
},
{
DomainName: {
"Fn::Join": [
"",
["some-bucket-repeat.s3.", { Ref: "AWS::Region" }, ".", { Ref: "AWS::URLSuffix" }],
],
},
Id: cfOriginId5,
OriginPath: "",
S3OriginConfig: {
OriginAccessIdentity: {
"Fn::Join": [
"",
["origin-access-identity/cloudfront/", { Ref: originAccessIdentityLogicalId4 }],
],
},
},
},
{
DomainName: {
"Fn::Join": [
"",
["some-bucket-repeat.s3.", { Ref: "AWS::Region" }, ".", { Ref: "AWS::URLSuffix" }],
],
},
Id: cfOriginId6,
OriginPath: "",
S3OriginConfig: {
OriginAccessIdentity: {
"Fn::Join": [
"",
["origin-access-identity/cloudfront/", { Ref: originAccessIdentityLogicalId5 }],
],
},
},
Expand Down

0 comments on commit 1858711

Please sign in to comment.