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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
import { FastifyInstance } from "fastify";
import buildApp from "server/src/app";
import { EnvironmentVariables } from "../util";
import { readFile } from "fs-extra";
import { Info, Repository, RepositorySummary } from "api/src";
import axios, { AxiosResponse } from "axios";
import { Unzip } from "zlib";
import { Readable } from "stream";
const env = process.env as EnvironmentVariables;
const host = "localhost";
const port = 8080;
describe("API", () => {
let app: FastifyInstance;
beforeAll(async() => {
app = await buildApp({
host: host,
port: port,
title: "Bob's cool projects",
about: "All of my personal projects. Completely FOSS.",
git_dir: env.GIT_DIR,
cache: {
enabled: false
},
dev: {
port: 0
}
}, null);
await app.listen(port);
axios.defaults.baseURL = `http://${host}:${port}`;
});
afterAll(async() => {
await app.close();
});
describe("GET /api/v1/info", () => {
let res: AxiosResponse;
beforeAll(async() => {
res = await axios.get("/api/v1/info");
});
it("Should respond", () => {
expect.assertions(4);
expect(res).toBeDefined();
expect(res).toHaveProperty("status");
expect(res).toHaveProperty("data");
expect(res.data).toBeDefined();
});
it("Should respond without an error", () => {
expect.assertions(5);
expect(res.status).toEqual(200);
const json: Record<string, string> = res.data;
expect(json).toBeDefined();
expect(json.error).toBeUndefined();
expect(json).toHaveProperty("data");
expect(json.data).toBeDefined();
});
it("Should have a valid response", () => {
expect.assertions(4);
const info = res.data.data as Info;
expect(info).toHaveProperty("title");
expect(info.title).toEqual("Bob's cool projects");
expect(info).toHaveProperty("about");
expect(info.about).toEqual("All of my personal projects. Completely FOSS.");
});
});
describe("GET /api/v1/repos", () => {
let res: AxiosResponse;
beforeAll(async() => {
res = await axios.get("/api/v1/repos");
});
it("Should respond", () => {
expect.assertions(4);
expect(res).toBeDefined();
expect(res).toHaveProperty("status");
expect(res).toHaveProperty("data");
expect(res.data).toBeDefined();
});
it("Should respond without an error", () => {
expect.assertions(5);
expect(res.status).toEqual(200);
const json: Record<string, string> = res.data;
expect(json).toBeDefined();
expect(json.error).toBeUndefined();
expect(json).toHaveProperty("data");
expect(json.data).toBeDefined();
});
it("Should have a valid response", () => {
expect.hasAssertions();
const repositories = res.data.data as RepositorySummary[];
expect(repositories).toBeDefined();
for(const repository of repositories) {
expect(repository).toHaveProperty("name");
expect(repository.name).toEqual(env.AVAIL_REPO.slice(0, -4));
expect(repository).toHaveProperty("description");
expect(repository.description).toEqual("Unnamed repository; edit this file 'description' to name the repository.");
expect(repository).toHaveProperty("last_updated");
expect(repository.last_updated).toBeDefined();
}
});
});
describe("GET /api/v1/repos/:repository", () => {
let res: AxiosResponse;
beforeAll(async() => {
res = await axios.get(`/api/v1/repos/${env.AVAIL_REPO.slice(0, -4)}`);
});
it("Should respond", () => {
expect.assertions(4);
expect(res).toBeDefined();
expect(res).toHaveProperty("status");
expect(res).toHaveProperty("data");
expect(res.data).toBeDefined();
});
it("Should respond without an error", () => {
expect.assertions(5);
expect(res.status).toEqual(200);
const json: Record<string, string> = res.data;
expect(json).toBeDefined();
expect(json.error).toBeUndefined();
expect(json).toHaveProperty("data");
expect(json.data).toBeDefined();
});
it("Should have a valid response", () => {
expect.hasAssertions();
const repository = res.data.data as Repository;
expect(repository).toHaveProperty("name");
expect(repository.name).toEqual(env.AVAIL_REPO.slice(0, -4));
expect(repository).toHaveProperty("description");
expect(repository.description).toEqual("Unnamed repository; edit this file 'description' to name the repository.");
expect(repository).toHaveProperty("has_readme");
expect(repository.has_readme).toBeDefined();
});
});
describe("GET /:repository/refs/tags/:tag", () => {
let res: AxiosResponse;
beforeAll(async() => {
res = await axios.get(`/${env.AVAIL_REPO.slice(0, -4)}/refs/tags/1.2`, {
responseType: "stream"
});
});
it("Should respond", () => {
expect.assertions(4);
expect(res).toBeDefined();
expect(res).toHaveProperty("status");
expect(res).toHaveProperty("data");
expect(res.data).toBeDefined();
});
it("Should respond without an error", () => {
expect.assertions(1);
expect(res.status).toEqual(200);
});
it("Should have a valid response", async() => {
expect(res).toBeDefined();
const data = res.data as Unzip;
const content: Buffer[] = [];
data.on("data", (chunk: Buffer) => {
content.push(chunk);
});
await new Promise(resolve => {
data.on("end", () => {
resolve(null);
});
});
expect(content.length).toBeGreaterThanOrEqual(1);
expect(content.join().toString()).toBeDefined();
});
});
describe("Git HTTP", () => {
describe("GET /:repository/info/refs", () => {
let res: AxiosResponse;
beforeAll(async() => {
res = await axios.get(`/${env.AVAIL_REPO}/info/refs?service=git-upload-pack`);
});
it("Should respond", () => {
expect.assertions(4);
expect(res).toBeDefined();
expect(res).toHaveProperty("status");
expect(res).toHaveProperty("data");
expect(res.data).toBeDefined();
});
it("Should have a valid response when the service is git-upload-pack", () => {
expect.assertions(3);
expect(res.status).toEqual(200);
const payload_lines = res.data.split("\n");
expect(payload_lines[0]).toEqual("001e# service=git-upload-pack");
expect(payload_lines[payload_lines.length - 1]).toEqual("0000");
});
it("Should respond with 403 when the service is git-receive-pack", async() => {
expect.assertions(3);
const err_res = await axios.get(`/${env.AVAIL_REPO}/info/refs?service=git-receive-pack`, {
validateStatus: () => true
});
expect(err_res).toBeDefined();
expect(err_res).toHaveProperty("status");
expect(err_res.status).toEqual(403);
});
});
describe("POST /:repository/git-upload-pack", () => {
let res: AxiosResponse;
beforeAll(async() => {
const body = new Readable({ read: () => null });
let head = (await readFile(`${env.GIT_DIR}/${env.AVAIL_REPO}/FETCH_HEAD`)).toString();
const find_head = /^[a-f0-9]+/.exec(head);
if(!find_head) {
throw(new Error("Failed to get repository head!"));
}
head = find_head[0];
body.push(`0098want ${head} multi_ack_detailed no-done side-band-64k thin-pack ofs-delta deepen-since deepen-not agent=git/2.32.0\n00000009done\n`);
body.push(null);
res = await axios.post(`/${env.AVAIL_REPO.slice(0, -4)}/git-upload-pack`, body, {
responseType: "arraybuffer",
headers: {
"Content-Type": "application/x-git-upload-pack-request",
Accept: "*/*",
"Transfer-Encoding": "chunked",
Connection: "keep-alive",
Expect: "100-continue"
},
data: body,
decompress: false
});
});
it("Should respond", () => {
expect.assertions(4);
expect(res).toBeDefined();
expect(res).toHaveProperty("status");
expect(res).toHaveProperty("data");
expect(res.data).toBeDefined();
});
it("Should respond without an error", () => {
expect.assertions(1);
expect(res.status).toEqual(200);
});
it("Should have a valid response", async() => {
expect.hasAssertions();
const data_lines = res.data.toString().split("\n");
expect(data_lines.length).toBeGreaterThan(5);
/*
Replaces carriage returns with '###' and removes some headers & some unnecessary stuff at the last line.
This is so that parsing is easier.
*/
data_lines[2] = data_lines[2]
.replace(/[\x0D]/g, "###")
.replace(/[0-9a-f]{4}[\x02]/g, "");
data_lines[3] = data_lines[3]
.replace(/[\x0D]/g, "###")
.replace(/[0-9a-f]{4}[\x02]/g, "");
data_lines[data_lines.length - 1] = data_lines[data_lines.length - 1].replace(/.*[\x01]/, "");
expect(data_lines[0]).toEqual("0008NAK");
expect(data_lines[1]).toMatch(/Enumerating objects: \d+, done\.$/);
// Make sure the progress output for counting objects is fine and dandy
const counting_objects = data_lines[2].split("###");
for(const progress of counting_objects.slice(0, -1)) {
expect(progress).toMatch(/^Counting objects:\s+\d+%\s\(\d+\/\d+\)/);
}
expect(counting_objects[counting_objects.length - 1]).toMatch(/^Counting objects:\s+\d+%\s\(\d+\/\d+\),\sdone\./);
// Make sure the progress output for compressing objects is fine and dandy
const compressing_objects = data_lines[3].split("###");
for(const progress of compressing_objects.slice(0, -1)) {
expect(progress).toMatch(/^Compressing objects:\s+\d+%\s\(\d+\/\d+\)/);
}
expect(compressing_objects[counting_objects.length - 1]).toMatch(/^Compressing objects:\s+\d+%\s\(\d+\/\d+\),\sdone\./);
// Just to be sure
expect(data_lines[data_lines.length - 1]).toMatch(/^.?0{4}/);
});
});
describe("POST /:repository/git-receive-pack", () => {
it("Should respond with 403", async() => {
expect.assertions(3);
const res = await axios.post(`/${env.AVAIL_REPO}/git-receive-pack`, null, {
headers: {
"Content-Type": "application/x-git-receive-pack-request"
},
validateStatus: () => true
});
expect(res).toBeDefined();
expect(res).toHaveProperty("status");
expect(res.status).toEqual(403);
});
});
});
});
|