aboutsummaryrefslogtreecommitdiff
path: root/test/unit/git-http.unit.test.ts
blob: 944ba7dbbf7e2a1f06bd54dfbf172e602c0a685b (plain)
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
import { FastifyInstance } from "fastify";
import buildApp from "server/src/app";
import { EnvironmentVariables } from "../util";
import { readFile } from "fs/promises";
import { Readable } from "stream";

const env = process.env as EnvironmentVariables;

describe("Git HTTP backend", () => {
	let app: FastifyInstance;

	beforeAll(() => {
		app = buildApp({
			host: "localhost",
			port: 1337,
			dev_port: 8080,
			title: "lmao",
			about: "lmao",
			base_dir: "/tmp/githermit_test",
			production: false
		}, "");
	});

	describe("info/refs", () => {
		it("Should make a valid response when the service is git-upload-pack", async () => {
			expect.assertions(7);

			const res = await app.inject({
				method: "GET",
				url: `${env.AVAIL_REPO}/info/refs?service=git-upload-pack`
			});

			expect(res).toBeDefined();
			expect(res).toHaveProperty("statusCode");
			expect(res.statusCode).toEqual(200);
			expect(res).toHaveProperty("payload");
			expect(res.payload).toBeDefined();

			const payload_lines = res.payload.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 res = await app.inject({
				method: "GET",
				url: `${env.AVAIL_REPO}/info/refs?service=git-receive-pack`
			});

			expect(res).toBeDefined();
			expect(res).toHaveProperty("statusCode");
			expect(res.statusCode).toEqual(403);
		});
	});

	describe("git-upload-pack", () => {
		it("Should make a valid response", async () => {
			expect.assertions(6);

			const head = /^[a-f0-9]+/.exec((await readFile(env.BASE_DIR + "/" + env.AVAIL_REPO + "/FETCH_HEAD")).toString());

			const res = await app.inject({
				method: "POST",
				url: `${env.AVAIL_REPO}/git-upload-pack`,
				payload: Readable.from([`0098want ${head} multi_ack_detailed no-done side-band-64k thin-pack ofs-delta deepen-since deepen-not agent=git/2.32.0\n\
00000009done`]),
				headers: {
					"content-type": "application/x-git-upload-pack-request"
				}
			});

			expect(res).toBeDefined();
			expect(res).toHaveProperty("statusCode");
			expect(res.statusCode).toEqual(200);
			expect(res).toHaveProperty("rawPayload");
			expect(res.rawPayload).toBeDefined();
			expect(res.rawPayload).toBeInstanceOf(Buffer);
		});
	});

	describe("git-receive-pack", () => {
		it("Should respond with 403", async () => {
			expect.assertions(3);

			const res = await app.inject({
				method: "POST",
				url: `${env.AVAIL_REPO}/git-receive-pack`,
				headers: {
					"content-type": "application/x-git-receive-pack-request"
				}
			});
			
			expect(res).toBeDefined();
			expect(res).toHaveProperty("statusCode");
			expect(res.statusCode).toEqual(403);
		});
	});
});