|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#34225] With SSO-only mode on, Setup → Users does not offer Set Password |
| 5 | + * (`sys_user.set_user_password`) on a row its door refuses. The door, |
| 6 | + * plugin-auth's `admin/set-user-password`, refuses an IdP-provisioned user |
| 7 | + * (`source == 'idp_provisioned'`) under SSO-only mode unless the platform |
| 8 | + * administrator is setting their own break-glass password |
| 9 | + * (`sso-only-local-password.ts`, `judgeSsoOnlyLocalPassword`). The predicate |
| 10 | + * reads the two facts that door reads: `features.ssoEnforced` (served from the |
| 11 | + * same SSO-only accessor) and the row's `source`. |
| 12 | + * |
| 13 | + * CONTROLs: a local-password row keeps the offer under SSO-only mode, the |
| 14 | + * administrator's own IdP-provisioned row keeps it (break-glass), and with |
| 15 | + * SSO-only mode off, or no flag served, or `source` not projected, every row |
| 16 | + * keeps today's offer. The cloud-managed and platform-admin terms are pinned |
| 17 | + * by `cloud-managed-visibility.test.ts` and `action-state-visibility.test.ts`. |
| 18 | + * |
| 19 | + * The predicate is EVALUATED through the canonical engine the console |
| 20 | + * delegates to (`@objectstack/formula`), on the parsed object. |
| 21 | + */ |
| 22 | + |
| 23 | +import { describe, expect, it } from 'vitest'; |
| 24 | +import { celEngine } from '@objectstack/formula'; |
| 25 | +import { SysUser } from './sys-user.object.js'; |
| 26 | + |
| 27 | +const ADMIN = { id: 'u_admin', email: 'admin@example.com', positions: ['org_owner'], isPlatformAdmin: true }; |
| 28 | + |
| 29 | +/** What a self-hosted kernel serves, SSO-only mode on. */ |
| 30 | +const SSO_ONLY = { admin: true, cloudManaged: false, ssoEnforced: true }; |
| 31 | + |
| 32 | +const IDP_ROW = { id: 'usr_idp', email: 'idp@example.com', source: 'idp_provisioned' }; |
| 33 | + |
| 34 | +function visibleSource(): string { |
| 35 | + const action = ((SysUser.actions ?? []) as Array<Record<string, unknown>>).find((a) => a.name === 'set_user_password'); |
| 36 | + if (!action) throw new Error('sys_user declares no action named "set_user_password"'); |
| 37 | + const raw = action.visible; |
| 38 | + const source = typeof raw === 'string' ? raw : (raw as { source?: unknown } | undefined)?.source; |
| 39 | + if (typeof source !== 'string') throw new Error('set_user_password has no CEL source'); |
| 40 | + return source; |
| 41 | +} |
| 42 | + |
| 43 | +/** The verdict, or the fault spelled out so a faulting predicate fails loudly. */ |
| 44 | +function offered(record: Record<string, unknown>, features: Record<string, unknown>, subject = ADMIN): boolean | string { |
| 45 | + const r = celEngine.evaluate( |
| 46 | + { dialect: 'cel', source: visibleSource() }, |
| 47 | + { record, extra: { current_user: subject, user: subject, ctx: { user: subject }, features } }, |
| 48 | + ); |
| 49 | + if (!r.ok) return `FAULT ${r.error.message.split('\n')[0].trim()}`; |
| 50 | + return typeof r.value === 'boolean' ? r.value : `NON-BOOLEAN ${JSON.stringify(r.value)}`; |
| 51 | +} |
| 52 | + |
| 53 | +describe('Set Password is not offered on a row SSO-only mode refuses', () => { |
| 54 | + it('is hidden on an IdP-provisioned row while SSO-only mode is on', () => { |
| 55 | + expect(offered(IDP_ROW, SSO_ONLY)).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it.each([ |
| 59 | + ['a local-password row (env_native)', { ...IDP_ROW, source: 'env_native' }], |
| 60 | + ['a row with no stored source (null)', { ...IDP_ROW, source: null }], |
| 61 | + ['a pending row (idp_pending), which this rule does not refuse', { ...IDP_ROW, source: 'idp_pending' }], |
| 62 | + ["the administrator's own IdP-provisioned row (break-glass)", { ...IDP_ROW, id: ADMIN.id }], |
| 63 | + ['a row the console did not project source for', { id: 'usr_idp', email: 'idp@example.com' }], |
| 64 | + ])('CONTROL: is offered on %s while SSO-only mode is on', (_row, record) => { |
| 65 | + expect(offered(record, SSO_ONLY)).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + it.each([ |
| 69 | + ['SSO-only mode is off', { ...SSO_ONLY, ssoEnforced: false }], |
| 70 | + ['no ssoEnforced flag is served', { admin: true, cloudManaged: false }], |
| 71 | + ])('CONTROL: every row keeps today\'s offer when %s', (_case, features) => { |
| 72 | + for (const source of ['idp_provisioned', 'env_native', null]) { |
| 73 | + expect(offered({ ...IDP_ROW, source }, features), String(source)).toBe(true); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it('CONTROL: a platform admin is still required, and cloud-managed still hides it', () => { |
| 78 | + const notAdmin = { ...ADMIN, isPlatformAdmin: false }; |
| 79 | + expect(offered({ ...IDP_ROW, source: 'env_native' }, SSO_ONLY, notAdmin)).toBe(false); |
| 80 | + expect(offered({ ...IDP_ROW, source: 'env_native' }, { ...SSO_ONLY, cloudManaged: true })).toBe(false); |
| 81 | + }); |
| 82 | +}); |
0 commit comments