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,174 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { DoctorsController } from "./doctors.controller";
|
||||
import { DoctorsService } from "./doctors.service";
|
||||
import { DoctorQueryDto } from "./dto/doctor-query.dto";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
describe("DoctorsController", () => {
|
||||
let controller: DoctorsController;
|
||||
let service: DoctorsService;
|
||||
|
||||
const mockDoctorsService = {
|
||||
findAll: jest.fn(),
|
||||
findOne: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
remove: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [DoctorsController],
|
||||
providers: [
|
||||
{
|
||||
provide: DoctorsService,
|
||||
useValue: mockDoctorsService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<DoctorsController>(DoctorsController);
|
||||
service = module.get<DoctorsService>(DoctorsService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(controller).toBeDefined();
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return list of doctors with pagination metadata", async () => {
|
||||
const query: DoctorQueryDto = {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
activeOnly: true,
|
||||
search: "دکتر",
|
||||
};
|
||||
|
||||
const mockResponse = {
|
||||
doctors: [
|
||||
{
|
||||
id: "doc-1",
|
||||
name: "دکتر احمد رضایی",
|
||||
title: "متخصص تغذیه و مفاصل حیوانات خانگی",
|
||||
clinicName: "کلینیک مرکزی",
|
||||
isActive: true,
|
||||
_count: { videos: 3 },
|
||||
},
|
||||
],
|
||||
meta: {
|
||||
total: 1,
|
||||
page: 1,
|
||||
limit: 10,
|
||||
totalPages: 1,
|
||||
},
|
||||
};
|
||||
|
||||
mockDoctorsService.findAll.mockResolvedValue(mockResponse);
|
||||
|
||||
const result = await controller.findAll(query);
|
||||
|
||||
expect(service.findAll).toHaveBeenCalledWith(query);
|
||||
expect(result).toEqual(mockResponse);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findOne", () => {
|
||||
it("should return a doctor with related videos by id", async () => {
|
||||
const docId = "doc-1";
|
||||
const mockDoctor = {
|
||||
id: docId,
|
||||
name: "دکتر مریم سعیدی",
|
||||
title: "جراح و متخصص ارتوپدی دامپزشکی",
|
||||
videos: [
|
||||
{
|
||||
id: "vid-1",
|
||||
title: "راهنمای استفاده از کانهیدروکس برای درمان دیسپلازی",
|
||||
url: "https://aparat.com/v/12345",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
mockDoctorsService.findOne.mockResolvedValue(mockDoctor);
|
||||
|
||||
const result = await controller.findOne(docId);
|
||||
|
||||
expect(service.findOne).toHaveBeenCalledWith(docId);
|
||||
expect(result).toEqual(mockDoctor);
|
||||
});
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a new doctor via service.create", async () => {
|
||||
const createDto: Prisma.DoctorCreateInput = {
|
||||
name: "دکتر کاوه نوری",
|
||||
title: "متخصص پوست و موی حیوانات",
|
||||
clinicName: "بیمارستان تخصصی پارس",
|
||||
bio: "بیش از ۱۰ سال تجربه درمان بیماریهای پوستی سگ و گربه",
|
||||
avatarUrl: "https://canina.ir/doctors/kaveh.jpg",
|
||||
isActive: true,
|
||||
order: 1,
|
||||
};
|
||||
|
||||
const createdDoctor = {
|
||||
id: "doc-new-1",
|
||||
...createDto,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
mockDoctorsService.create.mockResolvedValue(createdDoctor);
|
||||
|
||||
const result = await controller.create(createDto);
|
||||
|
||||
expect(service.create).toHaveBeenCalledWith(createDto);
|
||||
expect(result).toEqual(createdDoctor);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update doctor information via service.update", async () => {
|
||||
const docId = "doc-1";
|
||||
const updateDto: Prisma.DoctorUpdateInput = {
|
||||
clinicName: "بیمارستان دامپزشکی تهران",
|
||||
order: 2,
|
||||
};
|
||||
|
||||
const updatedDoctor = {
|
||||
id: docId,
|
||||
name: "دکتر کاوه نوری",
|
||||
clinicName: "بیمارستان دامپزشکی تهران",
|
||||
order: 2,
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
mockDoctorsService.update.mockResolvedValue(updatedDoctor);
|
||||
|
||||
const result = await controller.update(docId, updateDto);
|
||||
|
||||
expect(service.update).toHaveBeenCalledWith(docId, updateDto);
|
||||
expect(result).toEqual(updatedDoctor);
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should delete a doctor by id via service.remove", async () => {
|
||||
const docId = "doc-delete-1";
|
||||
const deletedDoctor = {
|
||||
id: docId,
|
||||
name: "دکتر حذف شده",
|
||||
};
|
||||
|
||||
mockDoctorsService.remove.mockResolvedValue(deletedDoctor);
|
||||
|
||||
const result = await controller.remove(docId);
|
||||
|
||||
expect(service.remove).toHaveBeenCalledWith(docId);
|
||||
expect(result).toEqual(deletedDoctor);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -8,59 +8,59 @@ import {
|
||||
Param,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { DoctorsService } from './doctors.service';
|
||||
import { DoctorQueryDto } from './dto/doctor-query.dto';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from '../common/guards/roles.guard';
|
||||
import { Roles } from '../common/decorators/roles.decorator';
|
||||
import { Prisma } from '@prisma/client';
|
||||
} from "@nestjs/common";
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from "@nestjs/swagger";
|
||||
import { DoctorsService } from "./doctors.service";
|
||||
import { DoctorQueryDto } from "./dto/doctor-query.dto";
|
||||
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
|
||||
import { RolesGuard } from "../common/guards/roles.guard";
|
||||
import { Roles } from "../common/decorators/roles.decorator";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
@ApiTags('Doctors')
|
||||
@Controller('doctors')
|
||||
@ApiTags("Doctors")
|
||||
@Controller("doctors")
|
||||
export class DoctorsController {
|
||||
constructor(private readonly doctorsService: DoctorsService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'Get list of all doctors / specialists' })
|
||||
@ApiOperation({ summary: "Get list of all doctors / specialists" })
|
||||
findAll(@Query() query: DoctorQueryDto) {
|
||||
return this.doctorsService.findAll(query);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get doctor by ID with their videos and CV' })
|
||||
findOne(@Param('id') id: string) {
|
||||
@Get(":id")
|
||||
@ApiOperation({ summary: "Get doctor by ID with their videos and CV" })
|
||||
findOne(@Param("id") id: string) {
|
||||
return this.doctorsService.findOne(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin', 'ADMIN')
|
||||
@Roles("Admin", "ADMIN")
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Create new doctor' })
|
||||
@ApiOperation({ summary: "Create new doctor" })
|
||||
create(@Body() createDoctorDto: Prisma.DoctorCreateInput) {
|
||||
return this.doctorsService.create(createDoctorDto);
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
@Put(":id")
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin', 'ADMIN')
|
||||
@Roles("Admin", "ADMIN")
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Update doctor' })
|
||||
@ApiOperation({ summary: "Update doctor" })
|
||||
update(
|
||||
@Param('id') id: string,
|
||||
@Param("id") id: string,
|
||||
@Body() updateDoctorDto: Prisma.DoctorUpdateInput,
|
||||
) {
|
||||
return this.doctorsService.update(id, updateDoctorDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@Delete(":id")
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin', 'ADMIN')
|
||||
@Roles("Admin", "ADMIN")
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Delete doctor' })
|
||||
remove(@Param('id') id: string) {
|
||||
@ApiOperation({ summary: "Delete doctor" })
|
||||
remove(@Param("id") id: string) {
|
||||
return this.doctorsService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { DoctorsService } from './doctors.service';
|
||||
import { DoctorsController } from './doctors.controller';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { DoctorsService } from "./doctors.service";
|
||||
import { DoctorsController } from "./doctors.controller";
|
||||
import { PrismaModule } from "../prisma/prisma.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { DoctorsService } from "./doctors.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
|
||||
describe("DoctorsService", () => {
|
||||
let service: DoctorsService;
|
||||
|
||||
const mockPrisma = {
|
||||
doctor: {
|
||||
findMany: jest.fn(),
|
||||
count: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
DoctorsService,
|
||||
{
|
||||
provide: PrismaService,
|
||||
useValue: mockPrisma,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<DoctorsService>(DoctorsService);
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should filter doctors by activeOnly and search query with pagination", async () => {
|
||||
const mockDoctors = [
|
||||
{
|
||||
id: "doc-1",
|
||||
name: "دکتر محمدی",
|
||||
isActive: true,
|
||||
_count: { videos: 2 },
|
||||
},
|
||||
];
|
||||
mockPrisma.doctor.findMany.mockResolvedValue(mockDoctors);
|
||||
mockPrisma.doctor.count.mockResolvedValue(1);
|
||||
|
||||
const res = await service.findAll({
|
||||
activeOnly: true,
|
||||
search: "محمدی",
|
||||
page: 1,
|
||||
limit: 10,
|
||||
});
|
||||
|
||||
expect(mockPrisma.doctor.findMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
isActive: true,
|
||||
OR: [
|
||||
{ name: { contains: "محمدی", mode: "insensitive" } },
|
||||
{ title: { contains: "محمدی", mode: "insensitive" } },
|
||||
{ clinicName: { contains: "محمدی", mode: "insensitive" } },
|
||||
{ bio: { contains: "محمدی", mode: "insensitive" } },
|
||||
{ cv: { contains: "محمدی", mode: "insensitive" } },
|
||||
],
|
||||
},
|
||||
skip: 0,
|
||||
take: 10,
|
||||
orderBy: [{ order: "asc" }, { createdAt: "desc" }],
|
||||
include: { _count: { select: { videos: true } } },
|
||||
});
|
||||
expect(res.doctors).toEqual(mockDoctors);
|
||||
expect(res.meta.total).toBe(1);
|
||||
expect(res.meta.totalPages).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findOne", () => {
|
||||
it("should throw NotFoundException if doctor does not exist", async () => {
|
||||
mockPrisma.doctor.findUnique.mockResolvedValue(null);
|
||||
await expect(service.findOne("missing-id")).rejects.toThrow(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it("should return doctor with videos when found", async () => {
|
||||
const doctor = {
|
||||
id: "doc-1",
|
||||
name: "دکتر علوی",
|
||||
videos: [{ id: "v-1", title: "مراقبت از مفاصل سگ" }],
|
||||
};
|
||||
mockPrisma.doctor.findUnique.mockResolvedValue(doctor);
|
||||
|
||||
const res = await service.findOne("doc-1");
|
||||
expect(res).toEqual(doctor);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update & remove", () => {
|
||||
it("should verify doctor existence before update", async () => {
|
||||
mockPrisma.doctor.findUnique.mockResolvedValue(null);
|
||||
await expect(
|
||||
service.update("missing-id", { name: "نام جدید" }),
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it("should verify doctor existence before remove", async () => {
|
||||
mockPrisma.doctor.findUnique.mockResolvedValue(null);
|
||||
await expect(service.remove("missing-id")).rejects.toThrow(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { DoctorQueryDto } from './dto/doctor-query.dto';
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { DoctorQueryDto } from "./dto/doctor-query.dto";
|
||||
|
||||
export { DoctorQueryDto as DoctorQuery };
|
||||
|
||||
@@ -22,11 +22,11 @@ export class DoctorsService {
|
||||
|
||||
if (query.search) {
|
||||
where.OR = [
|
||||
{ name: { contains: query.search, mode: 'insensitive' } },
|
||||
{ title: { contains: query.search, mode: 'insensitive' } },
|
||||
{ clinicName: { contains: query.search, mode: 'insensitive' } },
|
||||
{ bio: { contains: query.search, mode: 'insensitive' } },
|
||||
{ cv: { contains: query.search, mode: 'insensitive' } },
|
||||
{ name: { contains: query.search, mode: "insensitive" } },
|
||||
{ title: { contains: query.search, mode: "insensitive" } },
|
||||
{ clinicName: { contains: query.search, mode: "insensitive" } },
|
||||
{ bio: { contains: query.search, mode: "insensitive" } },
|
||||
{ cv: { contains: query.search, mode: "insensitive" } },
|
||||
];
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ export class DoctorsService {
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: [{ order: 'asc' }, { createdAt: 'desc' }],
|
||||
orderBy: [{ order: "asc" }, { createdAt: "desc" }],
|
||||
include: {
|
||||
_count: {
|
||||
select: { videos: true },
|
||||
@@ -61,7 +61,7 @@ export class DoctorsService {
|
||||
where: { id },
|
||||
include: {
|
||||
videos: {
|
||||
orderBy: { createdAt: 'desc' },
|
||||
orderBy: { createdAt: "desc" },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsOptional, IsInt, Min, IsString, IsBoolean } from 'class-validator';
|
||||
import { Type, Transform } from 'class-transformer';
|
||||
import { ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsOptional, IsInt, Min, IsString, IsBoolean } from "class-validator";
|
||||
import { Type, Transform } from "class-transformer";
|
||||
|
||||
export class DoctorQueryDto {
|
||||
@ApiPropertyOptional({ description: 'شماره صفحه', minimum: 1, default: 1 })
|
||||
@ApiPropertyOptional({ description: "شماره صفحه", minimum: 1, default: 1 })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@@ -11,7 +11,7 @@ export class DoctorQueryDto {
|
||||
page?: number = 1;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'تعداد آیتم در هر صفحه',
|
||||
description: "تعداد آیتم در هر صفحه",
|
||||
minimum: 1,
|
||||
default: 50,
|
||||
})
|
||||
@@ -21,16 +21,16 @@ export class DoctorQueryDto {
|
||||
@Min(1)
|
||||
limit?: number = 50;
|
||||
|
||||
@ApiPropertyOptional({ description: 'عبارت جستجو' })
|
||||
@ApiPropertyOptional({ description: "عبارت جستجو" })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
search?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'فقط پزشکان فعال' })
|
||||
@ApiPropertyOptional({ description: "فقط پزشکان فعال" })
|
||||
@IsOptional()
|
||||
@Transform(({ value }) => {
|
||||
if (value === 'true' || value === true) return true;
|
||||
if (value === 'false' || value === false) return false;
|
||||
if (value === "true" || value === true) return true;
|
||||
if (value === "false" || value === false) return false;
|
||||
return undefined;
|
||||
})
|
||||
@IsBoolean()
|
||||
|
||||
Reference in new issue
Block a user