|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { validateNavAccess, NAV_OBJECT_UNGRANTED } from './validate-nav-access.js'; |
| 5 | + |
| 6 | +const objects = [ |
| 7 | + { name: 'crm_lead', fields: { name: { type: 'text' } } }, |
| 8 | + { name: 'crm_forecast', fields: { name: { type: 'text' } } }, |
| 9 | +]; |
| 10 | + |
| 11 | +const navApp = (items: unknown[]) => [{ name: 'crm', label: 'CRM', navigation: items }]; |
| 12 | + |
| 13 | +const objectNav = (id: string, objectName: string) => ({ |
| 14 | + id, |
| 15 | + type: 'object', |
| 16 | + label: objectName, |
| 17 | + objectName, |
| 18 | +}); |
| 19 | + |
| 20 | +describe('validateNavAccess', () => { |
| 21 | + // The HotCRM instance: an object in the menu that no set grants. |
| 22 | + it('warns on a nav-exposed object no permission set grants read on', () => { |
| 23 | + const findings = validateNavAccess({ |
| 24 | + objects, |
| 25 | + apps: navApp([objectNav('nav_leads', 'crm_lead'), objectNav('nav_forecast', 'crm_forecast')]), |
| 26 | + permissions: [ |
| 27 | + { name: 'crm_user', label: 'CRM User', objects: { crm_lead: { allowRead: true } } }, |
| 28 | + ], |
| 29 | + }); |
| 30 | + expect(findings).toHaveLength(1); |
| 31 | + expect(findings[0].severity).toBe('warning'); |
| 32 | + expect(findings[0].rule).toBe(NAV_OBJECT_UNGRANTED); |
| 33 | + expect(findings[0].path).toBe('apps[0].navigation[1].objectName'); |
| 34 | + expect(findings[0].message).toContain('crm_forecast'); |
| 35 | + }); |
| 36 | + |
| 37 | + // The platform's own `admin_full_access` uses this shape; a stack may too. |
| 38 | + // Without wildcard support the matrix records the literal key `*` and the |
| 39 | + // rule would fire on exactly the stacks that granted the most. |
| 40 | + it('accepts a wildcard read grant as covering every object', () => { |
| 41 | + const findings = validateNavAccess({ |
| 42 | + objects, |
| 43 | + apps: navApp([objectNav('nav_forecast', 'crm_forecast')]), |
| 44 | + permissions: [ |
| 45 | + { name: 'full', label: 'Full', objects: { '*': { allowRead: true, viewAllRecords: true } } }, |
| 46 | + ], |
| 47 | + }); |
| 48 | + expect(findings).toEqual([]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('accepts read granted through viewAllRecords or modifyAllRecords', () => { |
| 52 | + for (const bit of ['viewAllRecords', 'modifyAllRecords']) { |
| 53 | + const findings = validateNavAccess({ |
| 54 | + objects, |
| 55 | + apps: navApp([objectNav('nav_forecast', 'crm_forecast')]), |
| 56 | + permissions: [ |
| 57 | + { name: 'admin', label: 'Admin', objects: { crm_forecast: { [bit]: true } } }, |
| 58 | + ], |
| 59 | + }); |
| 60 | + expect(findings, `${bit} should grant read`).toEqual([]); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + it('does not treat a write-only grant as read', () => { |
| 65 | + const findings = validateNavAccess({ |
| 66 | + objects, |
| 67 | + apps: navApp([objectNav('nav_forecast', 'crm_forecast')]), |
| 68 | + permissions: [ |
| 69 | + { name: 'writer', label: 'Writer', objects: { crm_forecast: { allowCreate: true, allowEdit: true } } }, |
| 70 | + ], |
| 71 | + }); |
| 72 | + expect(findings).toHaveLength(1); |
| 73 | + }); |
| 74 | + |
| 75 | + it('reports an object once even when several nav entries expose it', () => { |
| 76 | + const findings = validateNavAccess({ |
| 77 | + objects, |
| 78 | + apps: [ |
| 79 | + { |
| 80 | + name: 'crm', |
| 81 | + navigation: [objectNav('nav_a', 'crm_forecast')], |
| 82 | + areas: [{ id: 'area', label: 'Area', navigation: [objectNav('nav_b', 'crm_forecast')] }], |
| 83 | + }, |
| 84 | + ], |
| 85 | + permissions: [{ name: 'p', label: 'P', objects: { crm_lead: { allowRead: true } } }], |
| 86 | + }); |
| 87 | + expect(findings).toHaveLength(1); |
| 88 | + }); |
| 89 | + |
| 90 | + it('walks area navigation and nested children', () => { |
| 91 | + const findings = validateNavAccess({ |
| 92 | + objects, |
| 93 | + apps: [ |
| 94 | + { |
| 95 | + name: 'crm', |
| 96 | + areas: [ |
| 97 | + { |
| 98 | + id: 'area_sales', |
| 99 | + label: 'Sales', |
| 100 | + navigation: [ |
| 101 | + { |
| 102 | + id: 'grp', |
| 103 | + type: 'group', |
| 104 | + label: 'Group', |
| 105 | + children: [objectNav('nav_forecast', 'crm_forecast')], |
| 106 | + }, |
| 107 | + ], |
| 108 | + }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + ], |
| 112 | + permissions: [{ name: 'p', label: 'P', objects: { crm_lead: { allowRead: true } } }], |
| 113 | + }); |
| 114 | + expect(findings).toHaveLength(1); |
| 115 | + expect(findings[0].path).toBe('apps[0].areas[0].navigation[0].children[0].objectName'); |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +describe('validateNavAccess — exemptions (false-positive floor)', () => { |
| 120 | + it('skips platform-provided objects — their own packages grant them', () => { |
| 121 | + const findings = validateNavAccess({ |
| 122 | + objects, |
| 123 | + apps: navApp([ |
| 124 | + objectNav('nav_users', 'sys_user'), |
| 125 | + objectNav('nav_approvals', 'sys_approval_request'), |
| 126 | + ]), |
| 127 | + permissions: [{ name: 'p', label: 'P', objects: { crm_lead: { allowRead: true } } }], |
| 128 | + }); |
| 129 | + expect(findings).toEqual([]); |
| 130 | + }); |
| 131 | + |
| 132 | + it('skips a stack that declares no permission sets at all', () => { |
| 133 | + const findings = validateNavAccess({ |
| 134 | + objects, |
| 135 | + apps: navApp([objectNav('nav_forecast', 'crm_forecast')]), |
| 136 | + }); |
| 137 | + expect(findings).toEqual([]); |
| 138 | + }); |
| 139 | + |
| 140 | + it('skips an object this stack does not define', () => { |
| 141 | + // A dangling nav target is `validate-object-references`' finding, not this |
| 142 | + // rule's — reporting it here would double-report one mistake. |
| 143 | + const findings = validateNavAccess({ |
| 144 | + objects, |
| 145 | + apps: navApp([objectNav('nav_ghost', 'crm_ghost')]), |
| 146 | + permissions: [{ name: 'p', label: 'P', objects: { crm_lead: { allowRead: true } } }], |
| 147 | + }); |
| 148 | + expect(findings).toEqual([]); |
| 149 | + }); |
| 150 | + |
| 151 | + it('ignores non-object nav entries', () => { |
| 152 | + const findings = validateNavAccess({ |
| 153 | + objects, |
| 154 | + apps: navApp([ |
| 155 | + { id: 'nav_dash', type: 'dashboard', label: 'D', dashboardName: 'd' }, |
| 156 | + { id: 'nav_url', type: 'url', label: 'U', url: '/x' }, |
| 157 | + { id: 'nav_sep', type: 'separator' }, |
| 158 | + ]), |
| 159 | + permissions: [{ name: 'p', label: 'P', objects: { crm_lead: { allowRead: true } } }], |
| 160 | + }); |
| 161 | + expect(findings).toEqual([]); |
| 162 | + }); |
| 163 | + |
| 164 | + it('is silent when every exposed object is granted, and tolerates empty input', () => { |
| 165 | + const findings = validateNavAccess({ |
| 166 | + objects, |
| 167 | + apps: navApp([objectNav('nav_leads', 'crm_lead'), objectNav('nav_forecast', 'crm_forecast')]), |
| 168 | + permissions: [ |
| 169 | + { |
| 170 | + name: 'p', |
| 171 | + label: 'P', |
| 172 | + objects: { crm_lead: { allowRead: true }, crm_forecast: { allowRead: true } }, |
| 173 | + }, |
| 174 | + ], |
| 175 | + }); |
| 176 | + expect(findings).toEqual([]); |
| 177 | + expect(validateNavAccess({})).toEqual([]); |
| 178 | + expect(validateNavAccess(null as unknown as Record<string, unknown>)).toEqual([]); |
| 179 | + }); |
| 180 | + |
| 181 | + it('accepts a grant coming from any one of several sets', () => { |
| 182 | + const findings = validateNavAccess({ |
| 183 | + objects, |
| 184 | + apps: navApp([objectNav('nav_forecast', 'crm_forecast')]), |
| 185 | + permissions: [ |
| 186 | + { name: 'a', label: 'A', objects: { crm_lead: { allowRead: true } } }, |
| 187 | + { name: 'b', label: 'B', objects: { crm_forecast: { allowRead: true } } }, |
| 188 | + ], |
| 189 | + }); |
| 190 | + expect(findings).toEqual([]); |
| 191 | + }); |
| 192 | +}); |
0 commit comments