Compare commits

...

6 Commits

Author SHA1 Message Date
Mathis HERRIOT
9ab737b8c7 chore: add .env.example file with environment variable templates
All checks were successful
Backend Tests / test (push) Successful in 9m37s
Lint / lint (push) Successful in 9m37s
2026-01-08 12:41:52 +01:00
Mathis HERRIOT
b3035eb2ab feat: add MailModule to app imports 2026-01-08 12:41:42 +01:00
Mathis HERRIOT
a6fdbdb06d chore: refactor crypto service tests for readability and lint compliance 2026-01-08 12:41:35 +01:00
Mathis HERRIOT
48b233eae4 chore: add comment to ignore lint rule in S3 service test 2026-01-08 12:41:31 +01:00
Mathis HERRIOT
89bd9d65e7 feat: implement MailModule with email services and tests
Added MailModule with services for email validation and password reset functionalities. Includes configuration via `@nestjs-modules/mailer` and comprehensive unit tests.
2026-01-08 12:41:27 +01:00
Mathis HERRIOT
8cf1699717 feat: add mailing dependencies to support email functionality
Added `@nestjs-modules/mailer`, `nodemailer`, and their respective types to backend dependencies for implementing email services. Updated `pnpm-lock.yaml` to reflect these changes.
2026-01-08 12:41:06 +01:00
9 changed files with 10242 additions and 5817 deletions

36
.env.example Normal file
View File

@@ -0,0 +1,36 @@
# Global
NODE_ENV=development
# Backend
BACKEND_PORT=3001
# Frontend
FRONTEND_PORT=3000
# Database (PostgreSQL)
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=memegoat
POSTGRES_USER=app
POSTGRES_PASSWORD=app
# Storage (S3/MinIO) - À configurer lors de l'implémentation
# S3_ENDPOINT=localhost
# S3_PORT=9000
# S3_ACCESS_KEY=
# S3_SECRET_KEY=
# S3_BUCKET=memegoat
# Security (PGP & Auth) - À configurer lors de l'implémentation
# PGP_PASSPHRASE=
JWT_SECRET=super-secret-key-change-me-in-production
ENCRYPTION_KEY=another-super-secret-key-32-chars
# Mail
MAIL_HOST=localhost
MAIL_PORT=1025
MAIL_SECURE=false
MAIL_USER=user
MAIL_PASS=password
MAIL_FROM=noreply@memegoat.fr
DOMAIN_NAME=memegoat.fr

View File

@@ -24,6 +24,7 @@
"db:studio": "drizzle-kit studio"
},
"dependencies": {
"@nestjs-modules/mailer": "^2.0.2",
"@nestjs/common": "^11.0.1",
"@nestjs/config": "^4.0.2",
"@nestjs/core": "^11.0.1",
@@ -34,6 +35,7 @@
"drizzle-orm": "^0.45.1",
"jose": "^6.1.3",
"minio": "^8.0.6",
"nodemailer": "^7.0.12",
"pg": "^8.16.3",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1"
@@ -45,6 +47,7 @@
"@types/express": "^5.0.0",
"@types/jest": "^30.0.0",
"@types/node": "^22.10.7",
"@types/nodemailer": "^7.0.4",
"@types/pg": "^8.16.0",
"@types/supertest": "^6.0.2",
"drizzle-kit": "^0.31.8",

View File

@@ -4,6 +4,7 @@ import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { CryptoModule } from "./crypto/crypto.module";
import { DatabaseModule } from "./database/database.module";
import { MailModule } from "./mail/mail.module";
import { S3Module } from "./s3/s3.module";
@Module({
@@ -11,6 +12,7 @@ import { S3Module } from "./s3/s3.module";
DatabaseModule,
CryptoModule,
S3Module,
MailModule,
ConfigModule.forRoot({
isGlobal: true,
}),

View File

@@ -7,11 +7,13 @@ jest.mock("@noble/post-quantum/ml-kem.js", () => ({
publicKey: new Uint8Array(1184),
secretKey: new Uint8Array(2400),
})),
encapsulate: jest.fn((pk: Uint8Array) => ({
encapsulate: jest.fn((_pk: Uint8Array) => ({
cipherText: new Uint8Array(1088),
sharedSecret: new Uint8Array(32),
})),
decapsulate: jest.fn((ct: Uint8Array, sk: Uint8Array) => new Uint8Array(32)),
decapsulate: jest.fn(
(_ct: Uint8Array, _sk: Uint8Array) => new Uint8Array(32),
),
},
}));
@@ -51,7 +53,10 @@ jest.mock("jose", () => ({
if (jws.includes("tampered") || jws.split(".").length !== 3) {
throw new Error("Tampered or invalid content");
}
const payload = jws === "mocked.jws.token" ? "Important document content" : "Original content";
const payload =
jws === "mocked.jws.token"
? "Important document content"
: "Original content";
return Promise.resolve({
payload: new TextEncoder().encode(payload),
});
@@ -149,7 +154,7 @@ describe("CryptoService", () => {
it("should fail to verify tampered content", async () => {
const content = "Original content";
const jws = await service.signContent(content);
const parts = jws.split(".");
const _parts = jws.split(".");
// Tamper with the payload (middle part)
const tamperedJws = "this.is.tampered";

View File

@@ -0,0 +1,29 @@
import { Module } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { MailerModule } from "@nestjs-modules/mailer";
import { MailService } from "./mail.service";
@Module({
imports: [
MailerModule.forRootAsync({
useFactory: async (config: ConfigService) => ({
transport: {
host: config.get("MAIL_HOST"),
port: Number(config.get("MAIL_PORT")),
secure: config.get("MAIL_SECURE") === "true",
auth: {
user: config.get("MAIL_USER"),
pass: config.get("MAIL_PASS"),
},
},
defaults: {
from: `"No Reply" <${config.get("MAIL_FROM")}>`,
},
}),
inject: [ConfigService],
}),
],
providers: [MailService],
exports: [MailService],
})
export class MailModule {}

View File

@@ -0,0 +1,87 @@
import { Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { Test, TestingModule } from "@nestjs/testing";
import { MailerService } from "@nestjs-modules/mailer";
import { MailService } from "./mail.service";
describe("MailService", () => {
let service: MailService;
let mailerService: MailerService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
MailService,
{
provide: MailerService,
useValue: {
sendMail: jest.fn().mockResolvedValue({}),
},
},
{
provide: ConfigService,
useValue: {
get: jest.fn().mockReturnValue("test.io"),
},
},
],
}).compile();
service = module.get<MailService>(MailService);
mailerService = module.get<MailerService>(MailerService);
});
it("should be defined", () => {
expect(service).toBeDefined();
});
it("should send validation email with correct URL", async () => {
const email = "test@example.com";
const token = "token123";
await service.sendEmailValidation(email, token);
expect(mailerService.sendMail).toHaveBeenCalledWith(
expect.objectContaining({
to: email,
subject: "Validation de votre adresse email",
html: expect.stringContaining(
"https://test.io/api/auth/verify-email?token=token123",
),
}),
);
});
it("should send password reset email with correct URL", async () => {
const email = "test@example.com";
const token = "token123";
await service.sendPasswordReset(email, token);
expect(mailerService.sendMail).toHaveBeenCalledWith(
expect.objectContaining({
to: email,
subject: "Réinitialisation de votre mot de passe",
html: expect.stringContaining(
"https://test.io/api/auth/reset-password?token=token123",
),
}),
);
});
it("should throw and log error when sending fails", async () => {
const email = "test@example.com";
const token = "token123";
const error = new Error("SMTP Error");
(mailerService.sendMail as jest.Mock).mockRejectedValueOnce(error);
const loggerSpy = jest
.spyOn(Logger.prototype, "error")
.mockImplementation(() => {});
await expect(service.sendEmailValidation(email, token)).rejects.toThrow(
"SMTP Error",
);
expect(loggerSpy).toHaveBeenCalled();
loggerSpy.mockRestore();
});
});

View File

@@ -0,0 +1,62 @@
import { Injectable, Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { MailerService } from "@nestjs-modules/mailer";
@Injectable()
export class MailService {
private readonly logger = new Logger(MailService.name);
private readonly domain: string;
constructor(
private mailerService: MailerService,
private configService: ConfigService,
) {
this.domain = this.configService.get<string>("DOMAIN_NAME", "memegoat.io");
}
async sendEmailValidation(email: string, token: string) {
const url = `https://${this.domain}/api/auth/verify-email?token=${token}`;
try {
await this.mailerService.sendMail({
to: email,
subject: "Validation de votre adresse email",
html: `
<p>Bonjour,</p>
<p>Veuillez cliquer sur le lien ci-dessous pour valider votre adresse email :</p>
<p><a href="${url}">${url}</a></p>
<p>Si vous n'avez pas demandé cette validation, vous pouvez ignorer cet email.</p>
`,
});
} catch (error) {
this.logger.error(
`Failed to send validation email to ${email}: ${error.message}`,
error.stack,
);
throw error;
}
}
async sendPasswordReset(email: string, token: string) {
const url = `https://${this.domain}/api/auth/reset-password?token=${token}`;
try {
await this.mailerService.sendMail({
to: email,
subject: "Réinitialisation de votre mot de passe",
html: `
<p>Bonjour,</p>
<p>Veuillez cliquer sur le lien ci-dessous pour réinitialiser votre mot de passe :</p>
<p><a href="${url}">${url}</a></p>
<p>Si vous n'avez pas demandé de réinitialisation, vous pouvez ignorer cet email.</p>
`,
});
} catch (error) {
this.logger.error(
`Failed to send password reset email to ${email}: ${error.message}`,
error.stack,
);
throw error;
}
}
}

View File

@@ -8,6 +8,7 @@ jest.mock("minio");
describe("S3Service", () => {
let service: S3Service;
let _configService: ConfigService;
// biome-ignore lint/suspicious/noExplicitAny: Fine for testing purposes
let minioClient: any;
beforeEach(async () => {

15826
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff