|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { SensitiveOperationsService } from './sensitive-operations.service'; |
| 3 | +import { AuditLogService } from '../audit-log.service'; |
| 4 | +import { AuditAction, AuditCategory, AuditSeverity } from '../enums/audit-action.enum'; |
| 5 | + |
| 6 | +describe('SensitiveOperationsService', () => { |
| 7 | + let service: SensitiveOperationsService; |
| 8 | + let auditLogService: { log: jest.Mock }; |
| 9 | + |
| 10 | + beforeEach(async () => { |
| 11 | + const module: TestingModule = await Test.createTestingModule({ |
| 12 | + providers: [ |
| 13 | + SensitiveOperationsService, |
| 14 | + { provide: AuditLogService, useValue: { log: jest.fn().mockResolvedValue(undefined) } }, |
| 15 | + ], |
| 16 | + }).compile(); |
| 17 | + |
| 18 | + service = module.get<SensitiveOperationsService>(SensitiveOperationsService); |
| 19 | + auditLogService = module.get(AuditLogService); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('logSensitiveOperation', () => { |
| 23 | + const baseOperation = { |
| 24 | + userId: 'u1', |
| 25 | + userEmail: 'u1@example.com', |
| 26 | + action: AuditAction.USER_DELETED, |
| 27 | + entityType: 'User', |
| 28 | + entityId: 'u2', |
| 29 | + description: 'deleted', |
| 30 | + ipAddress: '127.0.0.1', |
| 31 | + userAgent: 'jest', |
| 32 | + }; |
| 33 | + |
| 34 | + it('logs with WARNING severity, a resolved category, and the sensitive-operation flag', async () => { |
| 35 | + await service.logSensitiveOperation(baseOperation); |
| 36 | + |
| 37 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 38 | + expect.objectContaining({ |
| 39 | + userId: 'u1', |
| 40 | + action: AuditAction.USER_DELETED, |
| 41 | + category: AuditCategory.AUTHORIZATION, |
| 42 | + severity: AuditSeverity.WARNING, |
| 43 | + metadata: expect.objectContaining({ isSensitiveOperation: true }), |
| 44 | + }), |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it('falls back to the SYSTEM category for an action with no explicit mapping', async () => { |
| 49 | + await service.logSensitiveOperation({ |
| 50 | + ...baseOperation, |
| 51 | + action: 'UNMAPPED_ACTION' as AuditAction, |
| 52 | + }); |
| 53 | + |
| 54 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 55 | + expect.objectContaining({ category: AuditCategory.SYSTEM }), |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it('logs the error and rethrows when the underlying audit log write fails', async () => { |
| 60 | + const error = new Error('write failed'); |
| 61 | + auditLogService.log.mockRejectedValue(error); |
| 62 | + |
| 63 | + await expect(service.logSensitiveOperation(baseOperation)).rejects.toThrow(error); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('logUserDeletion', () => { |
| 68 | + it('delegates to logSensitiveOperation with a USER_DELETED entry', async () => { |
| 69 | + await service.logUserDeletion('u1', 'admin@x.com', 'u2', 'victim@x.com', '1.2.3.4', 'ua'); |
| 70 | + |
| 71 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 72 | + expect.objectContaining({ |
| 73 | + action: AuditAction.USER_DELETED, |
| 74 | + entityType: 'User', |
| 75 | + entityId: 'u2', |
| 76 | + description: expect.stringContaining('victim@x.com'), |
| 77 | + }), |
| 78 | + ); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('logRoleChange', () => { |
| 83 | + it('records the old and new role in oldValues/newValues', async () => { |
| 84 | + await service.logRoleChange( |
| 85 | + 'u1', |
| 86 | + 'admin@x.com', |
| 87 | + 'u2', |
| 88 | + 'target@x.com', |
| 89 | + 'student', |
| 90 | + 'instructor', |
| 91 | + '1.2.3.4', |
| 92 | + 'ua', |
| 93 | + ); |
| 94 | + |
| 95 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 96 | + expect.objectContaining({ |
| 97 | + action: AuditAction.USER_ROLE_CHANGED, |
| 98 | + oldValues: { role: 'student' }, |
| 99 | + newValues: { role: 'instructor' }, |
| 100 | + }), |
| 101 | + ); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('logPasswordChange', () => { |
| 106 | + it('logs a PASSWORD_CHANGE entry scoped to the user themselves', async () => { |
| 107 | + await service.logPasswordChange('u1', 'u1@example.com', '1.2.3.4', 'ua'); |
| 108 | + |
| 109 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 110 | + expect.objectContaining({ |
| 111 | + action: AuditAction.PASSWORD_CHANGE, |
| 112 | + entityId: 'u1', |
| 113 | + }), |
| 114 | + ); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('logConfigChange', () => { |
| 119 | + it('logs directly with CRITICAL severity and the old/new config values', async () => { |
| 120 | + await service.logConfigChange( |
| 121 | + 'u1', |
| 122 | + 'admin@x.com', |
| 123 | + 'feature.flag', |
| 124 | + false, |
| 125 | + true, |
| 126 | + '1.2.3.4', |
| 127 | + 'ua', |
| 128 | + ); |
| 129 | + |
| 130 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 131 | + expect.objectContaining({ |
| 132 | + action: AuditAction.CONFIG_CHANGED, |
| 133 | + category: AuditCategory.SYSTEM, |
| 134 | + severity: AuditSeverity.CRITICAL, |
| 135 | + oldValues: { value: false }, |
| 136 | + newValues: { value: true }, |
| 137 | + }), |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it('propagates a failure from the underlying audit log write', async () => { |
| 142 | + const error = new Error('write failed'); |
| 143 | + auditLogService.log.mockRejectedValue(error); |
| 144 | + |
| 145 | + await expect( |
| 146 | + service.logConfigChange('u1', 'a@x.com', 'k', 1, 2, '1.2.3.4', 'ua'), |
| 147 | + ).rejects.toThrow(error); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe('logDataExport', () => { |
| 152 | + it('describes the export and includes record count/format in metadata', async () => { |
| 153 | + await service.logDataExport('u1', 'u1@x.com', 'User', 42, 'csv', '1.2.3.4', 'ua'); |
| 154 | + |
| 155 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 156 | + expect.objectContaining({ |
| 157 | + action: AuditAction.DATA_EXPORTED, |
| 158 | + description: expect.stringContaining('42'), |
| 159 | + metadata: expect.objectContaining({ recordCount: 42, exportFormat: 'csv' }), |
| 160 | + }), |
| 161 | + ); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('logBackupOperation', () => { |
| 166 | + it('maps CREATE to BACKUP_CREATED', async () => { |
| 167 | + await service.logBackupOperation('u1', 'a@x.com', 'CREATE', 'backup-1', '1.2.3.4', 'ua'); |
| 168 | + |
| 169 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 170 | + expect.objectContaining({ action: AuditAction.BACKUP_CREATED, entityId: 'backup-1' }), |
| 171 | + ); |
| 172 | + }); |
| 173 | + |
| 174 | + it('maps RESTORE to BACKUP_RESTORED', async () => { |
| 175 | + await service.logBackupOperation('u1', 'a@x.com', 'RESTORE', 'backup-1', '1.2.3.4', 'ua'); |
| 176 | + |
| 177 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 178 | + expect.objectContaining({ action: AuditAction.BACKUP_RESTORED }), |
| 179 | + ); |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + describe('logPermissionDenied', () => { |
| 184 | + it('logs with SECURITY category and WARNING severity', async () => { |
| 185 | + await service.logPermissionDenied('u1', 'u1@x.com', 'Course', 'delete', '1.2.3.4', 'ua'); |
| 186 | + |
| 187 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 188 | + expect.objectContaining({ |
| 189 | + action: AuditAction.PERMISSION_DENIED, |
| 190 | + category: AuditCategory.SECURITY, |
| 191 | + severity: AuditSeverity.WARNING, |
| 192 | + }), |
| 193 | + ); |
| 194 | + }); |
| 195 | + |
| 196 | + it('converts a null userId/userEmail to undefined for anonymous attempts', async () => { |
| 197 | + await service.logPermissionDenied(null, null, 'Course', 'delete', '1.2.3.4', 'ua'); |
| 198 | + |
| 199 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 200 | + expect.objectContaining({ userId: undefined, userEmail: undefined }), |
| 201 | + ); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + describe('logSuspiciousActivity', () => { |
| 206 | + it('logs with SECURITY category and CRITICAL severity, merging extra metadata', async () => { |
| 207 | + await service.logSuspiciousActivity( |
| 208 | + 'u1', |
| 209 | + 'u1@x.com', |
| 210 | + 'brute-force', |
| 211 | + 'multiple failed logins', |
| 212 | + '1.2.3.4', |
| 213 | + 'ua', |
| 214 | + undefined, |
| 215 | + { attempts: 5 }, |
| 216 | + ); |
| 217 | + |
| 218 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 219 | + expect.objectContaining({ |
| 220 | + action: AuditAction.SUSPICIOUS_ACTIVITY, |
| 221 | + category: AuditCategory.SECURITY, |
| 222 | + severity: AuditSeverity.CRITICAL, |
| 223 | + metadata: expect.objectContaining({ activityType: 'brute-force', attempts: 5 }), |
| 224 | + }), |
| 225 | + ); |
| 226 | + }); |
| 227 | + |
| 228 | + it('supports an anonymous actor', async () => { |
| 229 | + await service.logSuspiciousActivity( |
| 230 | + null, |
| 231 | + null, |
| 232 | + 'scraping', |
| 233 | + 'unusual request pattern', |
| 234 | + '1.2.3.4', |
| 235 | + 'ua', |
| 236 | + ); |
| 237 | + |
| 238 | + expect(auditLogService.log).toHaveBeenCalledWith( |
| 239 | + expect.objectContaining({ userId: undefined, userEmail: undefined }), |
| 240 | + ); |
| 241 | + }); |
| 242 | + }); |
| 243 | +}); |
0 commit comments