|
| 1 | +/** |
| 2 | + * K10.3 — Access Control & Separation of Duties Tests |
| 3 | + */ |
| 4 | +import { describe, it, expect } from 'vitest'; |
| 5 | +import { |
| 6 | + ROLES, |
| 7 | + SEPARATION_RULES, |
| 8 | + BRANCH_PROTECTIONS, |
| 9 | + AUDIT_SCHEDULES, |
| 10 | + buildAccessControlModel, |
| 11 | + checkSoDViolations, |
| 12 | + validateRoleSoD, |
| 13 | + getEffectivePermissions, |
| 14 | + exportAccessControlMarkdown, |
| 15 | +} from '../governance/access-control.js'; |
| 16 | +import type { Permission, Role } from '../governance/access-control.js'; |
| 17 | +import { SCHEMA_VERSION } from '../types.js'; |
| 18 | + |
| 19 | +describe('K10.3 — Access Control & Separation of Duties', () => { |
| 20 | + describe('Role Definitions', () => { |
| 21 | + it('should define at least 6 roles', () => { |
| 22 | + expect(ROLES.length).toBeGreaterThanOrEqual(6); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should have unique role IDs', () => { |
| 26 | + const ids = ROLES.map(r => r.id); |
| 27 | + expect(new Set(ids).size).toBe(ids.length); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should define sample-creator role', () => { |
| 31 | + const creator = ROLES.find(r => r.id === 'sample-creator'); |
| 32 | + expect(creator).toBeDefined(); |
| 33 | + expect(creator!.permissions).toContain('create_sample'); |
| 34 | + expect(creator!.permissions).not.toContain('label_sample'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should define corpus-curator role', () => { |
| 38 | + const curator = ROLES.find(r => r.id === 'corpus-curator'); |
| 39 | + expect(curator).toBeDefined(); |
| 40 | + expect(curator!.permissions).toContain('label_sample'); |
| 41 | + expect(curator!.permissions).not.toContain('create_sample'); |
| 42 | + expect(curator!.permissions).not.toContain('run_validation'); |
| 43 | + expect(curator!.permissions).not.toContain('approve_report'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should define validation-operator role', () => { |
| 47 | + const operator = ROLES.find(r => r.id === 'validation-operator'); |
| 48 | + expect(operator).toBeDefined(); |
| 49 | + expect(operator!.permissions).toContain('run_validation'); |
| 50 | + expect(operator!.permissions).not.toContain('create_sample'); |
| 51 | + expect(operator!.permissions).not.toContain('approve_report'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should define report-reviewer role', () => { |
| 55 | + const reviewer = ROLES.find(r => r.id === 'report-reviewer'); |
| 56 | + expect(reviewer).toBeDefined(); |
| 57 | + expect(reviewer!.permissions).toContain('approve_report'); |
| 58 | + expect(reviewer!.permissions).not.toContain('run_validation'); |
| 59 | + expect(reviewer!.permissions).not.toContain('create_sample'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should define key-custodian with only manage_keys', () => { |
| 63 | + const custodian = ROLES.find(r => r.id === 'key-custodian'); |
| 64 | + expect(custodian).toBeDefined(); |
| 65 | + expect(custodian!.permissions).toEqual(['manage_keys']); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('Separation of Duties Rules', () => { |
| 70 | + it('should define at least 5 SoD rules', () => { |
| 71 | + expect(SEPARATION_RULES.length).toBeGreaterThanOrEqual(5); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should have unique rule IDs', () => { |
| 75 | + const ids = SEPARATION_RULES.map(r => r.id); |
| 76 | + expect(new Set(ids).size).toBe(ids.length); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should prohibit create_sample + label_sample', () => { |
| 80 | + const rule = SEPARATION_RULES.find(r => |
| 81 | + r.prohibited_combination.includes('create_sample') && |
| 82 | + r.prohibited_combination.includes('label_sample') |
| 83 | + ); |
| 84 | + expect(rule).toBeDefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should prohibit run_validation + approve_report', () => { |
| 88 | + const rule = SEPARATION_RULES.find(r => |
| 89 | + r.prohibited_combination.includes('run_validation') && |
| 90 | + r.prohibited_combination.includes('approve_report') |
| 91 | + ); |
| 92 | + expect(rule).toBeDefined(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should prohibit manage_keys + run_validation', () => { |
| 96 | + const rule = SEPARATION_RULES.find(r => |
| 97 | + r.prohibited_combination.includes('manage_keys') && |
| 98 | + r.prohibited_combination.includes('run_validation') |
| 99 | + ); |
| 100 | + expect(rule).toBeDefined(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('checkSoDViolations', () => { |
| 105 | + it('should return no violations for single permission', () => { |
| 106 | + const violations = checkSoDViolations(['run_validation']); |
| 107 | + expect(violations).toEqual([]); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should detect create_sample + label_sample violation', () => { |
| 111 | + const violations = checkSoDViolations(['create_sample', 'label_sample']); |
| 112 | + expect(violations.length).toBeGreaterThanOrEqual(1); |
| 113 | + expect(violations[0].rule_id).toBe('SOD-01'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should detect run_validation + approve_report violation', () => { |
| 117 | + const violations = checkSoDViolations(['run_validation', 'approve_report']); |
| 118 | + expect(violations.length).toBeGreaterThanOrEqual(1); |
| 119 | + const found = violations.find(v => v.rule_id === 'SOD-03'); |
| 120 | + expect(found).toBeDefined(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should detect the full pipeline violation', () => { |
| 124 | + const violations = checkSoDViolations([ |
| 125 | + 'create_sample', 'label_sample', 'run_validation', 'approve_report', |
| 126 | + ]); |
| 127 | + const fullPipeline = violations.find(v => v.rule_id === 'SOD-05'); |
| 128 | + expect(fullPipeline).toBeDefined(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should return no violations for non-overlapping permissions', () => { |
| 132 | + const violations = checkSoDViolations(['run_validation', 'audit_corpus']); |
| 133 | + expect(violations).toEqual([]); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('validateRoleSoD', () => { |
| 138 | + it('should validate default roles have no SoD violations', () => { |
| 139 | + const violations = validateRoleSoD(ROLES); |
| 140 | + expect(violations).toEqual([]); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should detect violations in poorly designed roles', () => { |
| 144 | + const badRoles: Role[] = [{ |
| 145 | + id: 'superuser', |
| 146 | + name: 'Superuser', |
| 147 | + description: 'Has all permissions', |
| 148 | + permissions: ['create_sample', 'label_sample', 'run_validation', 'approve_report', 'manage_keys'], |
| 149 | + }]; |
| 150 | + const violations = validateRoleSoD(badRoles); |
| 151 | + expect(violations.length).toBeGreaterThan(0); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + describe('getEffectivePermissions', () => { |
| 156 | + it('should return empty for unknown role IDs', () => { |
| 157 | + const perms = getEffectivePermissions(['nonexistent']); |
| 158 | + expect(perms).toEqual([]); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should return single role permissions', () => { |
| 162 | + const perms = getEffectivePermissions(['corpus-curator']); |
| 163 | + expect(perms).toContain('label_sample'); |
| 164 | + expect(perms).toContain('review_label'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should merge permissions from multiple roles', () => { |
| 168 | + const perms = getEffectivePermissions(['corpus-curator', 'validation-operator']); |
| 169 | + expect(perms).toContain('label_sample'); |
| 170 | + expect(perms).toContain('run_validation'); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should deduplicate permissions', () => { |
| 174 | + const perms = getEffectivePermissions(['corpus-curator', 'corpus-curator']); |
| 175 | + const uniquePerms = new Set(perms); |
| 176 | + expect(uniquePerms.size).toBe(perms.length); |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + describe('Branch Protections', () => { |
| 181 | + it('should protect ground-truth path', () => { |
| 182 | + const gt = BRANCH_PROTECTIONS.find(bp => bp.path_pattern.includes('ground-truth')); |
| 183 | + expect(gt).toBeDefined(); |
| 184 | + expect(gt!.min_approvals).toBeGreaterThanOrEqual(2); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should protect reference-sets path', () => { |
| 188 | + const rs = BRANCH_PROTECTIONS.find(bp => bp.path_pattern.includes('reference-sets')); |
| 189 | + expect(rs).toBeDefined(); |
| 190 | + expect(rs!.min_approvals).toBeGreaterThanOrEqual(2); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should protect holdout path', () => { |
| 194 | + const ho = BRANCH_PROTECTIONS.find(bp => bp.path_pattern.includes('holdout')); |
| 195 | + expect(ho).toBeDefined(); |
| 196 | + expect(ho!.min_approvals).toBeGreaterThanOrEqual(2); |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + describe('Audit Schedules', () => { |
| 201 | + it('should include quarterly corpus label audit', () => { |
| 202 | + const audit = AUDIT_SCHEDULES.find(a => a.frequency === 'quarterly' && a.name.includes('Label')); |
| 203 | + expect(audit).toBeDefined(); |
| 204 | + expect(audit!.sample_size).toBe(50); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should include annual key rotation', () => { |
| 208 | + const audit = AUDIT_SCHEDULES.find(a => a.frequency === 'annual' && a.name.includes('Key')); |
| 209 | + expect(audit).toBeDefined(); |
| 210 | + }); |
| 211 | + }); |
| 212 | + |
| 213 | + describe('buildAccessControlModel', () => { |
| 214 | + it('should produce a valid model', () => { |
| 215 | + const model = buildAccessControlModel(); |
| 216 | + expect(model.schema_version).toBe(SCHEMA_VERSION); |
| 217 | + expect(model.document_id).toBe('KATANA-AC-001'); |
| 218 | + expect(model.roles.length).toBeGreaterThanOrEqual(5); |
| 219 | + expect(model.separation_rules.length).toBeGreaterThanOrEqual(5); |
| 220 | + }); |
| 221 | + }); |
| 222 | + |
| 223 | + describe('exportAccessControlMarkdown', () => { |
| 224 | + it('should produce markdown with all sections', () => { |
| 225 | + const model = buildAccessControlModel(); |
| 226 | + const md = exportAccessControlMarkdown(model); |
| 227 | + expect(md).toContain('# KATANA Access Control'); |
| 228 | + expect(md).toContain('## Roles'); |
| 229 | + expect(md).toContain('## Separation of Duties'); |
| 230 | + expect(md).toContain('## Branch Protections'); |
| 231 | + expect(md).toContain('## Audit Schedules'); |
| 232 | + expect(md).toContain('ISO 17025 Clause:**'); |
| 233 | + }); |
| 234 | + }); |
| 235 | +}); |
0 commit comments