test: add comprehensive unit tests for core services

Added unit tests for the `api-keys`, `auth`, `categories`, `contents`, `favorites`, `media`, and `purge` services to improve test coverage and ensure core functionality integrity.
This commit is contained in:
Mathis HERRIOT
2026-01-08 16:22:23 +01:00
parent cc2823db7d
commit 399bdab86c
13 changed files with 1502 additions and 0 deletions

View File

@@ -0,0 +1,124 @@
jest.mock("@noble/post-quantum/ml-kem.js", () => ({
ml_kem768: {
keygen: jest.fn(),
encapsulate: jest.fn(),
decapsulate: jest.fn(),
},
}));
jest.mock("jose", () => ({
SignJWT: jest.fn(),
jwtVerify: jest.fn(),
}));
import { UnauthorizedException } from "@nestjs/common";
import { Test, TestingModule } from "@nestjs/testing";
import { CryptoService } from "../crypto/crypto.service";
import { DatabaseService } from "../database/database.service";
import { SessionsService } from "./sessions.service";
describe("SessionsService", () => {
let service: SessionsService;
const mockDb = {
insert: jest.fn(),
select: jest.fn(),
update: jest.fn(),
};
const mockCryptoService = {
generateJwt: jest.fn().mockResolvedValue("mock-jwt"),
hashIp: jest.fn().mockResolvedValue("mock-ip-hash"),
};
beforeEach(async () => {
jest.clearAllMocks();
const chain = {
insert: jest.fn().mockReturnThis(),
values: jest.fn().mockReturnThis(),
returning: jest.fn().mockReturnThis(),
select: jest.fn().mockReturnThis(),
from: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
update: jest.fn().mockReturnThis(),
set: jest.fn().mockReturnThis(),
// biome-ignore lint/suspicious/noThenProperty: Fine for testing purposes
then: jest.fn().mockImplementation(function (cb) {
return Promise.resolve(this).then(cb);
}),
};
const mockImplementation = () => Object.assign(Promise.resolve([]), chain);
for (const mock of Object.values(chain)) {
if (mock !== chain.then) {
mock.mockImplementation(mockImplementation);
}
}
Object.assign(mockDb, chain);
const module: TestingModule = await Test.createTestingModule({
providers: [
SessionsService,
{ provide: DatabaseService, useValue: { db: mockDb } },
{ provide: CryptoService, useValue: mockCryptoService },
],
}).compile();
service = module.get<SessionsService>(SessionsService);
});
it("should be defined", () => {
expect(service).toBeDefined();
});
describe("createSession", () => {
it("should create a session", async () => {
mockDb.returning.mockResolvedValue([{ id: "s1", refreshToken: "mock-jwt" }]);
const result = await service.createSession("u1", "agent", "1.2.3.4");
expect(result.id).toBe("s1");
expect(mockCryptoService.generateJwt).toHaveBeenCalledWith(
{ sub: "u1", type: "refresh" },
"7d",
);
});
});
describe("refreshSession", () => {
it("should refresh a valid session", async () => {
const expiresAt = new Date();
expiresAt.setDate(expiresAt.getDate() + 1);
const session = { id: "s1", userId: "u1", expiresAt, isValid: true };
mockDb.where.mockImplementation(() => {
const chain = {
limit: jest.fn().mockReturnThis(),
returning: jest
.fn()
.mockResolvedValue([{ ...session, refreshToken: "new-jwt" }]),
// biome-ignore lint/suspicious/noThenProperty: Fine for testing purposes
then: jest.fn().mockResolvedValue(session),
};
return Object.assign(Promise.resolve([session]), chain);
});
const result = await service.refreshSession("old-jwt");
expect(result.refreshToken).toBe("new-jwt");
});
it("should throw UnauthorizedException if session not found", async () => {
mockDb.where.mockImplementation(() => {
const chain = {
limit: jest.fn().mockReturnThis(),
// biome-ignore lint/suspicious/noThenProperty: Fine for testing purposes
then: jest.fn().mockResolvedValue(null),
};
return Object.assign(Promise.resolve([]), chain);
});
await expect(service.refreshSession("invalid")).rejects.toThrow(
UnauthorizedException,
);
});
});
});