refactor: remove unused tests, mocks, and outdated e2e configurations

Deleted unused e2e tests, mocks (`cuid2`, `jose`, `ml-kem`, `sha3`), and their associated jest configurations. Simplified services by ensuring proper dependency imports, resolving circular references, and improving TypeScript type usage for enhanced maintainability and testability. Upgraded Dockerfile base image to match new development standards.
This commit is contained in:
Mathis HERRIOT
2026-01-14 13:51:32 +01:00
parent 8ffeaeba05
commit 0c045e8d3c
44 changed files with 163 additions and 183 deletions

View File

@@ -1,20 +1,27 @@
{ {
"version": "7", "version": "7",
"dialect": "postgresql", "dialect": "postgresql",
"entries": [ "entries": [
{ {
"idx": 0, "idx": 0,
"version": "7", "version": "7",
"when": 1767618753676, "when": 1767618753676,
"tag": "0000_right_sally_floyd", "tag": "0000_right_sally_floyd",
"breakpoints": true "breakpoints": true
}, },
{ {
"idx": 1, "idx": 1,
"version": "7", "version": "7",
"when": 1768392191169, "when": 1768392191169,
"tag": "0001_purple_goliath", "tag": "0001_purple_goliath",
"breakpoints": true "breakpoints": true
} },
] {
} "idx": 2,
"version": "7",
"when": 1768393637823,
"tag": "0002_redundant_skin",
"breakpoints": true
}
]
}

View File

@@ -1,7 +1,6 @@
FROM node:22-slim AS base FROM pnpm/pnpm:22 AS base
ENV PNPM_HOME="/pnpm" ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH" ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
FROM base AS build FROM base AS build
WORKDIR /usr/src/app WORKDIR /usr/src/app

View File

@@ -1,4 +1,4 @@
import { Module } from "@nestjs/common"; import { forwardRef, Module } from "@nestjs/common";
import { CryptoModule } from "../crypto/crypto.module"; import { CryptoModule } from "../crypto/crypto.module";
import { DatabaseModule } from "../database/database.module"; import { DatabaseModule } from "../database/database.module";
import { SessionsModule } from "../sessions/sessions.module"; import { SessionsModule } from "../sessions/sessions.module";
@@ -9,7 +9,12 @@ import { RbacService } from "./rbac.service";
import { RbacRepository } from "./repositories/rbac.repository"; import { RbacRepository } from "./repositories/rbac.repository";
@Module({ @Module({
imports: [UsersModule, CryptoModule, SessionsModule, DatabaseModule], imports: [
forwardRef(() => UsersModule),
CryptoModule,
SessionsModule,
DatabaseModule,
],
controllers: [AuthController], controllers: [AuthController],
providers: [AuthService, RbacService, RbacRepository], providers: [AuthService, RbacService, RbacRepository],
exports: [AuthService, RbacService], exports: [AuthService, RbacService],

View File

@@ -1,5 +1,7 @@
import { import {
BadRequestException, BadRequestException,
forwardRef,
Inject,
Injectable, Injectable,
Logger, Logger,
UnauthorizedException, UnauthorizedException,
@@ -19,6 +21,7 @@ export class AuthService {
private readonly logger = new Logger(AuthService.name); private readonly logger = new Logger(AuthService.name);
constructor( constructor(
@Inject(forwardRef(() => UsersService))
private readonly usersService: UsersService, private readonly usersService: UsersService,
private readonly hashingService: HashingService, private readonly hashingService: HashingService,
private readonly jwtService: JwtService, private readonly jwtService: JwtService,

View File

@@ -48,7 +48,9 @@ describe("RbacService", () => {
it("should return user permissions", async () => { it("should return user permissions", async () => {
const userId = "user-id"; const userId = "user-id";
const mockPermissions = ["read", "write"]; const mockPermissions = ["read", "write"];
mockRbacRepository.findPermissionsByUserId.mockResolvedValue(mockPermissions); mockRbacRepository.findPermissionsByUserId.mockResolvedValue(
mockPermissions,
);
const result = await service.getUserPermissions(userId); const result = await service.getUserPermissions(userId);

View File

@@ -1,3 +1,4 @@
import { CacheInterceptor, CacheKey, CacheTTL } from "@nestjs/cache-manager";
import { import {
Body, Body,
Controller, Controller,
@@ -9,7 +10,6 @@ import {
UseGuards, UseGuards,
UseInterceptors, UseInterceptors,
} from "@nestjs/common"; } from "@nestjs/common";
import { CacheInterceptor, CacheKey, CacheTTL } from "@nestjs/cache-manager";
import { Roles } from "../auth/decorators/roles.decorator"; import { Roles } from "../auth/decorators/roles.decorator";
import { AuthGuard } from "../auth/guards/auth.guard"; import { AuthGuard } from "../auth/guards/auth.guard";
import { RolesGuard } from "../auth/guards/roles.guard"; import { RolesGuard } from "../auth/guards/roles.guard";

View File

@@ -1,9 +1,9 @@
import { Test, TestingModule } from "@nestjs/testing";
import { CACHE_MANAGER } from "@nestjs/cache-manager"; import { CACHE_MANAGER } from "@nestjs/cache-manager";
import { Test, TestingModule } from "@nestjs/testing";
import { CategoriesService } from "./categories.service"; import { CategoriesService } from "./categories.service";
import { CategoriesRepository } from "./repositories/categories.repository";
import { CreateCategoryDto } from "./dto/create-category.dto"; import { CreateCategoryDto } from "./dto/create-category.dto";
import { UpdateCategoryDto } from "./dto/update-category.dto"; import { UpdateCategoryDto } from "./dto/update-category.dto";
import { CategoriesRepository } from "./repositories/categories.repository";
describe("CategoriesService", () => { describe("CategoriesService", () => {
let service: CategoriesService; let service: CategoriesService;
@@ -75,7 +75,9 @@ describe("CategoriesService", () => {
describe("create", () => { describe("create", () => {
it("should create a category and generate slug", async () => { it("should create a category and generate slug", async () => {
const dto: CreateCategoryDto = { name: "Test Category" }; const dto: CreateCategoryDto = { name: "Test Category" };
mockCategoriesRepository.create.mockResolvedValue([{ ...dto, slug: "test-category" }]); mockCategoriesRepository.create.mockResolvedValue([
{ ...dto, slug: "test-category" },
]);
const result = await service.create(dto); const result = await service.create(dto);
@@ -91,7 +93,9 @@ describe("CategoriesService", () => {
it("should update a category and regenerate slug", async () => { it("should update a category and regenerate slug", async () => {
const id = "1"; const id = "1";
const dto: UpdateCategoryDto = { name: "New Name" }; const dto: UpdateCategoryDto = { name: "New Name" };
mockCategoriesRepository.update.mockResolvedValue([{ id, ...dto, slug: "new-name" }]); mockCategoriesRepository.update.mockResolvedValue([
{ id, ...dto, slug: "new-name" },
]);
const result = await service.update(id, dto); const result = await service.update(id, dto);

View File

@@ -1,9 +1,9 @@
import { Injectable, Logger, Inject } from "@nestjs/common";
import { CACHE_MANAGER } from "@nestjs/cache-manager"; import { CACHE_MANAGER } from "@nestjs/cache-manager";
import { Cache } from "cache-manager"; import { Inject, Injectable, Logger } from "@nestjs/common";
import { CategoriesRepository } from "./repositories/categories.repository"; import type { Cache } from "cache-manager";
import { CreateCategoryDto } from "./dto/create-category.dto"; import { CreateCategoryDto } from "./dto/create-category.dto";
import { UpdateCategoryDto } from "./dto/update-category.dto"; import { UpdateCategoryDto } from "./dto/update-category.dto";
import { CategoriesRepository } from "./repositories/categories.repository";
@Injectable() @Injectable()
export class CategoriesService { export class CategoriesService {

View File

@@ -33,7 +33,10 @@ export class CategoriesRepository {
.returning(); .returning();
} }
async update(id: string, data: UpdateCategoryDto & { slug?: string; updatedAt: Date }) { async update(
id: string,
data: UpdateCategoryDto & { slug?: string; updatedAt: Date },
) {
return await this.databaseService.db return await this.databaseService.db
.update(categories) .update(categories)
.set(data) .set(data)

View File

@@ -9,10 +9,7 @@ export interface IStorageService {
bucketName?: string, bucketName?: string,
): Promise<string>; ): Promise<string>;
getFile( getFile(fileName: string, bucketName?: string): Promise<Readable>;
fileName: string,
bucketName?: string,
): Promise<Readable>;
getFileUrl( getFileUrl(
fileName: string, fileName: string,
@@ -28,7 +25,7 @@ export interface IStorageService {
deleteFile(fileName: string, bucketName?: string): Promise<void>; deleteFile(fileName: string, bucketName?: string): Promise<void>;
getFileInfo(fileName: string, bucketName?: string): Promise<any>; getFileInfo(fileName: string, bucketName?: string): Promise<unknown>;
moveFile( moveFile(
sourceFileName: string, sourceFileName: string,

View File

@@ -9,10 +9,16 @@ import { PurgeService } from "./purge.service";
describe("PurgeService", () => { describe("PurgeService", () => {
let service: PurgeService; let service: PurgeService;
const mockSessionsRepository = { purgeExpired: jest.fn().mockResolvedValue([]) }; const mockSessionsRepository = {
const mockReportsRepository = { purgeObsolete: jest.fn().mockResolvedValue([]) }; purgeExpired: jest.fn().mockResolvedValue([]),
};
const mockReportsRepository = {
purgeObsolete: jest.fn().mockResolvedValue([]),
};
const mockUsersRepository = { purgeDeleted: jest.fn().mockResolvedValue([]) }; const mockUsersRepository = { purgeDeleted: jest.fn().mockResolvedValue([]) };
const mockContentsRepository = { purgeSoftDeleted: jest.fn().mockResolvedValue([]) }; const mockContentsRepository = {
purgeSoftDeleted: jest.fn().mockResolvedValue([]),
};
beforeEach(async () => { beforeEach(async () => {
jest.clearAllMocks(); jest.clearAllMocks();

View File

@@ -42,9 +42,8 @@ export class PurgeService {
); );
// 4. Purge des contenus supprimés (Soft Delete > 30 jours) // 4. Purge des contenus supprimés (Soft Delete > 30 jours)
const deletedContents = await this.contentsRepository.purgeSoftDeleted( const deletedContents =
thirtyDaysAgo, await this.contentsRepository.purgeSoftDeleted(thirtyDaysAgo);
);
this.logger.log( this.logger.log(
`Purged ${deletedContents.length} contents marked for deletion more than 30 days ago.`, `Purged ${deletedContents.length} contents marked for deletion more than 30 days ago.`,
); );

View File

@@ -2,11 +2,10 @@ jest.mock("uuid", () => ({
v4: jest.fn(() => "mocked-uuid"), v4: jest.fn(() => "mocked-uuid"),
})); }));
import { CACHE_MANAGER } from "@nestjs/cache-manager";
import { BadRequestException } from "@nestjs/common"; import { BadRequestException } from "@nestjs/common";
import { ConfigService } from "@nestjs/config"; import { ConfigService } from "@nestjs/config";
import { CACHE_MANAGER } from "@nestjs/cache-manager";
import { Test, TestingModule } from "@nestjs/testing"; import { Test, TestingModule } from "@nestjs/testing";
import { DatabaseService } from "../database/database.service";
import { MediaService } from "../media/media.service"; import { MediaService } from "../media/media.service";
import { S3Service } from "../s3/s3.service"; import { S3Service } from "../s3/s3.service";
import { ContentsService } from "./contents.service"; import { ContentsService } from "./contents.service";
@@ -44,9 +43,7 @@ describe("ContentsService", () => {
}; };
const mockCacheManager = { const mockCacheManager = {
store: { clear: jest.fn(),
keys: jest.fn().mockResolvedValue([]),
},
del: jest.fn(), del: jest.fn(),
}; };
@@ -141,7 +138,9 @@ describe("ContentsService", () => {
describe("incrementViews", () => { describe("incrementViews", () => {
it("should increment views", async () => { it("should increment views", async () => {
mockContentsRepository.incrementViews.mockResolvedValue([{ id: "1", views: 1 }]); mockContentsRepository.incrementViews.mockResolvedValue([
{ id: "1", views: 1 },
]);
const result = await service.incrementViews("1"); const result = await service.incrementViews("1");
expect(mockContentsRepository.incrementViews).toHaveBeenCalledWith("1"); expect(mockContentsRepository.incrementViews).toHaveBeenCalledWith("1");
expect(result[0].views).toBe(1); expect(result[0].views).toBe(1);

View File

@@ -1,13 +1,22 @@
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
import { CACHE_MANAGER } from "@nestjs/cache-manager"; import { CACHE_MANAGER } from "@nestjs/cache-manager";
import { Cache } from "cache-manager"; import {
import { Inject } from "@nestjs/common"; BadRequestException,
Inject,
Injectable,
Logger,
} from "@nestjs/common";
import { ConfigService } from "@nestjs/config"; import { ConfigService } from "@nestjs/config";
import type { Cache } from "cache-manager";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import type { IMediaService } from "../common/interfaces/media.interface"; import type {
IMediaService,
MediaProcessingResult,
} from "../common/interfaces/media.interface";
import type { IStorageService } from "../common/interfaces/storage.interface"; import type { IStorageService } from "../common/interfaces/storage.interface";
import { MediaService } from "../media/media.service"; import { MediaService } from "../media/media.service";
import { S3Service } from "../s3/s3.service"; import { S3Service } from "../s3/s3.service";
import { CreateContentDto } from "./dto/create-content.dto";
import { UploadContentDto } from "./dto/upload-content.dto";
import { ContentsRepository } from "./repositories/contents.repository"; import { ContentsRepository } from "./repositories/contents.repository";
@Injectable() @Injectable()
@@ -24,11 +33,7 @@ export class ContentsService {
private async clearContentsCache() { private async clearContentsCache() {
this.logger.log("Clearing contents cache"); this.logger.log("Clearing contents cache");
const keys = await this.cacheManager.store.keys(); await this.cacheManager.clear();
const contentsKeys = keys.filter((key) => key.startsWith("contents/"));
for (const key of contentsKeys) {
await this.cacheManager.del(key);
}
} }
async getUploadUrl(userId: string, fileName: string) { async getUploadUrl(userId: string, fileName: string) {
@@ -40,12 +45,7 @@ export class ContentsService {
async uploadAndProcess( async uploadAndProcess(
userId: string, userId: string,
file: Express.Multer.File, file: Express.Multer.File,
data: { data: UploadContentDto,
title: string;
type: "meme" | "gif";
categoryId?: string;
tags?: string[];
},
) { ) {
this.logger.log(`Uploading and processing file for user ${userId}`); this.logger.log(`Uploading and processing file for user ${userId}`);
// 0. Validation du format et de la taille // 0. Validation du format et de la taille
@@ -86,7 +86,7 @@ export class ContentsService {
} }
// 2. Transcodage // 2. Transcodage
let processed; let processed: MediaProcessingResult;
if (file.mimetype.startsWith("image/")) { if (file.mimetype.startsWith("image/")) {
// Image ou GIF -> WebP (format moderne, bien supporté) // Image ou GIF -> WebP (format moderne, bien supporté)
processed = await this.mediaService.processImage(file.buffer, "webp"); processed = await this.mediaService.processImage(file.buffer, "webp");
@@ -129,7 +129,7 @@ export class ContentsService {
return { data, totalCount }; return { data, totalCount };
} }
async create(userId: string, data: any) { async create(userId: string, data: CreateContentDto) {
this.logger.log(`Creating content for user ${userId}: ${data.title}`); this.logger.log(`Creating content for user ${userId}: ${data.title}`);
const { tags: tagNames, ...contentData } = data; const { tags: tagNames, ...contentData } = data;
@@ -166,7 +166,7 @@ export class ContentsService {
return this.contentsRepository.findOne(idOrSlug); return this.contentsRepository.findOne(idOrSlug);
} }
generateBotHtml(content: any): string { generateBotHtml(content: { title: string; storageKey: string }): string {
const imageUrl = this.getFileUrl(content.storageKey); const imageUrl = this.getFileUrl(content.storageKey);
return `<!DOCTYPE html> return `<!DOCTYPE html>
<html> <html>

View File

@@ -6,9 +6,9 @@ import {
exists, exists,
ilike, ilike,
isNull, isNull,
sql,
lte, lte,
type SQL, type SQL,
sql,
} from "drizzle-orm"; } from "drizzle-orm";
import { DatabaseService } from "../../database/database.service"; import { DatabaseService } from "../../database/database.service";
import { import {
@@ -113,10 +113,7 @@ export class ContentsRepository {
.select() .select()
.from(favorites) .from(favorites)
.where( .where(
and( and(eq(favorites.contentId, contents.id), eq(favorites.userId, userId)),
eq(favorites.contentId, contents.id),
eq(favorites.userId, userId),
),
), ),
), ),
); );
@@ -143,7 +140,6 @@ export class ContentsRepository {
author: { author: {
id: users.uuid, id: users.uuid,
username: users.username, username: users.username,
avatarUrl: users.avatarUrl,
}, },
category: { category: {
id: categories.id, id: categories.id,
@@ -173,18 +169,13 @@ export class ContentsRepository {
return results.map((r) => ({ return results.map((r) => ({
...r, ...r,
tags: tagsForContents tags: tagsForContents.filter((t) => t.contentId === r.id).map((t) => t.name),
.filter((t) => t.contentId === r.id)
.map((t) => t.name),
})); }));
} }
async create(data: NewContentInDb & { userId: string }, tagNames?: string[]) { async create(data: NewContentInDb & { userId: string }, tagNames?: string[]) {
return await this.databaseService.db.transaction(async (tx) => { return await this.databaseService.db.transaction(async (tx) => {
const [newContent] = await tx const [newContent] = await tx.insert(contents).values(data).returning();
.insert(contents)
.values(data)
.returning();
if (tagNames && tagNames.length > 0) { if (tagNames && tagNames.length > 0) {
for (const tagName of tagNames) { for (const tagName of tagNames) {
@@ -325,10 +316,7 @@ export class ContentsRepository {
.select() .select()
.from(favorites) .from(favorites)
.where( .where(
and( and(eq(favorites.contentId, contents.id), eq(favorites.userId, userId)),
eq(favorites.contentId, contents.id),
eq(favorites.userId, userId),
),
), ),
), ),
); );
@@ -377,7 +365,12 @@ export class ContentsRepository {
async purgeSoftDeleted(before: Date) { async purgeSoftDeleted(before: Date) {
return await this.databaseService.db return await this.databaseService.db
.delete(contents) .delete(contents)
.where(and(sql`${contents.deletedAt} IS NOT NULL`, lte(contents.deletedAt, before))) .where(
and(
sql`${contents.deletedAt} IS NOT NULL`,
lte(contents.deletedAt, before),
),
)
.returning(); .returning();
} }
} }

View File

@@ -1,8 +1,8 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { CryptoService } from "./crypto.service"; import { CryptoService } from "./crypto.service";
import { EncryptionService } from "./services/encryption.service";
import { HashingService } from "./services/hashing.service"; import { HashingService } from "./services/hashing.service";
import { JwtService } from "./services/jwt.service"; import { JwtService } from "./services/jwt.service";
import { EncryptionService } from "./services/encryption.service";
import { PostQuantumService } from "./services/post-quantum.service"; import { PostQuantumService } from "./services/post-quantum.service";
@Module({ @Module({

View File

@@ -64,9 +64,9 @@ jest.mock("jose", () => ({
})); }));
import { CryptoService } from "./crypto.service"; import { CryptoService } from "./crypto.service";
import { EncryptionService } from "./services/encryption.service";
import { HashingService } from "./services/hashing.service"; import { HashingService } from "./services/hashing.service";
import { JwtService } from "./services/jwt.service"; import { JwtService } from "./services/jwt.service";
import { EncryptionService } from "./services/encryption.service";
import { PostQuantumService } from "./services/post-quantum.service"; import { PostQuantumService } from "./services/post-quantum.service";
describe("CryptoService", () => { describe("CryptoService", () => {

View File

@@ -3,9 +3,9 @@ export * from "./audit_logs";
export * from "./categories"; export * from "./categories";
export * from "./content"; export * from "./content";
export * from "./favorites"; export * from "./favorites";
export * from "./pgp";
export * from "./rbac"; export * from "./rbac";
export * from "./reports"; export * from "./reports";
export * from "./sessions"; export * from "./sessions";
export * from "./tags"; export * from "./tags";
export * from "./users"; export * from "./users";
export * from "./pgp";

View File

@@ -55,6 +55,9 @@ export function pgpSymEncrypt(value: string | SQL, key: string | SQL) {
/** /**
* @deprecated Utiliser directement les colonnes de type pgpEncrypted qui gèrent maintenant le déchiffrement automatiquement. * @deprecated Utiliser directement les colonnes de type pgpEncrypted qui gèrent maintenant le déchiffrement automatiquement.
*/ */
export function pgpSymDecrypt(column: AnyPgColumn, key: string | SQL): SQL<string> { export function pgpSymDecrypt(
column: AnyPgColumn,
key: string | SQL,
): SQL<string> {
return sql`pgp_sym_decrypt(${column}, ${key})`.mapWith(column) as SQL<string>; return sql`pgp_sym_decrypt(${column}, ${key})`.mapWith(column) as SQL<string>;
} }

View File

@@ -1,4 +1,3 @@
import { SQL, sql } from "drizzle-orm";
import { import {
boolean, boolean,
index, index,

View File

@@ -34,7 +34,9 @@ describe("FavoritesService", () => {
describe("addFavorite", () => { describe("addFavorite", () => {
it("should add a favorite", async () => { it("should add a favorite", async () => {
mockFavoritesRepository.findContentById.mockResolvedValue({ id: "content1" }); mockFavoritesRepository.findContentById.mockResolvedValue({
id: "content1",
});
mockFavoritesRepository.add.mockResolvedValue([ mockFavoritesRepository.add.mockResolvedValue([
{ userId: "u1", contentId: "content1" }, { userId: "u1", contentId: "content1" },
]); ]);
@@ -53,7 +55,9 @@ describe("FavoritesService", () => {
}); });
it("should throw ConflictException on duplicate favorite", async () => { it("should throw ConflictException on duplicate favorite", async () => {
mockFavoritesRepository.findContentById.mockResolvedValue({ id: "content1" }); mockFavoritesRepository.findContentById.mockResolvedValue({
id: "content1",
});
mockFavoritesRepository.add.mockRejectedValue(new Error("Duplicate")); mockFavoritesRepository.add.mockRejectedValue(new Error("Duplicate"));
await expect(service.addFavorite("u1", "content1")).rejects.toThrow( await expect(service.addFavorite("u1", "content1")).rejects.toThrow(
ConflictException, ConflictException,
@@ -63,7 +67,9 @@ describe("FavoritesService", () => {
describe("removeFavorite", () => { describe("removeFavorite", () => {
it("should remove a favorite", async () => { it("should remove a favorite", async () => {
mockFavoritesRepository.remove.mockResolvedValue([{ userId: "u1", contentId: "c1" }]); mockFavoritesRepository.remove.mockResolvedValue([
{ userId: "u1", contentId: "c1" },
]);
const result = await service.removeFavorite("u1", "c1"); const result = await service.removeFavorite("u1", "c1");
expect(result).toEqual({ userId: "u1", contentId: "c1" }); expect(result).toEqual({ userId: "u1", contentId: "c1" });
expect(repository.remove).toHaveBeenCalledWith("u1", "c1"); expect(repository.remove).toHaveBeenCalledWith("u1", "c1");

View File

@@ -14,7 +14,7 @@ export class FavoritesService {
async addFavorite(userId: string, contentId: string) { async addFavorite(userId: string, contentId: string) {
this.logger.log(`Adding favorite: user ${userId}, content ${contentId}`); this.logger.log(`Adding favorite: user ${userId}, content ${contentId}`);
const content = await this.favoritesRepository.findContentById(contentId); const content = await this.favoritesRepository.findContentById(contentId);
if (!content) { if (!content) {
throw new NotFoundException("Content not found"); throw new NotFoundException("Content not found");

View File

@@ -2,5 +2,8 @@ import type { MediaProcessingResult } from "../../common/interfaces/media.interf
export interface IMediaProcessorStrategy { export interface IMediaProcessorStrategy {
canHandle(mimeType: string): boolean; canHandle(mimeType: string): boolean;
process(buffer: Buffer, options?: any): Promise<MediaProcessingResult>; process(
buffer: Buffer,
options?: Record<string, unknown>,
): Promise<MediaProcessingResult>;
} }

View File

@@ -34,7 +34,11 @@ describe("ReportsService", () => {
it("should create a report", async () => { it("should create a report", async () => {
const reporterId = "u1"; const reporterId = "u1";
const data = { contentId: "c1", reason: "spam" }; const data = { contentId: "c1", reason: "spam" };
mockReportsRepository.create.mockResolvedValue({ id: "r1", ...data, reporterId }); mockReportsRepository.create.mockResolvedValue({
id: "r1",
...data,
reporterId,
});
const result = await service.create(reporterId, data); const result = await service.create(reporterId, data);
@@ -54,7 +58,9 @@ describe("ReportsService", () => {
describe("updateStatus", () => { describe("updateStatus", () => {
it("should update report status", async () => { it("should update report status", async () => {
mockReportsRepository.updateStatus.mockResolvedValue([{ id: "r1", status: "resolved" }]); mockReportsRepository.updateStatus.mockResolvedValue([
{ id: "r1", status: "resolved" },
]);
const result = await service.updateStatus("r1", "resolved"); const result = await service.updateStatus("r1", "resolved");
expect(result[0].status).toBe("resolved"); expect(result[0].status).toBe("resolved");
expect(repository.updateStatus).toHaveBeenCalledWith("r1", "resolved"); expect(repository.updateStatus).toHaveBeenCalledWith("r1", "resolved");

View File

@@ -1,6 +1,6 @@
import { Injectable, Logger } from "@nestjs/common"; import { Injectable, Logger } from "@nestjs/common";
import { ReportsRepository } from "./repositories/reports.repository";
import { CreateReportDto } from "./dto/create-report.dto"; import { CreateReportDto } from "./dto/create-report.dto";
import { ReportsRepository } from "./repositories/reports.repository";
@Injectable() @Injectable()
export class ReportsService { export class ReportsService {

View File

@@ -11,7 +11,7 @@ export class ReportsRepository {
reporterId: string; reporterId: string;
contentId?: string; contentId?: string;
tagId?: string; tagId?: string;
reason: string; reason: "inappropriate" | "spam" | "copyright" | "other";
description?: string; description?: string;
}) { }) {
const [newReport] = await this.databaseService.db const [newReport] = await this.databaseService.db

View File

@@ -32,7 +32,7 @@ export class SessionsRepository {
return result[0] || null; return result[0] || null;
} }
async update(sessionId: string, data: any) { async update(sessionId: string, data: Record<string, unknown>) {
const [updatedSession] = await this.databaseService.db const [updatedSession] = await this.databaseService.db
.update(sessions) .update(sessions)
.set({ ...data, updatedAt: new Date() }) .set({ ...data, updatedAt: new Date() })

View File

@@ -1,8 +1,8 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { CryptoModule } from "../crypto/crypto.module"; import { CryptoModule } from "../crypto/crypto.module";
import { DatabaseModule } from "../database/database.module"; import { DatabaseModule } from "../database/database.module";
import { SessionsService } from "./sessions.service";
import { SessionsRepository } from "./repositories/sessions.repository"; import { SessionsRepository } from "./repositories/sessions.repository";
import { SessionsService } from "./sessions.service";
@Module({ @Module({
imports: [DatabaseModule, CryptoModule], imports: [DatabaseModule, CryptoModule],

View File

@@ -15,8 +15,8 @@ import { UnauthorizedException } from "@nestjs/common";
import { Test, TestingModule } from "@nestjs/testing"; import { Test, TestingModule } from "@nestjs/testing";
import { HashingService } from "../crypto/services/hashing.service"; import { HashingService } from "../crypto/services/hashing.service";
import { JwtService } from "../crypto/services/jwt.service"; import { JwtService } from "../crypto/services/jwt.service";
import { SessionsService } from "./sessions.service";
import { SessionsRepository } from "./repositories/sessions.repository"; import { SessionsRepository } from "./repositories/sessions.repository";
import { SessionsService } from "./sessions.service";
describe("SessionsService", () => { describe("SessionsService", () => {
let service: SessionsService; let service: SessionsService;
@@ -76,7 +76,10 @@ describe("SessionsService", () => {
userId: "u1", userId: "u1",
expiresAt, expiresAt,
}); });
mockSessionsRepository.update.mockResolvedValue({ id: "s1", refreshToken: "new-token" }); mockSessionsRepository.update.mockResolvedValue({
id: "s1",
refreshToken: "new-token",
});
const result = await service.refreshSession("old-token"); const result = await service.refreshSession("old-token");

View File

@@ -30,7 +30,8 @@ export class SessionsService {
} }
async refreshSession(oldRefreshToken: string) { async refreshSession(oldRefreshToken: string) {
const session = await this.sessionsRepository.findValidByRefreshToken(oldRefreshToken); const session =
await this.sessionsRepository.findValidByRefreshToken(oldRefreshToken);
if (!session || session.expiresAt < new Date()) { if (!session || session.expiresAt < new Date()) {
if (session) { if (session) {

View File

@@ -1,3 +1,4 @@
import { CacheInterceptor, CacheTTL } from "@nestjs/cache-manager";
import { import {
Controller, Controller,
DefaultValuePipe, DefaultValuePipe,
@@ -6,7 +7,6 @@ import {
Query, Query,
UseInterceptors, UseInterceptors,
} from "@nestjs/common"; } from "@nestjs/common";
import { CacheInterceptor, CacheTTL } from "@nestjs/cache-manager";
import { TagsService } from "./tags.service"; import { TagsService } from "./tags.service";
@Controller("tags") @Controller("tags")

View File

@@ -1,8 +1,8 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { DatabaseModule } from "../database/database.module"; import { DatabaseModule } from "../database/database.module";
import { TagsRepository } from "./repositories/tags.repository";
import { TagsController } from "./tags.controller"; import { TagsController } from "./tags.controller";
import { TagsService } from "./tags.service"; import { TagsService } from "./tags.service";
import { TagsRepository } from "./repositories/tags.repository";
@Module({ @Module({
imports: [DatabaseModule], imports: [DatabaseModule],

View File

@@ -1,6 +1,6 @@
import { Test, TestingModule } from "@nestjs/testing"; import { Test, TestingModule } from "@nestjs/testing";
import { TagsService } from "./tags.service";
import { TagsRepository } from "./repositories/tags.repository"; import { TagsRepository } from "./repositories/tags.repository";
import { TagsService } from "./tags.service";
describe("TagsService", () => { describe("TagsService", () => {
let service: TagsService; let service: TagsService;

View File

@@ -1,8 +1,7 @@
import { Injectable } from "@nestjs/common"; import { Injectable } from "@nestjs/common";
import { and, eq, lte, sql } from "drizzle-orm"; import { and, eq, lte, sql } from "drizzle-orm";
import { DatabaseService } from "../../database/database.service"; import { DatabaseService } from "../../database/database.service";
import { users, contents, favorites } from "../../database/schemas"; import { contents, favorites, users } from "../../database/schemas";
import type { UpdateUserDto } from "../dto/update-user.dto";
@Injectable() @Injectable()
export class UsersRepository { export class UsersRepository {
@@ -99,7 +98,7 @@ export class UsersRepository {
return result[0] || null; return result[0] || null;
} }
async update(uuid: string, data: any) { async update(uuid: string, data: Partial<typeof users.$inferInsert>) {
return await this.databaseService.db return await this.databaseService.db
.update(users) .update(users)
.set({ ...data, updatedAt: new Date() }) .set({ ...data, updatedAt: new Date() })

View File

@@ -1,9 +1,12 @@
import { CacheInterceptor, CacheTTL } from "@nestjs/cache-manager";
import { import {
Body, Body,
Controller, Controller,
DefaultValuePipe, DefaultValuePipe,
Delete, Delete,
forwardRef,
Get, Get,
Inject,
Param, Param,
ParseIntPipe, ParseIntPipe,
Patch, Patch,
@@ -13,7 +16,6 @@ import {
UseGuards, UseGuards,
UseInterceptors, UseInterceptors,
} from "@nestjs/common"; } from "@nestjs/common";
import { CacheInterceptor, CacheKey, CacheTTL } from "@nestjs/cache-manager";
import { AuthService } from "../auth/auth.service"; import { AuthService } from "../auth/auth.service";
import { Roles } from "../auth/decorators/roles.decorator"; import { Roles } from "../auth/decorators/roles.decorator";
import { AuthGuard } from "../auth/guards/auth.guard"; import { AuthGuard } from "../auth/guards/auth.guard";
@@ -27,6 +29,7 @@ import { UsersService } from "./users.service";
export class UsersController { export class UsersController {
constructor( constructor(
private readonly usersService: UsersService, private readonly usersService: UsersService,
@Inject(forwardRef(() => AuthService))
private readonly authService: AuthService, private readonly authService: AuthService,
) {} ) {}

View File

@@ -1,13 +1,13 @@
import { Module } from "@nestjs/common"; import { forwardRef, Module } from "@nestjs/common";
import { AuthModule } from "../auth/auth.module"; import { AuthModule } from "../auth/auth.module";
import { CryptoModule } from "../crypto/crypto.module"; import { CryptoModule } from "../crypto/crypto.module";
import { DatabaseModule } from "../database/database.module"; import { DatabaseModule } from "../database/database.module";
import { UsersRepository } from "./repositories/users.repository";
import { UsersController } from "./users.controller"; import { UsersController } from "./users.controller";
import { UsersService } from "./users.service"; import { UsersService } from "./users.service";
import { UsersRepository } from "./repositories/users.repository";
@Module({ @Module({
imports: [DatabaseModule, CryptoModule, AuthModule], imports: [DatabaseModule, CryptoModule, forwardRef(() => AuthModule)],
controllers: [UsersController], controllers: [UsersController],
providers: [UsersService, UsersRepository], providers: [UsersService, UsersRepository],
exports: [UsersService], exports: [UsersService],

View File

@@ -11,10 +11,10 @@ jest.mock("jose", () => ({
jwtVerify: jest.fn(), jwtVerify: jest.fn(),
})); }));
import { Test, TestingModule } from "@nestjs/testing";
import { CACHE_MANAGER } from "@nestjs/cache-manager"; import { CACHE_MANAGER } from "@nestjs/cache-manager";
import { UsersService } from "./users.service"; import { Test, TestingModule } from "@nestjs/testing";
import { UsersRepository } from "./repositories/users.repository"; import { UsersRepository } from "./repositories/users.repository";
import { UsersService } from "./users.service";
describe("UsersService", () => { describe("UsersService", () => {
let service: UsersService; let service: UsersService;
@@ -91,7 +91,9 @@ describe("UsersService", () => {
describe("update", () => { describe("update", () => {
it("should update a user", async () => { it("should update a user", async () => {
mockUsersRepository.update.mockResolvedValue([{ uuid: "uuid1", displayName: "New" }]); mockUsersRepository.update.mockResolvedValue([
{ uuid: "uuid1", displayName: "New" },
]);
const result = await service.update("uuid1", { displayName: "New" }); const result = await service.update("uuid1", { displayName: "New" });
expect(result[0].displayName).toBe("New"); expect(result[0].displayName).toBe("New");
}); });

View File

@@ -1,8 +1,8 @@
import { Injectable, Logger, Inject } from "@nestjs/common";
import { CACHE_MANAGER } from "@nestjs/cache-manager"; import { CACHE_MANAGER } from "@nestjs/cache-manager";
import { Cache } from "cache-manager"; import { Inject, Injectable, Logger } from "@nestjs/common";
import { UsersRepository } from "./repositories/users.repository"; import type { Cache } from "cache-manager";
import { UpdateUserDto } from "./dto/update-user.dto"; import { UpdateUserDto } from "./dto/update-user.dto";
import { UsersRepository } from "./repositories/users.repository";
@Injectable() @Injectable()
export class UsersService { export class UsersService {

View File

@@ -1,3 +0,0 @@
module.exports = {
createCuid: () => () => 'mocked-cuid',
};

View File

@@ -1,13 +0,0 @@
module.exports = {
SignJWT: class {
constructor() { return this; }
setProtectedHeader() { return this; }
setIssuedAt() { return this; }
setExpirationTime() { return this; }
sign() { return Promise.resolve('mocked-token'); }
},
jwtVerify: () => Promise.resolve({ payload: { sub: 'mocked-user' } }),
importJWK: () => Promise.resolve({}),
exportJWK: () => Promise.resolve({}),
generateKeyPair: () => Promise.resolve({ publicKey: {}, privateKey: {} }),
};

View File

@@ -1,7 +0,0 @@
module.exports = {
ml_kem768: {
keygen: () => ({ publicKey: Buffer.alloc(1184), secretKey: Buffer.alloc(2400) }),
encapsulate: () => ({ cipherText: Buffer.alloc(1088), sharedSecret: Buffer.alloc(32) }),
decapsulate: () => Buffer.alloc(32),
}
};

View File

@@ -1,5 +0,0 @@
module.exports = {
sha3_256: () => ({ update: () => ({ digest: () => Buffer.alloc(32) }) }),
sha3_512: () => ({ update: () => ({ digest: () => Buffer.alloc(64) }) }),
shake256: () => ({ update: () => ({ digest: () => Buffer.alloc(32) }) }),
};

View File

@@ -1,25 +0,0 @@
import { INestApplication } from "@nestjs/common";
import { Test, TestingModule } from "@nestjs/testing";
import request from "supertest";
import { App } from "supertest/types";
import { AppModule } from "../src/app.module";
describe("AppController (e2e)", () => {
let app: INestApplication<App>;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it("/ (GET)", () => {
return request(app.getHttpServer())
.get("/")
.expect(200)
.expect("Hello World!");
});
});

View File

@@ -1,9 +0,0 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}