@simplelayer/graniteer-core 0.1.2 → 0.2.1
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/index.js +2 -2
- package/package.json +73 -2
- package/src/agreementCheckout.ts +250 -0
- package/src/agreementPortalEmail.ts +128 -0
- package/src/billingPortalLinkEmail.ts +117 -0
- package/src/bookingAgreementEmail.ts +149 -0
- package/src/bookingEffects.ts +388 -0
- package/src/busClient.ts +268 -0
- package/src/busSignature.ts +71 -0
- package/src/checkoutCoupon.test.ts +675 -0
- package/src/checkoutCoupon.ts +455 -0
- package/src/commonsDeposit.ts +298 -0
- package/src/commonsDepositWebhook.ts +237 -0
- package/src/dayPassConfirmation.ts +230 -0
- package/src/dunningEmails.test.ts +263 -0
- package/src/dunningEmails.ts +203 -0
- package/src/email-templates/brand.ts +93 -0
- package/src/email-templates/magic-link.tsx +59 -0
- package/src/email-templates/recovery.tsx +59 -0
- package/src/email-templates/signup.tsx +67 -0
- package/src/functions/admin-cancel-membership.ts +311 -311
- package/src/functions/admin-change-membership-plan.ts +312 -308
- package/src/functions/admin-charge-guest-print.ts +229 -229
- package/src/functions/admin-correct-booking.ts +333 -333
- package/src/functions/admin-create-booking.ts +460 -0
- package/src/functions/admin-create-commons-event.ts +244 -244
- package/src/functions/admin-debug-boldsign-doc.ts +93 -93
- package/src/functions/admin-door-write.ts +272 -272
- package/src/functions/admin-issue-day-pass.ts +231 -0
- package/src/functions/admin-mark-booking-paid.ts +203 -203
- package/src/functions/admin-mint-billing-portal-link.ts +168 -0
- package/src/functions/admin-mint-portal-link.ts +62 -0
- package/src/functions/admin-photo-upload.ts +128 -128
- package/src/functions/admin-remove-team-member.ts +150 -150
- package/src/functions/admin-request-trial.ts +366 -0
- package/src/functions/admin-resend-commons-deposit-link.ts +323 -0
- package/src/functions/admin-reset-mfa.ts +138 -138
- package/src/functions/admin-retry-invoice-payment.test.ts +567 -0
- package/src/functions/admin-retry-invoice-payment.ts +333 -0
- package/src/functions/admin-reverse-coupon-redemption.ts +98 -98
- package/src/functions/admin-revoke-agreement.ts +467 -0
- package/src/functions/admin-send-commons-agreement.ts +214 -214
- package/src/functions/admin-set-credit-amount.ts +123 -123
- package/src/functions/admin-skip-commons-contract.ts +165 -165
- package/src/functions/admin-undo-membership-cancel.ts +144 -144
- package/src/functions/admin-wall-photo-write.ts +141 -141
- package/src/functions/auth-email-hook.ts +259 -0
- package/src/functions/boldsign-sender-identity.ts +114 -114
- package/src/functions/booking-availability.ts +419 -419
- package/src/functions/booking-member-preview.ts +136 -136
- package/src/functions/booking-status.ts +149 -0
- package/src/functions/conductor.ts +505 -0
- package/src/functions/create-agreement.ts +537 -0
- package/src/functions/create-booking.ts +648 -0
- package/src/functions/create-day-pass-checkout.ts +218 -0
- package/src/functions/create-portal-session.ts +142 -0
- package/src/functions/create-subscription-checkout.test.ts +222 -0
- package/src/functions/create-subscription-checkout.ts +440 -0
- package/src/functions/day-pass-status.ts +89 -89
- package/src/functions/diag-webpush-test.ts +45 -45
- package/src/functions/event-access-admin.ts +277 -0
- package/src/functions/event-access-store.ts +334 -309
- package/src/functions/ezeep-print-webhook.ts +213 -213
- package/src/functions/ezeep-usage-reconcile.ts +299 -299
- package/src/functions/fetch-avatar-original.ts +457 -0
- package/src/functions/get-provider-sign-link.ts +155 -0
- package/src/functions/get-signing-link.ts +211 -211
- package/src/functions/guest-print-setup-confirm.ts +128 -128
- package/src/functions/guest-print-submit.ts +149 -149
- package/src/functions/luma-webhook.ts +165 -165
- package/src/functions/manage-booking.ts +472 -0
- package/src/functions/mcp.ts +67 -0
- package/src/functions/member-door-action.test.ts +1248 -0
- package/src/functions/member-door-action.ts +863 -0
- package/src/functions/member-unifi-mapping.test.ts +516 -0
- package/src/functions/member-unifi-mapping.ts +206 -0
- package/src/functions/membership-door-sync.ts +956 -0
- package/src/functions/migration-collision-alert.copy.ts +232 -232
- package/src/functions/migration-collision-alert.test.ts +349 -349
- package/src/functions/migration-collision-alert.ts +232 -232
- package/src/functions/office-assignment-end.ts +146 -146
- package/src/functions/portal-booking-access.ts +217 -95
- package/src/functions/portal-list-invoices.ts +99 -99
- package/src/functions/portal-request-login.ts +192 -0
- package/src/functions/portal-resend-invite.ts +248 -0
- package/src/functions/portal-submit-event.ts +168 -0
- package/src/functions/print-billing-recover.ts +97 -97
- package/src/functions/print-billing-run.ts +183 -183
- package/src/functions/public-facts.ts +168 -168
- package/src/functions/push-send.ts +348 -0
- package/src/functions/push-test.ts +234 -0
- package/src/functions/request-portal-link.ts +211 -0
- package/src/functions/reschedule-booking.ts +649 -0
- package/src/functions/review-ratings.ts +113 -0
- package/src/functions/space-availability-range.acceptance.test.ts +161 -161
- package/src/functions/space-availability-range.ts +142 -142
- package/src/functions/stripe-db-reconcile.ts +319 -0
- package/src/functions/stripe-webhook.moved-regression.test.ts +78 -0
- package/src/functions/stripe-webhook.ts +2595 -0
- package/src/functions/sync-gcal-blackouts.ts +298 -298
- package/src/functions/timetable-event-register.ts +162 -162
- package/src/functions/timetable-event-status.ts +90 -90
- package/src/functions/timetable-spaces.ts +219 -219
- package/src/functions/touch-pass-reconciler.ts +689 -689
- package/src/functions/touch-pass-signup.ts +478 -0
- package/src/functions/upload-booking-display-image.ts +179 -0
- package/src/functions/virtual-onboarding-purge.ts +134 -134
- package/src/functions/virtual-onboarding-submit.ts +315 -315
- package/src/functions/voice-booking.ts +453 -0
- package/src/functions/voice-day-pass.ts +346 -0
- package/src/functions/voice-inquiry.test.ts +301 -301
- package/src/functions/voice-inquiry.ts +198 -198
- package/src/functions/voice-membership-signup.ts +355 -0
- package/src/functions/voice-tour-availability.ts +204 -204
- package/src/functions/voice-tour-booking.ts +429 -0
- package/src/ghlEventInquiry.ts +143 -0
- package/src/mailArrivalEmail.ts +121 -0
- package/src/manageToken.ts +363 -0
- package/src/membershipBusClient.ts +537 -0
- package/src/membershipWelcome.ts +683 -0
- package/src/migrationCheckout.ts +366 -0
- package/src/pacificDate.ts +54 -0
- package/src/paymentLinkEmail.ts +123 -0
- package/src/signedUnpaidReminderEmail.test.ts +246 -0
- package/src/signedUnpaidReminderEmail.ts +132 -0
- package/src/stripeCoworkingPortal.ts +91 -0
- package/src/supabaseTestFakes.ts +131 -131
- package/src/supportTicketEmail.ts +119 -0
- package/src/unifiAccessPolicies.ts +228 -0
- package/src/unifiGroups.ts +129 -0
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
// Unit tests for the member-unifi-mapping edge function.
|
|
2
|
+
//
|
|
3
|
+
// Run with:
|
|
4
|
+
// deno test --no-check --node-modules-dir=auto --allow-env --allow-net --allow-read \
|
|
5
|
+
// src/functions/member-unifi-mapping.test.ts
|
|
6
|
+
//
|
|
7
|
+
// `--node-modules-dir=auto` matters: with `=none` the npm:@supabase/supabase-js
|
|
8
|
+
// graph fails to resolve `tslib` on a clean checkout. `=none` is still correct
|
|
9
|
+
// for `deno check`, which does not need the runtime graph.
|
|
10
|
+
//
|
|
11
|
+
// NO REAL NETWORK. Supabase Auth is never touched (this endpoint has no user
|
|
12
|
+
// session), and PostgREST is answered by an in-memory `globalThis.fetch` stub
|
|
13
|
+
// keyed off the request URL — same stub style as voice-inquiry/index.test.ts.
|
|
14
|
+
import { assert, assertEquals, assertFalse } from 'https://deno.land/std@0.224.0/assert/mod.ts';
|
|
15
|
+
import {
|
|
16
|
+
handle,
|
|
17
|
+
secretIsolationViolations,
|
|
18
|
+
bearerToken,
|
|
19
|
+
distinctMappings,
|
|
20
|
+
} from './member-unifi-mapping.ts';
|
|
21
|
+
|
|
22
|
+
const SUB = '11111111-1111-4111-8111-111111111111';
|
|
23
|
+
const SECRET = 'mapping-secret-unique-to-this-endpoint';
|
|
24
|
+
|
|
25
|
+
const ENV_DEFAULTS: Record<string, string> = {
|
|
26
|
+
SUPABASE_URL: 'https://example.supabase.co',
|
|
27
|
+
SUPABASE_SERVICE_ROLE_KEY: 'test-service-role-key',
|
|
28
|
+
MEMBER_ACTIONS_MAPPING_SECRET: SECRET,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Cleared every test so a value from the developer's real shell cannot make the
|
|
32
|
+
// isolation check pass or fail by accident.
|
|
33
|
+
const ENV_CLEARED: Record<string, string | undefined> = {
|
|
34
|
+
MEMBER_ACTIONS_HMAC_SECRET: undefined,
|
|
35
|
+
MEMBER_ASSERTION_SIGNING_KEY: undefined,
|
|
36
|
+
SUPABASE_ANON_KEY: undefined,
|
|
37
|
+
SUPABASE_PUBLISHABLE_KEY: undefined,
|
|
38
|
+
BUS_HMAC_SECRET: undefined,
|
|
39
|
+
BUS_HMAC_SECRET_MEMBERSHIP: undefined,
|
|
40
|
+
BUS_HMAC_SECRET_RESEND: undefined,
|
|
41
|
+
OPS_ALERT_WEBHOOK_URL: undefined,
|
|
42
|
+
SLACK_BOT_TOKEN: undefined,
|
|
43
|
+
OPS_ALERT_SLACK_CHANNEL_ID: undefined,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
function withEnvs(vars: Record<string, string | undefined>, fn: () => Promise<void>): Promise<void> {
|
|
47
|
+
const all = { ...ENV_DEFAULTS, ...ENV_CLEARED, ...vars };
|
|
48
|
+
const prev = new Map<string, string | undefined>();
|
|
49
|
+
for (const [k, v] of Object.entries(all)) {
|
|
50
|
+
prev.set(k, Deno.env.get(k));
|
|
51
|
+
if (v === undefined) Deno.env.delete(k);
|
|
52
|
+
else Deno.env.set(k, v);
|
|
53
|
+
}
|
|
54
|
+
return (async () => {
|
|
55
|
+
try {
|
|
56
|
+
await fn();
|
|
57
|
+
} finally {
|
|
58
|
+
for (const [k, v] of prev) {
|
|
59
|
+
if (v === undefined) Deno.env.delete(k);
|
|
60
|
+
else Deno.env.set(k, v);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
})();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface StubOpts {
|
|
67
|
+
rows?: unknown[] | 'error' | 'throw';
|
|
68
|
+
rateLimitAllows?: boolean;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface Stub {
|
|
72
|
+
membershipQueries: string[];
|
|
73
|
+
opsEventInserts: unknown[];
|
|
74
|
+
otherUrls: string[];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function withFetchStub(opts: StubOpts, fn: (stub: Stub) => Promise<void>): Promise<void> {
|
|
78
|
+
const stub: Stub = { membershipQueries: [], opsEventInserts: [], otherUrls: [] };
|
|
79
|
+
const original = globalThis.fetch;
|
|
80
|
+
globalThis.fetch = ((input: string | URL | Request, init?: RequestInit) => {
|
|
81
|
+
const url = typeof input === 'string'
|
|
82
|
+
? input
|
|
83
|
+
: input instanceof URL ? input.toString() : input.url;
|
|
84
|
+
|
|
85
|
+
if (url.includes('/rest/v1/rpc/check_rate_limit')) {
|
|
86
|
+
return Promise.resolve(new Response(
|
|
87
|
+
JSON.stringify(opts.rateLimitAllows ?? true),
|
|
88
|
+
{ status: 200, headers: { 'Content-Type': 'application/json' } },
|
|
89
|
+
));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (url.includes('/rest/v1/ops_events')) {
|
|
93
|
+
if (typeof init?.body === 'string') stub.opsEventInserts.push(JSON.parse(init.body));
|
|
94
|
+
return Promise.resolve(new Response('[]', { status: 201 }));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (url.includes('/rest/v1/memberships')) {
|
|
98
|
+
stub.membershipQueries.push(url);
|
|
99
|
+
if (opts.rows === 'throw') return Promise.reject(new Error('socket hang up'));
|
|
100
|
+
if (opts.rows === 'error') {
|
|
101
|
+
return Promise.resolve(new Response(
|
|
102
|
+
JSON.stringify({ message: 'permission denied for table memberships', code: '42501' }),
|
|
103
|
+
{ status: 400, headers: { 'Content-Type': 'application/json' } },
|
|
104
|
+
));
|
|
105
|
+
}
|
|
106
|
+
return Promise.resolve(new Response(JSON.stringify(opts.rows ?? []), {
|
|
107
|
+
status: 200, headers: { 'Content-Type': 'application/json' },
|
|
108
|
+
}));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
stub.otherUrls.push(url);
|
|
112
|
+
return Promise.resolve(new Response('{}', { status: 200 }));
|
|
113
|
+
}) as typeof fetch;
|
|
114
|
+
|
|
115
|
+
return (async () => {
|
|
116
|
+
try {
|
|
117
|
+
await fn(stub);
|
|
118
|
+
} finally {
|
|
119
|
+
globalThis.fetch = original;
|
|
120
|
+
}
|
|
121
|
+
})();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function makeRequest(
|
|
125
|
+
body: unknown,
|
|
126
|
+
opts: { token?: string | null; method?: string; rawBody?: string } = {},
|
|
127
|
+
): Request {
|
|
128
|
+
const headers: Record<string, string> = {
|
|
129
|
+
'Content-Type': 'application/json',
|
|
130
|
+
'x-forwarded-for': '198.51.100.9',
|
|
131
|
+
};
|
|
132
|
+
const token = opts.token === undefined ? SECRET : opts.token;
|
|
133
|
+
if (token !== null) headers['Authorization'] = `Bearer ${token}`;
|
|
134
|
+
const method = opts.method ?? 'POST';
|
|
135
|
+
const bodyless = method === 'GET' || method === 'HEAD' || method === 'OPTIONS';
|
|
136
|
+
return new Request('https://example.com/member-unifi-mapping', {
|
|
137
|
+
method,
|
|
138
|
+
headers,
|
|
139
|
+
...(bodyless ? {} : { body: opts.rawBody ?? JSON.stringify(body) }),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function row(id: string | null) {
|
|
144
|
+
return { unifi_access_user_id: id };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ===========================================================================
|
|
148
|
+
// 1. Correct secret — the three mapping cardinalities
|
|
149
|
+
// ===========================================================================
|
|
150
|
+
|
|
151
|
+
Deno.test('correct secret + a single mapping returns that one id', async () => {
|
|
152
|
+
await withEnvs({}, async () => {
|
|
153
|
+
await withFetchStub({ rows: [row('unifi-user-abc')] }, async (stub) => {
|
|
154
|
+
const res = await handle(makeRequest({ sub: SUB }));
|
|
155
|
+
assertEquals(res.status, 200);
|
|
156
|
+
assertEquals(await res.json(), { unifi_user_ids: ['unifi-user-abc'] });
|
|
157
|
+
assertEquals(stub.membershipQueries.length, 1);
|
|
158
|
+
// Scoped to the requested sub via auth_user_id, and selecting only the
|
|
159
|
+
// mapping column -- no other member field is read or exposed.
|
|
160
|
+
assert(stub.membershipQueries[0].includes('auth_user_id=eq.') );
|
|
161
|
+
assert(stub.membershipQueries[0].includes(SUB));
|
|
162
|
+
assert(stub.membershipQueries[0].includes('select=unifi_access_user_id'));
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
Deno.test('correct secret + MULTIPLE distinct mappings returns ALL of them, never collapsed', async () => {
|
|
168
|
+
await withEnvs({}, async () => {
|
|
169
|
+
await withFetchStub({
|
|
170
|
+
rows: [row('unifi-a'), row('unifi-b'), row('unifi-c')],
|
|
171
|
+
}, async () => {
|
|
172
|
+
const res = await handle(makeRequest({ sub: SUB }));
|
|
173
|
+
assertEquals(res.status, 200);
|
|
174
|
+
const body = await res.json();
|
|
175
|
+
// The ambiguity rule is the CALLER's to enforce; this endpoint must expose
|
|
176
|
+
// every candidate so it can.
|
|
177
|
+
assertEquals(body.unifi_user_ids, ['unifi-a', 'unifi-b', 'unifi-c']);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
Deno.test('duplicate and blank mapping values are de-duplicated, not miscounted as ambiguity', async () => {
|
|
183
|
+
await withEnvs({}, async () => {
|
|
184
|
+
await withFetchStub({
|
|
185
|
+
rows: [row('unifi-a'), row('unifi-a'), row(null), row(' '), row('unifi-b')],
|
|
186
|
+
}, async () => {
|
|
187
|
+
const res = await handle(makeRequest({ sub: SUB }));
|
|
188
|
+
assertEquals(res.status, 200);
|
|
189
|
+
assertEquals((await res.json()).unifi_user_ids, ['unifi-a', 'unifi-b']);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
Deno.test('correct secret + ZERO mappings returns an empty array with 200, not an error', async () => {
|
|
195
|
+
await withEnvs({}, async () => {
|
|
196
|
+
await withFetchStub({ rows: [] }, async () => {
|
|
197
|
+
const res = await handle(makeRequest({ sub: SUB }));
|
|
198
|
+
assertEquals(res.status, 200);
|
|
199
|
+
assertEquals(await res.json(), { unifi_user_ids: [] });
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
Deno.test('rows that exist but carry no mapping yet also return an empty array with 200', async () => {
|
|
205
|
+
await withEnvs({}, async () => {
|
|
206
|
+
await withFetchStub({ rows: [row(null), row('')] }, async () => {
|
|
207
|
+
const res = await handle(makeRequest({ sub: SUB }));
|
|
208
|
+
assertEquals(res.status, 200);
|
|
209
|
+
assertEquals(await res.json(), { unifi_user_ids: [] });
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
Deno.test('an unknown sub is a successful empty answer, not a 404 (no account enumeration)', async () => {
|
|
215
|
+
await withEnvs({}, async () => {
|
|
216
|
+
await withFetchStub({ rows: [] }, async () => {
|
|
217
|
+
const res = await handle(makeRequest({ sub: 'no-such-user' }));
|
|
218
|
+
assertEquals(res.status, 200);
|
|
219
|
+
assertEquals(await res.json(), { unifi_user_ids: [] });
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// ===========================================================================
|
|
225
|
+
// 2. Auth — 401, and nothing else attempted
|
|
226
|
+
// ===========================================================================
|
|
227
|
+
|
|
228
|
+
Deno.test('a WRONG bearer secret is 401 and no query is ever issued', async () => {
|
|
229
|
+
await withEnvs({}, async () => {
|
|
230
|
+
await withFetchStub({ rows: [row('unifi-a')] }, async (stub) => {
|
|
231
|
+
const res = await handle(makeRequest({ sub: SUB }, { token: 'wrong-secret' }));
|
|
232
|
+
assertEquals(res.status, 401);
|
|
233
|
+
assertEquals(await res.json(), { error: 'unauthorized' });
|
|
234
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
235
|
+
assertEquals(stub.opsEventInserts.length, 0);
|
|
236
|
+
assertEquals(stub.otherUrls.length, 0);
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
Deno.test('a MISSING Authorization header is 401 and no query is ever issued', async () => {
|
|
242
|
+
await withEnvs({}, async () => {
|
|
243
|
+
await withFetchStub({ rows: [row('unifi-a')] }, async (stub) => {
|
|
244
|
+
const res = await handle(makeRequest({ sub: SUB }, { token: null }));
|
|
245
|
+
assertEquals(res.status, 401);
|
|
246
|
+
assertEquals(await res.json(), { error: 'unauthorized' });
|
|
247
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
248
|
+
assertEquals(stub.otherUrls.length, 0);
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
Deno.test('a bearer that is a PREFIX of the real secret is rejected (no length-tolerant compare)', async () => {
|
|
254
|
+
await withEnvs({}, async () => {
|
|
255
|
+
await withFetchStub({}, async (stub) => {
|
|
256
|
+
const res = await handle(
|
|
257
|
+
makeRequest({ sub: SUB }, { token: SECRET.slice(0, -1) }),
|
|
258
|
+
);
|
|
259
|
+
assertEquals(res.status, 401);
|
|
260
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
Deno.test('a non-Bearer Authorization scheme is rejected', async () => {
|
|
266
|
+
await withEnvs({}, async () => {
|
|
267
|
+
await withFetchStub({}, async (stub) => {
|
|
268
|
+
const req = new Request('https://example.com/member-unifi-mapping', {
|
|
269
|
+
method: 'POST',
|
|
270
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Basic ${SECRET}` },
|
|
271
|
+
body: JSON.stringify({ sub: SUB }),
|
|
272
|
+
});
|
|
273
|
+
const res = await handle(req);
|
|
274
|
+
assertEquals(res.status, 401);
|
|
275
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
Deno.test('an UNSET configured secret is 401 for everyone, never an open endpoint', async () => {
|
|
281
|
+
await withEnvs({ MEMBER_ACTIONS_MAPPING_SECRET: undefined }, async () => {
|
|
282
|
+
await withFetchStub({ rows: [row('unifi-a')] }, async (stub) => {
|
|
283
|
+
// Even a caller that sends nothing, and a caller that sends the empty
|
|
284
|
+
// string, must both be refused.
|
|
285
|
+
const a = await handle(makeRequest({ sub: SUB }, { token: null }));
|
|
286
|
+
assertEquals(a.status, 401);
|
|
287
|
+
const b = await handle(makeRequest({ sub: SUB }, { token: '' }));
|
|
288
|
+
assertEquals(b.status, 401);
|
|
289
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
Deno.test('an EMPTY configured secret is 401 for everyone', async () => {
|
|
295
|
+
await withEnvs({ MEMBER_ACTIONS_MAPPING_SECRET: ' ' }, async () => {
|
|
296
|
+
await withFetchStub({ rows: [row('unifi-a')] }, async (stub) => {
|
|
297
|
+
const res = await handle(makeRequest({ sub: SUB }, { token: ' ' }));
|
|
298
|
+
assertEquals(res.status, 401);
|
|
299
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
Deno.test('a mapping secret that COLLIDES with another repo secret is unusable (401 + alert)', async () => {
|
|
305
|
+
await withEnvs({ MEMBER_ACTIONS_HMAC_SECRET: SECRET }, async () => {
|
|
306
|
+
await withFetchStub({ rows: [row('unifi-a')] }, async (stub) => {
|
|
307
|
+
const res = await handle(makeRequest({ sub: SUB }));
|
|
308
|
+
assertEquals(res.status, 401);
|
|
309
|
+
assertEquals(await res.json(), { error: 'unauthorized' });
|
|
310
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
311
|
+
assert(
|
|
312
|
+
JSON.stringify(stub.opsEventInserts).includes('member_unifi_mapping_secret_reuse'),
|
|
313
|
+
'the collision was not alerted',
|
|
314
|
+
);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
Deno.test('a mapping secret equal to the SERVICE ROLE KEY is unusable', async () => {
|
|
320
|
+
await withEnvs({ MEMBER_ACTIONS_MAPPING_SECRET: 'test-service-role-key' }, async () => {
|
|
321
|
+
await withFetchStub({ rows: [row('unifi-a')] }, async (stub) => {
|
|
322
|
+
const res = await handle(
|
|
323
|
+
makeRequest({ sub: SUB }, { token: 'test-service-role-key' }),
|
|
324
|
+
);
|
|
325
|
+
assertEquals(res.status, 401);
|
|
326
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
// ===========================================================================
|
|
332
|
+
// 3. Method and body validation
|
|
333
|
+
// ===========================================================================
|
|
334
|
+
|
|
335
|
+
Deno.test('non-POST is 405 before auth or any query', async () => {
|
|
336
|
+
await withEnvs({}, async () => {
|
|
337
|
+
await withFetchStub({}, async (stub) => {
|
|
338
|
+
for (const method of ['GET', 'PUT', 'DELETE', 'OPTIONS']) {
|
|
339
|
+
const res = await handle(makeRequest(null, { method }));
|
|
340
|
+
assertEquals(res.status, 405, method);
|
|
341
|
+
assertEquals(await res.json(), { error: 'method_not_allowed' });
|
|
342
|
+
}
|
|
343
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
Deno.test('a missing sub is 400 invalid_sub and no query is issued', async () => {
|
|
349
|
+
await withEnvs({}, async () => {
|
|
350
|
+
await withFetchStub({ rows: [row('unifi-a')] }, async (stub) => {
|
|
351
|
+
const res = await handle(makeRequest({}));
|
|
352
|
+
assertEquals(res.status, 400);
|
|
353
|
+
assertEquals(await res.json(), { error: 'invalid_sub' });
|
|
354
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
Deno.test('a non-string or blank sub is 400 invalid_sub', async () => {
|
|
360
|
+
await withEnvs({}, async () => {
|
|
361
|
+
for (const sub of [null, 42, true, '', ' ', { nested: 'x' }, ['a']]) {
|
|
362
|
+
await withFetchStub({ rows: [row('unifi-a')] }, async (stub) => {
|
|
363
|
+
const res = await handle(makeRequest({ sub }));
|
|
364
|
+
assertEquals(res.status, 400, JSON.stringify(sub));
|
|
365
|
+
assertEquals(await res.json(), { error: 'invalid_sub' });
|
|
366
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
Deno.test('an unparseable body is 400 invalid_json', async () => {
|
|
373
|
+
await withEnvs({}, async () => {
|
|
374
|
+
await withFetchStub({}, async (stub) => {
|
|
375
|
+
const res = await handle(makeRequest(null, { rawBody: '{not json' }));
|
|
376
|
+
assertEquals(res.status, 400);
|
|
377
|
+
assertEquals(await res.json(), { error: 'invalid_json' });
|
|
378
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
Deno.test('a non-object JSON body is 400, not a crash', async () => {
|
|
384
|
+
await withEnvs({}, async () => {
|
|
385
|
+
await withFetchStub({}, async (stub) => {
|
|
386
|
+
for (const raw of ['[]', '"a string"', '7', 'null']) {
|
|
387
|
+
const res = await handle(makeRequest(null, { rawBody: raw }));
|
|
388
|
+
assertEquals(res.status, 400, raw);
|
|
389
|
+
assertEquals(await res.json(), { error: 'invalid_sub' });
|
|
390
|
+
}
|
|
391
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
// ===========================================================================
|
|
397
|
+
// 4. Failure modes
|
|
398
|
+
// ===========================================================================
|
|
399
|
+
|
|
400
|
+
Deno.test('a DB lookup failure is 500 lookup_failed and is alerted', async () => {
|
|
401
|
+
await withEnvs({}, async () => {
|
|
402
|
+
await withFetchStub({ rows: 'error' }, async (stub) => {
|
|
403
|
+
const res = await handle(makeRequest({ sub: SUB }));
|
|
404
|
+
assertEquals(res.status, 500);
|
|
405
|
+
assertEquals(await res.json(), { error: 'lookup_failed' });
|
|
406
|
+
assert(
|
|
407
|
+
JSON.stringify(stub.opsEventInserts).includes('member_unifi_mapping_lookup_failed'),
|
|
408
|
+
'the DB failure was not alerted',
|
|
409
|
+
);
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
Deno.test('a thrown transport error is 500 lookup_failed, never an empty success', async () => {
|
|
415
|
+
await withEnvs({}, async () => {
|
|
416
|
+
await withFetchStub({ rows: 'throw' }, async () => {
|
|
417
|
+
const res = await handle(makeRequest({ sub: SUB }));
|
|
418
|
+
assertEquals(res.status, 500);
|
|
419
|
+
const body = await res.json();
|
|
420
|
+
assertEquals(body, { error: 'lookup_failed' });
|
|
421
|
+
// Critically: NOT `{unifi_user_ids: []}`. An empty array means "checked,
|
|
422
|
+
// nothing mapped"; a failure must never be dressed up as that.
|
|
423
|
+
assertFalse('unifi_user_ids' in body);
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
Deno.test('rate limiting returns 429 before any query', async () => {
|
|
429
|
+
await withEnvs({}, async () => {
|
|
430
|
+
await withFetchStub({ rateLimitAllows: false, rows: [row('unifi-a')] }, async (stub) => {
|
|
431
|
+
const res = await handle(makeRequest({ sub: SUB }));
|
|
432
|
+
assertEquals(res.status, 429);
|
|
433
|
+
assertEquals(await res.json(), { error: 'rate_limited' });
|
|
434
|
+
assertEquals(stub.membershipQueries.length, 0);
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
Deno.test('the response carries no CORS headers: this endpoint is not for browsers', async () => {
|
|
440
|
+
await withEnvs({}, async () => {
|
|
441
|
+
await withFetchStub({ rows: [row('unifi-a')] }, async () => {
|
|
442
|
+
const res = await handle(makeRequest({ sub: SUB }));
|
|
443
|
+
assertEquals(res.headers.get('Access-Control-Allow-Origin'), null);
|
|
444
|
+
});
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
// ===========================================================================
|
|
449
|
+
// 5. Pure helpers
|
|
450
|
+
// ===========================================================================
|
|
451
|
+
|
|
452
|
+
Deno.test('bearerToken: extracts only a well-formed Bearer value', () => {
|
|
453
|
+
const mk = (h: Record<string, string>) =>
|
|
454
|
+
new Request('https://x.test', { method: 'POST', headers: h, body: '{}' });
|
|
455
|
+
assertEquals(bearerToken(mk({ Authorization: 'Bearer abc' })), 'abc');
|
|
456
|
+
assertEquals(bearerToken(mk({ authorization: 'bearer abc ' })), 'abc');
|
|
457
|
+
assertEquals(bearerToken(mk({ Authorization: 'Basic abc' })), '');
|
|
458
|
+
assertEquals(bearerToken(mk({ Authorization: 'abc' })), '');
|
|
459
|
+
assertEquals(bearerToken(mk({})), '');
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
Deno.test('distinctMappings: de-dupes, drops blanks, preserves order, never collapses', () => {
|
|
463
|
+
assertEquals(distinctMappings([]), []);
|
|
464
|
+
assertEquals(distinctMappings([{ unifi_access_user_id: null }]), []);
|
|
465
|
+
assertEquals(distinctMappings([{ unifi_access_user_id: ' a ' }]), ['a']);
|
|
466
|
+
assertEquals(
|
|
467
|
+
distinctMappings([
|
|
468
|
+
{ unifi_access_user_id: 'b' },
|
|
469
|
+
{ unifi_access_user_id: 'a' },
|
|
470
|
+
{ unifi_access_user_id: 'b' },
|
|
471
|
+
]),
|
|
472
|
+
['b', 'a'],
|
|
473
|
+
);
|
|
474
|
+
// Case matters: UniFi Access ids are case-sensitive, so these are two
|
|
475
|
+
// genuinely different candidates and the caller must see both.
|
|
476
|
+
assertEquals(
|
|
477
|
+
distinctMappings([{ unifi_access_user_id: 'A' }, { unifi_access_user_id: 'a' }]),
|
|
478
|
+
['A', 'a'],
|
|
479
|
+
);
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
Deno.test('secretIsolationViolations: flags every collision, clean when all differ', () => {
|
|
483
|
+
assertEquals(
|
|
484
|
+
secretIsolationViolations({
|
|
485
|
+
MEMBER_ACTIONS_MAPPING_SECRET: 'k0',
|
|
486
|
+
MEMBER_ACTIONS_HMAC_SECRET: 'k1',
|
|
487
|
+
MEMBER_ASSERTION_SIGNING_KEY: 'k2',
|
|
488
|
+
SUPABASE_SERVICE_ROLE_KEY: 'k3',
|
|
489
|
+
SUPABASE_ANON_KEY: 'k4',
|
|
490
|
+
SUPABASE_PUBLISHABLE_KEY: 'k5',
|
|
491
|
+
BUS_HMAC_SECRET: 'k6',
|
|
492
|
+
BUS_HMAC_SECRET_MEMBERSHIP: 'k7',
|
|
493
|
+
BUS_HMAC_SECRET_RESEND: 'k8',
|
|
494
|
+
}),
|
|
495
|
+
[],
|
|
496
|
+
);
|
|
497
|
+
for (
|
|
498
|
+
const other of [
|
|
499
|
+
'MEMBER_ACTIONS_HMAC_SECRET', 'MEMBER_ASSERTION_SIGNING_KEY',
|
|
500
|
+
'SUPABASE_SERVICE_ROLE_KEY', 'SUPABASE_ANON_KEY', 'SUPABASE_PUBLISHABLE_KEY',
|
|
501
|
+
'BUS_HMAC_SECRET', 'BUS_HMAC_SECRET_MEMBERSHIP', 'BUS_HMAC_SECRET_RESEND',
|
|
502
|
+
]
|
|
503
|
+
) {
|
|
504
|
+
assertEquals(
|
|
505
|
+
secretIsolationViolations({ MEMBER_ACTIONS_MAPPING_SECRET: 'same', [other]: 'same' }),
|
|
506
|
+
[`MEMBER_ACTIONS_MAPPING_SECRET==${other}`],
|
|
507
|
+
other,
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
// Unset must not report a spurious "everything equals everything".
|
|
511
|
+
assertEquals(secretIsolationViolations({}), []);
|
|
512
|
+
assertEquals(
|
|
513
|
+
secretIsolationViolations({ MEMBER_ACTIONS_HMAC_SECRET: 'x', BUS_HMAC_SECRET: 'x' }),
|
|
514
|
+
[],
|
|
515
|
+
);
|
|
516
|
+
});
|