2024-03-20 22:02:51 +01:00
|
|
|
import { DatabaseAction, EntityType } from 'src/entities/audit.entity';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IAssetRepository } from 'src/interfaces/asset.interface';
|
|
|
|
import { IAuditRepository } from 'src/interfaces/audit.interface';
|
|
|
|
import { ICryptoRepository } from 'src/interfaces/crypto.interface';
|
|
|
|
import { JobStatus } from 'src/interfaces/job.interface';
|
2024-04-16 23:30:31 +02:00
|
|
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IPersonRepository } from 'src/interfaces/person.interface';
|
|
|
|
import { IStorageRepository } from 'src/interfaces/storage.interface';
|
|
|
|
import { IUserRepository } from 'src/interfaces/user.interface';
|
2024-03-21 00:07:30 +01:00
|
|
|
import { AuditService } from 'src/services/audit.service';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { auditStub } from 'test/fixtures/audit.stub';
|
|
|
|
import { authStub } from 'test/fixtures/auth.stub';
|
|
|
|
import { IAccessRepositoryMock, newAccessRepositoryMock } from 'test/repositories/access.repository.mock';
|
|
|
|
import { newAssetRepositoryMock } from 'test/repositories/asset.repository.mock';
|
|
|
|
import { newAuditRepositoryMock } from 'test/repositories/audit.repository.mock';
|
|
|
|
import { newCryptoRepositoryMock } from 'test/repositories/crypto.repository.mock';
|
2024-04-16 23:30:31 +02:00
|
|
|
import { newLoggerRepositoryMock } from 'test/repositories/logger.repository.mock';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { newPersonRepositoryMock } from 'test/repositories/person.repository.mock';
|
|
|
|
import { newStorageRepositoryMock } from 'test/repositories/storage.repository.mock';
|
|
|
|
import { newUserRepositoryMock } from 'test/repositories/user.repository.mock';
|
2024-04-16 16:44:45 +02:00
|
|
|
import { Mocked } from 'vitest';
|
2023-08-24 21:28:50 +02:00
|
|
|
|
|
|
|
describe(AuditService.name, () => {
|
|
|
|
let sut: AuditService;
|
|
|
|
let accessMock: IAccessRepositoryMock;
|
2024-04-16 16:44:45 +02:00
|
|
|
let assetMock: Mocked<IAssetRepository>;
|
|
|
|
let auditMock: Mocked<IAuditRepository>;
|
|
|
|
let cryptoMock: Mocked<ICryptoRepository>;
|
|
|
|
let personMock: Mocked<IPersonRepository>;
|
|
|
|
let storageMock: Mocked<IStorageRepository>;
|
|
|
|
let userMock: Mocked<IUserRepository>;
|
2024-04-16 23:30:31 +02:00
|
|
|
let loggerMock: Mocked<ILoggerRepository>;
|
2023-08-24 21:28:50 +02:00
|
|
|
|
2024-03-05 23:23:06 +01:00
|
|
|
beforeEach(() => {
|
2023-08-24 21:28:50 +02:00
|
|
|
accessMock = newAccessRepositoryMock();
|
2023-10-14 19:12:59 +02:00
|
|
|
assetMock = newAssetRepositoryMock();
|
|
|
|
cryptoMock = newCryptoRepositoryMock();
|
2023-08-24 21:28:50 +02:00
|
|
|
auditMock = newAuditRepositoryMock();
|
2023-10-14 19:12:59 +02:00
|
|
|
personMock = newPersonRepositoryMock();
|
|
|
|
storageMock = newStorageRepositoryMock();
|
|
|
|
userMock = newUserRepositoryMock();
|
2024-04-16 23:30:31 +02:00
|
|
|
loggerMock = newLoggerRepositoryMock();
|
|
|
|
sut = new AuditService(accessMock, assetMock, cryptoMock, personMock, auditMock, storageMock, userMock, loggerMock);
|
2023-08-24 21:28:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', () => {
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('handleCleanup', () => {
|
|
|
|
it('should delete old audit entries', async () => {
|
2024-03-15 14:16:54 +01:00
|
|
|
await expect(sut.handleCleanup()).resolves.toBe(JobStatus.SUCCESS);
|
|
|
|
expect(auditMock.removeBefore).toHaveBeenCalledWith(expect.any(Date));
|
2023-08-24 21:28:50 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getDeletes', () => {
|
|
|
|
it('should require full sync if the request is older than 100 days', async () => {
|
|
|
|
auditMock.getAfter.mockResolvedValue([]);
|
|
|
|
|
|
|
|
const date = new Date(2022, 0, 1);
|
|
|
|
await expect(sut.getDeletes(authStub.admin, { after: date, entityType: EntityType.ASSET })).resolves.toEqual({
|
|
|
|
needsFullSync: true,
|
|
|
|
ids: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(auditMock.getAfter).toHaveBeenCalledWith(date, {
|
|
|
|
action: DatabaseAction.DELETE,
|
2024-04-16 07:26:37 +02:00
|
|
|
userIds: [authStub.admin.user.id],
|
2023-08-24 21:28:50 +02:00
|
|
|
entityType: EntityType.ASSET,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get any new or updated assets and deleted ids', async () => {
|
2024-04-16 07:26:37 +02:00
|
|
|
auditMock.getAfter.mockResolvedValue([auditStub.delete.entityId]);
|
2023-08-24 21:28:50 +02:00
|
|
|
|
|
|
|
const date = new Date();
|
|
|
|
await expect(sut.getDeletes(authStub.admin, { after: date, entityType: EntityType.ASSET })).resolves.toEqual({
|
|
|
|
needsFullSync: false,
|
|
|
|
ids: ['asset-deleted'],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(auditMock.getAfter).toHaveBeenCalledWith(date, {
|
|
|
|
action: DatabaseAction.DELETE,
|
2024-04-16 07:26:37 +02:00
|
|
|
userIds: [authStub.admin.user.id],
|
2023-08-24 21:28:50 +02:00
|
|
|
entityType: EntityType.ASSET,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|