Skip to content

Commit

Permalink
Merge pull request #77 from getlift/improve-tests
Browse files Browse the repository at this point in the history
Improve tests
  • Loading branch information
fredericbarthelet authored Jul 14, 2021
2 parents 94e25c2 + 1d9cd45 commit 53d46aa
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 78 deletions.
5 changes: 4 additions & 1 deletion test/fixtures/storage/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ provider:
name: aws

constructs:
avatars:
default:
type: storage
kmsEncryption:
type: storage
encryption: kms
10 changes: 7 additions & 3 deletions test/fixtures/webhooks/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
service: storage
service: webhook

provider:
name: aws

constructs:
stripe:
secure:
type: webhook
authorizer:
handler: authorizer.main
path: /webhook
path: /secure
insecure:
type: webhook
insecure: true
path: /insecure
11 changes: 0 additions & 11 deletions test/fixtures/webhooksInsecure/serverless.yml

This file was deleted.

93 changes: 46 additions & 47 deletions test/unit/storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
import { pluginConfigExt, runServerless } from "../utils/runServerless";

describe("storage", () => {
it("should create an S3 bucket", async () => {
const { cfTemplate, computeLogicalId } = await runServerless({
let cfTemplate: { Resources: Record<string, { Properties: Record<string, unknown> }> };
let computeLogicalId: (...address: string[]) => string;
beforeAll(async () => {
({ cfTemplate, computeLogicalId } = await runServerless({
fixture: "storage",
configExt: pluginConfigExt,
command: "package",
}));
});
describe("common tests", () => {
const useCases = [["default"], ["kmsEncryption"]];
test.each(useCases)("%p - should configure a lifecycle policy", (useCase) => {
expect(
cfTemplate.Resources[computeLogicalId(useCase, "Bucket")].Properties.LifecycleConfiguration
).toMatchObject({
Rules: [
{
Status: "Enabled",
Transitions: [
{
StorageClass: "INTELLIGENT_TIERING",
TransitionInDays: 0,
},
],
},
{
NoncurrentVersionExpirationInDays: 30,
Status: "Enabled",
},
],
});
});
test.each(useCases)("%p - should have versionning enabled", (useCase) => {
expect(
cfTemplate.Resources[computeLogicalId(useCase, "Bucket")].Properties.VersioningConfiguration
).toStrictEqual({ Status: "Enabled" });
});
const bucketId = computeLogicalId("avatars", "Bucket");
expect(Object.keys(cfTemplate.Resources)).toStrictEqual([
"ServerlessDeploymentBucket",
"ServerlessDeploymentBucketPolicy",
bucketId,
computeLogicalId("avatars", "Bucket", "Policy"),
]);
expect(cfTemplate.Resources[bucketId]).toStrictEqual({
Type: "AWS::S3::Bucket",
UpdateReplacePolicy: "Retain",
DeletionPolicy: "Retain",
Properties: {
BucketEncryption: {
ServerSideEncryptionConfiguration: [
{
ServerSideEncryptionByDefault: { SSEAlgorithm: "AES256" },
},
],
},
LifecycleConfiguration: {
Rules: [
{
Status: "Enabled",
Transitions: [
{
StorageClass: "INTELLIGENT_TIERING",
TransitionInDays: 0,
},
],
},
{
NoncurrentVersionExpirationInDays: 30,
Status: "Enabled",
},
],
},
PublicAccessBlockConfiguration: {
BlockPublicAcls: true,
BlockPublicPolicy: true,
IgnorePublicAcls: true,
RestrictPublicBuckets: true,
},
VersioningConfiguration: {
Status: "Enabled",
},
});

test.each([
["default", "AES256"],
["kmsEncryption", "aws:kms"],
])("should allow %p encryption", (construct, expectedSSEAlgorithm) => {
expect(cfTemplate.Resources[computeLogicalId(construct, "Bucket")].Properties).toMatchObject({
BucketEncryption: {
ServerSideEncryptionConfiguration: [
{
ServerSideEncryptionByDefault: { SSEAlgorithm: expectedSSEAlgorithm },
},
],
},
});
});
Expand Down
27 changes: 11 additions & 16 deletions test/unit/webhooks.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import { pluginConfigExt, runServerless } from "../utils/runServerless";

describe("webhooks", () => {
it("should implement custom authorizer by default", async () => {
const { cfTemplate, computeLogicalId } = await runServerless({
let cfTemplate: { Resources: Record<string, { Properties: Record<string, unknown> }> };
let computeLogicalId: (...address: string[]) => string;
beforeAll(async () => {
({ cfTemplate, computeLogicalId } = await runServerless({
fixture: "webhooks",
configExt: pluginConfigExt,
command: "package",
});
expect(cfTemplate.Resources[computeLogicalId("stripe", "Route")]).toMatchObject({
Properties: {
AuthorizationType: "CUSTOM",
},
});
}));
});
it("should allow insecure webhook", async () => {
const { cfTemplate, computeLogicalId } = await runServerless({
fixture: "webhooksInsecure",
configExt: pluginConfigExt,
command: "package",
});
expect(cfTemplate.Resources[computeLogicalId("github", "Route")]).toMatchObject({
test.each([
["secure", "CUSTOM"],
["insecure", "NONE"],
])("%p webhook should have authorization type %p", (useCase, expectedAuthorizationType) => {
expect(cfTemplate.Resources[computeLogicalId(useCase, "Route")]).toMatchObject({
Properties: {
AuthorizationType: "NONE",
AuthorizationType: expectedAuthorizationType,
},
});
});
Expand Down

0 comments on commit 53d46aa

Please sign in to comment.