Skip to content

Commit f7455a4

Browse files
committed
chore: Add tests for GuAllowPolicy and GuDenyPolicy
Quite simple tests to demonstrate these two helper constructs.
1 parent d69bdbe commit f7455a4

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import "@aws-cdk/assert/jest";
2+
import "../../../utils/test/jest";
3+
import { attachPolicyToTestRole, simpleGuStackForTesting } from "../../../utils/test";
4+
import { GuAllowPolicy, GuDenyPolicy } from "./base-policy";
5+
6+
describe("GuAllowPolicy", () => {
7+
test("if a single action is provided, the resulting resource's action will be a single item", () => {
8+
const stack = simpleGuStackForTesting();
9+
attachPolicyToTestRole(
10+
stack,
11+
new GuAllowPolicy(stack, "AllowS3GetObject", {
12+
actions: ["s3:GetObject"],
13+
resources: ["*"],
14+
})
15+
);
16+
17+
expect(stack).toHaveResource("AWS::IAM::Policy", {
18+
PolicyDocument: {
19+
Version: "2012-10-17",
20+
Statement: [
21+
{
22+
Action: "s3:GetObject",
23+
Effect: "Allow",
24+
Resource: "*",
25+
},
26+
],
27+
},
28+
});
29+
});
30+
31+
test("if multiple actions are provided, the resulting resource's action will be an array", () => {
32+
const stack = simpleGuStackForTesting();
33+
attachPolicyToTestRole(
34+
stack,
35+
new GuAllowPolicy(stack, "AllowS3GetObject", {
36+
actions: ["s3:GetObject", "s3:ListBucket"],
37+
resources: ["*"],
38+
})
39+
);
40+
41+
expect(stack).toHaveResource("AWS::IAM::Policy", {
42+
PolicyDocument: {
43+
Version: "2012-10-17",
44+
Statement: [
45+
{
46+
Action: ["s3:GetObject", "s3:ListBucket"],
47+
Effect: "Allow",
48+
Resource: "*",
49+
},
50+
],
51+
},
52+
});
53+
});
54+
});
55+
56+
describe("GuDenyPolicy", () => {
57+
test("if a single action is provided, the resulting resource's action will be a single item", () => {
58+
const stack = simpleGuStackForTesting();
59+
attachPolicyToTestRole(
60+
stack,
61+
new GuDenyPolicy(stack, "DenyS3GetObject", {
62+
actions: ["s3:GetObject"],
63+
resources: ["*"],
64+
})
65+
);
66+
67+
expect(stack).toHaveResource("AWS::IAM::Policy", {
68+
PolicyDocument: {
69+
Version: "2012-10-17",
70+
Statement: [
71+
{
72+
Action: "s3:GetObject",
73+
Effect: "Deny",
74+
Resource: "*",
75+
},
76+
],
77+
},
78+
});
79+
});
80+
81+
test("if multiple actions are provided, the resulting resource's action will be an array", () => {
82+
const stack = simpleGuStackForTesting();
83+
attachPolicyToTestRole(
84+
stack,
85+
new GuDenyPolicy(stack, "DenyS3GetObject", {
86+
actions: ["s3:GetObject", "s3:ListBucket"],
87+
resources: ["*"],
88+
})
89+
);
90+
91+
expect(stack).toHaveResource("AWS::IAM::Policy", {
92+
PolicyDocument: {
93+
Version: "2012-10-17",
94+
Statement: [
95+
{
96+
Action: ["s3:GetObject", "s3:ListBucket"],
97+
Effect: "Deny",
98+
Resource: "*",
99+
},
100+
],
101+
},
102+
});
103+
});
104+
});

0 commit comments

Comments
 (0)