-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcouncil-server.test.ts
More file actions
97 lines (88 loc) · 3.59 KB
/
Copy pathcouncil-server.test.ts
File metadata and controls
97 lines (88 loc) · 3.59 KB
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
import {beforeEach, describe, expect, test} from "@jest/globals";
import request from "supertest";
import createServer from "../src/council-server.js";
import {CouncilProvisioner} from "../src/council-provisioner.js";
import {Logger} from "../src/logger.js";
import {State} from "../src/state-handler.js";
let provisioner: CouncilProvisioner, state: State, logger: Logger;
beforeEach(() => {
state = {kings: [], services: [], revision: 0, lings: []};
logger = new Logger();
provisioner = new CouncilProvisioner({logger});
});
test("GET /state", async () => {
const server = createServer({provisioner, state}).httpServer;
const res = await request(server).get("/state");
expect(res.statusCode).toEqual(200);
expect(res.body).toEqual({kings: [], services: [], revision: 0, lings: []});
});
test("GET /not-found", async () => {
const server = createServer({provisioner, state}).httpServer;
const res = await request(server).get("/not-found");
expect(res.statusCode).toEqual(404);
expect(res.text).toEqual("Page could not be found");
});
describe("PUT /king", () => {
test("empty body", async () => {
const server = createServer({provisioner, state}).httpServer;
const res = await request(server).put("/king");
expect(res.statusCode).toEqual(400);
expect(res.text).toEqual("no json data received");
});
test("success", async () => {
const server = createServer({provisioner, state}).httpServer;
const res = await request(server).put("/king").send({
ratholes: [],
ready_service_ids: [],
location: "mylocation",
host: "example.com",
});
expect(res.text).toEqual("ok");
expect(res.statusCode).toEqual(200);
});
test("stores noise_public_key on king creation", async () => {
const server = createServer({provisioner, state}).httpServer;
await request(server).put("/king").send({
ratholes: [{bind_port: 2333, ports: "5000-5001"}],
ready_service_ids: [],
location: "mylocation",
host: "example.com",
noise_public_key: "abc123pubkey",
});
expect(state.kings).toHaveLength(1);
expect(state.kings[0].noise_public_key).toEqual("abc123pubkey");
});
test("stores null noise_public_key when not provided", async () => {
const server = createServer({provisioner, state}).httpServer;
await request(server).put("/king").send({
ratholes: [{bind_port: 2333, ports: "5000-5001"}],
ready_service_ids: [],
location: "mylocation",
host: "example.com",
});
expect(state.kings).toHaveLength(1);
expect(state.kings[0].noise_public_key).toBeNull();
});
test("updates noise_public_key on existing king", async () => {
state.kings.push({
bind_port: 2333,
ports: "5000-5001",
host: "example.com",
location: "mylocation",
beat: 0,
shutting_down: false,
noise_public_key: null,
});
const initialRevision = state.revision;
const server = createServer({provisioner, state}).httpServer;
await request(server).put("/king").send({
ratholes: [{bind_port: 2333, ports: "5000-5001"}],
ready_service_ids: [],
location: "mylocation",
host: "example.com",
noise_public_key: "newkey123",
});
expect(state.kings[0].noise_public_key).toEqual("newkey123");
expect(state.revision).toBeGreaterThan(initialRevision);
});
});