@terreno/api 0.29.0 → 0.31.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/CHANGELOG.md +2 -43
- package/LICENSE +21 -202
- package/dist/actions.js +17 -8
- package/dist/actions.openApi.test.js +1 -3
- package/dist/actions.test.js +8 -5
- package/dist/api.arrayOperations.test.js +16 -8
- package/dist/api.coverage.test.js +2 -3
- package/dist/api.d.ts +3 -2
- package/dist/api.errorPaths.test.js +608 -1
- package/dist/api.errors.test.js +9 -0
- package/dist/api.js +137 -170
- package/dist/api.query.test.js +20 -8
- package/dist/api.test.js +12 -6
- package/dist/auth.coverage.test.d.ts +1 -0
- package/dist/auth.coverage.test.js +202 -0
- package/dist/auth.test.js +159 -0
- package/dist/betterAuthSetup.d.ts +15 -1
- package/dist/betterAuthSetup.js +4 -6
- package/dist/configurationPlugin.js +2 -2
- package/dist/consentApp.test.js +1 -5
- package/dist/docLoader.js +5 -1
- package/dist/envConfigurationPlugin.d.ts +1 -1
- package/dist/envConfigurationPlugin.js +0 -2
- package/dist/errors.d.ts +14 -0
- package/dist/errors.js +24 -2
- package/dist/errors.test.js +137 -0
- package/dist/expressServer.js +11 -5
- package/dist/githubAuth.js +1 -4
- package/dist/httpClient.js +5 -1
- package/dist/openApiBuilder.test.js +0 -2
- package/dist/openApiValidator.test.js +34 -0
- package/dist/plugins.d.ts +6 -5
- package/dist/plugins.js +56 -10
- package/dist/plugins.test.js +73 -17
- package/dist/populate.test.js +9 -12
- package/dist/realtime/realtime.test.js +231 -0
- package/dist/realtime/realtimeApp.d.ts +1 -1
- package/dist/realtime/realtimeApp.js +6 -2
- package/dist/realtime/realtimeAppSetup.test.d.ts +1 -0
- package/dist/realtime/realtimeAppSetup.test.js +213 -0
- package/dist/realtime/registry.d.ts +3 -2
- package/dist/tests/models.d.ts +27 -11
- package/dist/tests/models.js +5 -3
- package/dist/tests/testHelper.d.ts +9 -3
- package/package.json +4 -5
- package/src/actions.openApi.test.ts +7 -10
- package/src/actions.test.ts +27 -15
- package/src/actions.ts +21 -12
- package/src/api.arrayOperations.test.ts +16 -8
- package/src/api.coverage.test.ts +9 -10
- package/src/api.errorPaths.test.ts +360 -4
- package/src/api.errors.test.ts +12 -0
- package/src/api.query.test.ts +20 -8
- package/src/api.test.ts +12 -6
- package/src/api.ts +162 -171
- package/src/auth.coverage.test.ts +101 -0
- package/src/auth.test.ts +125 -2
- package/src/betterAuthSetup.ts +7 -7
- package/src/configurationPlugin.ts +2 -2
- package/src/consentApp.test.ts +12 -12
- package/src/docLoader.ts +6 -2
- package/src/envConfigurationPlugin.ts +2 -6
- package/src/errors.test.ts +106 -1
- package/src/errors.ts +25 -1
- package/src/expressServer.ts +11 -5
- package/src/githubAuth.ts +1 -3
- package/src/httpClient.ts +6 -2
- package/src/models/versionConfig.ts +1 -1
- package/src/openApiBuilder.test.ts +24 -9
- package/src/openApiValidator.test.ts +41 -0
- package/src/plugins.test.ts +59 -9
- package/src/plugins.ts +81 -39
- package/src/populate.test.ts +25 -16
- package/src/realtime/changeStreamWatcher.ts +8 -1
- package/src/realtime/realtime.test.ts +239 -0
- package/src/realtime/realtimeApp.ts +8 -5
- package/src/realtime/realtimeAppSetup.test.ts +161 -0
- package/src/realtime/registry.ts +3 -4
- package/src/tests/models.ts +8 -6
package/dist/api.query.test.js
CHANGED
|
@@ -309,7 +309,9 @@ var tests_1 = require("./tests");
|
|
|
309
309
|
case 0: return [4 /*yield*/, agent.get("/food?limit=1&page=0").expect(400)];
|
|
310
310
|
case 1:
|
|
311
311
|
res = _a.sent();
|
|
312
|
-
(0, bun_test_1.expect)(res.body.title).toBe("Invalid page
|
|
312
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Invalid page");
|
|
313
|
+
(0, bun_test_1.expect)(res.body.detail).toBe("Invalid page: 0");
|
|
314
|
+
(0, bun_test_1.expect)(res.body.source).toEqual({ parameter: "page" });
|
|
313
315
|
return [2 /*return*/];
|
|
314
316
|
}
|
|
315
317
|
});
|
|
@@ -321,7 +323,9 @@ var tests_1 = require("./tests");
|
|
|
321
323
|
case 0: return [4 /*yield*/, agent.get("/food?limit=1&page=abc").expect(400)];
|
|
322
324
|
case 1:
|
|
323
325
|
res = _a.sent();
|
|
324
|
-
(0, bun_test_1.expect)(res.body.title).toBe("Invalid page
|
|
326
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Invalid page");
|
|
327
|
+
(0, bun_test_1.expect)(res.body.detail).toBe("Invalid page: abc");
|
|
328
|
+
(0, bun_test_1.expect)(res.body.source).toEqual({ parameter: "page" });
|
|
325
329
|
return [2 /*return*/];
|
|
326
330
|
}
|
|
327
331
|
});
|
|
@@ -382,7 +386,9 @@ var tests_1 = require("./tests");
|
|
|
382
386
|
case 0: return [4 /*yield*/, agent.get("/food").query({ created_gte: "not-a-date", limit: 10 }).expect(400)];
|
|
383
387
|
case 1:
|
|
384
388
|
res = _a.sent();
|
|
385
|
-
(0, bun_test_1.expect)(res.body.title).toBe("Invalid date
|
|
389
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Invalid date query parameter");
|
|
390
|
+
(0, bun_test_1.expect)(res.body.detail).toBe("Invalid date for query parameter created_gte");
|
|
391
|
+
(0, bun_test_1.expect)(res.body.source).toEqual({ parameter: "created_gte" });
|
|
386
392
|
return [2 /*return*/];
|
|
387
393
|
}
|
|
388
394
|
});
|
|
@@ -397,7 +403,9 @@ var tests_1 = require("./tests");
|
|
|
397
403
|
.expect(400)];
|
|
398
404
|
case 1:
|
|
399
405
|
res = _a.sent();
|
|
400
|
-
(0, bun_test_1.expect)(res.body.title).toBe("Invalid date
|
|
406
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Invalid date query parameter");
|
|
407
|
+
(0, bun_test_1.expect)(res.body.detail).toBe("Invalid date for query parameter created_lte");
|
|
408
|
+
(0, bun_test_1.expect)(res.body.source).toEqual({ parameter: "created_lte" });
|
|
401
409
|
return [2 /*return*/];
|
|
402
410
|
}
|
|
403
411
|
});
|
|
@@ -481,7 +489,8 @@ var tests_1 = require("./tests");
|
|
|
481
489
|
case 0: return [4 /*yield*/, agent.get("/food?ownerId=".concat(admin._id)).expect(400)];
|
|
482
490
|
case 1:
|
|
483
491
|
res = _a.sent();
|
|
484
|
-
(0, bun_test_1.expect)(res.body.title).toBe("
|
|
492
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Query parameter not allowed");
|
|
493
|
+
(0, bun_test_1.expect)(res.body.detail).toBe("ownerId is not allowed as a query param.");
|
|
485
494
|
return [2 /*return*/];
|
|
486
495
|
}
|
|
487
496
|
});
|
|
@@ -747,19 +756,22 @@ var tests_1 = require("./tests");
|
|
|
747
756
|
.expect(400)];
|
|
748
757
|
case 1:
|
|
749
758
|
res = _a.sent();
|
|
750
|
-
(0, bun_test_1.expect)(res.body.title).toBe("
|
|
759
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Query parameter not allowed");
|
|
760
|
+
(0, bun_test_1.expect)(res.body.detail).toBe("ownerId is not allowed as a query param.");
|
|
751
761
|
return [4 /*yield*/, agent
|
|
752
762
|
.get("/food?".concat(qs_1.default.stringify({ $and: [{ tags: "cheap" }, { ownerId: "healthy" }] })))
|
|
753
763
|
.expect(400)];
|
|
754
764
|
case 2:
|
|
755
765
|
res = _a.sent();
|
|
756
|
-
(0, bun_test_1.expect)(res.body.title).toBe("
|
|
766
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Query parameter not allowed");
|
|
767
|
+
(0, bun_test_1.expect)(res.body.detail).toBe("ownerId is not allowed as a query param.");
|
|
757
768
|
return [4 /*yield*/, agent
|
|
758
769
|
.get("/food?".concat(qs_1.default.stringify({ $or: [{ tags: "cheap" }, { ownerId: "healthy" }] })))
|
|
759
770
|
.expect(400)];
|
|
760
771
|
case 3:
|
|
761
772
|
res = _a.sent();
|
|
762
|
-
(0, bun_test_1.expect)(res.body.title).toBe("
|
|
773
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Query parameter not allowed");
|
|
774
|
+
(0, bun_test_1.expect)(res.body.detail).toBe("ownerId is not allowed as a query param.");
|
|
763
775
|
return [2 /*return*/];
|
|
764
776
|
}
|
|
765
777
|
});
|
package/dist/api.test.js
CHANGED
|
@@ -698,7 +698,8 @@ var transformers_1 = require("./transformers");
|
|
|
698
698
|
return [4 /*yield*/, server.get("/food?calories=100").expect(400)];
|
|
699
699
|
case 1:
|
|
700
700
|
res = _a.sent();
|
|
701
|
-
(0, bun_test_1.expect)(res.body.title).
|
|
701
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Query parameter not allowed");
|
|
702
|
+
(0, bun_test_1.expect)(res.body.detail).toContain("calories is not allowed as a query param");
|
|
702
703
|
return [2 /*return*/];
|
|
703
704
|
}
|
|
704
705
|
});
|
|
@@ -832,8 +833,9 @@ var transformers_1 = require("./transformers");
|
|
|
832
833
|
return [4 /*yield*/, server.post("/required").send({ about: "test" }).expect(400)];
|
|
833
834
|
case 1:
|
|
834
835
|
res = _a.sent();
|
|
835
|
-
(0, bun_test_1.expect)(res.body.title).toBe("
|
|
836
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Validation failed");
|
|
836
837
|
(0, bun_test_1.expect)(res.body.detail).toContain("Required");
|
|
838
|
+
(0, bun_test_1.expect)(res.body.meta.fields.name).toContain("required");
|
|
837
839
|
return [2 /*return*/];
|
|
838
840
|
}
|
|
839
841
|
});
|
|
@@ -1562,7 +1564,8 @@ var transformers_1 = require("./transformers");
|
|
|
1562
1564
|
return [4 /*yield*/, agent.post("/food/".concat(apple._id, "/tags")).send({ tags: "organic" }).expect(405)];
|
|
1563
1565
|
case 2:
|
|
1564
1566
|
res = _a.sent();
|
|
1565
|
-
(0, bun_test_1.expect)(res.body.title).
|
|
1567
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Access denied");
|
|
1568
|
+
(0, bun_test_1.expect)(res.body.detail).toContain("Access to PATCH");
|
|
1566
1569
|
return [2 /*return*/];
|
|
1567
1570
|
}
|
|
1568
1571
|
});
|
|
@@ -1590,7 +1593,8 @@ var transformers_1 = require("./transformers");
|
|
|
1590
1593
|
return [4 /*yield*/, agent.post("/food/".concat(fakeId, "/tags")).send({ tags: "organic" }).expect(404)];
|
|
1591
1594
|
case 2:
|
|
1592
1595
|
res = _a.sent();
|
|
1593
|
-
(0, bun_test_1.expect)(res.body.title).
|
|
1596
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Document not found");
|
|
1597
|
+
(0, bun_test_1.expect)(res.body.detail).toContain("Could not find document to PATCH");
|
|
1594
1598
|
return [2 /*return*/];
|
|
1595
1599
|
}
|
|
1596
1600
|
});
|
|
@@ -1619,7 +1623,8 @@ var transformers_1 = require("./transformers");
|
|
|
1619
1623
|
return [4 /*yield*/, agent.post("/food/".concat(apple._id, "/tags")).send({ tags: "organic" }).expect(403)];
|
|
1620
1624
|
case 2:
|
|
1621
1625
|
res = _a.sent();
|
|
1622
|
-
(0, bun_test_1.expect)(res.body.title).
|
|
1626
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Update not allowed");
|
|
1627
|
+
(0, bun_test_1.expect)(res.body.detail).toContain("Patch not allowed");
|
|
1623
1628
|
return [2 /*return*/];
|
|
1624
1629
|
}
|
|
1625
1630
|
});
|
|
@@ -1762,8 +1767,9 @@ var transformers_1 = require("./transformers");
|
|
|
1762
1767
|
return [4 /*yield*/, server.post("/required").send({ about: "test" }).expect(400)];
|
|
1763
1768
|
case 2:
|
|
1764
1769
|
res = _a.sent();
|
|
1765
|
-
(0, bun_test_1.expect)(res.body.title).toBe("
|
|
1770
|
+
(0, bun_test_1.expect)(res.body.title).toBe("Validation failed");
|
|
1766
1771
|
(0, bun_test_1.expect)(res.body.detail).toContain("Required");
|
|
1772
|
+
(0, bun_test_1.expect)(res.body.meta.fields.name).toContain("required");
|
|
1767
1773
|
return [2 /*return*/];
|
|
1768
1774
|
}
|
|
1769
1775
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
24
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
var bun_test_1 = require("bun:test");
|
|
54
|
+
var jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
55
|
+
var supertest_1 = __importDefault(require("supertest"));
|
|
56
|
+
var auth_1 = require("./auth");
|
|
57
|
+
var tests_1 = require("./tests");
|
|
58
|
+
var testUserModel = tests_1.UserModel;
|
|
59
|
+
var signTokenForId = function (id) {
|
|
60
|
+
return jsonwebtoken_1.default.sign({ id: id }, process.env.TOKEN_SECRET, { issuer: process.env.TOKEN_ISSUER });
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Builds a stand-in UserModel that reuses the real passport strategy but lets each test
|
|
64
|
+
* control what `findById` resolves to.
|
|
65
|
+
*/
|
|
66
|
+
var createStubUserModel = function (overrides) {
|
|
67
|
+
return __assign({ createStrategy: function () { return testUserModel.createStrategy(); }, findById: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
68
|
+
return [2 /*return*/, null];
|
|
69
|
+
}); }); } }, overrides);
|
|
70
|
+
};
|
|
71
|
+
(0, bun_test_1.describe)("setupAuth token extraction", function () {
|
|
72
|
+
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
73
|
+
return __generator(this, function (_a) {
|
|
74
|
+
switch (_a.label) {
|
|
75
|
+
case 0:
|
|
76
|
+
(0, bun_test_1.setSystemTime)();
|
|
77
|
+
return [4 /*yield*/, (0, tests_1.setupTestData)()];
|
|
78
|
+
case 1:
|
|
79
|
+
_a.sent();
|
|
80
|
+
return [2 /*return*/];
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}); });
|
|
84
|
+
(0, bun_test_1.it)("reads the token from the jwt cookie when no authorization header is present", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
85
|
+
var app, admin, token, res;
|
|
86
|
+
return __generator(this, function (_a) {
|
|
87
|
+
switch (_a.label) {
|
|
88
|
+
case 0:
|
|
89
|
+
app = (0, tests_1.getBaseServer)();
|
|
90
|
+
return [4 /*yield*/, tests_1.UserModel.findOne({ email: "admin@example.com" })];
|
|
91
|
+
case 1:
|
|
92
|
+
admin = _a.sent();
|
|
93
|
+
token = signTokenForId(String(admin === null || admin === void 0 ? void 0 : admin._id));
|
|
94
|
+
// Emulate cookie-parser, which consumers mount before setupAuth.
|
|
95
|
+
app.use(function (req, _res, next) {
|
|
96
|
+
req.cookies = { jwt: token };
|
|
97
|
+
next();
|
|
98
|
+
});
|
|
99
|
+
(0, auth_1.setupAuth)(app, testUserModel);
|
|
100
|
+
(0, auth_1.addMeRoutes)(app, testUserModel);
|
|
101
|
+
return [4 /*yield*/, supertest_1.default.agent(app).get("/auth/me").expect(200)];
|
|
102
|
+
case 2:
|
|
103
|
+
res = _a.sent();
|
|
104
|
+
(0, bun_test_1.expect)(res.body.data.email).toBe("admin@example.com");
|
|
105
|
+
return [2 /*return*/];
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}); });
|
|
109
|
+
(0, bun_test_1.it)("logs the JWT setup message when not running in test mode", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
110
|
+
var app, res;
|
|
111
|
+
return __generator(this, function (_a) {
|
|
112
|
+
switch (_a.label) {
|
|
113
|
+
case 0:
|
|
114
|
+
app = (0, tests_1.getBaseServer)();
|
|
115
|
+
process.env.NODE_ENV = "development";
|
|
116
|
+
_a.label = 1;
|
|
117
|
+
case 1:
|
|
118
|
+
_a.trys.push([1, , 3, 4]);
|
|
119
|
+
(0, auth_1.setupAuth)(app, testUserModel);
|
|
120
|
+
(0, auth_1.addAuthRoutes)(app, testUserModel);
|
|
121
|
+
(0, auth_1.addMeRoutes)(app, testUserModel);
|
|
122
|
+
return [4 /*yield*/, supertest_1.default
|
|
123
|
+
.agent(app)
|
|
124
|
+
.post("/auth/login")
|
|
125
|
+
.send({ email: "admin@example.com", password: "securePassword" })
|
|
126
|
+
.expect(200)];
|
|
127
|
+
case 2:
|
|
128
|
+
res = _a.sent();
|
|
129
|
+
(0, bun_test_1.expect)(res.body.data.token).toBeDefined();
|
|
130
|
+
return [3 /*break*/, 4];
|
|
131
|
+
case 3:
|
|
132
|
+
process.env.NODE_ENV = "test";
|
|
133
|
+
return [7 /*endfinally*/];
|
|
134
|
+
case 4: return [2 /*return*/];
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
}); });
|
|
138
|
+
});
|
|
139
|
+
(0, bun_test_1.describe)("addMeRoutes without a resolvable user", function () {
|
|
140
|
+
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
141
|
+
return __generator(this, function (_a) {
|
|
142
|
+
switch (_a.label) {
|
|
143
|
+
case 0:
|
|
144
|
+
(0, bun_test_1.setSystemTime)();
|
|
145
|
+
return [4 /*yield*/, (0, tests_1.setupTestData)()];
|
|
146
|
+
case 1:
|
|
147
|
+
_a.sent();
|
|
148
|
+
return [2 /*return*/];
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}); });
|
|
152
|
+
(0, bun_test_1.it)("returns 401 when the authenticated user has no id", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
153
|
+
var app, anonymousModel, agent;
|
|
154
|
+
return __generator(this, function (_a) {
|
|
155
|
+
switch (_a.label) {
|
|
156
|
+
case 0:
|
|
157
|
+
app = (0, tests_1.getBaseServer)();
|
|
158
|
+
anonymousModel = createStubUserModel({
|
|
159
|
+
createAnonymousUser: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
160
|
+
return [2 /*return*/, ({})];
|
|
161
|
+
}); }); },
|
|
162
|
+
});
|
|
163
|
+
(0, auth_1.setupAuth)(app, anonymousModel);
|
|
164
|
+
(0, auth_1.addMeRoutes)(app, anonymousModel);
|
|
165
|
+
agent = supertest_1.default
|
|
166
|
+
.agent(app)
|
|
167
|
+
.set("authorization", "Bearer ".concat(signTokenForId("507f1f77bcf86cd799439011")));
|
|
168
|
+
return [4 /*yield*/, agent.get("/auth/me").expect(401)];
|
|
169
|
+
case 1:
|
|
170
|
+
_a.sent();
|
|
171
|
+
return [4 /*yield*/, agent.patch("/auth/me").send({ name: "New Name" }).expect(401)];
|
|
172
|
+
case 2:
|
|
173
|
+
_a.sent();
|
|
174
|
+
return [2 /*return*/];
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}); });
|
|
178
|
+
(0, bun_test_1.it)("returns 404 when the user cannot be found for the me routes", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
179
|
+
var app, admin, agent;
|
|
180
|
+
return __generator(this, function (_a) {
|
|
181
|
+
switch (_a.label) {
|
|
182
|
+
case 0:
|
|
183
|
+
app = (0, tests_1.getBaseServer)();
|
|
184
|
+
return [4 /*yield*/, tests_1.UserModel.findOne({ email: "admin@example.com" })];
|
|
185
|
+
case 1:
|
|
186
|
+
admin = _a.sent();
|
|
187
|
+
(0, auth_1.setupAuth)(app, testUserModel);
|
|
188
|
+
(0, auth_1.addMeRoutes)(app, createStubUserModel({}));
|
|
189
|
+
agent = supertest_1.default
|
|
190
|
+
.agent(app)
|
|
191
|
+
.set("authorization", "Bearer ".concat(signTokenForId(String(admin === null || admin === void 0 ? void 0 : admin._id))));
|
|
192
|
+
return [4 /*yield*/, agent.get("/auth/me").expect(404)];
|
|
193
|
+
case 2:
|
|
194
|
+
_a.sent();
|
|
195
|
+
return [4 /*yield*/, agent.patch("/auth/me").send({ name: "New Name" }).expect(404)];
|
|
196
|
+
case 3:
|
|
197
|
+
_a.sent();
|
|
198
|
+
return [2 /*return*/];
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}); });
|
|
202
|
+
});
|
package/dist/auth.test.js
CHANGED
|
@@ -102,9 +102,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
102
102
|
// noExplicitAny: test mock typing
|
|
103
103
|
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
104
104
|
var bun_test_1 = require("bun:test");
|
|
105
|
+
var express_1 = __importDefault(require("express"));
|
|
105
106
|
var supertest_1 = __importDefault(require("supertest"));
|
|
106
107
|
var api_1 = require("./api");
|
|
107
108
|
var auth_1 = require("./auth");
|
|
109
|
+
var logger_1 = require("./logger");
|
|
108
110
|
var permissions_1 = require("./permissions");
|
|
109
111
|
var requestContext_1 = require("./requestContext");
|
|
110
112
|
var terrenoApp_1 = require("./terrenoApp");
|
|
@@ -2125,3 +2127,160 @@ var decodeTokenPayload = function (token) {
|
|
|
2125
2127
|
});
|
|
2126
2128
|
}); });
|
|
2127
2129
|
});
|
|
2130
|
+
(0, bun_test_1.describe)("cookie based JWT extraction", function () {
|
|
2131
|
+
(0, bun_test_1.afterEach)(function () {
|
|
2132
|
+
(0, bun_test_1.setSystemTime)();
|
|
2133
|
+
});
|
|
2134
|
+
(0, bun_test_1.it)("authenticates /auth/me from a jwt cookie", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
2135
|
+
var users, jwtLib, token, app, res;
|
|
2136
|
+
return __generator(this, function (_a) {
|
|
2137
|
+
switch (_a.label) {
|
|
2138
|
+
case 0:
|
|
2139
|
+
(0, bun_test_1.setSystemTime)();
|
|
2140
|
+
return [4 /*yield*/, (0, tests_1.setupTestData)()];
|
|
2141
|
+
case 1:
|
|
2142
|
+
users = (_a.sent()).users;
|
|
2143
|
+
return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require("jsonwebtoken")); })];
|
|
2144
|
+
case 2:
|
|
2145
|
+
jwtLib = (_a.sent()).default;
|
|
2146
|
+
token = jwtLib.sign({ id: String(users.admin._id) }, process.env.TOKEN_SECRET, {
|
|
2147
|
+
issuer: process.env.TOKEN_ISSUER,
|
|
2148
|
+
});
|
|
2149
|
+
app = (0, tests_1.getBaseServer)();
|
|
2150
|
+
// Stand in for cookie-parser: the extractor prefers req.cookies.jwt over the auth header.
|
|
2151
|
+
app.use(function (req, _res, next) {
|
|
2152
|
+
req.cookies = { jwt: token };
|
|
2153
|
+
next();
|
|
2154
|
+
});
|
|
2155
|
+
(0, auth_1.setupAuth)(app, tests_1.UserModel);
|
|
2156
|
+
(0, auth_1.addMeRoutes)(app, tests_1.UserModel);
|
|
2157
|
+
return [4 /*yield*/, supertest_1.default.agent(app).get("/auth/me").expect(200)];
|
|
2158
|
+
case 3:
|
|
2159
|
+
res = _a.sent();
|
|
2160
|
+
(0, bun_test_1.expect)(res.body.data.email).toBe("admin@example.com");
|
|
2161
|
+
return [2 /*return*/];
|
|
2162
|
+
}
|
|
2163
|
+
});
|
|
2164
|
+
}); });
|
|
2165
|
+
});
|
|
2166
|
+
(0, bun_test_1.describe)("auth logging outside of the test environment", function () {
|
|
2167
|
+
var originalNodeEnv = process.env.NODE_ENV;
|
|
2168
|
+
(0, bun_test_1.afterEach)(function () {
|
|
2169
|
+
(0, bun_test_1.setSystemTime)();
|
|
2170
|
+
process.env.NODE_ENV = originalNodeEnv;
|
|
2171
|
+
});
|
|
2172
|
+
(0, bun_test_1.it)("logs JWT setup and successful logins when NODE_ENV is not test", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
2173
|
+
var debugSpy, infoSpy, app, res;
|
|
2174
|
+
return __generator(this, function (_a) {
|
|
2175
|
+
switch (_a.label) {
|
|
2176
|
+
case 0:
|
|
2177
|
+
(0, bun_test_1.setSystemTime)();
|
|
2178
|
+
return [4 /*yield*/, (0, tests_1.setupTestData)()];
|
|
2179
|
+
case 1:
|
|
2180
|
+
_a.sent();
|
|
2181
|
+
process.env.NODE_ENV = "development";
|
|
2182
|
+
debugSpy = (0, bun_test_1.spyOn)(logger_1.logger, "debug");
|
|
2183
|
+
infoSpy = (0, bun_test_1.spyOn)(logger_1.logger, "info");
|
|
2184
|
+
_a.label = 2;
|
|
2185
|
+
case 2:
|
|
2186
|
+
_a.trys.push([2, , 4, 5]);
|
|
2187
|
+
app = (0, tests_1.getBaseServer)();
|
|
2188
|
+
(0, auth_1.setupAuth)(app, tests_1.UserModel);
|
|
2189
|
+
(0, auth_1.addAuthRoutes)(app, tests_1.UserModel);
|
|
2190
|
+
return [4 /*yield*/, supertest_1.default
|
|
2191
|
+
.agent(app)
|
|
2192
|
+
.post("/auth/login")
|
|
2193
|
+
.send({ email: "notAdmin@example.com", password: "password" })
|
|
2194
|
+
.expect(200)];
|
|
2195
|
+
case 3:
|
|
2196
|
+
res = _a.sent();
|
|
2197
|
+
(0, bun_test_1.expect)(res.body.data.token).toBeDefined();
|
|
2198
|
+
(0, bun_test_1.expect)(debugSpy.mock.calls.some(function (_a) {
|
|
2199
|
+
var _b = __read(_a, 1), message = _b[0];
|
|
2200
|
+
return String(message).includes("Setting up JWT Authentication");
|
|
2201
|
+
})).toBe(true);
|
|
2202
|
+
(0, bun_test_1.expect)(infoSpy.mock.calls.some(function (_a) {
|
|
2203
|
+
var _b = __read(_a, 1), message = _b[0];
|
|
2204
|
+
return String(message).includes("User logged in");
|
|
2205
|
+
})).toBe(true);
|
|
2206
|
+
return [3 /*break*/, 5];
|
|
2207
|
+
case 4:
|
|
2208
|
+
debugSpy.mockRestore();
|
|
2209
|
+
infoSpy.mockRestore();
|
|
2210
|
+
return [7 /*endfinally*/];
|
|
2211
|
+
case 5: return [2 /*return*/];
|
|
2212
|
+
}
|
|
2213
|
+
});
|
|
2214
|
+
}); });
|
|
2215
|
+
});
|
|
2216
|
+
(0, bun_test_1.describe)("/me routes with an already populated req.user", function () {
|
|
2217
|
+
var buildApp = function (user, foundUser) {
|
|
2218
|
+
var app = (0, express_1.default)();
|
|
2219
|
+
app.use(express_1.default.json());
|
|
2220
|
+
app.use(function (req, _res, next) {
|
|
2221
|
+
req.user = user;
|
|
2222
|
+
next();
|
|
2223
|
+
});
|
|
2224
|
+
var stubUserModel = {
|
|
2225
|
+
findById: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
2226
|
+
return [2 /*return*/, foundUser];
|
|
2227
|
+
}); }); },
|
|
2228
|
+
};
|
|
2229
|
+
(0, auth_1.addMeRoutes)(app, stubUserModel);
|
|
2230
|
+
return app;
|
|
2231
|
+
};
|
|
2232
|
+
(0, bun_test_1.it)("returns 401 from GET /auth/me when the request user has no id", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
2233
|
+
return __generator(this, function (_a) {
|
|
2234
|
+
switch (_a.label) {
|
|
2235
|
+
case 0: return [4 /*yield*/, supertest_1.default
|
|
2236
|
+
.agent(buildApp({ _id: "abc" }, null))
|
|
2237
|
+
.get("/auth/me")
|
|
2238
|
+
.expect(401)];
|
|
2239
|
+
case 1:
|
|
2240
|
+
_a.sent();
|
|
2241
|
+
return [2 /*return*/];
|
|
2242
|
+
}
|
|
2243
|
+
});
|
|
2244
|
+
}); });
|
|
2245
|
+
(0, bun_test_1.it)("returns 401 from PATCH /auth/me when the request user has no id", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
2246
|
+
return __generator(this, function (_a) {
|
|
2247
|
+
switch (_a.label) {
|
|
2248
|
+
case 0: return [4 /*yield*/, supertest_1.default
|
|
2249
|
+
.agent(buildApp({ _id: "abc" }, null))
|
|
2250
|
+
.patch("/auth/me")
|
|
2251
|
+
.send({ name: "Updated" })
|
|
2252
|
+
.expect(401)];
|
|
2253
|
+
case 1:
|
|
2254
|
+
_a.sent();
|
|
2255
|
+
return [2 /*return*/];
|
|
2256
|
+
}
|
|
2257
|
+
});
|
|
2258
|
+
}); });
|
|
2259
|
+
(0, bun_test_1.it)("returns 404 from GET /auth/me when the user record no longer exists", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
2260
|
+
return __generator(this, function (_a) {
|
|
2261
|
+
switch (_a.label) {
|
|
2262
|
+
case 0: return [4 /*yield*/, supertest_1.default
|
|
2263
|
+
.agent(buildApp({ id: "abc" }, null))
|
|
2264
|
+
.get("/auth/me")
|
|
2265
|
+
.expect(404)];
|
|
2266
|
+
case 1:
|
|
2267
|
+
_a.sent();
|
|
2268
|
+
return [2 /*return*/];
|
|
2269
|
+
}
|
|
2270
|
+
});
|
|
2271
|
+
}); });
|
|
2272
|
+
(0, bun_test_1.it)("returns 404 from PATCH /auth/me when the user record no longer exists", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
2273
|
+
return __generator(this, function (_a) {
|
|
2274
|
+
switch (_a.label) {
|
|
2275
|
+
case 0: return [4 /*yield*/, supertest_1.default
|
|
2276
|
+
.agent(buildApp({ id: "abc" }, null))
|
|
2277
|
+
.patch("/auth/me")
|
|
2278
|
+
.send({ name: "Updated" })
|
|
2279
|
+
.expect(404)];
|
|
2280
|
+
case 1:
|
|
2281
|
+
_a.sent();
|
|
2282
|
+
return [2 /*return*/];
|
|
2283
|
+
}
|
|
2284
|
+
});
|
|
2285
|
+
}); });
|
|
2286
|
+
});
|
|
@@ -32,7 +32,20 @@ export declare const createBetterAuth: (options: CreateBetterAuthOptions) => Bet
|
|
|
32
32
|
* and populates req.user with the application User model.
|
|
33
33
|
*/
|
|
34
34
|
export declare const createBetterAuthSessionMiddleware: (auth: BetterAuthInstance, userModel?: UserModel) => (req: Request, _res: Response, next: NextFunction) => Promise<void>;
|
|
35
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Syncs a Better Auth user to the application User model.
|
|
37
|
+
* Creates or updates the user as needed.
|
|
38
|
+
*/
|
|
39
|
+
interface MutableUserDoc {
|
|
40
|
+
admin?: boolean;
|
|
41
|
+
email?: string;
|
|
42
|
+
name?: string;
|
|
43
|
+
betterAuthId?: string;
|
|
44
|
+
oauthProvider?: string | null;
|
|
45
|
+
id?: string;
|
|
46
|
+
save: () => Promise<unknown>;
|
|
47
|
+
}
|
|
48
|
+
export declare const syncBetterAuthUser: (userModel: UserModel, betterAuthUser: BetterAuthUser, oauthProvider?: string) => Promise<MutableUserDoc>;
|
|
36
49
|
/**
|
|
37
50
|
* Mounts Better Auth routes on the Express app.
|
|
38
51
|
*/
|
|
@@ -57,3 +70,4 @@ export declare const getBetterAuthSession: (req: Request) => BetterAuthSessionDa
|
|
|
57
70
|
* Checks if the request has a valid Better Auth session.
|
|
58
71
|
*/
|
|
59
72
|
export declare const hasBetterAuthSession: (req: Request) => boolean;
|
|
73
|
+
export {};
|
package/dist/betterAuthSetup.js
CHANGED
|
@@ -190,11 +190,8 @@ var createBetterAuthSessionMiddleware = function (auth, userModel) {
|
|
|
190
190
|
}); };
|
|
191
191
|
};
|
|
192
192
|
exports.createBetterAuthSessionMiddleware = createBetterAuthSessionMiddleware;
|
|
193
|
-
var syncBetterAuthUser = function (userModel, betterAuthUser, oauthProvider
|
|
194
|
-
|
|
195
|
-
// biome-ignore lint/suspicious/noExplicitAny: return is a consumer-defined user document; tests inspect varied fields
|
|
196
|
-
) { return __awaiter(void 0, void 0, void 0, function () {
|
|
197
|
-
var existingUser, userByEmail, useAsId, newUser, error_2;
|
|
193
|
+
var syncBetterAuthUser = function (userModel, betterAuthUser, oauthProvider) { return __awaiter(void 0, void 0, void 0, function () {
|
|
194
|
+
var existingUser, userByEmail, useAsId, UserDocumentConstructor, newUser, error_2;
|
|
198
195
|
return __generator(this, function (_a) {
|
|
199
196
|
switch (_a.label) {
|
|
200
197
|
case 0:
|
|
@@ -231,7 +228,8 @@ var syncBetterAuthUser = function (userModel, betterAuthUser, oauthProvider
|
|
|
231
228
|
return [2 /*return*/, userByEmail];
|
|
232
229
|
case 6:
|
|
233
230
|
useAsId = mongoose_1.default.isValidObjectId(betterAuthUser.id) ? { _id: betterAuthUser.id } : {};
|
|
234
|
-
|
|
231
|
+
UserDocumentConstructor = userModel;
|
|
232
|
+
newUser = new UserDocumentConstructor(__assign(__assign({}, useAsId), { admin: false, betterAuthId: betterAuthUser.id, email: betterAuthUser.email, name: betterAuthUser.name || betterAuthUser.email.split("@")[0], oauthProvider: oauthProvider || null }));
|
|
235
233
|
return [4 /*yield*/, newUser.save()];
|
|
236
234
|
case 7:
|
|
237
235
|
_a.sent();
|
|
@@ -314,7 +314,7 @@ var configurationPlugin = function (schema, options) {
|
|
|
314
314
|
if (Object.keys(setFields).length === 0) {
|
|
315
315
|
return [2 /*return*/, this.getConfig()];
|
|
316
316
|
}
|
|
317
|
-
return [4 /*yield*/, this.findOneAndUpdate(singletonFilter, { $set: setFields }, {
|
|
317
|
+
return [4 /*yield*/, this.findOneAndUpdate(singletonFilter, { $set: setFields }, { returnDocument: "after", runValidators: true })];
|
|
318
318
|
case 1:
|
|
319
319
|
updated = _e.sent();
|
|
320
320
|
if (updated) {
|
|
@@ -327,7 +327,7 @@ var configurationPlugin = function (schema, options) {
|
|
|
327
327
|
// No singleton yet — create one (with subdocument defaults applied), then
|
|
328
328
|
// apply the patch.
|
|
329
329
|
_e.sent();
|
|
330
|
-
return [2 /*return*/, this.findOneAndUpdate(singletonFilter, { $set: setFields }, {
|
|
330
|
+
return [2 /*return*/, this.findOneAndUpdate(singletonFilter, { $set: setFields }, { returnDocument: "after", runValidators: true }).orFail()];
|
|
331
331
|
}
|
|
332
332
|
});
|
|
333
333
|
});
|
package/dist/consentApp.test.js
CHANGED
|
@@ -55,8 +55,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
55
55
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
56
56
|
};
|
|
57
57
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
58
|
-
// noExplicitAny: test mock typing
|
|
59
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
60
58
|
var bun_test_1 = require("bun:test");
|
|
61
59
|
var supertest_1 = __importDefault(require("supertest"));
|
|
62
60
|
var consentApp_1 = require("./consentApp");
|
|
@@ -556,9 +554,7 @@ var buildApp = function (consentAppOptions) {
|
|
|
556
554
|
case 1:
|
|
557
555
|
_a.sent();
|
|
558
556
|
filteredApp = buildApp({
|
|
559
|
-
resolveConsentForms: function (_user, forms) {
|
|
560
|
-
return forms.filter(function (f) { return f.type === "privacy"; });
|
|
561
|
-
},
|
|
557
|
+
resolveConsentForms: function (_user, forms) { return forms.filter(function (f) { return f.type === "privacy"; }); },
|
|
562
558
|
});
|
|
563
559
|
return [4 /*yield*/, (0, tests_1.authAsUser)(filteredApp, "notAdmin")];
|
|
564
560
|
case 2:
|
package/dist/docLoader.js
CHANGED
|
@@ -69,7 +69,8 @@ var loadDocOr404 = function (model, id, populatePaths) { return __awaiter(void 0
|
|
|
69
69
|
throw new errors_1.APIError({
|
|
70
70
|
cause: error_1,
|
|
71
71
|
code: "get-error",
|
|
72
|
-
detail: "GET failed on ".concat(id),
|
|
72
|
+
detail: "GET failed on ".concat(id, ": ").concat((0, errors_1.errorDetail)(error_1)),
|
|
73
|
+
meta: { model: model.modelName },
|
|
73
74
|
status: 500,
|
|
74
75
|
title: "GET error",
|
|
75
76
|
});
|
|
@@ -83,6 +84,7 @@ var loadDocOr404 = function (model, id, populatePaths) { return __awaiter(void 0
|
|
|
83
84
|
notFoundDetail = "Document ".concat(id, " not found for model ").concat(model.modelName);
|
|
84
85
|
if (!hiddenDoc) {
|
|
85
86
|
throw new errors_1.NotFoundError({
|
|
87
|
+
code: "document-not-found",
|
|
86
88
|
detail: notFoundDetail,
|
|
87
89
|
title: "Document not found",
|
|
88
90
|
});
|
|
@@ -99,11 +101,13 @@ var loadDocOr404 = function (model, id, populatePaths) { return __awaiter(void 0
|
|
|
99
101
|
}
|
|
100
102
|
if (!reason) {
|
|
101
103
|
throw new errors_1.NotFoundError({
|
|
104
|
+
code: "document-not-found",
|
|
102
105
|
detail: notFoundDetail,
|
|
103
106
|
title: "Document not found",
|
|
104
107
|
});
|
|
105
108
|
}
|
|
106
109
|
throw new errors_1.NotFoundError({
|
|
110
|
+
code: "document-not-found",
|
|
107
111
|
detail: notFoundDetail,
|
|
108
112
|
disableExternalErrorTracking: true,
|
|
109
113
|
meta: reason,
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { Schema } from "mongoose";
|
|
2
|
-
export declare const envConfigurationPlugin: (schema: Schema
|
|
2
|
+
export declare const envConfigurationPlugin: (schema: Schema) => void;
|