|
26 | 26 | import { describe, it, expect, afterAll, vi } from 'vitest'; |
27 | 27 | import { ObjectQL } from '@objectstack/objectql'; |
28 | 28 | import { SqlDriver } from '@objectstack/driver-sql'; |
| 29 | +import type { PluginContext } from '@objectstack/core'; |
29 | 30 | import { SysOrganization } from '@objectstack/platform-objects/identity'; |
30 | 31 | import { AuthManager } from './auth-manager'; |
| 32 | +import { AuthPlugin } from './auth-plugin.js'; |
31 | 33 | import { |
32 | 34 | ORGANIZATION_MFA_ENROLLMENT_GUARD_PRIORITY, |
33 | 35 | organizationMfaWithoutEnrollmentRefusal, |
@@ -141,6 +143,58 @@ describe('organization MFA enrollment guard — the rule', () => { |
141 | 143 | }); |
142 | 144 | }); |
143 | 145 |
|
| 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 | + |
144 | 198 | // ── 2. On a real engine ───────────────────────────────────────────────────── |
145 | 199 |
|
146 | 200 | const engines: ObjectQL[] = []; |
|
0 commit comments