test: comprehensive test suites and quality gates pass across backend, storefront and admin
- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
This commit is contained in:
1 parent
1e73715169
commit
ddbf1ca026
439 files changed
+32911
-6107
No files matched your search
@@ -0,0 +1,145 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { IngredientsController } from "./ingredients.controller";
|
||||
import { IngredientsService } from "./ingredients.service";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
describe("IngredientsController", () => {
|
||||
let controller: IngredientsController;
|
||||
let service: IngredientsService;
|
||||
|
||||
const mockIngredients = [
|
||||
{
|
||||
id: "ing-1",
|
||||
nameFa: "گلیکوزآمینوگلیکان (GAG)",
|
||||
nameEn: "Glycosaminoglycans",
|
||||
slug: "glycosaminoglycan-gag",
|
||||
scientificName: "Perna Canaliculus",
|
||||
description:
|
||||
"ترکیب استخراج شده از صدف سبز نیوزیلندی برای تقویت غضروفها و مفاصل.",
|
||||
imageUrl: "https://canina.ir/ingredients/gag.jpg",
|
||||
benefits: ["ترمیم بافت مفصلی", "افزایش انعطافپذیری"],
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
{
|
||||
id: "ing-2",
|
||||
nameFa: "هیدروکسی آپاتیت",
|
||||
nameEn: "Hydroxyapatite",
|
||||
slug: "hydroxyapatite",
|
||||
scientificName: "Calcium phosphate hydroxide",
|
||||
description: "ماده معدنی اصلی استخوان جهت تراکم استخوانی و دندانها.",
|
||||
imageUrl: "https://canina.ir/ingredients/hydroxyapatite.jpg",
|
||||
benefits: ["استحکام استخوانها", "تقویت ساختار اسکلتی توله سگ"],
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
];
|
||||
|
||||
const mockIngredientsService = {
|
||||
findAll: jest.fn(),
|
||||
findOne: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
remove: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [IngredientsController],
|
||||
providers: [
|
||||
{
|
||||
provide: IngredientsService,
|
||||
useValue: mockIngredientsService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<IngredientsController>(IngredientsController);
|
||||
service = module.get<IngredientsService>(IngredientsService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(controller).toBeDefined();
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return all active ingredients", async () => {
|
||||
mockIngredientsService.findAll.mockResolvedValue(mockIngredients);
|
||||
|
||||
const result = await controller.findAll();
|
||||
|
||||
expect(service.findAll).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual(mockIngredients);
|
||||
expect(result).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findOne", () => {
|
||||
it("should return ingredient by id or slug", async () => {
|
||||
mockIngredientsService.findOne.mockResolvedValue(mockIngredients[0]);
|
||||
|
||||
const result = await controller.findOne("glycosaminoglycan-gag");
|
||||
|
||||
expect(service.findOne).toHaveBeenCalledWith("glycosaminoglycan-gag");
|
||||
expect(result).toEqual(mockIngredients[0]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a new ingredient", async () => {
|
||||
const createInput: Prisma.IngredientCreateInput = {
|
||||
nameFa: "عصاره مخمر آبجو",
|
||||
nameEn: "Brewer's Yeast",
|
||||
slug: "brewers-yeast",
|
||||
scientificName: "Saccharomyces cerevisiae",
|
||||
description:
|
||||
"منبع غنی ویتامینهای گروه ب و بیوتین برای تقویت پوست و مو",
|
||||
benefits: ["تقویت سیستم ایمنی", "سلامت پوست و مو"],
|
||||
};
|
||||
|
||||
const createdIngredient = { id: "ing-3", ...createInput };
|
||||
mockIngredientsService.create.mockResolvedValue(createdIngredient);
|
||||
|
||||
const result = await controller.create(createInput);
|
||||
|
||||
expect(service.create).toHaveBeenCalledWith(createInput);
|
||||
expect(result).toEqual(createdIngredient);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update ingredient by id", async () => {
|
||||
const updateInput: Prisma.IngredientUpdateInput = {
|
||||
description: "متن ویرایش شده توضیحات ترکیب دارویی",
|
||||
nameFa: "عصاره طبیعی مخمر آبجو",
|
||||
};
|
||||
|
||||
const updated = { ...mockIngredients[0], ...updateInput };
|
||||
mockIngredientsService.update.mockResolvedValue(updated);
|
||||
|
||||
const result = await controller.update("ing-1", updateInput);
|
||||
|
||||
expect(service.update).toHaveBeenCalledWith("ing-1", updateInput);
|
||||
expect(result).toEqual(updated);
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should remove ingredient by id", async () => {
|
||||
mockIngredientsService.remove.mockResolvedValue({
|
||||
success: true,
|
||||
id: "ing-1",
|
||||
});
|
||||
|
||||
const result = await controller.remove("ing-1");
|
||||
|
||||
expect(service.remove).toHaveBeenCalledWith("ing-1");
|
||||
expect(result).toEqual({ success: true, id: "ing-1" });
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -7,55 +7,55 @@ import {
|
||||
Body,
|
||||
Param,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { IngredientsService } from './ingredients.service';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from '../common/guards/roles.guard';
|
||||
import { Roles } from '../common/decorators/roles.decorator';
|
||||
} from "@nestjs/common";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { IngredientsService } from "./ingredients.service";
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from "@nestjs/swagger";
|
||||
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
|
||||
import { RolesGuard } from "../common/guards/roles.guard";
|
||||
import { Roles } from "../common/decorators/roles.decorator";
|
||||
|
||||
@ApiTags('Ingredients - دانشنامه ترکیبات و مواد موثره')
|
||||
@Controller('ingredients')
|
||||
@ApiTags("Ingredients - دانشنامه ترکیبات و مواد موثره")
|
||||
@Controller("ingredients")
|
||||
export class IngredientsController {
|
||||
constructor(private readonly ingredientsService: IngredientsService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'دریافت لیست ترکیبات فعال' })
|
||||
@ApiOperation({ summary: "دریافت لیست ترکیبات فعال" })
|
||||
findAll() {
|
||||
return this.ingredientsService.findAll();
|
||||
}
|
||||
|
||||
@Get(':idOrSlug')
|
||||
@ApiOperation({ summary: 'دریافت اطلاعات یک ترکیب بر اساس آیدی یا اسلاگ' })
|
||||
findOne(@Param('idOrSlug') idOrSlug: string) {
|
||||
@Get(":idOrSlug")
|
||||
@ApiOperation({ summary: "دریافت اطلاعات یک ترکیب بر اساس آیدی یا اسلاگ" })
|
||||
findOne(@Param("idOrSlug") idOrSlug: string) {
|
||||
return this.ingredientsService.findOne(idOrSlug);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Roles("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'ایجاد ترکیب جدید (نیازمند ادمین)' })
|
||||
@ApiOperation({ summary: "ایجاد ترکیب جدید (نیازمند ادمین)" })
|
||||
create(@Body() body: Prisma.IngredientCreateInput) {
|
||||
return this.ingredientsService.create(body);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Roles("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'ویرایش اطلاعات ترکیب (نیازمند ادمین)' })
|
||||
update(@Param('id') id: string, @Body() body: Prisma.IngredientUpdateInput) {
|
||||
@Patch(":id")
|
||||
@ApiOperation({ summary: "ویرایش اطلاعات ترکیب (نیازمند ادمین)" })
|
||||
update(@Param("id") id: string, @Body() body: Prisma.IngredientUpdateInput) {
|
||||
return this.ingredientsService.update(id, body);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Roles("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Delete(':id')
|
||||
@ApiOperation({ summary: 'حذف ترکیب (نیازمند ادمین)' })
|
||||
remove(@Param('id') id: string) {
|
||||
@Delete(":id")
|
||||
@ApiOperation({ summary: "حذف ترکیب (نیازمند ادمین)" })
|
||||
remove(@Param("id") id: string) {
|
||||
return this.ingredientsService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { IngredientsController } from './ingredients.controller';
|
||||
import { IngredientsService } from './ingredients.service';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { IngredientsController } from "./ingredients.controller";
|
||||
import { IngredientsService } from "./ingredients.service";
|
||||
import { PrismaModule } from "../prisma/prisma.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { IngredientsService } from "./ingredients.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
|
||||
describe("IngredientsService", () => {
|
||||
let service: IngredientsService;
|
||||
|
||||
const mockPrisma = {
|
||||
ingredient: {
|
||||
findMany: jest.fn(),
|
||||
findFirst: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
IngredientsService,
|
||||
{
|
||||
provide: PrismaService,
|
||||
useValue: mockPrisma,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<IngredientsService>(IngredientsService);
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return ingredients ordered by nameFa asc", async () => {
|
||||
const items = [{ id: "1", nameFa: "کلسیم" }];
|
||||
mockPrisma.ingredient.findMany.mockResolvedValue(items);
|
||||
|
||||
const result = await service.findAll();
|
||||
expect(mockPrisma.ingredient.findMany).toHaveBeenCalledWith({
|
||||
orderBy: { nameFa: "asc" },
|
||||
});
|
||||
expect(result).toEqual(items);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findOne", () => {
|
||||
it("should find by UUID when given a valid uuid", async () => {
|
||||
const uuid = "123e4567-e89b-12d3-a456-426614174000";
|
||||
mockPrisma.ingredient.findFirst.mockResolvedValue({
|
||||
id: uuid,
|
||||
nameFa: "ویتامین A",
|
||||
});
|
||||
|
||||
const result = await service.findOne(uuid);
|
||||
expect(mockPrisma.ingredient.findFirst).toHaveBeenCalledWith({
|
||||
where: { id: uuid },
|
||||
});
|
||||
expect(result.nameFa).toBe("ویتامین A");
|
||||
});
|
||||
|
||||
it("should find by slug when given a slug string", async () => {
|
||||
mockPrisma.ingredient.findFirst.mockResolvedValue({
|
||||
id: "1",
|
||||
slug: "calcium",
|
||||
});
|
||||
|
||||
const result = await service.findOne("calcium");
|
||||
expect(mockPrisma.ingredient.findFirst).toHaveBeenCalledWith({
|
||||
where: { slug: "calcium" },
|
||||
});
|
||||
expect(result.slug).toBe("calcium");
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if ingredient not found", async () => {
|
||||
mockPrisma.ingredient.findFirst.mockResolvedValue(null);
|
||||
await expect(service.findOne("missing-slug")).rejects.toThrow(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update & remove", () => {
|
||||
it("should throw NotFoundException on update if not found", async () => {
|
||||
mockPrisma.ingredient.findUnique.mockResolvedValue(null);
|
||||
await expect(service.update("id-1", {})).rejects.toThrow(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw NotFoundException on remove if not found", async () => {
|
||||
mockPrisma.ingredient.findUnique.mockResolvedValue(null);
|
||||
await expect(service.remove("id-1")).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it("should update ingredient successfully", async () => {
|
||||
mockPrisma.ingredient.findUnique.mockResolvedValue({ id: "id-1" });
|
||||
mockPrisma.ingredient.update.mockResolvedValue({
|
||||
id: "id-1",
|
||||
nameFa: "روی",
|
||||
});
|
||||
|
||||
const result = await service.update("id-1", { nameFa: "روی" });
|
||||
expect(result.nameFa).toBe("روی");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
@Injectable()
|
||||
export class IngredientsService {
|
||||
@@ -8,7 +8,7 @@ export class IngredientsService {
|
||||
|
||||
async findAll() {
|
||||
return this.prisma.ingredient.findMany({
|
||||
orderBy: { nameFa: 'asc' },
|
||||
orderBy: { nameFa: "asc" },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user