@7365admin1/core 3.102.2 → 3.106.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.
@@ -0,0 +1,110 @@
1
+ import assert from "node:assert/strict";
2
+ import crypto from "node:crypto";
3
+ import { test } from "node:test";
4
+
5
+ import {
6
+ ROBOT_CREDENTIALS_ENV,
7
+ RobotCredentialsError,
8
+ decryptCredential,
9
+ encryptCredential,
10
+ encryptedPairUpdate,
11
+ isEncryptedCredential,
12
+ robotCloudCredentials,
13
+ robotCredentialsKey,
14
+ } from "./.build/utils/robot-credentials.util.mjs";
15
+ import { adapterForRobot, clearRobotBrands, registerRobotBrand } from "./.build/utils/robot-adapter.util.mjs";
16
+
17
+ const HEX = crypto.randomBytes(32).toString("hex");
18
+ const B64 = crypto.randomBytes(32).toString("base64");
19
+
20
+ function withKey(value, fn) {
21
+ const before = process.env[ROBOT_CREDENTIALS_ENV];
22
+ if (value === undefined) delete process.env[ROBOT_CREDENTIALS_ENV];
23
+ else process.env[ROBOT_CREDENTIALS_ENV] = value;
24
+ try {
25
+ return fn();
26
+ } finally {
27
+ if (before === undefined) delete process.env[ROBOT_CREDENTIALS_ENV];
28
+ else process.env[ROBOT_CREDENTIALS_ENV] = before;
29
+ }
30
+ }
31
+
32
+ test("key: 64 hex or base64 of 32 bytes; anything else counts as no key", () => {
33
+ assert.equal(robotCredentialsKey({ [ROBOT_CREDENTIALS_ENV]: HEX }).length, 32);
34
+ assert.equal(robotCredentialsKey({ [ROBOT_CREDENTIALS_ENV]: B64 }).length, 32);
35
+ assert.equal(robotCredentialsKey({ [ROBOT_CREDENTIALS_ENV]: "" }), null);
36
+ assert.equal(robotCredentialsKey({ [ROBOT_CREDENTIALS_ENV]: "short" }), null);
37
+ assert.equal(robotCredentialsKey({ [ROBOT_CREDENTIALS_ENV]: crypto.randomBytes(16).toString("hex") }), null);
38
+ assert.equal(robotCredentialsKey({}), null);
39
+ });
40
+
41
+ test("roundtrip: encrypted value is prefixed, random per write, and decrypts", () => {
42
+ const key = Buffer.from(HEX, "hex");
43
+ const a = encryptCredential("app-key-123", "appKey", key);
44
+ const b = encryptCredential("app-key-123", "appKey", key);
45
+ assert.ok(isEncryptedCredential(a));
46
+ assert.notEqual(a, b, "fresh IV every time");
47
+ assert.equal(a.includes("app-key-123"), false);
48
+ assert.equal(decryptCredential(a, "appKey", key), "app-key-123");
49
+ assert.equal(decryptCredential(b, "appKey", key), "app-key-123");
50
+ assert.equal(encryptCredential(a, "appKey", key), a, "never double-encrypted");
51
+ });
52
+
53
+ test("tamper detection: a changed byte, the wrong field, or the wrong key all refuse", () => {
54
+ const key = Buffer.from(HEX, "hex");
55
+ const enc = encryptCredential("secret-token", "appToken", key);
56
+ const [pre, iv, tag, ct] = enc.split(":");
57
+ const flipped = Buffer.from(ct, "base64url");
58
+ flipped[0] ^= 1;
59
+ assert.throws(() => decryptCredential(`${pre}:${iv}:${tag}:${flipped.toString("base64url")}`, "appToken", key), RobotCredentialsError);
60
+ assert.throws(() => decryptCredential(enc, "appKey", key), RobotCredentialsError, "field is bound as AAD");
61
+ assert.throws(() => decryptCredential(enc, "appToken", crypto.randomBytes(32)), RobotCredentialsError);
62
+ assert.throws(() => decryptCredential("rc1:broken", "appToken", key), RobotCredentialsError);
63
+ });
64
+
65
+ test("missing key: plain passes through both ways; encrypted fails closed by name", () => {
66
+ const key = Buffer.from(HEX, "hex");
67
+ const enc = encryptCredential("k", "appKey", key);
68
+ assert.equal(encryptCredential("plain", "appKey", null), "plain");
69
+ assert.equal(decryptCredential("plain", "appKey", null), "plain");
70
+ assert.throws(() => decryptCredential(enc, "appKey", null), /no key/);
71
+ });
72
+
73
+ test("mixed: plain and encrypted pairs both reach the adapter as plain", () => {
74
+ clearRobotBrands();
75
+ const seen = [];
76
+ registerRobotBrand({
77
+ brand: "t-brand",
78
+ displayName: "T",
79
+ capabilities: {},
80
+ create: (c) => (seen.push(c), { listRobots: async () => [] }),
81
+ });
82
+ withKey(HEX, () => {
83
+ const key = robotCredentialsKey();
84
+ adapterForRobot({ brand: "t-brand", appKey: "plain-k", appToken: "plain-t" });
85
+ adapterForRobot({ brand: "t-brand", appKey: encryptCredential("enc-k", "appKey", key), appToken: encryptCredential("enc-t", "appToken", key) });
86
+ adapterForRobot({ brand: "t-brand", appKey: encryptCredential("half-k", "appKey", key), appToken: "half-t" });
87
+ });
88
+ assert.deepEqual(seen, [
89
+ { appid: "plain-k", secret: "plain-t" },
90
+ { appid: "enc-k", secret: "enc-t" },
91
+ { appid: "half-k", secret: "half-t" },
92
+ ]);
93
+ // Without the key an encrypted robot is refused, never handed an empty or ciphertext credential.
94
+ const enc = withKey(HEX, () => ({ brand: "t-brand", appKey: encryptCredential("x", "appKey"), appToken: encryptCredential("y", "appToken") }));
95
+ withKey(undefined, () => assert.throws(() => adapterForRobot(enc), /no key to read them/));
96
+ assert.equal(seen.length, 3);
97
+ clearRobotBrands();
98
+ });
99
+
100
+ test("encryptedPairUpdate: only plain, non-empty fields; nothing without a key", () => {
101
+ const key = Buffer.from(HEX, "hex");
102
+ assert.equal(encryptedPairUpdate({ appKey: "a", appToken: "b" }, null), null);
103
+ const up = encryptedPairUpdate({ appKey: "a", appToken: "b" }, key);
104
+ assert.ok(isEncryptedCredential(up.appKey) && isEncryptedCredential(up.appToken));
105
+ assert.deepEqual(Object.keys(encryptedPairUpdate({ appKey: up.appKey, appToken: "b" }, key)), ["appToken"]);
106
+ assert.equal(encryptedPairUpdate({ appKey: up.appKey, appToken: up.appToken }, key), null);
107
+ assert.equal(encryptedPairUpdate({ appKey: "", appToken: null }, key), null);
108
+ const creds = withKey(HEX, () => robotCloudCredentials({ appKey: up.appKey, appToken: " b " }));
109
+ assert.deepEqual(creds, { appid: "a", secret: "b" });
110
+ });
@@ -0,0 +1,52 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+
4
+ import {
5
+ mayEditSiteDetails,
6
+ siteOwnerOnlyMessage,
7
+ touchesSiteOwnerFields,
8
+ } from "./.build/utils/site-owner-edit.util.mjs";
9
+
10
+ // Owner decision 2026-09-25: only the owning client edits a site's details.
11
+ const OWNER = "6a0000000000000000000001";
12
+ const PROVIDER = "6a0000000000000000000002";
13
+
14
+ test("a member of the owning organisation may edit", () => {
15
+ assert.equal(mayEditSiteDetails({ staff: false, actorOrgIds: [OWNER], ownerOrg: OWNER }), true);
16
+ });
17
+
18
+ test("a provider that only serves the site may not", () => {
19
+ assert.equal(mayEditSiteDetails({ staff: false, actorOrgIds: [PROVIDER], ownerOrg: OWNER }), false);
20
+ });
21
+
22
+ test("Seven365 staff may", () => {
23
+ assert.equal(mayEditSiteDetails({ staff: true, actorOrgIds: [], ownerOrg: OWNER }), true);
24
+ });
25
+
26
+ test("an ObjectId-like owner compares by its string", () => {
27
+ const oid = { toString: () => OWNER };
28
+ assert.equal(mayEditSiteDetails({ staff: false, actorOrgIds: [OWNER], ownerOrg: oid }), true);
29
+ });
30
+
31
+ test("a site with no owner on record is staff-only", () => {
32
+ assert.equal(mayEditSiteDetails({ staff: false, actorOrgIds: [OWNER], ownerOrg: undefined }), false);
33
+ assert.equal(mayEditSiteDetails({ staff: false, actorOrgIds: [""], ownerOrg: "" }), false);
34
+ });
35
+
36
+ test("only address and timezone are owner fields on PATCH /sites/id/:id", () => {
37
+ assert.equal(touchesSiteOwnerFields({ address: { line1: "x" } }), true);
38
+ assert.equal(touchesSiteOwnerFields({ timezone: "Asia/Singapore" }), true);
39
+ assert.equal(touchesSiteOwnerFields({ deliveryCompanyList: [], metadata: { guardPosts: 2 } }), false);
40
+ assert.equal(touchesSiteOwnerFields(null), false);
41
+ });
42
+
43
+ test("the refusal names the owner, or says another organisation", () => {
44
+ assert.equal(
45
+ siteOwnerOnlyMessage("Management Pte Ltd"),
46
+ "This site belongs to Management Pte Ltd. Only they can change its details.",
47
+ );
48
+ assert.equal(
49
+ siteOwnerOnlyMessage(""),
50
+ "This site belongs to another organisation. Only they can change its details.",
51
+ );
52
+ });
@@ -43,8 +43,8 @@ test("the category delivers in-app and push, and is switchable", () => {
43
43
  assert.equal(platform.rendered, true);
44
44
  assert.equal(categorySupportsChannel("platform", "inApp"), true);
45
45
  assert.equal(categorySupportsChannel("platform", "push"), true);
46
- // No mailer recipient for a staff list, so no dead email switch.
47
- assert.equal(categorySupportsChannel("platform", "email"), false);
46
+ // Staff can opt in to email too (2026-09-25): opt-in, verified-only.
47
+ assert.equal(categorySupportsChannel("platform", "email"), true);
48
48
  });
49
49
 
50
50
  test("the stored module tag is one core accepts", () => {