Skip to content

Commit 9b25680

Browse files
committed
test(plugin-auth): pin that AuthPlugin registers the org MFA enrollment guard on kernel:ready (#30201)
The guard is registered on sys_organization for insert and update, after the ADR-0092 D2 identity write guard, and it asks the live auth manager per write, so a settings pass that turns two-factor on admits the next write. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Utp6CYRZxPEjGeTbGfW4YC
1 parent 4a878a7 commit 9b25680

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

‎packages/plugins/plugin-auth/src/organization-mfa-enrollment-guard.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
import { describe, it, expect, afterAll, vi } from 'vitest';
2727
import { ObjectQL } from '@objectstack/objectql';
2828
import { SqlDriver } from '@objectstack/driver-sql';
29+
import type { PluginContext } from '@objectstack/core';
2930
import { SysOrganization } from '@objectstack/platform-objects/identity';
3031
import { AuthManager } from './auth-manager';
32+
import { AuthPlugin } from './auth-plugin.js';
3133
import {
3234
ORGANIZATION_MFA_ENROLLMENT_GUARD_PRIORITY,
3335
organizationMfaWithoutEnrollmentRefusal,
@@ -141,6 +143,58 @@ describe('organization MFA enrollment guard — the rule', () => {
141143
});
142144
});
143145

146+
describe('auth-plugin registers the guard on `kernel:ready` and asks the live manager', () => {
147+
it('both hooks on sys_organization, after the D2 guard, judging the manager\'s two-factor state per write', async () => {
148+
const registered: Array<{ event: string; handler: (ctx: any) => Promise<void>; options: any }> = [];
149+
const engine = {
150+
registerHook: (event: string, handler: (ctx: any) => Promise<void>, options: any) => {
151+
registered.push({ event, handler, options });
152+
},
153+
getSchema: () => undefined,
154+
};
155+
const ready: Array<() => Promise<void>> = [];
156+
const ctx = {
157+
registerService: vi.fn(),
158+
getService: vi.fn((name: string): any => {
159+
if (name === 'manifest') return { register: vi.fn() };
160+
if (name === 'objectql') return engine;
161+
return undefined;
162+
}),
163+
getServices: vi.fn(() => new Map()),
164+
hook: vi.fn((name: string, handler: () => Promise<void>) => { if (name === 'kernel:ready') ready.push(handler); }),
165+
trigger: vi.fn(),
166+
logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() },
167+
getKernel: vi.fn(),
168+
} as unknown as PluginContext;
169+
const plugin = new AuthPlugin({ secret: 'test-secret-at-least-32-chars-long', baseUrl: 'http://localhost:3000' });
170+
await plugin.init(ctx);
171+
await plugin.start(ctx);
172+
for (const handler of ready) {
173+
try { await handler(); } catch { /* other kernel:ready work needs services this double does not offer */ }
174+
}
175+
176+
const ours = registered.filter(
177+
(r) => r.options?.packageId === 'com.objectstack.plugin-auth.organization-mfa-enrollment-guard',
178+
);
179+
expect(ours.map((r) => [r.event, r.options.object, r.options.priority]).sort()).toEqual([
180+
['beforeInsert', 'sys_organization', ORGANIZATION_MFA_ENROLLMENT_GUARD_PRIORITY],
181+
['beforeUpdate', 'sys_organization', ORGANIZATION_MFA_ENROLLMENT_GUARD_PRIORITY],
182+
]);
183+
const d2 = registered.find(
184+
(r) => r.event === 'beforeUpdate' && r.options?.packageId === 'com.objectstack.plugin-auth.identity-write-guard',
185+
);
186+
expect(ORGANIZATION_MFA_ENROLLMENT_GUARD_PRIORITY).toBeGreaterThan(d2!.options.priority);
187+
188+
const update = ours.find((r) => r.event === 'beforeUpdate')!;
189+
const turnOn = () => update.handler({ object: 'sys_organization', input: { id: 'org_1', data: { require_mfa: true } } });
190+
// The default deployment: two-factor off.
191+
expectRequireMfaRefusal(await turnOn().then(() => null, (e) => e));
192+
// The settings pass turns the plugin on (as `auth.mfa_required` does): the next write is admitted.
193+
(plugin as any).authManager.applyConfigPatch({ plugins: { twoFactor: true } });
194+
await expect(turnOn()).resolves.toBeUndefined();
195+
});
196+
});
197+
144198
// ── 2. On a real engine ─────────────────────────────────────────────────────
145199

146200
const engines: ObjectQL[] = [];

0 commit comments

Comments
 (0)