Repository navigation
Expand file tree
/
Copy pathfixture.ts
More file actions
121 lines (100 loc) · 2.81 KB
/
Copy pathfixture.ts
File metadata and controls
121 lines (100 loc) · 2.81 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
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
import crypto from "node:crypto";
import { promisify } from "node:util";
import { Client as PgClient } from "pg";
import type { StartedNetwork } from "testcontainers";
import { test as baseTest, inject } from "vitest";
import type { APIServer } from "../src/http/server.js";
import { ConfigSchema } from "../src/util/ConfigSchema.js";
import { KEY_OPTIONS } from "../src/util/rsa.js";
import { createTestDatabase } from "./testUtils/database.js";
import { getTestNetwork } from "./testUtils/network.js";
const generateKeyPair = promisify(crypto.generateKeyPair);
export const test = baseTest.extend<{
network: StartedNetwork;
dbClient: PgClient;
database: string;
config: ConfigSchema;
api: APIServer;
}>({
network: [
async ({}, use) => {
const network = await getTestNetwork();
await use(network);
},
{ scope: "file" },
],
dbClient: [
async ({ database }, use) => {
const postgres = inject("POSTGRES_AUTH");
const client = new PgClient({
...postgres,
database,
});
await client.connect();
await use(client);
await client.end();
},
{ scope: "file" },
],
database: [
async ({}, use) => {
await use(await createTestDatabase());
},
{ scope: "file" },
],
config: [
async ({ database }, use) => {
const postgres = inject("POSTGRES_AUTH");
const keys = await generateKeyPair("rsa", KEY_OPTIONS);
const config = ConfigSchema.parse({
database: {
url: `postgres://${postgres.user}:${postgres.password}@${postgres.host}:${postgres.port}/${database}`,
log: false,
},
security: {
jwt_secret: crypto.randomBytes(256).toString("base64"),
trust_proxy: "loopback,uniquelocal",
},
federation: {
enabled: true,
public_key: keys.publicKey,
private_key: keys.privateKey,
webapp_url: "http://localhost",
instance_url: "http://localhost",
},
registration: {
enabled: true,
},
});
const handler: ProxyHandler<ConfigSchema> = {
set(target, prop, value, receiver) {
Reflect.set(target, prop, value, receiver);
process.env.NODE_CONFIG = JSON.stringify(config);
return true;
},
get(target, prop, receiver) {
const value = Reflect.get(target, prop, receiver);
if (typeof value === "object" && value !== null) {
return new Proxy(Reflect.get(target, prop), handler);
}
return value;
},
};
const proxy = new Proxy(config, handler);
process.env.NODE_CONFIG = JSON.stringify(config);
await use(proxy);
},
{ auto: true, scope: "file" },
],
api: [
async ({}, use) => {
const { APIServer } = await import("../src/http/server.js");
const { initDatabase, closeDatabase } = await import("../src/util/database.js");
const api = new APIServer();
await initDatabase();
await use(api);
closeDatabase();
},
{ scope: "file" },
],
});