@terreno/api 57.19.2 → 57.21.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 +443 -0
- package/biome.jsonc +1 -0
- package/dist/accountSecurity/accessActions.d.ts +38 -0
- package/dist/accountSecurity/accessActions.js +157 -0
- package/dist/accountSecurity/accountSecurityApp.d.ts +219 -0
- package/dist/accountSecurity/accountSecurityApp.js +438 -0
- package/dist/accountSecurity/accountSecurityService.d.ts +200 -0
- package/dist/accountSecurity/accountSecurityService.js +481 -0
- package/dist/accountSecurity/credentialActions.d.ts +16 -0
- package/dist/accountSecurity/credentialActions.js +189 -0
- package/dist/accountSecurity/membershipActions.d.ts +15 -0
- package/dist/accountSecurity/membershipActions.js +81 -0
- package/dist/accountSecurity/orgSecurityRoutes.d.ts +50 -0
- package/dist/accountSecurity/orgSecurityRoutes.js +149 -0
- package/dist/accountSecurity/securityEventHooks.d.ts +44 -0
- package/dist/accountSecurity/securityEventHooks.js +223 -0
- package/dist/accountSecurity/securityEvents.d.ts +162 -0
- package/dist/accountSecurity/securityEvents.js +114 -0
- package/dist/accountSecurity/securityNotifications.d.ts +125 -0
- package/dist/accountSecurity/securityNotifications.js +333 -0
- package/dist/api.d.ts +3 -3
- package/dist/api.js +38 -6
- package/dist/audit/auditEventModel.d.ts +5 -3
- package/dist/audit/auditEventModel.js +8 -6
- package/dist/audit/processQueue.js +4 -2
- package/dist/audit/record.d.ts +2 -2
- package/dist/audit/record.js +3 -0
- package/dist/authRecovery.js +29 -1
- package/dist/betterAuth.d.ts +179 -10
- package/dist/betterAuthApp.d.ts +15 -0
- package/dist/betterAuthApp.js +23 -0
- package/dist/betterAuthChallengeHeaders.d.ts +37 -0
- package/dist/betterAuthChallengeHeaders.js +180 -0
- package/dist/betterAuthFeatures.d.ts +54 -0
- package/dist/betterAuthFeatures.js +239 -0
- package/dist/betterAuthMethods.d.ts +73 -0
- package/dist/betterAuthMethods.js +55 -0
- package/dist/betterAuthOAuthHandoff.d.ts +10 -2
- package/dist/betterAuthOAuthHandoff.js +222 -24
- package/dist/betterAuthSetPassword.d.ts +33 -0
- package/dist/betterAuthSetPassword.js +166 -0
- package/dist/betterAuthSetup.d.ts +57 -8
- package/dist/betterAuthSetup.js +327 -36
- package/dist/consentApp.d.ts +9 -4
- package/dist/consentApp.js +344 -28
- package/dist/consentStatus.d.ts +60 -0
- package/dist/consentStatus.js +111 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.js +41 -2
- package/dist/expressServer.js +3 -2
- package/dist/frameworkModelNames.d.ts +4 -0
- package/dist/frameworkModelNames.js +4 -0
- package/dist/index.d.ts +18 -2
- package/dist/index.js +29 -3
- package/dist/mcp/adminTracking.d.ts +28 -0
- package/dist/mcp/adminTracking.js +80 -0
- package/dist/mcp/auth.d.ts +6 -0
- package/dist/mcp/auth.js +12 -2
- package/dist/mcp/oauth.js +151 -11
- package/dist/mcp/oauthMetadata.d.ts +2 -1
- package/dist/mcp/oauthMetadata.js +7 -4
- package/dist/mcp/server.d.ts +8 -0
- package/dist/mcp/server.js +48 -4
- package/dist/models/consentResponse.js +51 -0
- package/dist/models/mcpOAuthClient.js +78 -2
- package/dist/models/mcpOAuthToken.js +55 -4
- package/dist/models/mcpRequestLog.d.ts +2 -0
- package/dist/models/mcpRequestLog.js +73 -0
- package/dist/models/mcpServiceToken.js +4 -0
- package/dist/models/mcpToolSetting.d.ts +2 -0
- package/dist/models/mcpToolSetting.js +119 -0
- package/dist/models/versionConfig.d.ts +2 -0
- package/dist/orgs/membershipGuards.d.ts +7 -0
- package/dist/orgs/membershipGuards.js +32 -0
- package/dist/orgs/orgsApp.d.ts +19 -1
- package/dist/orgs/orgsApp.js +55 -24
- package/dist/rateLimit/policies.js +8 -0
- package/dist/rbac/access.js +2 -1
- package/dist/rbac/auditModel.d.ts +2 -1
- package/dist/rbac/resolve.d.ts +9 -1
- package/dist/rbac/resolve.js +21 -16
- package/dist/rbac/roleManager.d.ts +6 -0
- package/dist/rbac/roleManager.js +8 -7
- package/dist/rbac/roleModel.d.ts +1 -0
- package/dist/rbac/roleModel.js +45 -3
- package/dist/rbac/statements.d.ts +2 -2
- package/dist/rbac/statements.js +10 -1
- package/dist/rbac/types.d.ts +6 -0
- package/dist/rbac/userWriteGuards.js +2 -1
- package/dist/schemaMetadata.js +8 -1
- package/dist/seedRunner.js +7 -2
- package/dist/terrenoApp.d.ts +16 -0
- package/dist/terrenoApp.js +18 -0
- package/dist/tests/models.d.ts +22 -22
- package/dist/tests/models.js +15 -1
- package/dist/tests/testHelper.d.ts +6 -12
- package/dist/tests.d.ts +1 -0
- package/dist/tests.js +3 -1
- package/dist/types/consentResponse.d.ts +8 -0
- package/dist/types/mcpAdmin.d.ts +58 -0
- package/dist/types/mcpAdmin.js +2 -0
- package/dist/types/mcpOAuth.d.ts +37 -2
- package/dist/types/mcpServiceToken.d.ts +2 -0
- package/e2e/smoke.ts +193 -0
- package/package.json +10 -5
- package/src/__snapshots__/openApi.test.ts.snap +288 -0
- package/src/__snapshots__/openApiBuilder.test.ts.snap +96 -0
- package/src/accountSecurity/accessActions.test.ts +723 -0
- package/src/accountSecurity/accessActions.ts +173 -0
- package/src/accountSecurity/accountSecurity.test.ts +947 -0
- package/src/accountSecurity/accountSecurityApp.ts +720 -0
- package/src/accountSecurity/accountSecurityService.ts +826 -0
- package/src/accountSecurity/credentialActions.test.ts +867 -0
- package/src/accountSecurity/credentialActions.ts +260 -0
- package/src/accountSecurity/membershipActions.ts +89 -0
- package/src/accountSecurity/orgSecurityRoutes.test.ts +841 -0
- package/src/accountSecurity/orgSecurityRoutes.ts +232 -0
- package/src/accountSecurity/securityEventHooks.ts +294 -0
- package/src/accountSecurity/securityEvents.ts +165 -0
- package/src/accountSecurity/securityNotifications.test.ts +1323 -0
- package/src/accountSecurity/securityNotifications.ts +516 -0
- package/src/adminModelRouter.test.ts +8 -6
- package/src/api.arrayOperations.test.ts +102 -22
- package/src/api.hooks.test.ts +12 -11
- package/src/api.query.test.ts +2 -2
- package/src/api.test.ts +8 -7
- package/src/api.ts +41 -10
- package/src/audit/auditApp.test.ts +1 -1
- package/src/audit/auditEventModel.ts +10 -7
- package/src/audit/cloudTasksEnqueue.test.ts +19 -14
- package/src/audit/processQueue.ts +4 -2
- package/src/audit/record.test.ts +15 -0
- package/src/audit/record.ts +10 -2
- package/src/auth.test.ts +21 -7
- package/src/authRecovery.test.ts +33 -10
- package/src/authRecovery.ts +34 -1
- package/src/authRecoveryBetterAuth.test.ts +169 -0
- package/src/authVerification.test.ts +7 -7
- package/src/betterAuth.ts +194 -8
- package/src/betterAuthApp.ts +32 -0
- package/src/betterAuthChallengeHeaders.test.ts +476 -0
- package/src/betterAuthChallengeHeaders.ts +227 -0
- package/src/betterAuthFeatures.test.ts +490 -0
- package/src/betterAuthFeatures.ts +283 -0
- package/src/betterAuthLinking.test.ts +846 -0
- package/src/betterAuthMethods.test.ts +138 -0
- package/src/betterAuthMethods.ts +98 -0
- package/src/betterAuthOAuthHandoff.test.ts +482 -69
- package/src/betterAuthOAuthHandoff.ts +293 -33
- package/src/betterAuthOAuthProxy.test.ts +421 -0
- package/src/betterAuthPasswordReset.test.ts +190 -0
- package/src/betterAuthSetPassword.test.ts +378 -0
- package/src/betterAuthSetPassword.ts +213 -0
- package/src/betterAuthSetup.test.ts +115 -57
- package/src/betterAuthSetup.ts +457 -38
- package/src/collectionRegistry.test.ts +4 -4
- package/src/consentApp.test.ts +869 -2
- package/src/consentApp.ts +438 -36
- package/src/consentStatus.test.ts +226 -0
- package/src/consentStatus.ts +171 -0
- package/src/errors.test.ts +81 -0
- package/src/errors.ts +50 -1
- package/src/expressServer.test.ts +29 -0
- package/src/expressServer.ts +3 -2
- package/src/frameworkModelNames.test.ts +8 -0
- package/src/frameworkModelNames.ts +4 -0
- package/src/githubAuth.test.ts +11 -11
- package/src/index.ts +38 -0
- package/src/isolated/migrateCliConnect.isolated.ts +3 -2
- package/src/mcp/actionTools.test.ts +11 -5
- package/src/mcp/adminTracking.test.ts +272 -0
- package/src/mcp/adminTracking.ts +104 -0
- package/src/mcp/auth.ts +18 -2
- package/src/mcp/oauth.test.ts +463 -2
- package/src/mcp/oauth.ts +184 -13
- package/src/mcp/oauthMetadata.ts +5 -2
- package/src/mcp/server.ts +67 -4
- package/src/mcp/serviceTokens.test.ts +2 -1
- package/src/mcp/toolGenerator.test.ts +9 -5
- package/src/migrations/cli.test.ts +15 -14
- package/src/migrations/cli.ts +1 -1
- package/src/migrations/generate.test.ts +2 -2
- package/src/migrations/load.test.ts +1 -1
- package/src/migrations/lock.test.ts +8 -3
- package/src/migrations/runner.test.ts +16 -5
- package/src/migrations/schemaCatalog.test.ts +4 -1
- package/src/models/consentResponse.ts +53 -0
- package/src/models/mcpOAuthClient.ts +100 -3
- package/src/models/mcpOAuthToken.test.ts +42 -0
- package/src/models/mcpOAuthToken.ts +62 -5
- package/src/models/mcpRequestLog.ts +81 -0
- package/src/models/mcpServiceToken.test.ts +23 -0
- package/src/models/mcpServiceToken.ts +10 -0
- package/src/models/mcpToolSetting.test.ts +69 -0
- package/src/models/mcpToolSetting.ts +138 -0
- package/src/models/versionConfig.ts +2 -0
- package/src/notifications/notificationsApp.test.ts +5 -8
- package/src/notifiers/slackWebApi.test.ts +30 -8
- package/src/openApi.test.ts +4 -4
- package/src/openApiValidator.test.ts +3 -3
- package/src/orgs/membershipGuards.ts +39 -0
- package/src/orgs/orgPermissions.test.ts +2 -2
- package/src/orgs/orgPlugin.test.ts +4 -3
- package/src/orgs/orgRbac.test.ts +1 -1
- package/src/orgs/orgSecurityDefects.test.ts +3 -1
- package/src/orgs/organizationSettings.test.ts +1 -1
- package/src/orgs/orgsApp.test.ts +104 -3
- package/src/orgs/orgsApp.ts +69 -28
- package/src/permissions.test.ts +2 -2
- package/src/rateLimit/policies.test.ts +53 -0
- package/src/rateLimit/policies.ts +8 -0
- package/src/rateLimit/redisStore.ioredis.test.ts +3 -2
- package/src/rbac/access.ts +4 -1
- package/src/rbac/assertAllowed.test.ts +1 -1
- package/src/rbac/auditModel.ts +10 -2
- package/src/rbac/realtimeAccess.test.ts +3 -2
- package/src/rbac/resolve.test.ts +65 -0
- package/src/rbac/resolve.ts +37 -17
- package/src/rbac/roleManager.test.ts +64 -0
- package/src/rbac/roleManager.ts +15 -6
- package/src/rbac/roleModel.test.ts +174 -1
- package/src/rbac/roleModel.ts +66 -3
- package/src/rbac/routes.test.ts +58 -0
- package/src/rbac/statements.test.ts +4 -1
- package/src/rbac/statements.ts +11 -1
- package/src/rbac/types.ts +6 -1
- package/src/rbac/userWriteGuards.test.ts +56 -5
- package/src/rbac/userWriteGuards.ts +5 -2
- package/src/realtime/realtime.test.ts +35 -31
- package/src/schemaMetadata.test.ts +39 -0
- package/src/schemaMetadata.ts +11 -1
- package/src/scrubAdminFields.test.ts +1 -1
- package/src/seedRunner.test.ts +56 -0
- package/src/seedRunner.ts +7 -2
- package/src/sync/adminWindowMutation.test.ts +4 -3
- package/src/sync/executors.test.ts +1 -1
- package/src/sync/scripts/compactTombstones.test.ts +4 -3
- package/src/sync/sync.test.ts +1 -1
- package/src/sync/syncPhaseC.test.ts +14 -11
- package/src/sync/syncRoutes.test.ts +8 -8
- package/src/sync/syncSeqPlugin.test.ts +1 -2
- package/src/sync/syncSocket.test.ts +5 -4
- package/src/terrenoApp.mcpServiceTokens.test.ts +2 -2
- package/src/terrenoApp.migrations.test.ts +8 -7
- package/src/terrenoApp.ts +34 -0
- package/src/tests/accountSecurityTestApp.ts +290 -0
- package/src/tests/betterAuthStubs.ts +189 -0
- package/src/tests/bunSetup.ts +1 -1
- package/src/tests/mockOidcProvider.ts +105 -0
- package/src/tests/models.ts +23 -0
- package/src/tests/softwareAuthenticator.ts +183 -0
- package/src/tests/testEnv.ts +7 -0
- package/src/tests.ts +1 -0
- package/src/types/consentResponse.ts +8 -0
- package/src/types/mcpAdmin.ts +80 -0
- package/src/types/mcpOAuth.ts +44 -3
- package/src/types/mcpServiceToken.ts +5 -0
- package/src/webhooks/verifiers/hmac.test.ts +1 -1
- package/tsconfig.typecheck.json +14 -0
package/src/consentApp.test.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
import {afterEach, beforeEach, describe, expect, it} from "bun:test";
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, spyOn} from "bun:test";
|
|
2
2
|
import type express from "express";
|
|
3
|
-
import type
|
|
3
|
+
import mongoose, {type HydratedDocument} from "mongoose";
|
|
4
4
|
import supertest from "supertest";
|
|
5
5
|
import type TestAgent from "supertest/lib/agent";
|
|
6
|
+
import {AuditApp} from "./audit/auditApp";
|
|
7
|
+
import {flushAuditRecorderForTests, resetAuditRecorderForTests} from "./audit/record";
|
|
6
8
|
import type {UserModel as UserMongooseModel} from "./auth";
|
|
7
9
|
import {ConsentApp, type ConsentAppOptions} from "./consentApp";
|
|
10
|
+
import {logger} from "./logger";
|
|
8
11
|
import {ConsentForm} from "./models/consentForm";
|
|
9
12
|
import {ConsentResponse} from "./models/consentResponse";
|
|
13
|
+
import {createAccess} from "./rbac/access";
|
|
14
|
+
import {terrenoStatements} from "./rbac/statements";
|
|
10
15
|
import {TerrenoApp} from "./terrenoApp";
|
|
11
16
|
import {authAsUser, setupDb, type User, UserModel} from "./tests";
|
|
12
17
|
import type {ConsentFormDocument} from "./types/consentForm";
|
|
@@ -41,7 +46,20 @@ describe("ConsentApp", () => {
|
|
|
41
46
|
expect(contribution.models?.[1]?.populatePaths).toEqual([
|
|
42
47
|
{fields: ["title", "slug", "version", "type"], path: "consentFormId"},
|
|
43
48
|
{fields: ["name", "email"], path: "userId"},
|
|
49
|
+
{fields: ["name", "email"], path: "provisionalSignedBy"},
|
|
44
50
|
]);
|
|
51
|
+
expect(contribution.models?.[1]?.admin.listFields).toEqual([
|
|
52
|
+
"userId",
|
|
53
|
+
"agreed",
|
|
54
|
+
"provisional",
|
|
55
|
+
"locale",
|
|
56
|
+
"agreedAt",
|
|
57
|
+
]);
|
|
58
|
+
expect(contribution.models?.[1]?.admin.filters).toContainEqual({
|
|
59
|
+
field: "provisional",
|
|
60
|
+
kind: "boolean",
|
|
61
|
+
label: "Provisional",
|
|
62
|
+
});
|
|
45
63
|
});
|
|
46
64
|
|
|
47
65
|
let admin: HydratedDocument<User>;
|
|
@@ -961,4 +979,853 @@ describe("ConsentApp", () => {
|
|
|
961
979
|
await noAuditAdmin.get(`/consents/audit/${notAdmin._id}`).expect(404);
|
|
962
980
|
});
|
|
963
981
|
});
|
|
982
|
+
describe("POST /consent-responses/provisional", () => {
|
|
983
|
+
const createActiveForm = async (
|
|
984
|
+
overrides: Partial<Record<string, unknown>> = {}
|
|
985
|
+
): Promise<ConsentFormDocument> =>
|
|
986
|
+
ConsentForm.create({
|
|
987
|
+
active: true,
|
|
988
|
+
content: new Map([
|
|
989
|
+
["en", "# Intake consent"],
|
|
990
|
+
["es", "# Consentimiento"],
|
|
991
|
+
]),
|
|
992
|
+
order: 1,
|
|
993
|
+
slug: "intake",
|
|
994
|
+
title: "Intake consent",
|
|
995
|
+
type: "agreement",
|
|
996
|
+
version: 3,
|
|
997
|
+
...overrides,
|
|
998
|
+
});
|
|
999
|
+
|
|
1000
|
+
const buildRbacApp = async (): Promise<express.Application> => {
|
|
1001
|
+
const access = createAccess({
|
|
1002
|
+
connection: mongoose.connection,
|
|
1003
|
+
defaultRoles: [
|
|
1004
|
+
{
|
|
1005
|
+
displayName: "Intake staff",
|
|
1006
|
+
name: "intake-staff",
|
|
1007
|
+
permissions: {consentResponse: ["signOnBehalf"]},
|
|
1008
|
+
},
|
|
1009
|
+
],
|
|
1010
|
+
statements: terrenoStatements,
|
|
1011
|
+
});
|
|
1012
|
+
await access.roles.seedDefaults();
|
|
1013
|
+
return buildApp({accessControl: access, auditTrail: true});
|
|
1014
|
+
};
|
|
1015
|
+
|
|
1016
|
+
it("lets an admin sign provisionally without accessControl", async () => {
|
|
1017
|
+
const form = await createActiveForm();
|
|
1018
|
+
const before = Date.now();
|
|
1019
|
+
|
|
1020
|
+
const res = await adminAgent
|
|
1021
|
+
.post("/consent-responses/provisional")
|
|
1022
|
+
.set("User-Agent", "intake-tablet")
|
|
1023
|
+
.send({
|
|
1024
|
+
consentFormId: form._id.toString(),
|
|
1025
|
+
note: " Verbal consent from guardian at intake ",
|
|
1026
|
+
userId: notAdmin._id.toString(),
|
|
1027
|
+
})
|
|
1028
|
+
.expect(200);
|
|
1029
|
+
|
|
1030
|
+
expect(res.body.data.provisional).toBe(true);
|
|
1031
|
+
expect(res.body.data.provisionalSignedBy.name).toBe("Admin");
|
|
1032
|
+
expect(res.body.data.provisionalSignedBy.email).toBe("admin@example.com");
|
|
1033
|
+
|
|
1034
|
+
const stored = await ConsentResponse.findExactlyOne({_id: res.body.data._id});
|
|
1035
|
+
expect(stored.provisional).toBe(true);
|
|
1036
|
+
expect(stored.agreed).toBe(true);
|
|
1037
|
+
expect(stored.userId.toString()).toBe(notAdmin._id.toString());
|
|
1038
|
+
expect(stored.provisionalSignedBy?.toString()).toBe(admin._id.toString());
|
|
1039
|
+
expect(stored.provisionalNote).toBe("Verbal consent from guardian at intake");
|
|
1040
|
+
expect(stored.agreedAt.getTime()).toBeGreaterThanOrEqual(before);
|
|
1041
|
+
expect(stored.agreedAt.getTime()).toBeLessThanOrEqual(Date.now());
|
|
1042
|
+
expect(stored.formVersionSnapshot).toBe(3);
|
|
1043
|
+
expect(stored.locale).toBe("en");
|
|
1044
|
+
expect(stored.contentSnapshot).toBe("# Intake consent");
|
|
1045
|
+
expect(stored.ipAddress).toBeUndefined();
|
|
1046
|
+
expect(stored.userAgent).toBeUndefined();
|
|
1047
|
+
expect(stored.signature).toBeUndefined();
|
|
1048
|
+
expect(stored.signedAt).toBeUndefined();
|
|
1049
|
+
expect(stored.provisionalSignerIp).toBeTruthy();
|
|
1050
|
+
expect(stored.provisionalSignerUserAgent).toBe("intake-tablet");
|
|
1051
|
+
});
|
|
1052
|
+
|
|
1053
|
+
it("stores the requested locale's content snapshot", async () => {
|
|
1054
|
+
const form = await createActiveForm();
|
|
1055
|
+
const res = await adminAgent
|
|
1056
|
+
.post("/consent-responses/provisional")
|
|
1057
|
+
.send({
|
|
1058
|
+
consentFormId: form._id.toString(),
|
|
1059
|
+
locale: "es",
|
|
1060
|
+
note: "Phone consent",
|
|
1061
|
+
userId: notAdmin._id.toString(),
|
|
1062
|
+
})
|
|
1063
|
+
.expect(200);
|
|
1064
|
+
const stored = await ConsentResponse.findExactlyOne({_id: res.body.data._id});
|
|
1065
|
+
expect(stored.locale).toBe("es");
|
|
1066
|
+
expect(stored.contentSnapshot).toBe("# Consentimiento");
|
|
1067
|
+
});
|
|
1068
|
+
|
|
1069
|
+
it("does not store signer request metadata without auditTrail", async () => {
|
|
1070
|
+
const app = buildApp({auditTrail: false});
|
|
1071
|
+
const agent = await authAsUser(app, "admin");
|
|
1072
|
+
const form = await createActiveForm();
|
|
1073
|
+
const res = await agent
|
|
1074
|
+
.post("/consent-responses/provisional")
|
|
1075
|
+
.send({consentFormId: form._id.toString(), note: "Verbal", userId: notAdmin._id.toString()})
|
|
1076
|
+
.expect(200);
|
|
1077
|
+
const stored = await ConsentResponse.findExactlyOne({_id: res.body.data._id});
|
|
1078
|
+
expect(stored.provisionalSignerIp).toBeUndefined();
|
|
1079
|
+
expect(stored.provisionalSignerUserAgent).toBeUndefined();
|
|
1080
|
+
expect(stored.contentSnapshot).toBeUndefined();
|
|
1081
|
+
expect(stored.formVersionSnapshot).toBe(3);
|
|
1082
|
+
});
|
|
1083
|
+
|
|
1084
|
+
it("rejects a non-admin without accessControl", async () => {
|
|
1085
|
+
const form = await createActiveForm();
|
|
1086
|
+
const res = await userAgent
|
|
1087
|
+
.post("/consent-responses/provisional")
|
|
1088
|
+
.send({consentFormId: form._id.toString(), note: "Verbal", userId: admin._id.toString()});
|
|
1089
|
+
expect(res.status).toBe(405);
|
|
1090
|
+
expect(await ConsentResponse.countDocuments({})).toBe(0);
|
|
1091
|
+
});
|
|
1092
|
+
|
|
1093
|
+
it("warns at register when accessControl is not set", () => {
|
|
1094
|
+
const warn = spyOn(logger, "warn");
|
|
1095
|
+
try {
|
|
1096
|
+
buildApp();
|
|
1097
|
+
expect(
|
|
1098
|
+
warn.mock.calls.some((call) => String(call[0]).includes("without accessControl"))
|
|
1099
|
+
).toBe(true);
|
|
1100
|
+
} finally {
|
|
1101
|
+
warn.mockRestore();
|
|
1102
|
+
}
|
|
1103
|
+
});
|
|
1104
|
+
|
|
1105
|
+
it("lets a non-admin with consentResponse:signOnBehalf sign when accessControl is set", async () => {
|
|
1106
|
+
const app = await buildRbacApp();
|
|
1107
|
+
await UserModel.updateOne({_id: notAdmin._id}, {$set: {roles: ["intake-staff"]}});
|
|
1108
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
1109
|
+
const form = await createActiveForm();
|
|
1110
|
+
|
|
1111
|
+
const res = await agent
|
|
1112
|
+
.post("/consent-responses/provisional")
|
|
1113
|
+
.send({consentFormId: form._id.toString(), note: "Verbal", userId: admin._id.toString()})
|
|
1114
|
+
.expect(200);
|
|
1115
|
+
expect(res.body.data.provisionalSignedBy.name).toBe("Not Admin");
|
|
1116
|
+
});
|
|
1117
|
+
|
|
1118
|
+
it("rejects a non-admin without the role when accessControl is set", async () => {
|
|
1119
|
+
const app = await buildRbacApp();
|
|
1120
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
1121
|
+
const form = await createActiveForm();
|
|
1122
|
+
|
|
1123
|
+
const res = await agent
|
|
1124
|
+
.post("/consent-responses/provisional")
|
|
1125
|
+
.send({consentFormId: form._id.toString(), note: "Verbal", userId: admin._id.toString()});
|
|
1126
|
+
expect(res.status).toBe(405);
|
|
1127
|
+
});
|
|
1128
|
+
|
|
1129
|
+
it("rejects an admin without the role when accessControl is set", async () => {
|
|
1130
|
+
const app = await buildRbacApp();
|
|
1131
|
+
const agent = await authAsUser(app, "admin");
|
|
1132
|
+
const form = await createActiveForm();
|
|
1133
|
+
|
|
1134
|
+
const res = await agent.post("/consent-responses/provisional").send({
|
|
1135
|
+
consentFormId: form._id.toString(),
|
|
1136
|
+
note: "Verbal",
|
|
1137
|
+
userId: notAdmin._id.toString(),
|
|
1138
|
+
});
|
|
1139
|
+
expect(res.status).toBe(405);
|
|
1140
|
+
});
|
|
1141
|
+
|
|
1142
|
+
it("rejects a blank note", async () => {
|
|
1143
|
+
const form = await createActiveForm();
|
|
1144
|
+
await adminAgent
|
|
1145
|
+
.post("/consent-responses/provisional")
|
|
1146
|
+
.send({consentFormId: form._id.toString(), note: " ", userId: notAdmin._id.toString()})
|
|
1147
|
+
.expect(400);
|
|
1148
|
+
});
|
|
1149
|
+
|
|
1150
|
+
it("rejects a note over 2000 characters", async () => {
|
|
1151
|
+
const form = await createActiveForm();
|
|
1152
|
+
await adminAgent
|
|
1153
|
+
.post("/consent-responses/provisional")
|
|
1154
|
+
.send({
|
|
1155
|
+
consentFormId: form._id.toString(),
|
|
1156
|
+
note: "x".repeat(2001),
|
|
1157
|
+
userId: notAdmin._id.toString(),
|
|
1158
|
+
})
|
|
1159
|
+
.expect(400);
|
|
1160
|
+
});
|
|
1161
|
+
|
|
1162
|
+
it("rejects missing ids", async () => {
|
|
1163
|
+
await adminAgent.post("/consent-responses/provisional").send({note: "Verbal"}).expect(400);
|
|
1164
|
+
});
|
|
1165
|
+
|
|
1166
|
+
it("rejects malformed ids", async () => {
|
|
1167
|
+
const form = await createActiveForm();
|
|
1168
|
+
const badUser = await adminAgent
|
|
1169
|
+
.post("/consent-responses/provisional")
|
|
1170
|
+
.send({consentFormId: form._id.toString(), note: "Verbal", userId: "abc"})
|
|
1171
|
+
.expect(400);
|
|
1172
|
+
expect(badUser.body.title).toBe("userId is invalid");
|
|
1173
|
+
const badForm = await adminAgent
|
|
1174
|
+
.post("/consent-responses/provisional")
|
|
1175
|
+
.send({consentFormId: "abc", note: "Verbal", userId: notAdmin._id.toString()})
|
|
1176
|
+
.expect(400);
|
|
1177
|
+
expect(badForm.body.title).toBe("consentFormId is invalid");
|
|
1178
|
+
});
|
|
1179
|
+
|
|
1180
|
+
it("rejects signing on your own behalf", async () => {
|
|
1181
|
+
const form = await createActiveForm();
|
|
1182
|
+
const res = await adminAgent
|
|
1183
|
+
.post("/consent-responses/provisional")
|
|
1184
|
+
.send({consentFormId: form._id.toString(), note: "Verbal", userId: admin._id.toString()})
|
|
1185
|
+
.expect(400);
|
|
1186
|
+
expect(res.body.title).toBe("Cannot sign provisionally for yourself");
|
|
1187
|
+
});
|
|
1188
|
+
|
|
1189
|
+
it("rejects an inactive form", async () => {
|
|
1190
|
+
const form = await createActiveForm({active: false});
|
|
1191
|
+
const res = await adminAgent
|
|
1192
|
+
.post("/consent-responses/provisional")
|
|
1193
|
+
.send({consentFormId: form._id.toString(), note: "Verbal", userId: notAdmin._id.toString()})
|
|
1194
|
+
.expect(400);
|
|
1195
|
+
expect(res.body.title).toBe("Consent form is not active");
|
|
1196
|
+
});
|
|
1197
|
+
|
|
1198
|
+
it("rejects a form that resolveConsentForms filters out for the target user", async () => {
|
|
1199
|
+
const app = buildApp({
|
|
1200
|
+
resolveConsentForms: (user, forms) =>
|
|
1201
|
+
String(user.id) === notAdmin._id.toString()
|
|
1202
|
+
? forms.filter((form) => form.slug !== "intake")
|
|
1203
|
+
: forms,
|
|
1204
|
+
});
|
|
1205
|
+
const agent = await authAsUser(app, "admin");
|
|
1206
|
+
const form = await createActiveForm();
|
|
1207
|
+
const res = await agent
|
|
1208
|
+
.post("/consent-responses/provisional")
|
|
1209
|
+
.send({consentFormId: form._id.toString(), note: "Verbal", userId: notAdmin._id.toString()})
|
|
1210
|
+
.expect(400);
|
|
1211
|
+
expect(res.body.title).toBe("Consent form does not apply to this user");
|
|
1212
|
+
});
|
|
1213
|
+
|
|
1214
|
+
it("returns 404 for an unknown user", async () => {
|
|
1215
|
+
const form = await createActiveForm();
|
|
1216
|
+
await adminAgent
|
|
1217
|
+
.post("/consent-responses/provisional")
|
|
1218
|
+
.send({
|
|
1219
|
+
consentFormId: form._id.toString(),
|
|
1220
|
+
note: "Verbal",
|
|
1221
|
+
userId: new mongoose.Types.ObjectId().toString(),
|
|
1222
|
+
})
|
|
1223
|
+
.expect(404);
|
|
1224
|
+
});
|
|
1225
|
+
|
|
1226
|
+
it("returns 404 for an unknown form", async () => {
|
|
1227
|
+
await adminAgent
|
|
1228
|
+
.post("/consent-responses/provisional")
|
|
1229
|
+
.send({
|
|
1230
|
+
consentFormId: new mongoose.Types.ObjectId().toString(),
|
|
1231
|
+
note: "Verbal",
|
|
1232
|
+
userId: notAdmin._id.toString(),
|
|
1233
|
+
})
|
|
1234
|
+
.expect(404);
|
|
1235
|
+
});
|
|
1236
|
+
|
|
1237
|
+
it("keeps the form pending for the user until they respond themselves", async () => {
|
|
1238
|
+
const form = await createActiveForm({captureSignature: false});
|
|
1239
|
+
await adminAgent
|
|
1240
|
+
.post("/consent-responses/provisional")
|
|
1241
|
+
.send({consentFormId: form._id.toString(), note: "Verbal", userId: notAdmin._id.toString()})
|
|
1242
|
+
.expect(200);
|
|
1243
|
+
|
|
1244
|
+
const pending = await userAgent.get("/consents/pending").expect(200);
|
|
1245
|
+
expect(pending.body.data.map((item: {_id: string}) => item._id)).toEqual([
|
|
1246
|
+
form._id.toString(),
|
|
1247
|
+
]);
|
|
1248
|
+
|
|
1249
|
+
await userAgent
|
|
1250
|
+
.post("/consents/respond")
|
|
1251
|
+
.send({agreed: true, consentFormId: form._id.toString(), locale: "en"})
|
|
1252
|
+
.expect(200);
|
|
1253
|
+
|
|
1254
|
+
const after = await userAgent.get("/consents/pending").expect(200);
|
|
1255
|
+
expect(after.body.data).toHaveLength(0);
|
|
1256
|
+
});
|
|
1257
|
+
|
|
1258
|
+
describe("with AuditApp registered", () => {
|
|
1259
|
+
beforeEach(() => {
|
|
1260
|
+
resetAuditRecorderForTests();
|
|
1261
|
+
});
|
|
1262
|
+
|
|
1263
|
+
afterEach(() => {
|
|
1264
|
+
resetAuditRecorderForTests();
|
|
1265
|
+
});
|
|
1266
|
+
|
|
1267
|
+
it("writes one AuditEvent naming the actor, target user, and form", async () => {
|
|
1268
|
+
const app = new TerrenoApp({
|
|
1269
|
+
skipListen: true,
|
|
1270
|
+
userModel: UserModel as unknown as UserMongooseModel,
|
|
1271
|
+
})
|
|
1272
|
+
.register(new AuditApp())
|
|
1273
|
+
.register(new ConsentApp())
|
|
1274
|
+
.build();
|
|
1275
|
+
const agent = await authAsUser(app, "admin");
|
|
1276
|
+
await mongoose.connection.collection("auditevents").deleteMany({});
|
|
1277
|
+
const form = await createActiveForm();
|
|
1278
|
+
|
|
1279
|
+
const res = await agent
|
|
1280
|
+
.post("/consent-responses/provisional")
|
|
1281
|
+
.send({
|
|
1282
|
+
consentFormId: form._id.toString(),
|
|
1283
|
+
note: "Verbal",
|
|
1284
|
+
userId: notAdmin._id.toString(),
|
|
1285
|
+
})
|
|
1286
|
+
.expect(200);
|
|
1287
|
+
await flushAuditRecorderForTests();
|
|
1288
|
+
|
|
1289
|
+
const events = await mongoose.connection
|
|
1290
|
+
.collection("auditevents")
|
|
1291
|
+
.find({modelName: "ConsentResponse"})
|
|
1292
|
+
.toArray();
|
|
1293
|
+
expect(events).toHaveLength(1);
|
|
1294
|
+
expect(events[0].actorId.toString()).toBe(admin._id.toString());
|
|
1295
|
+
expect(events[0].recordId).toBe(res.body.data._id);
|
|
1296
|
+
expect(events[0].after.action).toBe("signOnBehalf");
|
|
1297
|
+
expect(events[0].after.userId).toBe(notAdmin._id.toString());
|
|
1298
|
+
expect(events[0].after.consentFormId).toBe(form._id.toString());
|
|
1299
|
+
});
|
|
1300
|
+
});
|
|
1301
|
+
});
|
|
1302
|
+
describe("provisional signatures on user-facing payloads", () => {
|
|
1303
|
+
let form: ConsentFormDocument;
|
|
1304
|
+
|
|
1305
|
+
const signProvisionally = async (targetForm: ConsentFormDocument): Promise<string> => {
|
|
1306
|
+
const res = await adminAgent
|
|
1307
|
+
.post("/consent-responses/provisional")
|
|
1308
|
+
.set("User-Agent", "intake-desk")
|
|
1309
|
+
.send({
|
|
1310
|
+
consentFormId: targetForm._id.toString(),
|
|
1311
|
+
note: "Guardian agreed by phone",
|
|
1312
|
+
userId: notAdmin._id.toString(),
|
|
1313
|
+
})
|
|
1314
|
+
.expect(200);
|
|
1315
|
+
return res.body.data._id;
|
|
1316
|
+
};
|
|
1317
|
+
|
|
1318
|
+
beforeEach(async () => {
|
|
1319
|
+
form = await ConsentForm.create({
|
|
1320
|
+
active: true,
|
|
1321
|
+
content: new Map([["en", "# Telehealth consent"]]),
|
|
1322
|
+
order: 1,
|
|
1323
|
+
slug: "telehealth",
|
|
1324
|
+
title: "Telehealth consent",
|
|
1325
|
+
type: "agreement",
|
|
1326
|
+
version: 1,
|
|
1327
|
+
});
|
|
1328
|
+
});
|
|
1329
|
+
|
|
1330
|
+
it("adds the signer name and time to the pending form, without the note", async () => {
|
|
1331
|
+
const responseId = await signProvisionally(form);
|
|
1332
|
+
const stored = await ConsentResponse.findExactlyOne({_id: responseId});
|
|
1333
|
+
|
|
1334
|
+
const res = await userAgent.get("/consents/pending").expect(200);
|
|
1335
|
+
expect(res.body.data).toHaveLength(1);
|
|
1336
|
+
expect(res.body.data[0].title).toBe("Telehealth consent");
|
|
1337
|
+
expect(res.body.data[0].content).toEqual({en: "# Telehealth consent"});
|
|
1338
|
+
expect(res.body.data[0].provisionalSignature).toEqual({
|
|
1339
|
+
signedAt: stored.agreedAt.toISOString(),
|
|
1340
|
+
signedByName: "Admin",
|
|
1341
|
+
});
|
|
1342
|
+
expect(JSON.stringify(res.body.data[0])).not.toContain("Guardian agreed by phone");
|
|
1343
|
+
});
|
|
1344
|
+
|
|
1345
|
+
it("omits provisionalSignature on forms nobody signed provisionally", async () => {
|
|
1346
|
+
await ConsentForm.create({
|
|
1347
|
+
active: true,
|
|
1348
|
+
content: new Map([["en", "# Privacy"]]),
|
|
1349
|
+
order: 2,
|
|
1350
|
+
slug: "privacy-notice",
|
|
1351
|
+
title: "Privacy",
|
|
1352
|
+
type: "privacy",
|
|
1353
|
+
version: 1,
|
|
1354
|
+
});
|
|
1355
|
+
await signProvisionally(form);
|
|
1356
|
+
|
|
1357
|
+
const res = await userAgent.get("/consents/pending").expect(200);
|
|
1358
|
+
const privacy = res.body.data.find((item: {slug: string}) => item.slug === "privacy-notice");
|
|
1359
|
+
expect(privacy).toBeDefined();
|
|
1360
|
+
expect("provisionalSignature" in privacy).toBe(false);
|
|
1361
|
+
});
|
|
1362
|
+
|
|
1363
|
+
it("uses the latest provisional signature for the form's current version", async () => {
|
|
1364
|
+
await ConsentResponse.create({
|
|
1365
|
+
agreed: true,
|
|
1366
|
+
agreedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
1367
|
+
consentFormId: form._id,
|
|
1368
|
+
formVersionSnapshot: 1,
|
|
1369
|
+
locale: "en",
|
|
1370
|
+
provisional: true,
|
|
1371
|
+
provisionalNote: "Older",
|
|
1372
|
+
provisionalSignedBy: notAdmin._id,
|
|
1373
|
+
userId: notAdmin._id,
|
|
1374
|
+
});
|
|
1375
|
+
await ConsentResponse.create({
|
|
1376
|
+
agreed: true,
|
|
1377
|
+
agreedAt: new Date("2026-03-01T00:00:00.000Z"),
|
|
1378
|
+
consentFormId: form._id,
|
|
1379
|
+
formVersionSnapshot: 1,
|
|
1380
|
+
locale: "en",
|
|
1381
|
+
provisional: true,
|
|
1382
|
+
provisionalNote: "Newer",
|
|
1383
|
+
provisionalSignedBy: admin._id,
|
|
1384
|
+
userId: notAdmin._id,
|
|
1385
|
+
});
|
|
1386
|
+
|
|
1387
|
+
const res = await userAgent.get("/consents/pending").expect(200);
|
|
1388
|
+
expect(res.body.data[0].provisionalSignature).toEqual({
|
|
1389
|
+
signedAt: "2026-03-01T00:00:00.000Z",
|
|
1390
|
+
signedByName: "Admin",
|
|
1391
|
+
});
|
|
1392
|
+
});
|
|
1393
|
+
|
|
1394
|
+
it("ignores provisional signatures for an older form version", async () => {
|
|
1395
|
+
await ConsentResponse.create({
|
|
1396
|
+
agreed: true,
|
|
1397
|
+
agreedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
1398
|
+
consentFormId: form._id,
|
|
1399
|
+
formVersionSnapshot: 0,
|
|
1400
|
+
locale: "en",
|
|
1401
|
+
provisional: true,
|
|
1402
|
+
provisionalNote: "Old version",
|
|
1403
|
+
provisionalSignedBy: admin._id,
|
|
1404
|
+
userId: notAdmin._id,
|
|
1405
|
+
});
|
|
1406
|
+
|
|
1407
|
+
const res = await userAgent.get("/consents/pending").expect(200);
|
|
1408
|
+
expect(res.body.data).toHaveLength(1);
|
|
1409
|
+
expect("provisionalSignature" in res.body.data[0]).toBe(false);
|
|
1410
|
+
});
|
|
1411
|
+
|
|
1412
|
+
it("lists provisional entries in /consents/my with the signer name only", async () => {
|
|
1413
|
+
await signProvisionally(form);
|
|
1414
|
+
|
|
1415
|
+
const res = await userAgent.get("/consents/my").expect(200);
|
|
1416
|
+
expect(res.body.data).toHaveLength(1);
|
|
1417
|
+
const entry = res.body.data[0];
|
|
1418
|
+
expect(entry.provisional).toBe(true);
|
|
1419
|
+
expect(entry.provisionalSignedBy).toEqual({name: "Admin"});
|
|
1420
|
+
expect(entry).not.toHaveProperty("provisionalNote");
|
|
1421
|
+
expect(entry).not.toHaveProperty("provisionalSignerIp");
|
|
1422
|
+
expect(entry).not.toHaveProperty("provisionalSignerUserAgent");
|
|
1423
|
+
expect(JSON.stringify(entry)).not.toContain("Guardian agreed by phone");
|
|
1424
|
+
});
|
|
1425
|
+
|
|
1426
|
+
it("marks the user's own entries in /consents/my as not provisional", async () => {
|
|
1427
|
+
await userAgent
|
|
1428
|
+
.post("/consents/respond")
|
|
1429
|
+
.send({agreed: true, consentFormId: form._id.toString(), locale: "en"})
|
|
1430
|
+
.expect(200);
|
|
1431
|
+
|
|
1432
|
+
const res = await userAgent.get("/consents/my").expect(200);
|
|
1433
|
+
expect(res.body.data[0].provisional).toBe(false);
|
|
1434
|
+
expect(res.body.data[0].provisionalSignedBy).toBeNull();
|
|
1435
|
+
});
|
|
1436
|
+
|
|
1437
|
+
it("includes every provisional field in the admin audit history", async () => {
|
|
1438
|
+
const responseId = await signProvisionally(form);
|
|
1439
|
+
const stored = await ConsentResponse.findExactlyOne({_id: responseId});
|
|
1440
|
+
|
|
1441
|
+
const res = await adminAgent.get(`/consents/audit/${notAdmin._id}`).expect(200);
|
|
1442
|
+
expect(res.body.data).toHaveLength(1);
|
|
1443
|
+
const entry = res.body.data[0];
|
|
1444
|
+
expect(entry.provisional).toBe(true);
|
|
1445
|
+
expect(entry.provisionalSignedBy).toEqual({
|
|
1446
|
+
_id: admin._id.toString(),
|
|
1447
|
+
email: "admin@example.com",
|
|
1448
|
+
name: "Admin",
|
|
1449
|
+
});
|
|
1450
|
+
expect(entry.provisionalNote).toBe("Guardian agreed by phone");
|
|
1451
|
+
expect(entry.provisionalSignerIp).toBe(stored.provisionalSignerIp);
|
|
1452
|
+
expect(entry.provisionalSignerIp).toBeTruthy();
|
|
1453
|
+
expect(entry.provisionalSignerUserAgent).toBe("intake-desk");
|
|
1454
|
+
});
|
|
1455
|
+
});
|
|
1456
|
+
describe("GET /consent-responses/status", () => {
|
|
1457
|
+
let form: ConsentFormDocument;
|
|
1458
|
+
|
|
1459
|
+
beforeEach(async () => {
|
|
1460
|
+
form = await ConsentForm.create({
|
|
1461
|
+
active: true,
|
|
1462
|
+
content: new Map([["en", "# Release of information"]]),
|
|
1463
|
+
order: 1,
|
|
1464
|
+
slug: "release",
|
|
1465
|
+
title: "Release of information",
|
|
1466
|
+
type: "hipaa",
|
|
1467
|
+
version: 1,
|
|
1468
|
+
});
|
|
1469
|
+
});
|
|
1470
|
+
|
|
1471
|
+
const buildStatusRbacApp = async (): Promise<express.Application> => {
|
|
1472
|
+
const access = createAccess({
|
|
1473
|
+
connection: mongoose.connection,
|
|
1474
|
+
defaultRoles: [
|
|
1475
|
+
{
|
|
1476
|
+
displayName: "Intake staff",
|
|
1477
|
+
name: "intake-staff",
|
|
1478
|
+
permissions: {consentResponse: ["signOnBehalf"]},
|
|
1479
|
+
},
|
|
1480
|
+
],
|
|
1481
|
+
statements: terrenoStatements,
|
|
1482
|
+
});
|
|
1483
|
+
await access.roles.seedDefaults();
|
|
1484
|
+
return buildApp({accessControl: access});
|
|
1485
|
+
};
|
|
1486
|
+
|
|
1487
|
+
it("returns pending, then provisional after a provisional sign", async () => {
|
|
1488
|
+
const before = await adminAgent
|
|
1489
|
+
.get(`/consent-responses/status?userId=${notAdmin._id}`)
|
|
1490
|
+
.expect(200);
|
|
1491
|
+
expect(before.body.data).toHaveLength(1);
|
|
1492
|
+
expect(before.body.data[0].form.title).toBe("Release of information");
|
|
1493
|
+
expect(before.body.data[0].status).toBe("pending");
|
|
1494
|
+
expect(before.body.data[0].isConsented).toBe(false);
|
|
1495
|
+
|
|
1496
|
+
await adminAgent
|
|
1497
|
+
.post("/consent-responses/provisional")
|
|
1498
|
+
.send({consentFormId: form._id.toString(), note: "Phone", userId: notAdmin._id.toString()})
|
|
1499
|
+
.expect(200);
|
|
1500
|
+
|
|
1501
|
+
const after = await adminAgent
|
|
1502
|
+
.get(`/consent-responses/status?userId=${notAdmin._id}`)
|
|
1503
|
+
.expect(200);
|
|
1504
|
+
expect(after.body.data[0].status).toBe("provisional");
|
|
1505
|
+
expect(after.body.data[0].isConsented).toBe(true);
|
|
1506
|
+
expect(after.body.data[0].provisional.note).toBe("Phone");
|
|
1507
|
+
expect(after.body.data[0].provisional.signedBy).toEqual({
|
|
1508
|
+
email: "admin@example.com",
|
|
1509
|
+
id: admin._id.toString(),
|
|
1510
|
+
name: "Admin",
|
|
1511
|
+
});
|
|
1512
|
+
});
|
|
1513
|
+
|
|
1514
|
+
it("returns signed after the user agrees themselves", async () => {
|
|
1515
|
+
await adminAgent
|
|
1516
|
+
.post("/consent-responses/provisional")
|
|
1517
|
+
.send({consentFormId: form._id.toString(), note: "Phone", userId: notAdmin._id.toString()})
|
|
1518
|
+
.expect(200);
|
|
1519
|
+
await userAgent
|
|
1520
|
+
.post("/consents/respond")
|
|
1521
|
+
.send({agreed: true, consentFormId: form._id.toString(), locale: "en"})
|
|
1522
|
+
.expect(200);
|
|
1523
|
+
|
|
1524
|
+
const res = await adminAgent
|
|
1525
|
+
.get(`/consent-responses/status?userId=${notAdmin._id}`)
|
|
1526
|
+
.expect(200);
|
|
1527
|
+
expect(res.body.data[0].status).toBe("signed");
|
|
1528
|
+
});
|
|
1529
|
+
|
|
1530
|
+
it("applies resolveConsentForms for the target user", async () => {
|
|
1531
|
+
const app = buildApp({
|
|
1532
|
+
resolveConsentForms: (user, forms) =>
|
|
1533
|
+
String(user.id) === notAdmin._id.toString() ? [] : forms,
|
|
1534
|
+
});
|
|
1535
|
+
const agent = await authAsUser(app, "admin");
|
|
1536
|
+
const res = await agent.get(`/consent-responses/status?userId=${notAdmin._id}`).expect(200);
|
|
1537
|
+
expect(res.body.data).toEqual([]);
|
|
1538
|
+
});
|
|
1539
|
+
|
|
1540
|
+
it("rejects a missing userId", async () => {
|
|
1541
|
+
await adminAgent.get("/consent-responses/status").expect(400);
|
|
1542
|
+
});
|
|
1543
|
+
|
|
1544
|
+
it("documents the provisional and status actions in OpenAPI", async () => {
|
|
1545
|
+
const app = buildApp();
|
|
1546
|
+
const res = await supertest(app).get("/openapi.json").expect(200);
|
|
1547
|
+
expect(res.body.paths["/consent-responses/status"].get).toBeDefined();
|
|
1548
|
+
expect(res.body.paths["/consent-responses/provisional"].post).toBeDefined();
|
|
1549
|
+
});
|
|
1550
|
+
|
|
1551
|
+
it("returns 404 for an unknown user", async () => {
|
|
1552
|
+
await adminAgent
|
|
1553
|
+
.get(`/consent-responses/status?userId=${new mongoose.Types.ObjectId()}`)
|
|
1554
|
+
.expect(404);
|
|
1555
|
+
});
|
|
1556
|
+
|
|
1557
|
+
it("returns 400 for a malformed userId", async () => {
|
|
1558
|
+
const res = await adminAgent.get("/consent-responses/status?userId=abc").expect(400);
|
|
1559
|
+
expect(res.body.title).toBe("userId is invalid");
|
|
1560
|
+
});
|
|
1561
|
+
|
|
1562
|
+
it("rejects a non-admin without accessControl", async () => {
|
|
1563
|
+
await userAgent.get(`/consent-responses/status?userId=${admin._id}`).expect(405);
|
|
1564
|
+
});
|
|
1565
|
+
|
|
1566
|
+
it("lets a non-admin with consentResponse:signOnBehalf read statuses", async () => {
|
|
1567
|
+
const app = await buildStatusRbacApp();
|
|
1568
|
+
await UserModel.updateOne({_id: notAdmin._id}, {$set: {roles: ["intake-staff"]}});
|
|
1569
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
1570
|
+
const res = await agent.get(`/consent-responses/status?userId=${admin._id}`).expect(200);
|
|
1571
|
+
expect(res.body.data[0].status).toBe("pending");
|
|
1572
|
+
});
|
|
1573
|
+
|
|
1574
|
+
it("rejects a non-admin without the role when accessControl is set", async () => {
|
|
1575
|
+
const app = await buildStatusRbacApp();
|
|
1576
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
1577
|
+
await agent.get(`/consent-responses/status?userId=${admin._id}`).expect(405);
|
|
1578
|
+
});
|
|
1579
|
+
|
|
1580
|
+
describe("with AuditApp registered", () => {
|
|
1581
|
+
beforeEach(() => {
|
|
1582
|
+
resetAuditRecorderForTests();
|
|
1583
|
+
});
|
|
1584
|
+
|
|
1585
|
+
afterEach(() => {
|
|
1586
|
+
resetAuditRecorderForTests();
|
|
1587
|
+
});
|
|
1588
|
+
|
|
1589
|
+
it("writes an AuditEvent for each status read", async () => {
|
|
1590
|
+
const app = new TerrenoApp({
|
|
1591
|
+
skipListen: true,
|
|
1592
|
+
userModel: UserModel as unknown as UserMongooseModel,
|
|
1593
|
+
})
|
|
1594
|
+
.register(new AuditApp())
|
|
1595
|
+
.register(new ConsentApp())
|
|
1596
|
+
.build();
|
|
1597
|
+
const agent = await authAsUser(app, "admin");
|
|
1598
|
+
await mongoose.connection.collection("auditevents").deleteMany({});
|
|
1599
|
+
|
|
1600
|
+
await agent.get(`/consent-responses/status?userId=${notAdmin._id}`).expect(200);
|
|
1601
|
+
await agent.get(`/consent-responses/status?userId=${notAdmin._id}`).expect(200);
|
|
1602
|
+
await flushAuditRecorderForTests();
|
|
1603
|
+
|
|
1604
|
+
const events = await mongoose.connection
|
|
1605
|
+
.collection("auditevents")
|
|
1606
|
+
.find({modelName: "ConsentResponse"})
|
|
1607
|
+
.toArray();
|
|
1608
|
+
expect(events).toHaveLength(2);
|
|
1609
|
+
expect(events[0].actorId.toString()).toBe(admin._id.toString());
|
|
1610
|
+
expect(events[0].operation).toBe("read");
|
|
1611
|
+
expect(events[0].verb).toBe("viewed");
|
|
1612
|
+
expect(events[0].after).toEqual({action: "viewStatus", userId: notAdmin._id.toString()});
|
|
1613
|
+
});
|
|
1614
|
+
});
|
|
1615
|
+
});
|
|
1616
|
+
describe("POST /consent-responses/:id/void", () => {
|
|
1617
|
+
let form: ConsentFormDocument;
|
|
1618
|
+
|
|
1619
|
+
const signProvisionally = async (): Promise<string> => {
|
|
1620
|
+
const res = await adminAgent
|
|
1621
|
+
.post("/consent-responses/provisional")
|
|
1622
|
+
.send({consentFormId: form._id.toString(), note: "Phone", userId: notAdmin._id.toString()})
|
|
1623
|
+
.expect(200);
|
|
1624
|
+
return res.body.data._id;
|
|
1625
|
+
};
|
|
1626
|
+
|
|
1627
|
+
beforeEach(async () => {
|
|
1628
|
+
form = await ConsentForm.create({
|
|
1629
|
+
active: true,
|
|
1630
|
+
content: new Map([["en", "# Medication consent"]]),
|
|
1631
|
+
order: 1,
|
|
1632
|
+
slug: "medication",
|
|
1633
|
+
title: "Medication consent",
|
|
1634
|
+
type: "agreement",
|
|
1635
|
+
version: 1,
|
|
1636
|
+
});
|
|
1637
|
+
});
|
|
1638
|
+
|
|
1639
|
+
it("voids a provisional signature and keeps the record", async () => {
|
|
1640
|
+
const responseId = await signProvisionally();
|
|
1641
|
+
const before = Date.now();
|
|
1642
|
+
|
|
1643
|
+
const res = await adminAgent
|
|
1644
|
+
.post(`/consent-responses/${responseId}/void`)
|
|
1645
|
+
.send({reason: " Signed for the wrong member "})
|
|
1646
|
+
.expect(200);
|
|
1647
|
+
expect(res.body.data._id).toBe(responseId);
|
|
1648
|
+
|
|
1649
|
+
const stored = await ConsentResponse.findExactlyOne({_id: responseId});
|
|
1650
|
+
expect(stored.voidedAt?.getTime()).toBeGreaterThanOrEqual(before);
|
|
1651
|
+
expect(stored.voidedBy?.toString()).toBe(admin._id.toString());
|
|
1652
|
+
expect(stored.voidReason).toBe("Signed for the wrong member");
|
|
1653
|
+
expect(stored.provisional).toBe(true);
|
|
1654
|
+
expect(stored.provisionalNote).toBe("Phone");
|
|
1655
|
+
});
|
|
1656
|
+
|
|
1657
|
+
it("returns the form to pending in statuses and drops the pending notice", async () => {
|
|
1658
|
+
const responseId = await signProvisionally();
|
|
1659
|
+
const signed = await userAgent.get("/consents/pending").expect(200);
|
|
1660
|
+
expect(signed.body.data[0].provisionalSignature).toBeDefined();
|
|
1661
|
+
|
|
1662
|
+
await adminAgent
|
|
1663
|
+
.post(`/consent-responses/${responseId}/void`)
|
|
1664
|
+
.send({reason: "Wrong member"})
|
|
1665
|
+
.expect(200);
|
|
1666
|
+
|
|
1667
|
+
const status = await adminAgent
|
|
1668
|
+
.get(`/consent-responses/status?userId=${notAdmin._id}`)
|
|
1669
|
+
.expect(200);
|
|
1670
|
+
expect(status.body.data[0].status).toBe("pending");
|
|
1671
|
+
expect(status.body.data[0].provisional).toBeUndefined();
|
|
1672
|
+
|
|
1673
|
+
const pending = await userAgent.get("/consents/pending").expect(200);
|
|
1674
|
+
expect(pending.body.data).toHaveLength(1);
|
|
1675
|
+
expect("provisionalSignature" in pending.body.data[0]).toBe(false);
|
|
1676
|
+
});
|
|
1677
|
+
|
|
1678
|
+
it("shows void fields in /consents/my and the admin audit history", async () => {
|
|
1679
|
+
const responseId = await signProvisionally();
|
|
1680
|
+
await adminAgent
|
|
1681
|
+
.post(`/consent-responses/${responseId}/void`)
|
|
1682
|
+
.send({reason: "Wrong member"})
|
|
1683
|
+
.expect(200);
|
|
1684
|
+
const stored = await ConsentResponse.findExactlyOne({_id: responseId});
|
|
1685
|
+
|
|
1686
|
+
const my = await userAgent.get("/consents/my").expect(200);
|
|
1687
|
+
expect(my.body.data[0].voidedAt).toBe(stored.voidedAt?.toISOString());
|
|
1688
|
+
expect(my.body.data[0]).not.toHaveProperty("voidReason");
|
|
1689
|
+
|
|
1690
|
+
const audit = await adminAgent.get(`/consents/audit/${notAdmin._id}`).expect(200);
|
|
1691
|
+
expect(audit.body.data[0].voidedAt).toBe(stored.voidedAt?.toISOString());
|
|
1692
|
+
expect(audit.body.data[0].voidReason).toBe("Wrong member");
|
|
1693
|
+
expect(audit.body.data[0].voidedBy).toEqual({
|
|
1694
|
+
_id: admin._id.toString(),
|
|
1695
|
+
email: "admin@example.com",
|
|
1696
|
+
name: "Admin",
|
|
1697
|
+
});
|
|
1698
|
+
});
|
|
1699
|
+
|
|
1700
|
+
it("rejects a blank reason", async () => {
|
|
1701
|
+
const responseId = await signProvisionally();
|
|
1702
|
+
await adminAgent
|
|
1703
|
+
.post(`/consent-responses/${responseId}/void`)
|
|
1704
|
+
.send({reason: " "})
|
|
1705
|
+
.expect(400);
|
|
1706
|
+
});
|
|
1707
|
+
|
|
1708
|
+
it("rejects voiding the user's own response", async () => {
|
|
1709
|
+
await userAgent
|
|
1710
|
+
.post("/consents/respond")
|
|
1711
|
+
.send({agreed: true, consentFormId: form._id.toString(), locale: "en"})
|
|
1712
|
+
.expect(200);
|
|
1713
|
+
const own = await ConsentResponse.findExactlyOne({userId: notAdmin._id});
|
|
1714
|
+
|
|
1715
|
+
const res = await adminAgent
|
|
1716
|
+
.post(`/consent-responses/${own._id}/void`)
|
|
1717
|
+
.send({reason: "Mistake"})
|
|
1718
|
+
.expect(400);
|
|
1719
|
+
expect(res.body.title).toBe("Only provisional signatures can be voided");
|
|
1720
|
+
});
|
|
1721
|
+
|
|
1722
|
+
it("rejects voiding twice", async () => {
|
|
1723
|
+
const responseId = await signProvisionally();
|
|
1724
|
+
await adminAgent
|
|
1725
|
+
.post(`/consent-responses/${responseId}/void`)
|
|
1726
|
+
.send({reason: "Wrong member"})
|
|
1727
|
+
.expect(200);
|
|
1728
|
+
const res = await adminAgent
|
|
1729
|
+
.post(`/consent-responses/${responseId}/void`)
|
|
1730
|
+
.send({reason: "Again"})
|
|
1731
|
+
.expect(400);
|
|
1732
|
+
expect(res.body.title).toBe("Provisional signature is already voided");
|
|
1733
|
+
});
|
|
1734
|
+
|
|
1735
|
+
it("returns 404 for an unknown response", async () => {
|
|
1736
|
+
await adminAgent
|
|
1737
|
+
.post(`/consent-responses/${new mongoose.Types.ObjectId()}/void`)
|
|
1738
|
+
.send({reason: "Wrong member"})
|
|
1739
|
+
.expect(404);
|
|
1740
|
+
});
|
|
1741
|
+
|
|
1742
|
+
it("rejects a non-admin without accessControl", async () => {
|
|
1743
|
+
const responseId = await signProvisionally();
|
|
1744
|
+
await userAgent
|
|
1745
|
+
.post(`/consent-responses/${responseId}/void`)
|
|
1746
|
+
.send({reason: "Wrong member"})
|
|
1747
|
+
.expect(405);
|
|
1748
|
+
});
|
|
1749
|
+
|
|
1750
|
+
it("lets a non-admin with consentResponse:signOnBehalf void, and rejects one without", async () => {
|
|
1751
|
+
const responseId = await signProvisionally();
|
|
1752
|
+
const access = createAccess({
|
|
1753
|
+
connection: mongoose.connection,
|
|
1754
|
+
defaultRoles: [
|
|
1755
|
+
{
|
|
1756
|
+
displayName: "Intake staff",
|
|
1757
|
+
name: "intake-staff",
|
|
1758
|
+
permissions: {consentResponse: ["signOnBehalf"]},
|
|
1759
|
+
},
|
|
1760
|
+
],
|
|
1761
|
+
statements: terrenoStatements,
|
|
1762
|
+
});
|
|
1763
|
+
await access.roles.seedDefaults();
|
|
1764
|
+
const app = buildApp({accessControl: access});
|
|
1765
|
+
|
|
1766
|
+
const withoutRole = await authAsUser(app, "notAdmin");
|
|
1767
|
+
await withoutRole
|
|
1768
|
+
.post(`/consent-responses/${responseId}/void`)
|
|
1769
|
+
.send({reason: "Wrong member"})
|
|
1770
|
+
.expect(405);
|
|
1771
|
+
|
|
1772
|
+
await UserModel.updateOne({_id: notAdmin._id}, {$set: {roles: ["intake-staff"]}});
|
|
1773
|
+
const withRole = await authAsUser(app, "notAdmin");
|
|
1774
|
+
await withRole
|
|
1775
|
+
.post(`/consent-responses/${responseId}/void`)
|
|
1776
|
+
.send({reason: "Wrong member"})
|
|
1777
|
+
.expect(200);
|
|
1778
|
+
});
|
|
1779
|
+
|
|
1780
|
+
describe("with AuditApp registered", () => {
|
|
1781
|
+
beforeEach(() => {
|
|
1782
|
+
resetAuditRecorderForTests();
|
|
1783
|
+
});
|
|
1784
|
+
|
|
1785
|
+
afterEach(() => {
|
|
1786
|
+
resetAuditRecorderForTests();
|
|
1787
|
+
});
|
|
1788
|
+
|
|
1789
|
+
it("writes an AuditEvent for the void", async () => {
|
|
1790
|
+
const app = new TerrenoApp({
|
|
1791
|
+
skipListen: true,
|
|
1792
|
+
userModel: UserModel as unknown as UserMongooseModel,
|
|
1793
|
+
})
|
|
1794
|
+
.register(new AuditApp())
|
|
1795
|
+
.register(new ConsentApp())
|
|
1796
|
+
.build();
|
|
1797
|
+
const agent = await authAsUser(app, "admin");
|
|
1798
|
+
const created = await agent
|
|
1799
|
+
.post("/consent-responses/provisional")
|
|
1800
|
+
.send({
|
|
1801
|
+
consentFormId: form._id.toString(),
|
|
1802
|
+
note: "Phone",
|
|
1803
|
+
userId: notAdmin._id.toString(),
|
|
1804
|
+
})
|
|
1805
|
+
.expect(200);
|
|
1806
|
+
await flushAuditRecorderForTests();
|
|
1807
|
+
await mongoose.connection.collection("auditevents").deleteMany({});
|
|
1808
|
+
|
|
1809
|
+
await agent
|
|
1810
|
+
.post(`/consent-responses/${created.body.data._id}/void`)
|
|
1811
|
+
.send({reason: "Wrong member"})
|
|
1812
|
+
.expect(200);
|
|
1813
|
+
await flushAuditRecorderForTests();
|
|
1814
|
+
|
|
1815
|
+
const events = await mongoose.connection
|
|
1816
|
+
.collection("auditevents")
|
|
1817
|
+
.find({modelName: "ConsentResponse"})
|
|
1818
|
+
.toArray();
|
|
1819
|
+
expect(events).toHaveLength(1);
|
|
1820
|
+
expect(events[0].operation).toBe("update");
|
|
1821
|
+
expect(events[0].verb).toBe("updated");
|
|
1822
|
+
expect(events[0].recordId).toBe(created.body.data._id);
|
|
1823
|
+
expect(events[0].after).toEqual({
|
|
1824
|
+
action: "voidProvisional",
|
|
1825
|
+
consentFormId: form._id.toString(),
|
|
1826
|
+
userId: notAdmin._id.toString(),
|
|
1827
|
+
});
|
|
1828
|
+
});
|
|
1829
|
+
});
|
|
1830
|
+
});
|
|
964
1831
|
});
|