-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaws-https-with-aws.test.ts
More file actions
140 lines (121 loc) · 4.41 KB
/
aws-https-with-aws.test.ts
File metadata and controls
140 lines (121 loc) · 4.41 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Set AWS credentials early to override any set in the calling environment.
process.env.AWS_SECRET_ACCESS_KEY = "abc123";
process.env.AWS_ACCESS_KEY_ID = "deadbeaf";
delete process.env.AWS_SESSION_TOKEN;
import { AwsHttps } from "./aws-https";
import nock from "nock";
describe("AwsHttps with AWS", () => {
const serverHostname = "example.com";
const serverUrl = "https://" + serverHostname;
afterEach(() => {
nock.cleanAll();
});
describe("with provided AWS credentials", () => {
test("does a signed POST", async () => {
// // GIVEN
const awsCredentials = {
accessKeyId: "ACCESS-KEY-ID",
secretAccessKey: "SECRET-ACCESS-KEY",
sessionToken: "SESSION-TOKEN",
};
const body = JSON.stringify({ cursor: 3, map: "Irvine" });
const response = { success: true };
const scope = nock(serverUrl, {
reqheaders: {
"X-Amz-Date": /20......T......Z/,
Authorization:
/AWS4-HMAC-SHA256 Credential=ACCESS-KEY-ID.*, SignedHeaders=content-length;content-type;host;x-amz-date;x-amz-security-token, Signature=.*/,
},
})
.post("/test", body)
.reply(200, response);
const sut = new AwsHttps(true, awsCredentials);
expect(AwsHttps["credentialsInitializedPromise"]).toBeUndefined();
expect(sut["awsCredentials"]).toBe(awsCredentials);
// WHEN
const reply = await sut.request({
protocol: "https:",
method: "POST",
hostname: serverHostname,
path: "/test",
body: body,
awsSign: true,
headers: { "Content-Type": "application/json" },
});
// THEN
expect(reply).toEqual(response);
expect(scope.isDone()).toBeTruthy();
expect(AwsHttps["credentialsInitializedPromise"]).toBeUndefined();
expect(sut["awsCredentials"]).toBe(awsCredentials);
});
});
describe("with process AWS credentials", () => {
const body = JSON.stringify({ cursor: 3, map: "Irvine" });
const requestOptions = {
protocol: "https:",
method: "POST",
hostname: serverHostname,
path: "/test",
body: body,
awsSign: true,
headers: { "Content-Type": "application/json" },
};
const response = { success: true };
let scope: nock.Scope;
beforeEach(() => {
scope = nock(serverUrl, {
reqheaders: {
"X-Amz-Date": /20......T......Z/,
Authorization:
/AWS4-HMAC-SHA256 Credential=deadbeaf.*, SignedHeaders=content-length;content-type;host;x-amz-date, Signature=.*/,
},
})
.post("/test", body)
.reply(200, response);
});
test("does first signed POST", async () => {
// // GIVEN
const sut = new AwsHttps(true);
expect(AwsHttps["credentialsInitializedPromise"]).toBeUndefined();
expect(sut["awsCredentials"]).toBeUndefined();
// WHEN
const reply = await sut.request(requestOptions);
// THEN
expect(reply).toEqual(response);
expect(scope.isDone()).toBeTruthy();
expect(AwsHttps["credentialsInitializedPromise"]).toBeTruthy();
expect(sut["awsCredentials"]).toBeDefined();
});
test("does another signed POST; reuse creds", async () => {
// // GIVEN
const sut = new AwsHttps(true);
expect(AwsHttps["credentialsInitializedPromise"]).toBeDefined();
expect(sut["awsCredentials"]).toBeUndefined();
// WHEN
const reply = await sut.request(requestOptions);
// THEN
expect(reply).toEqual(response);
expect(scope.isDone()).toBeTruthy();
expect(AwsHttps["credentialsInitializedPromise"]).toBeTruthy();
expect(sut["awsCredentials"]).toBeDefined();
});
test("resets credentials", async () => {
const sut = new AwsHttps(false, true);
expect(AwsHttps["credentialsInitializedPromise"]).toBeUndefined();
expect(sut["awsCredentials"]).toBeUndefined();
});
test("after reset, again do signed POST", async () => {
// // GIVEN
const sut = new AwsHttps(true);
expect(AwsHttps["credentialsInitializedPromise"]).toBeUndefined();
expect(sut["awsCredentials"]).toBeUndefined();
// WHEN
const reply = await sut.request(requestOptions);
// THEN
expect(reply).toEqual(response);
expect(scope.isDone()).toBeTruthy();
expect(AwsHttps["credentialsInitializedPromise"]).toBeTruthy();
expect(sut["awsCredentials"]).toBeDefined();
});
});
});