/[sudobot]/branches/4.x/tests/api/Server.test.ts
ViewVC logotype

Contents of /branches/4.x/tests/api/Server.test.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 1846 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 import Server from "../../src/api/Server";
2 import { beforeEach, describe, test, expect } from '@jest/globals';
3 import request from 'supertest';
4 import DiscordClient from "../../src/client/Client";
5 import { Config } from "../../src/client/Config";
6
7 describe("API server", () => {
8 process.env.SUDO_PREFIX = '';
9
10 const server = new Server({} as any);
11 const config = new Config({} as any);
12 const serverIDs = ["87263215715323525", "87263215715323526"];
13
14 config.props[serverIDs[0]] = {
15 prefix: "-",
16 mod_role: "7262152378215621"
17 };
18
19 config.props[serverIDs[1]] = {
20 prefix: "+",
21 mod_role: "7262152378215622"
22 };
23
24 DiscordClient.client = { server } as DiscordClient;
25
26 beforeEach(async () => {
27 await server.boot();
28 });
29
30 test('check if the server works', async () => {
31 const response = await request(server.express)
32 .get('/')
33 .expect(200);
34
35 expect(response?.body?.message).toBe('Server is up.');
36 });
37
38 test('check if the server authentication works', async () => {
39 const response1 = await request(server.express)
40 .get('/config/' + serverIDs[0])
41 .expect(401);
42
43 expect(response1?.body?.error).toBe("No authorization header in the request");
44
45 const response2 = await request(server.express)
46 .get('/config/' + serverIDs[0])
47 .set("Authorization", "Basic 218h23hd461xh6sh1273brh6216y7r1bh")
48 .expect(401);
49
50 expect(response2?.body?.error).toBe("Only Bearer tokens are supported");
51
52 const response3 = await request(server.express)
53 .get('/config/' + serverIDs[0])
54 .set("Authorization", "Bearer")
55 .expect(401);
56
57 expect(response3?.body?.error).toBe("No Bearer token provided");
58 });
59 });

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26