-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.ts
149 lines (123 loc) · 4.65 KB
/
index.test.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
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
141
142
143
144
145
146
147
148
149
import index from "./index";
import { createServer, IncomingMessage, ServerResponse } from "http";
import listen from "test-listen";
import request from "request-promise-native";
import { RequestError } from "request-promise-native/errors";
import { createRequest, createResponse, MockResponse } from "node-mocks-http";
import { IPullRequestsForRepos } from "weekly-summary-typescript/dist/github";
// @sendgrid/mail did not export a class or interface to reference in the mock
// this constant implements what we need based on the current implementation:
// https://github.com/tmr08c/weekly-sumary-cron-with-now/blob/f993897c67dcadfd39e23ac240d5803adc651326/node_modules/@sendgrid/mail/src/mail.d.ts#L6
const mockEmailer = {
setApiKey: jest.fn(),
setSubstitutionWrappers: jest.fn(),
send: jest.fn(),
sendMultiple: jest.fn(),
MailService: {
setApiKey: jest.fn(),
setSubstitutionWrappers: jest.fn(),
send: jest.fn(),
sendMultiple: jest.fn()
}
};
// There is a guard clause before we send the email to make sure we have the API key set
// Set the environment variable to test the email sending logic
process.env.SENDGRID_API_KEY = "fake-api-key";
test("requests fail if 'organization' is not provided", async () => {
const server = createServer(index);
const url = await listen(server);
await request(url).catch((error: RequestError) => {
expect(error.response.statusCode).toEqual(400);
expect(error.message).toMatch(new RegExp(/provide.*organization/));
});
server.close();
});
test("organization getting passed correctly", async () => {
const req: IncomingMessage = createRequest({
url: "www.example.com?organization=my-org"
});
let resp: MockResponse<ServerResponse> = createResponse();
const mockFetcher = jest.fn();
const fakePullRequests: IPullRequestsForRepos = {
"my-repo-1": [
{
closedAt: new Date(),
createdAt: new Date(),
merged: true,
repository: {
name: "my-repo-1"
},
title: "my first PR",
url: "www.example.com/1"
}
]
};
mockFetcher.mockResolvedValue(fakePullRequests);
await index(req, resp, mockFetcher);
expect(resp.statusCode).toBe(200);
const responseBody = resp._getData();
expect(responseBody).toMatch(new RegExp(/h1.*my-repo-1.*h1/));
expect(responseBody).toMatch(new RegExp(/li.*my first PR.*li/));
});
test("emailing using a single `to` query parameter", async () => {
const req: IncomingMessage = createRequest({
url: "www.example.com?organization=my-org&[email protected]"
});
let resp: MockResponse<ServerResponse> = createResponse();
const mockFetcher = jest.fn();
mockFetcher.mockResolvedValue({});
await index(req, resp, mockFetcher, mockEmailer);
expect(resp.statusCode).toBe(200);
expect(mockEmailer.send).toHaveBeenCalledWith(
expect.objectContaining({ to: ["[email protected]"] })
);
});
test("emailing using a multiple items in the `to` query parameter (separated by ',')", async () => {
const req: IncomingMessage = createRequest({
url:
"www.example.com?organization=my-org&[email protected],[email protected]"
});
let resp: MockResponse<ServerResponse> = createResponse();
const mockFetcher = jest.fn();
mockFetcher.mockResolvedValue({});
await index(req, resp, mockFetcher, mockEmailer);
expect(resp.statusCode).toBe(200);
expect(mockEmailer.send).toHaveBeenCalledWith(
expect.objectContaining({
to: ["[email protected]", "[email protected]"]
})
);
});
test("emailing using a multiple `to` query parameters", async () => {
const req: IncomingMessage = createRequest({
url:
"www.example.com?organization=my-org&[email protected]&[email protected]"
});
let resp: MockResponse<ServerResponse> = createResponse();
const mockFetcher = jest.fn();
mockFetcher.mockResolvedValue({});
await index(req, resp, mockFetcher, mockEmailer);
expect(resp.statusCode).toBe(200);
expect(mockEmailer.send).toHaveBeenCalledWith(
expect.objectContaining({
to: ["[email protected]", "[email protected]"]
})
);
});
test("emails have a reasonable subject that includes the date", async () => {
const req: IncomingMessage = createRequest({
url: "www.example.com?organization=my-org&[email protected]"
});
let resp: MockResponse<ServerResponse> = createResponse();
const mockFetcher = jest.fn();
mockFetcher.mockResolvedValue({});
await index(req, resp, mockFetcher, mockEmailer);
expect(resp.statusCode).toBe(200);
expect(mockEmailer.send).toHaveBeenCalledWith(
expect.objectContaining({
subject: expect.stringMatching(
new RegExp(/weekly summary.*\w \d{1,2} \d{4}/i)
)
})
);
});