Skip to content

Commit

Permalink
chore: Add tests for GuAllowPolicy and GuDenyPolicy
Browse files Browse the repository at this point in the history
Quite simple tests to demonstrate these two helper constructs.
  • Loading branch information
akash1810 committed Apr 12, 2021
1 parent d69bdbe commit f7455a4
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/constructs/iam/policies/base-policy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import "@aws-cdk/assert/jest";
import "../../../utils/test/jest";
import { attachPolicyToTestRole, simpleGuStackForTesting } from "../../../utils/test";
import { GuAllowPolicy, GuDenyPolicy } from "./base-policy";

describe("GuAllowPolicy", () => {
test("if a single action is provided, the resulting resource's action will be a single item", () => {
const stack = simpleGuStackForTesting();
attachPolicyToTestRole(
stack,
new GuAllowPolicy(stack, "AllowS3GetObject", {
actions: ["s3:GetObject"],
resources: ["*"],
})
);

expect(stack).toHaveResource("AWS::IAM::Policy", {
PolicyDocument: {
Version: "2012-10-17",
Statement: [
{
Action: "s3:GetObject",
Effect: "Allow",
Resource: "*",
},
],
},
});
});

test("if multiple actions are provided, the resulting resource's action will be an array", () => {
const stack = simpleGuStackForTesting();
attachPolicyToTestRole(
stack,
new GuAllowPolicy(stack, "AllowS3GetObject", {
actions: ["s3:GetObject", "s3:ListBucket"],
resources: ["*"],
})
);

expect(stack).toHaveResource("AWS::IAM::Policy", {
PolicyDocument: {
Version: "2012-10-17",
Statement: [
{
Action: ["s3:GetObject", "s3:ListBucket"],
Effect: "Allow",
Resource: "*",
},
],
},
});
});
});

describe("GuDenyPolicy", () => {
test("if a single action is provided, the resulting resource's action will be a single item", () => {
const stack = simpleGuStackForTesting();
attachPolicyToTestRole(
stack,
new GuDenyPolicy(stack, "DenyS3GetObject", {
actions: ["s3:GetObject"],
resources: ["*"],
})
);

expect(stack).toHaveResource("AWS::IAM::Policy", {
PolicyDocument: {
Version: "2012-10-17",
Statement: [
{
Action: "s3:GetObject",
Effect: "Deny",
Resource: "*",
},
],
},
});
});

test("if multiple actions are provided, the resulting resource's action will be an array", () => {
const stack = simpleGuStackForTesting();
attachPolicyToTestRole(
stack,
new GuDenyPolicy(stack, "DenyS3GetObject", {
actions: ["s3:GetObject", "s3:ListBucket"],
resources: ["*"],
})
);

expect(stack).toHaveResource("AWS::IAM::Policy", {
PolicyDocument: {
Version: "2012-10-17",
Statement: [
{
Action: ["s3:GetObject", "s3:ListBucket"],
Effect: "Deny",
Resource: "*",
},
],
},
});
});
});

0 comments on commit f7455a4

Please sign in to comment.