-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathgithub-branches-get.test.ts
192 lines (168 loc) · 4.19 KB
/
github-branches-get.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
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
import { getLogger } from "config/logger";
import { GithubBranchesGet } from "~/src/routes/github/create-branch/github-branches-get";
import { Subscription } from "models/subscription";
import { mocked } from "jest-mock";
jest.mock("models/subscription");
describe("GitHub Branches Get", () => {
let req, res;
const gitHubInstallationId = 15;
beforeEach(async () => {
req = {
log: getLogger("request"),
params: {
owner: "ARC",
repo: "repo-1"
}
};
res = {
sendStatus: jest.fn(),
send: jest.fn(),
status: jest.fn(),
json: jest.fn(),
locals: {
jiraHost,
githubToken: "abc-token",
gitHubAppConfig: {}
}
};
await Subscription.create({
gitHubInstallationId,
owner: "ARC",
repo: "repo-1",
jiraHost
});
});
it("Should fetch branches", async () => {
setupNock();
mocked(Subscription.findForRepoOwner).mockResolvedValue({ gitHubInstallationId, id: 1 } as Subscription);
await GithubBranchesGet(req, res);
expect(res.send).toBeCalledWith(response);
});
it("Should 401 without gitHubAppConfig attributes", async () => {
delete res.locals.gitHubAppConfig;
await GithubBranchesGet(req, res);
expect(res.sendStatus).toHaveBeenCalledWith(401);
});
it.each(["owner", "repo"])("Should 400 when missing required fields", async (attribute) => {
res.status.mockReturnValue(res);
delete req.params[attribute];
await GithubBranchesGet(req, res);
expect(res.status).toHaveBeenCalledWith(400);
});
});
const defaultBranch = "sample-patch-2";
const setupNock = () => {
const gitHubInstallationId = 15;
githubNock
.post(`/app/installations/${gitHubInstallationId}/access_tokens`)
.reply(200);
githubNock
.post(`/app/installations/${gitHubInstallationId}/access_tokens`)
.reply(200);
githubNock
.get("/repos/ARC/repo-1/branches?per_page=100")
.reply(200, allBranches);
githubNock
.get("/repos/ARC/repo-1")
.reply(200, {
default_branch: defaultBranch
});
};
const allBranches = [
{
"name": "sample-patch-1",
"id": "sample-patch-1"
},
{
"name": "sample-patch-2",
"id": "sample-patch-2"
},
{
"name": "sample-patch-3",
"id": "sample-patch-3"
}
];
const response = {
branches: allBranches.filter(branch => branch.name !== defaultBranch),
defaultBranch: "sample-patch-2"
};
describe("Getting GitHub Branches securely avoiding XSS attacks", () => {
let req, res;
const gitHubInstallationId = 15;
beforeEach(async () => {
req = {
log: getLogger("request"),
params: {
owner: "Hacker",
repo: "xss-test"
}
};
res = {
sendStatus: jest.fn(),
send: jest.fn(),
status: jest.fn(),
json: jest.fn(),
locals: {
jiraHost,
githubToken: "random-token",
gitHubAppConfig: {}
}
};
await Subscription.create({
gitHubInstallationId,
owner: "ARC",
repo: "repo-1",
jiraHost
});
});
it("Should fetch branches securely", async () => {
setupNockForXSSBranches(gitHubInstallationId);
mocked(Subscription.findForRepoOwner).mockResolvedValue({ gitHubInstallationId, id: 1 } as Subscription);
await GithubBranchesGet(req, res);
expect(res.send).toBeCalledWith(responseForXSSBranches);
});
});
const defaultBranchForXSS = "DEFAULTTESTXSS\"><script>alert('🔫🔫🔫🔫🔫🔫')</script>";
const setupNockForXSSBranches = (gitHubInstallationId: number) => {
githubNock
.post(`/app/installations/${gitHubInstallationId}/access_tokens`)
.reply(200);
githubNock
.post(`/app/installations/${gitHubInstallationId}/access_tokens`)
.reply(200);
githubNock
.get("/repos/Hacker/xss-test/branches?per_page=100")
.reply(200, XSSBranches);
githubNock
.get("/repos/Hacker/xss-test")
.reply(200, {
default_branch: defaultBranchForXSS
});
};
const XSSBranches = [
{
"name": "TESTXSS\"><img/src/onerror=alert(\"You've been hit by\")>",
"id": "first-xss"
},
{
"name": "TESTXSS2\"><script>alert('A smooth criminal!!')</script>",
"id": "second-xss"
},
{
"name": "DEFAULTTESTXSS\"><script>alert('🔫🔫🔫🔫🔫🔫')</script>",
"id": "default-xss"
}
];
const responseForXSSBranches = {
branches: [
{
"name": "TESTXSS\">",
"id": "first-xss"
},
{
"name": "TESTXSS2\">",
"id": "second-xss"
}
],
defaultBranch: "DEFAULTTESTXSS\">"
};