-
Notifications
You must be signed in to change notification settings - Fork 544
/
index.spec.ts
79 lines (73 loc) · 2.74 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { ARN, build, parse, validate } from "./index";
describe("validate", () => {
it("should validate whether input is a qualified resource ARN", () => {
expect(validate("arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint")).toBe(true);
expect(validate("arn:aws:s3:us-east-1:123456789012:accesspoint:myendpoint")).toBe(true);
expect(validate("arn:aws:s3:us-east-1:123456789012:accesspoint/myendpoint")).toBe(true);
expect(validate("arn:aws-cn:s3:cn-north-1:123456789012:accesspoint:myendpoint")).toBe(true);
expect(validate("arn:aws:sns:us-west-2:123456789012:myTopic")).toBe(true);
expect(validate("some:random:string:separated:by:colons")).toBe(false);
expect(validate("arn:aws:too:short")).toBe(false);
});
});
describe("parser", () => {
it("should parse valid resource ARNs", () => {
expect(parse("arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint")).toEqual({
partition: "aws",
service: "s3",
region: "us-west-2",
accountId: "123456789012",
resource: "accesspoint:myendpoint",
});
expect(parse("arn:aws:s3:us-east-1:123456789012:accesspoint:myendpoint")).toEqual({
partition: "aws",
service: "s3",
region: "us-east-1",
accountId: "123456789012",
resource: "accesspoint:myendpoint",
});
expect(parse("arn:aws-cn:s3:cn-north-1:123456789012:accesspoint:myendpoint")).toEqual({
partition: "aws-cn",
service: "s3",
region: "cn-north-1",
accountId: "123456789012",
resource: "accesspoint:myendpoint",
});
expect(parse("arn:aws:sns:us-west-2:123456789012:myTopic")).toEqual({
partition: "aws",
service: "sns",
region: "us-west-2",
accountId: "123456789012",
resource: "myTopic",
});
expect(parse("arn:aws:sns:::myTopic")).toEqual({
partition: "aws",
service: "sns",
region: "",
accountId: "",
resource: "myTopic",
});
});
it("should throw at invalid ARN strings", () => {
const invalidArns = ["some:random:string:separated:by:colons", "arn:aws:too:short"];
invalidArns.forEach((arn) => {
expect(() => parse(arn)).toThrow("Malformed ARN");
});
});
});
describe("builder", () => {
const builderArn = {
service: "s3",
region: "us-east-1",
accountId: "123456789012",
resource: "accesspoint:myendpoint",
};
it("should build valid ARN object to string", () => {
expect(build(builderArn)).toBe("arn:aws:s3:us-east-1:123456789012:accesspoint:myendpoint");
});
["service", "region", "accountId", "resource"].forEach((option) => {
it(`should throw if "${option}" is missing`, () => {
expect(() => build({ ...builderArn, [option]: undefined })).toThrow(new Error("Input ARN object is invalid"));
});
});
});