|
2 | 2 | * Unit tests for PermissionManager |
3 | 3 | */ |
4 | 4 |
|
5 | | -import { describe, it, expect, beforeEach } from 'vitest'; |
6 | | -import { PermissionManager } from '@/features/permissions/manager/PermissionManager.js'; |
| 5 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 6 | +import { PermissionManager } from '@codedir/mimir-agents'; |
| 7 | +import type { |
| 8 | + PermissionManagerConfig, |
| 9 | + PermissionRequest, |
| 10 | + IAuditLogger, |
| 11 | + AuditLogEntry, |
| 12 | +} from '@codedir/mimir-agents'; |
7 | 13 |
|
8 | 14 | describe('PermissionManager', () => { |
9 | 15 | let manager: PermissionManager; |
| 16 | + let auditLog: AuditLogEntry[]; |
| 17 | + let mockAuditLogger: IAuditLogger; |
10 | 18 |
|
11 | 19 | beforeEach(() => { |
12 | | - manager = new PermissionManager(); |
| 20 | + auditLog = []; |
| 21 | + mockAuditLogger = { |
| 22 | + log: vi.fn(async (entry: AuditLogEntry) => { |
| 23 | + auditLog.push(entry); |
| 24 | + }), |
| 25 | + }; |
| 26 | + |
| 27 | + const config: PermissionManagerConfig = { |
| 28 | + allowlist: [], |
| 29 | + blocklist: [], |
| 30 | + acceptRiskLevel: 'low', |
| 31 | + autoAccept: true, |
| 32 | + auditLogger: mockAuditLogger, |
| 33 | + }; |
| 34 | + manager = new PermissionManager(config); |
13 | 35 | }); |
14 | 36 |
|
15 | 37 | it('should allow low risk commands by default', async () => { |
16 | | - const result = await manager.checkPermission('ls -la'); |
| 38 | + const request: PermissionRequest = { type: 'bash', command: 'ls -la' }; |
| 39 | + const result = await manager.checkPermission(request); |
17 | 40 | expect(result.allowed).toBe(true); |
18 | 41 | }); |
19 | 42 |
|
20 | 43 | it('should deny high risk commands by default', async () => { |
21 | | - const result = await manager.checkPermission('rm -rf /'); |
| 44 | + const request: PermissionRequest = { type: 'bash', command: 'rm -rf /' }; |
| 45 | + const result = await manager.checkPermission(request); |
22 | 46 | expect(result.allowed).toBe(false); |
23 | 47 | }); |
24 | 48 |
|
25 | 49 | it('should respect allowlist', async () => { |
26 | | - manager.addToAllowlist('rm -rf node_modules'); |
27 | | - const result = await manager.checkPermission('rm -rf node_modules'); |
| 50 | + const config: PermissionManagerConfig = { |
| 51 | + allowlist: ['rm -rf node_modules'], |
| 52 | + blocklist: [], |
| 53 | + acceptRiskLevel: 'low', |
| 54 | + autoAccept: true, |
| 55 | + auditLogger: mockAuditLogger, |
| 56 | + }; |
| 57 | + const managerWithAllowlist = new PermissionManager(config); |
| 58 | + |
| 59 | + const request: PermissionRequest = { type: 'bash', command: 'rm -rf node_modules' }; |
| 60 | + const result = await managerWithAllowlist.checkPermission(request); |
28 | 61 | expect(result.allowed).toBe(true); |
29 | 62 | }); |
30 | 63 |
|
31 | 64 | it('should respect blocklist', async () => { |
32 | | - manager.addToBlocklist('git push'); |
33 | | - const result = await manager.checkPermission('git push'); |
| 65 | + const config: PermissionManagerConfig = { |
| 66 | + allowlist: [], |
| 67 | + blocklist: ['git push'], |
| 68 | + acceptRiskLevel: 'high', // Allow high risk, but blocklist takes priority |
| 69 | + autoAccept: true, |
| 70 | + auditLogger: mockAuditLogger, |
| 71 | + }; |
| 72 | + const managerWithBlocklist = new PermissionManager(config); |
| 73 | + |
| 74 | + const request: PermissionRequest = { type: 'bash', command: 'git push' }; |
| 75 | + const result = await managerWithBlocklist.checkPermission(request); |
34 | 76 | expect(result.allowed).toBe(false); |
35 | 77 | }); |
36 | 78 |
|
37 | 79 | it('should maintain audit log', async () => { |
38 | | - await manager.checkPermission('ls -la'); |
39 | | - await manager.checkPermission('rm -rf /'); |
| 80 | + const request1: PermissionRequest = { type: 'bash', command: 'ls -la' }; |
| 81 | + const request2: PermissionRequest = { type: 'bash', command: 'rm -rf /' }; |
| 82 | + |
| 83 | + await manager.checkPermission(request1); |
| 84 | + await manager.checkPermission(request2); |
| 85 | + |
| 86 | + expect(auditLog).toHaveLength(2); |
| 87 | + expect(auditLog[0]?.operation).toBe('ls -la'); |
| 88 | + expect(auditLog[1]?.operation).toBe('rm -rf /'); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('risk level acceptance', () => { |
| 92 | + it('should auto-accept commands at or below accept risk level', async () => { |
| 93 | + const config: PermissionManagerConfig = { |
| 94 | + allowlist: [], |
| 95 | + blocklist: [], |
| 96 | + acceptRiskLevel: 'medium', |
| 97 | + autoAccept: true, |
| 98 | + }; |
| 99 | + const managerMedium = new PermissionManager(config); |
| 100 | + |
| 101 | + // Low risk - should be accepted |
| 102 | + const lowRisk: PermissionRequest = { type: 'bash', command: 'ls -la' }; |
| 103 | + const lowResult = await managerMedium.checkPermission(lowRisk); |
| 104 | + expect(lowResult.allowed).toBe(true); |
| 105 | + |
| 106 | + // Medium risk (npm install) - should be accepted |
| 107 | + const mediumRisk: PermissionRequest = { type: 'bash', command: 'npm install express' }; |
| 108 | + const mediumResult = await managerMedium.checkPermission(mediumRisk); |
| 109 | + expect(mediumResult.allowed).toBe(true); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should deny commands above accept risk level', async () => { |
| 113 | + const config: PermissionManagerConfig = { |
| 114 | + allowlist: [], |
| 115 | + blocklist: [], |
| 116 | + acceptRiskLevel: 'low', |
| 117 | + autoAccept: true, |
| 118 | + }; |
| 119 | + const managerLow = new PermissionManager(config); |
| 120 | + |
| 121 | + // High risk - should be denied |
| 122 | + const highRisk: PermissionRequest = { type: 'bash', command: 'rm -rf ./node_modules' }; |
| 123 | + const highResult = await managerLow.checkPermission(highRisk); |
| 124 | + expect(highResult.allowed).toBe(false); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should deny all when autoAccept is false', async () => { |
| 128 | + const config: PermissionManagerConfig = { |
| 129 | + allowlist: [], |
| 130 | + blocklist: [], |
| 131 | + acceptRiskLevel: 'critical', // Even with high accept level |
| 132 | + autoAccept: false, // Auto-accept disabled |
| 133 | + }; |
| 134 | + const managerNoAuto = new PermissionManager(config); |
| 135 | + |
| 136 | + const lowRisk: PermissionRequest = { type: 'bash', command: 'ls -la' }; |
| 137 | + const result = await managerNoAuto.checkPermission(lowRisk); |
| 138 | + expect(result.allowed).toBe(false); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + describe('file operations', () => { |
| 143 | + it('should handle file read requests', async () => { |
| 144 | + const request: PermissionRequest = { type: 'file_read', path: '/etc/passwd' }; |
| 145 | + const result = await manager.checkPermission(request); |
| 146 | + // Low risk file read with low accept level should be allowed |
| 147 | + expect(result.riskLevel).toBeDefined(); |
| 148 | + }); |
40 | 149 |
|
41 | | - const log = manager.getAuditLog(); |
42 | | - expect(log).toHaveLength(2); |
43 | | - expect(log[0]?.command).toBe('ls -la'); |
44 | | - expect(log[1]?.command).toBe('rm -rf /'); |
| 150 | + it('should handle file write requests', async () => { |
| 151 | + const request: PermissionRequest = { type: 'file_write', path: './output.txt' }; |
| 152 | + const result = await manager.checkPermission(request); |
| 153 | + expect(result.riskLevel).toBeDefined(); |
| 154 | + }); |
45 | 155 | }); |
46 | 156 | }); |
0 commit comments