forked from anuraghazra/github-readme-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.up.test.js
195 lines (151 loc) · 5.6 KB
/
status.up.test.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @file Tests for the status/up cloud function.
*/
import { jest } from "@jest/globals";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import up, { RATE_LIMIT_SECONDS } from "../api/status/up.js";
import { expect, it, describe, afterEach } from "@jest/globals";
const mock = new MockAdapter(axios);
const successData = {
rateLimit: {
remaining: 4986,
},
};
const faker = (query) => {
const req = {
query: { ...query },
};
const res = {
setHeader: jest.fn(),
send: jest.fn(),
};
return { req, res };
};
const rate_limit_error = {
errors: [
{
type: "RATE_LIMITED",
},
],
};
const bad_credentials_error = {
message: "Bad credentials",
};
const shields_up = {
schemaVersion: 1,
label: "Public Instance",
isError: true,
message: "up",
color: "brightgreen",
};
const shields_down = {
schemaVersion: 1,
label: "Public Instance",
isError: true,
message: "down",
color: "red",
};
afterEach(() => {
mock.reset();
});
describe("Test /api/status/up", () => {
it("should return `true` if request was successful", async () => {
mock.onPost("https://api.github.com/graphql").replyOnce(200, successData);
const { req, res } = faker({}, {});
await up(req, res);
expect(res.setHeader).toBeCalledWith("Content-Type", "application/json");
expect(res.send).toBeCalledWith(true);
});
it("should return `false` if all PATs are rate limited", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, rate_limit_error);
const { req, res } = faker({}, {});
await up(req, res);
expect(res.setHeader).toBeCalledWith("Content-Type", "application/json");
expect(res.send).toBeCalledWith(false);
});
it("should return JSON `true` if request was successful and type='json'", async () => {
mock.onPost("https://api.github.com/graphql").replyOnce(200, successData);
const { req, res } = faker({ type: "json" }, {});
await up(req, res);
expect(res.setHeader).toBeCalledWith("Content-Type", "application/json");
expect(res.send).toBeCalledWith({ up: true });
});
it("should return JSON `false` if all PATs are rate limited and type='json'", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, rate_limit_error);
const { req, res } = faker({ type: "json" }, {});
await up(req, res);
expect(res.setHeader).toBeCalledWith("Content-Type", "application/json");
expect(res.send).toBeCalledWith({ up: false });
});
it("should return UP shields.io config if request was successful and type='shields'", async () => {
mock.onPost("https://api.github.com/graphql").replyOnce(200, successData);
const { req, res } = faker({ type: "shields" }, {});
await up(req, res);
expect(res.setHeader).toBeCalledWith("Content-Type", "application/json");
expect(res.send).toBeCalledWith(shields_up);
});
it("should return DOWN shields.io config if all PATs are rate limited and type='shields'", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, rate_limit_error);
const { req, res } = faker({ type: "shields" }, {});
await up(req, res);
expect(res.setHeader).toBeCalledWith("Content-Type", "application/json");
expect(res.send).toBeCalledWith(shields_down);
});
it("should return `true` if the first PAT is rate limited but the second PATs works", async () => {
mock
.onPost("https://api.github.com/graphql")
.replyOnce(200, rate_limit_error)
.onPost("https://api.github.com/graphql")
.replyOnce(200, successData);
const { req, res } = faker({}, {});
await up(req, res);
expect(res.setHeader).toBeCalledWith("Content-Type", "application/json");
expect(res.send).toBeCalledWith(true);
});
it("should return `true` if the first PAT has 'Bad credentials' but the second PAT works", async () => {
mock
.onPost("https://api.github.com/graphql")
.replyOnce(404, bad_credentials_error)
.onPost("https://api.github.com/graphql")
.replyOnce(200, successData);
const { req, res } = faker({}, {});
await up(req, res);
expect(res.setHeader).toBeCalledWith("Content-Type", "application/json");
expect(res.send).toBeCalledWith(true);
});
it("should return `false` if all pats have 'Bad credentials'", async () => {
mock
.onPost("https://api.github.com/graphql")
.reply(404, bad_credentials_error);
const { req, res } = faker({}, {});
await up(req, res);
expect(res.setHeader).toBeCalledWith("Content-Type", "application/json");
expect(res.send).toBeCalledWith(false);
});
it("should throw an error if the request fails", async () => {
mock.onPost("https://api.github.com/graphql").networkError();
const { req, res } = faker({}, {});
await up(req, res);
expect(res.setHeader).toBeCalledWith("Content-Type", "application/json");
expect(res.send).toBeCalledWith(false);
});
it("should have proper cache when no error is thrown", async () => {
mock.onPost("https://api.github.com/graphql").replyOnce(200, successData);
const { req, res } = faker({}, {});
await up(req, res);
expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "application/json"],
["Cache-Control", `max-age=0, s-maxage=${RATE_LIMIT_SECONDS}`],
]);
});
it("should have proper cache when error is thrown", async () => {
mock.onPost("https://api.github.com/graphql").networkError();
const { req, res } = faker({}, {});
await up(req, res);
expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "application/json"],
["Cache-Control", "no-store"],
]);
});
});