@prosopo/client-example-server 2.7.123 → 2.7.124
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build$colon$cjs.log +4 -4
- package/.turbo/turbo-build$colon$tsc.log +21 -21
- package/.turbo/turbo-build.log +6 -6
- package/CHANGELOG.md +15 -0
- package/dist/app.d.ts +15 -1
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +71 -39
- package/dist/app.js.map +1 -1
- package/dist/cjs/app.cjs +79 -37
- package/dist/cjs/controllers/auth.cjs +11 -9
- package/dist/cjs/routes/routes.cjs +1 -1
- package/dist/controllers/auth.d.ts +1 -1
- package/dist/controllers/auth.d.ts.map +1 -1
- package/dist/controllers/auth.js +11 -9
- package/dist/controllers/auth.js.map +1 -1
- package/dist/routes/routes.d.ts.map +1 -1
- package/dist/routes/routes.js +1 -1
- package/dist/routes/routes.js.map +1 -1
- package/dist/tests/app.unit.test.d.ts +2 -0
- package/dist/tests/app.unit.test.d.ts.map +1 -0
- package/dist/tests/app.unit.test.js +318 -0
- package/dist/tests/app.unit.test.js.map +1 -0
- package/dist/tests/appEntrypoint.unit.test.d.ts +2 -0
- package/dist/tests/appEntrypoint.unit.test.d.ts.map +1 -0
- package/dist/tests/appEntrypoint.unit.test.js +100 -0
- package/dist/tests/appEntrypoint.unit.test.js.map +1 -0
- package/dist/tests/auth.unit.test.d.ts +2 -0
- package/dist/tests/auth.unit.test.d.ts.map +1 -0
- package/dist/tests/auth.unit.test.js +426 -0
- package/dist/tests/auth.unit.test.js.map +1 -0
- package/dist/tests/authHarness.d.ts +40 -0
- package/dist/tests/authHarness.d.ts.map +1 -0
- package/dist/tests/authHarness.js +57 -0
- package/dist/tests/authHarness.js.map +1 -0
- package/dist/tests/clientExampleServer.test-d.d.ts +2 -0
- package/dist/tests/clientExampleServer.test-d.d.ts.map +1 -0
- package/dist/tests/clientExampleServer.test-d.js +84 -0
- package/dist/tests/clientExampleServer.test-d.js.map +1 -0
- package/dist/tests/routes.unit.test.d.ts +2 -0
- package/dist/tests/routes.unit.test.d.ts.map +1 -0
- package/dist/tests/routes.unit.test.js +123 -0
- package/dist/tests/routes.unit.test.js.map +1 -0
- package/dist/tests/utils.unit.test.d.ts +2 -0
- package/dist/tests/utils.unit.test.d.ts.map +1 -0
- package/dist/tests/utils.unit.test.js +125 -0
- package/dist/tests/utils.unit.test.js.map +1 -0
- package/package.json +6 -3
- package/src/app.ts +114 -73
- package/src/controllers/auth.ts +40 -31
- package/src/routes/routes.ts +5 -2
- package/src/tests/app.unit.test.ts +428 -0
- package/src/tests/appEntrypoint.unit.test.ts +154 -0
- package/src/tests/auth.unit.test.ts +859 -0
- package/src/tests/authHarness.ts +131 -0
- package/src/tests/clientExampleServer.test-d.ts +146 -0
- package/src/tests/routes.unit.test.ts +166 -0
- package/src/tests/utils.unit.test.ts +169 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/vite.test.config.ts +18 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import type { Server } from "node:http";
|
|
16
|
+
import type { ProsopoServerConfigOutput } from "@prosopo/types";
|
|
17
|
+
import express from "express";
|
|
18
|
+
import type { Connection } from "mongoose";
|
|
19
|
+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
20
|
+
import { serverConfig } from "./authHarness.js";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Importing app.js as the process entrypoint: isMain is forced true so the
|
|
24
|
+
* module's boot block runs, with every external dependency stubbed.
|
|
25
|
+
*/
|
|
26
|
+
const mocks = vi.hoisted(() => ({
|
|
27
|
+
isMain: true,
|
|
28
|
+
servers: [] as Server[],
|
|
29
|
+
connectionThrows: undefined as Error | undefined,
|
|
30
|
+
config: {} as ProsopoServerConfigOutput,
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
vi.mock("@prosopo/util", async (importOriginal) => {
|
|
34
|
+
const actual = await importOriginal<typeof import("@prosopo/util")>();
|
|
35
|
+
return {
|
|
36
|
+
...actual,
|
|
37
|
+
isMain: () => mocks.isMain,
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
vi.mock("../utils/connection.js", () => ({
|
|
42
|
+
default: (): Connection => {
|
|
43
|
+
if (mocks.connectionThrows) throw mocks.connectionThrows;
|
|
44
|
+
return {} as unknown as Connection;
|
|
45
|
+
},
|
|
46
|
+
}));
|
|
47
|
+
|
|
48
|
+
vi.mock("../utils/database.js", () => ({
|
|
49
|
+
default: () => Promise.resolve("mongodb://memory:27017/"),
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
vi.mock("../routes/routes.js", () => ({
|
|
53
|
+
default: () => express.Router(),
|
|
54
|
+
}));
|
|
55
|
+
|
|
56
|
+
vi.mock("@prosopo/server", async (importOriginal) => {
|
|
57
|
+
const actual = await importOriginal<typeof import("@prosopo/server")>();
|
|
58
|
+
return { ...actual, getServerConfig: () => mocks.config };
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
vi.mock("@prosopo/dotenv", async (importOriginal) => {
|
|
62
|
+
const actual = await importOriginal<typeof import("@prosopo/dotenv")>();
|
|
63
|
+
return { ...actual, loadEnv: () => undefined };
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Loads app.js afresh and hands back the server it opened, if any. The module
|
|
68
|
+
* boots on import, so each case needs its own module registry.
|
|
69
|
+
*/
|
|
70
|
+
const importApp = async (
|
|
71
|
+
options: { expectServer?: boolean } = {},
|
|
72
|
+
): Promise<Server | undefined> => {
|
|
73
|
+
vi.resetModules();
|
|
74
|
+
const listeners: Server[] = [];
|
|
75
|
+
const httpModule = await import("node:http");
|
|
76
|
+
const httpsModule = await import("node:https");
|
|
77
|
+
const createServer = httpModule.default.createServer;
|
|
78
|
+
vi.spyOn(httpModule.default, "createServer").mockImplementation(
|
|
79
|
+
(...args: Parameters<typeof createServer>) => {
|
|
80
|
+
const server = createServer(...args);
|
|
81
|
+
listeners.push(server);
|
|
82
|
+
mocks.servers.push(server);
|
|
83
|
+
return server;
|
|
84
|
+
},
|
|
85
|
+
);
|
|
86
|
+
// startServer prefers HTTPS whenever dev certificates happen to be on
|
|
87
|
+
// disk, so both factories have to be watched — otherwise the assertion
|
|
88
|
+
// depends on whether ./setup_certs.sh has been run.
|
|
89
|
+
const createSecureServer = httpsModule.default.createServer;
|
|
90
|
+
vi.spyOn(httpsModule.default, "createServer").mockImplementation(
|
|
91
|
+
(...args: Parameters<typeof createSecureServer>) => {
|
|
92
|
+
const server = createSecureServer(...args);
|
|
93
|
+
listeners.push(server);
|
|
94
|
+
mocks.servers.push(server);
|
|
95
|
+
return server;
|
|
96
|
+
},
|
|
97
|
+
);
|
|
98
|
+
await import("../app.js");
|
|
99
|
+
// The boot block is fire-and-forget, so wait for its promise chain. How
|
|
100
|
+
// many turns that takes depends on the machine, so poll rather than
|
|
101
|
+
// assume a fixed number of ticks.
|
|
102
|
+
for (let attempt = 0; attempt < 100; attempt++) {
|
|
103
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
104
|
+
if (!options.expectServer) break;
|
|
105
|
+
if (listeners[0]?.listening) break;
|
|
106
|
+
}
|
|
107
|
+
return listeners[0];
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
beforeEach(() => {
|
|
111
|
+
mocks.isMain = true;
|
|
112
|
+
mocks.connectionThrows = undefined;
|
|
113
|
+
mocks.config = { ...serverConfig(), serverUrl: "https://localhost:0" };
|
|
114
|
+
process.env.NODE_ENV = "test";
|
|
115
|
+
process.env.MONGO_URI = "mongodb://configured:27017/";
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
afterEach(async () => {
|
|
119
|
+
vi.restoreAllMocks();
|
|
120
|
+
await Promise.all(
|
|
121
|
+
mocks.servers.splice(0).map(
|
|
122
|
+
(server) =>
|
|
123
|
+
new Promise<void>((resolve) => {
|
|
124
|
+
server.close(() => resolve());
|
|
125
|
+
}),
|
|
126
|
+
),
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe("running app.js as the entrypoint", () => {
|
|
131
|
+
test("boots the server", async () => {
|
|
132
|
+
const server = await importApp({ expectServer: true });
|
|
133
|
+
expect(server?.listening).toBe(true);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("stays quiet when the module is merely imported", async () => {
|
|
137
|
+
// A test, or another entrypoint reusing createApp, must not end up
|
|
138
|
+
// opening a port or spinning up mongo.
|
|
139
|
+
mocks.isMain = false;
|
|
140
|
+
const server = await importApp();
|
|
141
|
+
expect(server).toBeUndefined();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("exits when the server cannot start", async () => {
|
|
145
|
+
mocks.connectionThrows = new Error("mongo unreachable");
|
|
146
|
+
const exit = vi
|
|
147
|
+
.spyOn(process, "exit")
|
|
148
|
+
.mockImplementation((): never => undefined as never);
|
|
149
|
+
await importApp();
|
|
150
|
+
expect(exit).toHaveBeenCalledTimes(1);
|
|
151
|
+
// A zero status would tell a supervisor the process shut down cleanly.
|
|
152
|
+
expect(exit).toHaveBeenCalledWith(1);
|
|
153
|
+
});
|
|
154
|
+
});
|