@terreno/api 0.23.1 → 0.24.0
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/dist/auth.test.js +131 -0
- package/dist/docLoader.test.js +0 -1
- package/dist/expressServer.d.ts +25 -1
- package/dist/expressServer.js +1 -3
- package/dist/githubAuth.d.ts +1 -1
- package/dist/notifiers/googleChatNotifier.test.js +0 -1
- package/dist/populate.test.js +30 -0
- package/dist/transformers.test.js +0 -1
- package/package.json +2 -2
- package/src/auth.test.ts +97 -0
- package/src/docLoader.test.ts +4 -4
- package/src/expressServer.ts +30 -6
- package/src/githubAuth.ts +2 -4
- package/src/notifiers/googleChatNotifier.test.ts +5 -4
- package/src/populate.test.ts +32 -0
- package/src/transformers.test.ts +15 -8
package/dist/auth.test.js
CHANGED
|
@@ -1754,3 +1754,134 @@ var decodeTokenPayload = function (token) {
|
|
|
1754
1754
|
});
|
|
1755
1755
|
}); });
|
|
1756
1756
|
});
|
|
1757
|
+
(0, bun_test_1.describe)("generateTokens with SIGNUP_DISABLED and user.postCreate", function () {
|
|
1758
|
+
var OLD_ENV = process.env;
|
|
1759
|
+
(0, bun_test_1.beforeEach)(function () {
|
|
1760
|
+
process.env = __assign({}, OLD_ENV);
|
|
1761
|
+
process.env.TOKEN_SECRET = "secret";
|
|
1762
|
+
process.env.REFRESH_TOKEN_SECRET = "refresh_secret";
|
|
1763
|
+
});
|
|
1764
|
+
(0, bun_test_1.afterEach)(function () {
|
|
1765
|
+
process.env = OLD_ENV;
|
|
1766
|
+
});
|
|
1767
|
+
(0, bun_test_1.it)("generates token with sessionId in both token and refresh token", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1768
|
+
var jwtLib, result, tokenDecoded, refreshDecoded;
|
|
1769
|
+
return __generator(this, function (_a) {
|
|
1770
|
+
switch (_a.label) {
|
|
1771
|
+
case 0: return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require("jsonwebtoken")); })];
|
|
1772
|
+
case 1:
|
|
1773
|
+
jwtLib = _a.sent();
|
|
1774
|
+
return [4 /*yield*/, (0, auth_1.generateTokens)({ _id: "user-123" })];
|
|
1775
|
+
case 2:
|
|
1776
|
+
result = _a.sent();
|
|
1777
|
+
(0, bun_test_1.expect)(result.sessionId).toBeDefined();
|
|
1778
|
+
tokenDecoded = jwtLib.decode(result.token);
|
|
1779
|
+
refreshDecoded = jwtLib.decode(result.refreshToken);
|
|
1780
|
+
(0, bun_test_1.expect)(tokenDecoded.sid).toBe(result.sessionId);
|
|
1781
|
+
(0, bun_test_1.expect)(refreshDecoded.sid).toBe(result.sessionId);
|
|
1782
|
+
return [2 /*return*/];
|
|
1783
|
+
}
|
|
1784
|
+
});
|
|
1785
|
+
}); });
|
|
1786
|
+
});
|
|
1787
|
+
(0, bun_test_1.describe)("JWT cookie extraction", function () {
|
|
1788
|
+
var app;
|
|
1789
|
+
var agent;
|
|
1790
|
+
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1791
|
+
return __generator(this, function (_a) {
|
|
1792
|
+
switch (_a.label) {
|
|
1793
|
+
case 0:
|
|
1794
|
+
(0, bun_test_1.setSystemTime)();
|
|
1795
|
+
return [4 /*yield*/, (0, tests_1.setupTestData)()];
|
|
1796
|
+
case 1:
|
|
1797
|
+
_a.sent();
|
|
1798
|
+
app = new terrenoApp_1.TerrenoApp({
|
|
1799
|
+
configureApp: function (router) {
|
|
1800
|
+
router.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
|
|
1801
|
+
allowAnonymous: true,
|
|
1802
|
+
permissions: {
|
|
1803
|
+
create: [],
|
|
1804
|
+
delete: [],
|
|
1805
|
+
list: [permissions_1.Permissions.IsAny],
|
|
1806
|
+
read: [permissions_1.Permissions.IsAny],
|
|
1807
|
+
update: [],
|
|
1808
|
+
},
|
|
1809
|
+
}));
|
|
1810
|
+
},
|
|
1811
|
+
skipListen: true,
|
|
1812
|
+
userModel: tests_1.UserModel,
|
|
1813
|
+
}).build();
|
|
1814
|
+
agent = supertest_1.default.agent(app);
|
|
1815
|
+
return [2 /*return*/];
|
|
1816
|
+
}
|
|
1817
|
+
});
|
|
1818
|
+
}); });
|
|
1819
|
+
(0, bun_test_1.afterEach)(function () {
|
|
1820
|
+
(0, bun_test_1.setSystemTime)();
|
|
1821
|
+
});
|
|
1822
|
+
(0, bun_test_1.it)("skips decode when token is the string null", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1823
|
+
var res;
|
|
1824
|
+
return __generator(this, function (_a) {
|
|
1825
|
+
switch (_a.label) {
|
|
1826
|
+
case 0: return [4 /*yield*/, agent.get("/food").set("authorization", "Bearer null").expect(200)];
|
|
1827
|
+
case 1:
|
|
1828
|
+
res = _a.sent();
|
|
1829
|
+
(0, bun_test_1.expect)(res.body.data).toBeDefined();
|
|
1830
|
+
return [2 /*return*/];
|
|
1831
|
+
}
|
|
1832
|
+
});
|
|
1833
|
+
}); });
|
|
1834
|
+
(0, bun_test_1.it)("skips decode when token is the string undefined", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1835
|
+
var res;
|
|
1836
|
+
return __generator(this, function (_a) {
|
|
1837
|
+
switch (_a.label) {
|
|
1838
|
+
case 0: return [4 /*yield*/, agent.get("/food").set("authorization", "Bearer undefined").expect(200)];
|
|
1839
|
+
case 1:
|
|
1840
|
+
res = _a.sent();
|
|
1841
|
+
(0, bun_test_1.expect)(res.body.data).toBeDefined();
|
|
1842
|
+
return [2 /*return*/];
|
|
1843
|
+
}
|
|
1844
|
+
});
|
|
1845
|
+
}); });
|
|
1846
|
+
});
|
|
1847
|
+
(0, bun_test_1.describe)("signup disabled", function () {
|
|
1848
|
+
var app;
|
|
1849
|
+
var agent;
|
|
1850
|
+
var OLD_ENV = process.env;
|
|
1851
|
+
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1852
|
+
return __generator(this, function (_a) {
|
|
1853
|
+
switch (_a.label) {
|
|
1854
|
+
case 0:
|
|
1855
|
+
(0, bun_test_1.setSystemTime)();
|
|
1856
|
+
process.env = __assign({}, OLD_ENV);
|
|
1857
|
+
process.env.SIGNUP_DISABLED = "true";
|
|
1858
|
+
return [4 /*yield*/, (0, tests_1.setupTestData)()];
|
|
1859
|
+
case 1:
|
|
1860
|
+
_a.sent();
|
|
1861
|
+
app = new terrenoApp_1.TerrenoApp({
|
|
1862
|
+
configureApp: function () { },
|
|
1863
|
+
skipListen: true,
|
|
1864
|
+
userModel: tests_1.UserModel,
|
|
1865
|
+
}).build();
|
|
1866
|
+
agent = supertest_1.default.agent(app);
|
|
1867
|
+
return [2 /*return*/];
|
|
1868
|
+
}
|
|
1869
|
+
});
|
|
1870
|
+
}); });
|
|
1871
|
+
(0, bun_test_1.afterEach)(function () {
|
|
1872
|
+
(0, bun_test_1.setSystemTime)();
|
|
1873
|
+
process.env = OLD_ENV;
|
|
1874
|
+
});
|
|
1875
|
+
(0, bun_test_1.it)("returns 404 when SIGNUP_DISABLED is true", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1876
|
+
var res;
|
|
1877
|
+
return __generator(this, function (_a) {
|
|
1878
|
+
switch (_a.label) {
|
|
1879
|
+
case 0: return [4 /*yield*/, agent.post("/auth/signup").send({ email: "new@example.com", password: "123" })];
|
|
1880
|
+
case 1:
|
|
1881
|
+
res = _a.sent();
|
|
1882
|
+
(0, bun_test_1.expect)(res.status).toBe(404);
|
|
1883
|
+
return [2 /*return*/];
|
|
1884
|
+
}
|
|
1885
|
+
});
|
|
1886
|
+
}); });
|
|
1887
|
+
});
|
package/dist/docLoader.test.js
CHANGED
|
@@ -36,7 +36,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
40
39
|
var bun_test_1 = require("bun:test");
|
|
41
40
|
var docLoader_1 = require("./docLoader");
|
|
42
41
|
var errors_1 = require("./errors");
|
package/dist/expressServer.d.ts
CHANGED
|
@@ -1,9 +1,32 @@
|
|
|
1
1
|
import express, { type Router } from "express";
|
|
2
2
|
import type jwt from "jsonwebtoken";
|
|
3
3
|
import type { ModelRouterOptions } from "./api";
|
|
4
|
+
import { type LoggingOptions } from "./logger";
|
|
4
5
|
export declare const setupEnvironment: () => void;
|
|
5
6
|
export type AddRoutes = (router: Router, options?: Partial<ModelRouterOptions<unknown>>) => void;
|
|
6
|
-
|
|
7
|
+
interface LoggableRequest {
|
|
8
|
+
body?: Record<string, unknown>;
|
|
9
|
+
method: string;
|
|
10
|
+
originalUrl?: string;
|
|
11
|
+
route?: {
|
|
12
|
+
path: string;
|
|
13
|
+
};
|
|
14
|
+
routeMount?: string | string[];
|
|
15
|
+
url?: string;
|
|
16
|
+
user?: {
|
|
17
|
+
admin?: boolean;
|
|
18
|
+
id?: string;
|
|
19
|
+
testUser?: boolean;
|
|
20
|
+
type?: string;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
interface LoggableResponse {
|
|
24
|
+
locals?: {
|
|
25
|
+
loggingOptions?: LoggingOptions;
|
|
26
|
+
};
|
|
27
|
+
statusCode?: number;
|
|
28
|
+
}
|
|
29
|
+
export declare const logRequests: (req: LoggableRequest, res: LoggableResponse, next: express.NextFunction) => void;
|
|
7
30
|
export declare const createRouter: (rootPath: string, addRoutes: AddRoutes, middleware?: express.RequestHandler[]) => Array<string | express.RequestHandler | Router>;
|
|
8
31
|
export declare const createRouterWithAuth: (rootPath: string, addRoutes: (router: Router) => void, middleware?: express.RequestHandler[]) => Array<string | express.RequestHandler | Router>;
|
|
9
32
|
export interface AuthOptions {
|
|
@@ -18,3 +41,4 @@ export interface WrapScriptOptions {
|
|
|
18
41
|
slackChannel?: string;
|
|
19
42
|
}
|
|
20
43
|
export declare const wrapScript: (func: () => Promise<unknown>, options?: WrapScriptOptions) => Promise<void>;
|
|
44
|
+
export {};
|
package/dist/expressServer.js
CHANGED
|
@@ -130,7 +130,6 @@ var setupEnvironment = function () {
|
|
|
130
130
|
}
|
|
131
131
|
};
|
|
132
132
|
exports.setupEnvironment = setupEnvironment;
|
|
133
|
-
// biome-ignore lint/suspicious/noExplicitAny: also called from tests with mock request/response objects
|
|
134
133
|
var logRequestsFinished = function (req, res, startTime) {
|
|
135
134
|
var _a, _b, _c, _d;
|
|
136
135
|
var options = ((_b = (_a = res.locals) === null || _a === void 0 ? void 0 : _a.loggingOptions) !== null && _b !== void 0 ? _b : {});
|
|
@@ -145,7 +144,7 @@ var logRequestsFinished = function (req, res, startTime) {
|
|
|
145
144
|
else if (req.route) {
|
|
146
145
|
pathName = req.route.path;
|
|
147
146
|
}
|
|
148
|
-
else if (res.statusCode < 400) {
|
|
147
|
+
else if (res.statusCode != null && res.statusCode < 400) {
|
|
149
148
|
logger_1.logger.warn("Request without route: ".concat(req.originalUrl));
|
|
150
149
|
}
|
|
151
150
|
if (process.env.DISABLE_LOG_ALL_REQUESTS !== "true") {
|
|
@@ -168,7 +167,6 @@ var logRequestsFinished = function (req, res, startTime) {
|
|
|
168
167
|
}
|
|
169
168
|
}
|
|
170
169
|
};
|
|
171
|
-
// biome-ignore lint/suspicious/noExplicitAny: also called from tests with mock request/response objects
|
|
172
170
|
var logRequests = function (req, res, next) {
|
|
173
171
|
var _a, _b, _c, _d;
|
|
174
172
|
var startTime = process.hrtime.bigint();
|
package/dist/githubAuth.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export interface GitHubAuthOptions {
|
|
|
23
23
|
* Custom function to handle user creation or lookup from GitHub profile.
|
|
24
24
|
* If not provided, a default implementation will be used.
|
|
25
25
|
*/
|
|
26
|
-
findOrCreateUser?: (profile: Profile, accessToken: string, refreshToken: string, existingUser?:
|
|
26
|
+
findOrCreateUser?: (profile: Profile, accessToken: string, refreshToken: string, existingUser?: Record<string, unknown>) => Promise<unknown>;
|
|
27
27
|
}
|
|
28
28
|
/** Fields added to user documents for GitHub authentication */
|
|
29
29
|
export interface GitHubUserFields {
|
|
@@ -99,7 +99,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
99
99
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
100
100
|
};
|
|
101
101
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
102
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
103
102
|
var bun_test_1 = require("bun:test");
|
|
104
103
|
var Sentry = __importStar(require("@sentry/bun"));
|
|
105
104
|
var axios_1 = __importDefault(require("axios"));
|
package/dist/populate.test.js
CHANGED
|
@@ -319,6 +319,36 @@ var tests_1 = require("./tests");
|
|
|
319
319
|
(0, bun_test_1.expect)(eatenBy.items).toBeDefined();
|
|
320
320
|
});
|
|
321
321
|
});
|
|
322
|
+
(0, bun_test_1.describe)("getOpenApiSpecForModel populate property merge", function () {
|
|
323
|
+
(0, bun_test_1.it)("merges properties when the same path is populated twice", function () {
|
|
324
|
+
// First populate sets the field properties, second triggers the merge branch.
|
|
325
|
+
var result = (0, populate_1.getOpenApiSpecForModel)(tests_1.FoodModel, {
|
|
326
|
+
populatePaths: [{ fields: ["name"], path: "likesIds.userId" }, { path: "likesIds.userId" }],
|
|
327
|
+
});
|
|
328
|
+
var likesIds = result.properties.likesIds;
|
|
329
|
+
var items = likesIds.items;
|
|
330
|
+
var userIdProps = items.properties.userId;
|
|
331
|
+
// After the merge the wider populate should have contributed all user fields.
|
|
332
|
+
(0, bun_test_1.expect)(userIdProps.properties).toBeDefined();
|
|
333
|
+
(0, bun_test_1.expect)(userIdProps.properties.name).toBeDefined();
|
|
334
|
+
});
|
|
335
|
+
(0, bun_test_1.it)("merges openApiComponent ref into an already-populated path", function () {
|
|
336
|
+
var result = (0, populate_1.getOpenApiSpecForModel)(tests_1.FoodModel, {
|
|
337
|
+
populatePaths: [
|
|
338
|
+
{ path: "likesIds.userId" },
|
|
339
|
+
{ openApiComponent: "UserComponent", path: "likesIds.userId" },
|
|
340
|
+
],
|
|
341
|
+
});
|
|
342
|
+
var likesIds = result.properties.likesIds;
|
|
343
|
+
var items = likesIds.items;
|
|
344
|
+
var userIdProps = items.properties.userId;
|
|
345
|
+
// Merge path adds the $ref key inside the existing properties.
|
|
346
|
+
(0, bun_test_1.expect)(userIdProps.properties).toBeDefined();
|
|
347
|
+
(0, bun_test_1.expect)(userIdProps.properties.userId).toEqual({
|
|
348
|
+
$ref: "#/components/schemas/UserComponent",
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
});
|
|
322
352
|
(0, bun_test_1.describe)("filterKeys (via getOpenApiSpecForModel populatePaths)", function () {
|
|
323
353
|
(0, bun_test_1.it)("filters populated fields using dot-notation keys", function () {
|
|
324
354
|
var result = (0, populate_1.getOpenApiSpecForModel)(tests_1.FoodModel, {
|
|
@@ -50,7 +50,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
50
50
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
51
|
};
|
|
52
52
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
54
53
|
var bun_test_1 = require("bun:test");
|
|
55
54
|
var supertest_1 = __importDefault(require("supertest"));
|
|
56
55
|
var api_1 = require("./api");
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"@asteasolutions/zod-to-openapi": "^8.5.0",
|
|
8
8
|
"@sentry/bun": "^10.39.0",
|
|
9
9
|
"@sentry/profiling-node": "^10.25.0",
|
|
10
|
-
"@terreno/test": "0.
|
|
10
|
+
"@terreno/test": "0.24.0",
|
|
11
11
|
"@types/qs": "^6.14.0",
|
|
12
12
|
"ajv": "8.18.0",
|
|
13
13
|
"ajv-formats": "^3.0.1",
|
|
@@ -123,5 +123,5 @@
|
|
|
123
123
|
"updateSnapshot": "bun test --update-snapshots"
|
|
124
124
|
},
|
|
125
125
|
"types": "dist/index.d.ts",
|
|
126
|
-
"version": "0.
|
|
126
|
+
"version": "0.24.0"
|
|
127
127
|
}
|
package/src/auth.test.ts
CHANGED
|
@@ -1213,3 +1213,100 @@ describe("PATCH /me route edge cases", () => {
|
|
|
1213
1213
|
expect([401, 404]).toContain(res.status);
|
|
1214
1214
|
});
|
|
1215
1215
|
});
|
|
1216
|
+
|
|
1217
|
+
describe("generateTokens with SIGNUP_DISABLED and user.postCreate", () => {
|
|
1218
|
+
const OLD_ENV = process.env;
|
|
1219
|
+
|
|
1220
|
+
beforeEach(() => {
|
|
1221
|
+
process.env = {...OLD_ENV};
|
|
1222
|
+
process.env.TOKEN_SECRET = "secret";
|
|
1223
|
+
process.env.REFRESH_TOKEN_SECRET = "refresh_secret";
|
|
1224
|
+
});
|
|
1225
|
+
|
|
1226
|
+
afterEach(() => {
|
|
1227
|
+
process.env = OLD_ENV;
|
|
1228
|
+
});
|
|
1229
|
+
|
|
1230
|
+
it("generates token with sessionId in both token and refresh token", async () => {
|
|
1231
|
+
const jwtLib = await import("jsonwebtoken");
|
|
1232
|
+
const result = await generateTokens({_id: "user-123"});
|
|
1233
|
+
expect(result.sessionId).toBeDefined();
|
|
1234
|
+
const tokenDecoded = jwtLib.decode(result.token as string) as {sid?: string};
|
|
1235
|
+
const refreshDecoded = jwtLib.decode(result.refreshToken as string) as {sid?: string};
|
|
1236
|
+
expect(tokenDecoded.sid).toBe(result.sessionId);
|
|
1237
|
+
expect(refreshDecoded.sid).toBe(result.sessionId);
|
|
1238
|
+
});
|
|
1239
|
+
});
|
|
1240
|
+
|
|
1241
|
+
describe("JWT cookie extraction", () => {
|
|
1242
|
+
let app: express.Application;
|
|
1243
|
+
let agent: TestAgent;
|
|
1244
|
+
|
|
1245
|
+
beforeEach(async () => {
|
|
1246
|
+
setSystemTime();
|
|
1247
|
+
await setupTestData();
|
|
1248
|
+
app = new TerrenoApp({
|
|
1249
|
+
configureApp: (router: express.Router) => {
|
|
1250
|
+
router.use(
|
|
1251
|
+
"/food",
|
|
1252
|
+
modelRouter(FoodModel, {
|
|
1253
|
+
allowAnonymous: true,
|
|
1254
|
+
permissions: {
|
|
1255
|
+
create: [],
|
|
1256
|
+
delete: [],
|
|
1257
|
+
list: [Permissions.IsAny],
|
|
1258
|
+
read: [Permissions.IsAny],
|
|
1259
|
+
update: [],
|
|
1260
|
+
},
|
|
1261
|
+
})
|
|
1262
|
+
);
|
|
1263
|
+
},
|
|
1264
|
+
skipListen: true,
|
|
1265
|
+
userModel: UserModel as any,
|
|
1266
|
+
}).build();
|
|
1267
|
+
agent = supertest.agent(app);
|
|
1268
|
+
});
|
|
1269
|
+
|
|
1270
|
+
afterEach(() => {
|
|
1271
|
+
setSystemTime();
|
|
1272
|
+
});
|
|
1273
|
+
|
|
1274
|
+
it("skips decode when token is the string null", async () => {
|
|
1275
|
+
const res = await agent.get("/food").set("authorization", "Bearer null").expect(200);
|
|
1276
|
+
expect(res.body.data).toBeDefined();
|
|
1277
|
+
});
|
|
1278
|
+
|
|
1279
|
+
it("skips decode when token is the string undefined", async () => {
|
|
1280
|
+
const res = await agent.get("/food").set("authorization", "Bearer undefined").expect(200);
|
|
1281
|
+
expect(res.body.data).toBeDefined();
|
|
1282
|
+
});
|
|
1283
|
+
});
|
|
1284
|
+
|
|
1285
|
+
describe("signup disabled", () => {
|
|
1286
|
+
let app: express.Application;
|
|
1287
|
+
let agent: TestAgent;
|
|
1288
|
+
const OLD_ENV = process.env;
|
|
1289
|
+
|
|
1290
|
+
beforeEach(async () => {
|
|
1291
|
+
setSystemTime();
|
|
1292
|
+
process.env = {...OLD_ENV};
|
|
1293
|
+
process.env.SIGNUP_DISABLED = "true";
|
|
1294
|
+
await setupTestData();
|
|
1295
|
+
app = new TerrenoApp({
|
|
1296
|
+
configureApp: () => {},
|
|
1297
|
+
skipListen: true,
|
|
1298
|
+
userModel: UserModel as any,
|
|
1299
|
+
}).build();
|
|
1300
|
+
agent = supertest.agent(app);
|
|
1301
|
+
});
|
|
1302
|
+
|
|
1303
|
+
afterEach(() => {
|
|
1304
|
+
setSystemTime();
|
|
1305
|
+
process.env = OLD_ENV;
|
|
1306
|
+
});
|
|
1307
|
+
|
|
1308
|
+
it("returns 404 when SIGNUP_DISABLED is true", async () => {
|
|
1309
|
+
const res = await agent.post("/auth/signup").send({email: "new@example.com", password: "123"});
|
|
1310
|
+
expect(res.status).toBe(404);
|
|
1311
|
+
});
|
|
1312
|
+
});
|
package/src/docLoader.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
2
1
|
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import type {Model} from "mongoose";
|
|
3
3
|
import {loadDocOr404} from "./docLoader";
|
|
4
4
|
import {APIError} from "./errors";
|
|
5
5
|
|
|
@@ -9,7 +9,7 @@ describe("loadDocOr404", () => {
|
|
|
9
9
|
collection: {findOne: mock(async () => ({deleted: true}))},
|
|
10
10
|
findById: mock(() => ({exec: mock(async () => null)})),
|
|
11
11
|
modelName: "MockModel",
|
|
12
|
-
} as
|
|
12
|
+
} as unknown as Model<unknown>;
|
|
13
13
|
|
|
14
14
|
try {
|
|
15
15
|
await loadDocOr404(model, "507f1f77bcf86cd799439011");
|
|
@@ -33,7 +33,7 @@ describe("loadDocOr404", () => {
|
|
|
33
33
|
}),
|
|
34
34
|
})),
|
|
35
35
|
modelName: "MockModel",
|
|
36
|
-
} as
|
|
36
|
+
} as unknown as Model<unknown>;
|
|
37
37
|
|
|
38
38
|
await expect(loadDocOr404(model, "507f1f77bcf86cd799439011")).rejects.toBe(original);
|
|
39
39
|
});
|
|
@@ -43,7 +43,7 @@ describe("loadDocOr404", () => {
|
|
|
43
43
|
collection: {findOne: mock(async () => null)},
|
|
44
44
|
findById: mock(() => ({exec: mock(async () => null)})),
|
|
45
45
|
modelName: "MockModel",
|
|
46
|
-
} as
|
|
46
|
+
} as unknown as Model<unknown>;
|
|
47
47
|
|
|
48
48
|
try {
|
|
49
49
|
await loadDocOr404(model, "507f1f77bcf86cd799439011");
|
package/src/expressServer.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type {OutgoingMessage} from "node:http";
|
|
1
2
|
import * as Sentry from "@sentry/bun";
|
|
2
3
|
import cron from "cron";
|
|
3
4
|
import express, {type Router} from "express";
|
|
@@ -36,8 +37,27 @@ export const setupEnvironment = (): void => {
|
|
|
36
37
|
|
|
37
38
|
export type AddRoutes = (router: Router, options?: Partial<ModelRouterOptions<unknown>>) => void;
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
interface LoggableRequest {
|
|
41
|
+
body?: Record<string, unknown>;
|
|
42
|
+
method: string;
|
|
43
|
+
originalUrl?: string;
|
|
44
|
+
route?: {path: string};
|
|
45
|
+
routeMount?: string | string[];
|
|
46
|
+
url?: string;
|
|
47
|
+
user?: {
|
|
48
|
+
admin?: boolean;
|
|
49
|
+
id?: string;
|
|
50
|
+
testUser?: boolean;
|
|
51
|
+
type?: string;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface LoggableResponse {
|
|
56
|
+
locals?: {loggingOptions?: LoggingOptions};
|
|
57
|
+
statusCode?: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const logRequestsFinished = (req: LoggableRequest, res: LoggableResponse, startTime: bigint) => {
|
|
41
61
|
const options = (res.locals?.loggingOptions ?? {}) as LoggingOptions;
|
|
42
62
|
|
|
43
63
|
const slowReadMs = options.logSlowRequestsReadMs ?? SLOW_READ_MAX;
|
|
@@ -50,7 +70,7 @@ const logRequestsFinished = (req: any, res: any, startTime: bigint) => {
|
|
|
50
70
|
pathName = `${req.routeMount}${req.route.path}`;
|
|
51
71
|
} else if (req.route) {
|
|
52
72
|
pathName = req.route.path;
|
|
53
|
-
} else if (res.statusCode < 400) {
|
|
73
|
+
} else if (res.statusCode != null && res.statusCode < 400) {
|
|
54
74
|
logger.warn(`Request without route: ${req.originalUrl}`);
|
|
55
75
|
}
|
|
56
76
|
if (process.env.DISABLE_LOG_ALL_REQUESTS !== "true") {
|
|
@@ -77,8 +97,11 @@ const logRequestsFinished = (req: any, res: any, startTime: bigint) => {
|
|
|
77
97
|
}
|
|
78
98
|
};
|
|
79
99
|
|
|
80
|
-
|
|
81
|
-
|
|
100
|
+
export const logRequests = (
|
|
101
|
+
req: LoggableRequest,
|
|
102
|
+
res: LoggableResponse,
|
|
103
|
+
next: express.NextFunction
|
|
104
|
+
): void => {
|
|
82
105
|
const startTime = process.hrtime.bigint();
|
|
83
106
|
|
|
84
107
|
let userString = "";
|
|
@@ -106,7 +129,7 @@ export const logRequests = (req: any, res: any, next: express.NextFunction): voi
|
|
|
106
129
|
if (process.env.DISABLE_LOG_ALL_REQUESTS !== "true") {
|
|
107
130
|
logger.debug(`${req.method} <- ${req.url}${userString}${body}`);
|
|
108
131
|
}
|
|
109
|
-
onFinished(res, () => logRequestsFinished(req, res, startTime));
|
|
132
|
+
onFinished(res as unknown as OutgoingMessage, () => logRequestsFinished(req, res, startTime));
|
|
110
133
|
next();
|
|
111
134
|
};
|
|
112
135
|
|
|
@@ -145,6 +168,7 @@ export const createRouterWithAuth = (
|
|
|
145
168
|
};
|
|
146
169
|
|
|
147
170
|
export interface AuthOptions {
|
|
171
|
+
// noExplicitAny: user shape is provided by the consumer's User model — any preserves the loose-binding contract
|
|
148
172
|
// biome-ignore lint/suspicious/noExplicitAny: user shape is provided by the consumer's User model — any preserves the loose-binding contract
|
|
149
173
|
generateJWTPayload?: (user: any) => Record<string, unknown>;
|
|
150
174
|
// biome-ignore lint/suspicious/noExplicitAny: see above
|
package/src/githubAuth.ts
CHANGED
|
@@ -33,10 +33,8 @@ export interface GitHubAuthOptions {
|
|
|
33
33
|
profile: Profile,
|
|
34
34
|
accessToken: string,
|
|
35
35
|
refreshToken: string,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
// biome-ignore lint/suspicious/noExplicitAny: passport user value remains untyped
|
|
39
|
-
) => Promise<any>;
|
|
36
|
+
existingUser?: Record<string, unknown>
|
|
37
|
+
) => Promise<unknown>;
|
|
40
38
|
}
|
|
41
39
|
|
|
42
40
|
/** Fields added to user documents for GitHub authentication */
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
2
1
|
import {afterAll, afterEach, beforeEach, describe, expect, it, type Mock, spyOn} from "bun:test";
|
|
3
2
|
import * as Sentry from "@sentry/bun";
|
|
3
|
+
import type {AxiosResponse} from "axios";
|
|
4
4
|
import axios from "axios";
|
|
5
5
|
|
|
6
|
+
import type {APIError} from "../errors";
|
|
6
7
|
import {sendToGoogleChat} from "./googleChatNotifier";
|
|
7
8
|
|
|
8
9
|
describe("sendToGoogleChat", () => {
|
|
@@ -11,7 +12,7 @@ describe("sendToGoogleChat", () => {
|
|
|
11
12
|
const ORIGINAL_ENV = process.env;
|
|
12
13
|
|
|
13
14
|
beforeEach(() => {
|
|
14
|
-
mockAxiosPost = spyOn(axios, "post").mockResolvedValue({status: 200} as
|
|
15
|
+
mockAxiosPost = spyOn(axios, "post").mockResolvedValue({status: 200} as AxiosResponse);
|
|
15
16
|
process.env = {...ORIGINAL_ENV};
|
|
16
17
|
process.env.GOOGLE_CHAT_WEBHOOKS = undefined;
|
|
17
18
|
(Sentry.captureException as Mock<typeof Sentry.captureException>).mockClear();
|
|
@@ -97,8 +98,8 @@ describe("sendToGoogleChat", () => {
|
|
|
97
98
|
await sendToGoogleChat("err", {shouldThrow: true});
|
|
98
99
|
throw new Error("Expected sendToGoogleChat to throw APIError");
|
|
99
100
|
} catch (error) {
|
|
100
|
-
expect((error as
|
|
101
|
-
expect((error as
|
|
101
|
+
expect((error as APIError).name).toBe("APIError");
|
|
102
|
+
expect((error as APIError).title).toMatch(/Error posting to Google Chat/i);
|
|
102
103
|
}
|
|
103
104
|
expect(mockAxiosPost.mock.calls.length).toBe(1);
|
|
104
105
|
});
|
package/src/populate.test.ts
CHANGED
|
@@ -275,6 +275,38 @@ describe("getOpenApiSpecForModel populate with existing properties", () => {
|
|
|
275
275
|
});
|
|
276
276
|
});
|
|
277
277
|
|
|
278
|
+
describe("getOpenApiSpecForModel populate property merge", () => {
|
|
279
|
+
it("merges properties when the same path is populated twice", () => {
|
|
280
|
+
// First populate sets the field properties, second triggers the merge branch.
|
|
281
|
+
const result = getOpenApiSpecForModel(FoodModel, {
|
|
282
|
+
populatePaths: [{fields: ["name"], path: "likesIds.userId"}, {path: "likesIds.userId"}],
|
|
283
|
+
});
|
|
284
|
+
const likesIds = result.properties.likesIds as Record<string, unknown>;
|
|
285
|
+
const items = likesIds.items as Record<string, Record<string, unknown>>;
|
|
286
|
+
const userIdProps = items.properties.userId as Record<string, Record<string, unknown>>;
|
|
287
|
+
// After the merge the wider populate should have contributed all user fields.
|
|
288
|
+
expect(userIdProps.properties).toBeDefined();
|
|
289
|
+
expect(userIdProps.properties.name).toBeDefined();
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("merges openApiComponent ref into an already-populated path", () => {
|
|
293
|
+
const result = getOpenApiSpecForModel(FoodModel, {
|
|
294
|
+
populatePaths: [
|
|
295
|
+
{path: "likesIds.userId"},
|
|
296
|
+
{openApiComponent: "UserComponent", path: "likesIds.userId"},
|
|
297
|
+
],
|
|
298
|
+
});
|
|
299
|
+
const likesIds = result.properties.likesIds as Record<string, unknown>;
|
|
300
|
+
const items = likesIds.items as Record<string, Record<string, unknown>>;
|
|
301
|
+
const userIdProps = items.properties.userId as Record<string, Record<string, unknown>>;
|
|
302
|
+
// Merge path adds the $ref key inside the existing properties.
|
|
303
|
+
expect(userIdProps.properties).toBeDefined();
|
|
304
|
+
expect(userIdProps.properties.userId).toEqual({
|
|
305
|
+
$ref: "#/components/schemas/UserComponent",
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
|
|
278
310
|
describe("filterKeys (via getOpenApiSpecForModel populatePaths)", () => {
|
|
279
311
|
it("filters populated fields using dot-notation keys", () => {
|
|
280
312
|
const result = getOpenApiSpecForModel(FoodModel, {
|
package/src/transformers.test.ts
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
2
1
|
import {beforeEach, describe, expect, it} from "bun:test";
|
|
3
2
|
import type express from "express";
|
|
4
|
-
import type {Document, ObjectId} from "mongoose";
|
|
3
|
+
import type {Document, HydratedDocument, ObjectId} from "mongoose";
|
|
5
4
|
import supertest from "supertest";
|
|
6
5
|
import type TestAgent from "supertest/lib/agent";
|
|
7
6
|
|
|
8
7
|
import type {ModelRouterOptions} from "./api";
|
|
9
8
|
import {modelRouter} from "./api";
|
|
10
|
-
import {addAuthRoutes, setupAuth} from "./auth";
|
|
9
|
+
import {type UserModel as AuthUserModel, addAuthRoutes, setupAuth} from "./auth";
|
|
11
10
|
import {APIError} from "./errors";
|
|
12
11
|
import {Permissions} from "./permissions";
|
|
13
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
authAsUser,
|
|
14
|
+
type Food,
|
|
15
|
+
FoodModel,
|
|
16
|
+
getBaseServer,
|
|
17
|
+
setupTestData,
|
|
18
|
+
type User,
|
|
19
|
+
UserModel,
|
|
20
|
+
} from "./tests";
|
|
14
21
|
import {AdminOwnerTransformer, defaultResponseHandler, transform} from "./transformers";
|
|
15
22
|
|
|
16
23
|
describe("query and transform", () => {
|
|
17
|
-
let notAdmin:
|
|
18
|
-
let admin:
|
|
24
|
+
let notAdmin: HydratedDocument<User>;
|
|
25
|
+
let admin: HydratedDocument<User>;
|
|
19
26
|
let server: TestAgent;
|
|
20
27
|
let app: express.Application;
|
|
21
28
|
|
|
@@ -26,8 +33,8 @@ describe("query and transform", () => {
|
|
|
26
33
|
admin = testData.users.admin;
|
|
27
34
|
notAdmin = testData.users.notAdmin;
|
|
28
35
|
app = getBaseServer();
|
|
29
|
-
setupAuth(app, UserModel as
|
|
30
|
-
addAuthRoutes(app, UserModel as
|
|
36
|
+
setupAuth(app, UserModel as unknown as AuthUserModel);
|
|
37
|
+
addAuthRoutes(app, UserModel as unknown as AuthUserModel);
|
|
31
38
|
app.use(
|
|
32
39
|
"/food",
|
|
33
40
|
modelRouter(FoodModel, {
|