|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Setup's Users and API Keys offer an action only in the record state it |
| 5 | + * changes: Ban on a user who is not banned and Unban on one who is, Revoke on |
| 6 | + * a live key and Restore on a revoked one, Unlock on an account that can hold |
| 7 | + * a lock. Each pair used to judge the caller's standing alone, so both halves |
| 8 | + * were offered on every row. |
| 9 | + * |
| 10 | + * The predicates are EVALUATED through the canonical engine the console |
| 11 | + * delegates to (`@objectstack/formula`), on the parsed object, so the stored |
| 12 | + * envelope with its `requiresFeature` lowering is what is judged. A string |
| 13 | + * match would stay green on a predicate that faults or never answers true. |
| 14 | + * The bindings are the rows a list or record header can present: the field |
| 15 | + * projected with each value, projected as null, and not projected at all. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, expect, it } from 'vitest'; |
| 19 | +import { celEngine } from '@objectstack/formula'; |
| 20 | +import { SysUser } from './sys-user.object.js'; |
| 21 | +import { SysApiKey } from './sys-api-key.object.js'; |
| 22 | + |
| 23 | +type Principal = { id: string; email: string; positions: string[]; isPlatformAdmin: boolean }; |
| 24 | + |
| 25 | +const ADMIN: Principal = { id: 'u_admin', email: 'admin@example.com', positions: [], isPlatformAdmin: true }; |
| 26 | +const MEMBER: Principal = { id: 'u_member', email: 'member@example.com', positions: ['org_owner'], isPlatformAdmin: false }; |
| 27 | +const FEATURES = { admin: true, apiKey: true }; |
| 28 | + |
| 29 | +function visibleOf(def: { actions?: unknown }, name: string): string { |
| 30 | + const action = ((def.actions ?? []) as Array<Record<string, unknown>>).find((a) => a.name === name); |
| 31 | + if (!action) throw new Error(`no action ${name}`); |
| 32 | + const raw = action.visible; |
| 33 | + const source = typeof raw === 'string' ? raw : (raw as { source?: unknown } | undefined)?.source; |
| 34 | + if (typeof source !== 'string') throw new Error(`action ${name} has no CEL source`); |
| 35 | + return source; |
| 36 | +} |
| 37 | + |
| 38 | +/** The verdict, or the fault spelled out so a faulting predicate fails loudly. */ |
| 39 | +function offered(source: string, record: Record<string, unknown>, user: Principal = ADMIN): boolean | string { |
| 40 | + const r = celEngine.evaluate( |
| 41 | + { dialect: 'cel', source }, |
| 42 | + { record, extra: { current_user: user, user, ctx: { user }, features: FEATURES } }, |
| 43 | + ); |
| 44 | + if (!r.ok) return `FAULT ${r.error.message.split('\n')[0].trim()}`; |
| 45 | + return typeof r.value === 'boolean' ? r.value : `NON-BOOLEAN ${JSON.stringify(r.value)}`; |
| 46 | +} |
| 47 | + |
| 48 | +describe('sys_user Ban / Unban switch with `banned`', () => { |
| 49 | + const ban = visibleOf(SysUser, 'ban_user'); |
| 50 | + const unban = visibleOf(SysUser, 'unban_user'); |
| 51 | + |
| 52 | + it('a user who is not banned is offered Ban and not Unban', () => { |
| 53 | + for (const banned of [false, null]) { |
| 54 | + expect(offered(ban, { banned }), `banned: ${banned}`).toBe(true); |
| 55 | + expect(offered(unban, { banned }), `banned: ${banned}`).toBe(false); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + it('a banned user is offered Unban and not Ban, a timed ban past its expiry included', () => { |
| 60 | + // better-auth clears `banned` only at the user's next sign-in, so a row |
| 61 | + // past `ban_expires` still says banned and Unban must stay offered. |
| 62 | + for (const row of [{ banned: true }, { banned: true, ban_expires: '2020-01-01T00:00:00.000Z' }]) { |
| 63 | + expect(offered(unban, row), JSON.stringify(row)).toBe(true); |
| 64 | + expect(offered(ban, row), JSON.stringify(row)).toBe(false); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + it('a row that does not carry `banned` offers neither, and never both', () => { |
| 69 | + expect(offered(ban, {})).toBe(false); |
| 70 | + expect(offered(unban, {})).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + it('the standing leg stays: a caller without it is offered neither on any row', () => { |
| 74 | + for (const banned of [true, false, null]) { |
| 75 | + expect(offered(ban, { banned }, MEMBER)).toBe(false); |
| 76 | + expect(offered(unban, { banned }, MEMBER)).toBe(false); |
| 77 | + } |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe('sys_user Unlock is offered on an account that can hold a lock', () => { |
| 82 | + const unlock = visibleOf(SysUser, 'unlock_user'); |
| 83 | + |
| 84 | + it('an account with no lock stamp and no second factor is not offered Unlock', () => { |
| 85 | + expect(offered(unlock, { locked_until: null, two_factor_enabled: false })).toBe(false); |
| 86 | + expect(offered(unlock, { locked_until: null, two_factor_enabled: null })).toBe(false); |
| 87 | + }); |
| 88 | + |
| 89 | + it('a password-stage lock is offered Unlock, an expired stamp included', () => { |
| 90 | + // An expired lock leaves `failed_login_count` at the threshold, so the |
| 91 | + // next failure re-locks; Unlock is what resets that counter. |
| 92 | + for (const locked_until of ['2999-01-01T00:00:00.000Z', '2020-01-01T00:00:00.000Z']) { |
| 93 | + expect(offered(unlock, { locked_until, two_factor_enabled: false }), locked_until).toBe(true); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + it('a two-factor user is offered Unlock: a second-factor lock lives on `sys_two_factor`, not this row', () => { |
| 98 | + expect(offered(unlock, { locked_until: null, two_factor_enabled: true })).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + it('a row that carries neither field is not offered Unlock, and the standing leg stays', () => { |
| 102 | + expect(offered(unlock, {})).toBe(false); |
| 103 | + expect(offered(unlock, { locked_until: '2999-01-01T00:00:00.000Z' }, MEMBER)).toBe(false); |
| 104 | + expect(offered(unlock, { two_factor_enabled: true }, MEMBER)).toBe(false); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('sys_api_key Revoke / Restore switch with `revoked`', () => { |
| 109 | + const revoke = visibleOf(SysApiKey, 'revoke_api_key'); |
| 110 | + const restore = visibleOf(SysApiKey, 'restore_api_key'); |
| 111 | + |
| 112 | + it('a live key is offered Revoke and not Restore', () => { |
| 113 | + for (const revoked of [false, null]) { |
| 114 | + expect(offered(revoke, { revoked }), `revoked: ${revoked}`).toBe(true); |
| 115 | + expect(offered(restore, { revoked }), `revoked: ${revoked}`).toBe(false); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + it('a revoked key is offered Restore and not Revoke, whatever the reason', () => { |
| 120 | + // Who may restore a key a forced password reset revoked is the server's |
| 121 | + // (plugin-auth `api-key-restore-guard.ts`), not this predicate's. |
| 122 | + for (const row of [{ revoked: true }, { revoked: true, revoked_reason: 'admin_password_reset' }]) { |
| 123 | + expect(offered(restore, row), JSON.stringify(row)).toBe(true); |
| 124 | + expect(offered(revoke, row), JSON.stringify(row)).toBe(false); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + it('a row that does not carry `revoked` offers neither, and never both', () => { |
| 129 | + expect(offered(revoke, {})).toBe(false); |
| 130 | + expect(offered(restore, {})).toBe(false); |
| 131 | + }); |
| 132 | +}); |
0 commit comments