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,152 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { TestimonialsController } from "./testimonials.controller";
|
||||
import { TestimonialsService } from "./testimonials.service";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
describe("TestimonialsController", () => {
|
||||
let controller: TestimonialsController;
|
||||
let service: TestimonialsService;
|
||||
|
||||
const mockTestimonialsService = {
|
||||
findAll: jest.fn(),
|
||||
findHomepage: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
remove: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [TestimonialsController],
|
||||
providers: [
|
||||
{
|
||||
provide: TestimonialsService,
|
||||
useValue: mockTestimonialsService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<TestimonialsController>(TestimonialsController);
|
||||
service = module.get<TestimonialsService>(TestimonialsService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(controller).toBeDefined();
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return all testimonials", async () => {
|
||||
const mockList = [
|
||||
{
|
||||
id: "testi-1",
|
||||
authorName: "فرهاد پیروز",
|
||||
roleTitle: "سرپرست سگ ژرمن شپرد",
|
||||
content: "مکمل مفاصل کانین هیدروکس واقعا معجزه کرد برای پای سگم.",
|
||||
rating: 5,
|
||||
isActive: true,
|
||||
showOnHome: true,
|
||||
order: 1,
|
||||
},
|
||||
];
|
||||
|
||||
mockTestimonialsService.findAll.mockResolvedValue(mockList);
|
||||
|
||||
const result = await controller.findAll();
|
||||
|
||||
expect(service.findAll).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual(mockList);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findHomepage", () => {
|
||||
it("should return only active homepage testimonials", async () => {
|
||||
const mockHomeList = [
|
||||
{
|
||||
id: "testi-1",
|
||||
authorName: "فرهاد پیروز",
|
||||
showOnHome: true,
|
||||
isActive: true,
|
||||
},
|
||||
];
|
||||
|
||||
mockTestimonialsService.findHomepage.mockResolvedValue(mockHomeList);
|
||||
|
||||
const result = await controller.findHomepage();
|
||||
|
||||
expect(service.findHomepage).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual(mockHomeList);
|
||||
});
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a new testimonial via service.create", async () => {
|
||||
const createInput: Prisma.TestimonialCreateInput = {
|
||||
authorName: "مونا اسدی",
|
||||
roleTitle: "سرپرست گربه پرشین",
|
||||
content: "پودر بیوتین باعث براق شدن و قطع ریزش موی گربهام شد.",
|
||||
rating: 5,
|
||||
isActive: true,
|
||||
showOnHome: true,
|
||||
order: 2,
|
||||
};
|
||||
|
||||
const created = {
|
||||
id: "testi-2",
|
||||
...createInput,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
mockTestimonialsService.create.mockResolvedValue(created);
|
||||
|
||||
const result = await controller.create(createInput);
|
||||
|
||||
expect(service.create).toHaveBeenCalledWith(createInput);
|
||||
expect(result).toEqual(created);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update a testimonial by id via service.update", async () => {
|
||||
const testId = "testi-1";
|
||||
const updateInput: Prisma.TestimonialUpdateInput = {
|
||||
content: "متن ویرایش شده رضایتنامه مشتری",
|
||||
rating: 5,
|
||||
};
|
||||
|
||||
const updated = {
|
||||
id: testId,
|
||||
authorName: "فرهاد پیروز",
|
||||
content: "متن ویرایش شده رضایتنامه مشتری",
|
||||
rating: 5,
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
mockTestimonialsService.update.mockResolvedValue(updated);
|
||||
|
||||
const result = await controller.update(testId, updateInput);
|
||||
|
||||
expect(service.update).toHaveBeenCalledWith(testId, updateInput);
|
||||
expect(result).toEqual(updated);
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should delete a testimonial by id via service.remove", async () => {
|
||||
const testId = "testi-del-1";
|
||||
const deleted = { id: testId, authorName: "حذف شده" };
|
||||
|
||||
mockTestimonialsService.remove.mockResolvedValue(deleted);
|
||||
|
||||
const result = await controller.remove(testId);
|
||||
|
||||
expect(service.remove).toHaveBeenCalledWith(testId);
|
||||
expect(result).toEqual(deleted);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -7,55 +7,55 @@ import {
|
||||
Body,
|
||||
Param,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { TestimonialsService } from './testimonials.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 { TestimonialsService } from "./testimonials.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('Testimonials - نظرات و رضایتنامهها')
|
||||
@Controller('testimonials')
|
||||
@ApiTags("Testimonials - نظرات و رضایتنامهها")
|
||||
@Controller("testimonials")
|
||||
export class TestimonialsController {
|
||||
constructor(private readonly testimonialsService: TestimonialsService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'دریافت لیست نظرات و رضایتنامهها' })
|
||||
@ApiOperation({ summary: "دریافت لیست نظرات و رضایتنامهها" })
|
||||
findAll() {
|
||||
return this.testimonialsService.findAll();
|
||||
}
|
||||
|
||||
@Get('homepage')
|
||||
@ApiOperation({ summary: 'دریافت نظرات فعال برای نمایش در صفحه اصلی' })
|
||||
@Get("homepage")
|
||||
@ApiOperation({ summary: "دریافت نظرات فعال برای نمایش در صفحه اصلی" })
|
||||
findHomepage() {
|
||||
return this.testimonialsService.findHomepage();
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Roles("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'ایجاد نظر جدید (نیازمند ادمین)' })
|
||||
@ApiOperation({ summary: "ایجاد نظر جدید (نیازمند ادمین)" })
|
||||
create(@Body() body: Prisma.TestimonialCreateInput) {
|
||||
return this.testimonialsService.create(body);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Roles("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'ویرایش نظر (نیازمند ادمین)' })
|
||||
update(@Param('id') id: string, @Body() body: Prisma.TestimonialUpdateInput) {
|
||||
@Patch(":id")
|
||||
@ApiOperation({ summary: "ویرایش نظر (نیازمند ادمین)" })
|
||||
update(@Param("id") id: string, @Body() body: Prisma.TestimonialUpdateInput) {
|
||||
return this.testimonialsService.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.testimonialsService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TestimonialsController } from './testimonials.controller';
|
||||
import { TestimonialsService } from './testimonials.service';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { TestimonialsController } from "./testimonials.controller";
|
||||
import { TestimonialsService } from "./testimonials.service";
|
||||
import { PrismaModule } from "../prisma/prisma.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { TestimonialsService } from "./testimonials.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
|
||||
describe("TestimonialsService", () => {
|
||||
let service: TestimonialsService;
|
||||
|
||||
const mockPrisma = {
|
||||
testimonial: {
|
||||
findMany: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
TestimonialsService,
|
||||
{
|
||||
provide: PrismaService,
|
||||
useValue: mockPrisma,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<TestimonialsService>(TestimonialsService);
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("findHomepage", () => {
|
||||
it("should query only active testimonials flagged for showOnHome ordered by order", async () => {
|
||||
const activeItems = [
|
||||
{
|
||||
id: "t1",
|
||||
authorName: "دکتر علوی",
|
||||
order: 1,
|
||||
isActive: true,
|
||||
showOnHome: true,
|
||||
},
|
||||
];
|
||||
mockPrisma.testimonial.findMany.mockResolvedValue(activeItems);
|
||||
|
||||
const result = await service.findHomepage();
|
||||
expect(mockPrisma.testimonial.findMany).toHaveBeenCalledWith({
|
||||
where: { isActive: true, showOnHome: true },
|
||||
orderBy: { order: "asc" },
|
||||
});
|
||||
expect(result).toEqual(activeItems);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should throw NotFoundException if testimonial does not exist", async () => {
|
||||
mockPrisma.testimonial.findUnique.mockResolvedValue(null);
|
||||
await expect(
|
||||
service.update("invalid-id", { authorName: "تغییر نام" }),
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it("should update and return testimonial when exists", async () => {
|
||||
mockPrisma.testimonial.findUnique.mockResolvedValue({ id: "t1" });
|
||||
mockPrisma.testimonial.update.mockResolvedValue({
|
||||
id: "t1",
|
||||
authorName: "تغییر نام",
|
||||
});
|
||||
|
||||
const result = await service.update("t1", { authorName: "تغییر نام" });
|
||||
expect(result.authorName).toBe("تغییر نام");
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should throw NotFoundException if testimonial does not exist on remove", async () => {
|
||||
mockPrisma.testimonial.findUnique.mockResolvedValue(null);
|
||||
await expect(service.remove("missing-id")).rejects.toThrow(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it("should delete and return testimonial when found", async () => {
|
||||
mockPrisma.testimonial.findUnique.mockResolvedValue({ id: "t1" });
|
||||
mockPrisma.testimonial.delete.mockResolvedValue({ id: "t1" });
|
||||
|
||||
const result = await service.remove("t1");
|
||||
expect(result.id).toBe("t1");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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 TestimonialsService {
|
||||
@@ -8,14 +8,14 @@ export class TestimonialsService {
|
||||
|
||||
async findAll() {
|
||||
return this.prisma.testimonial.findMany({
|
||||
orderBy: { order: 'asc' },
|
||||
orderBy: { order: "asc" },
|
||||
});
|
||||
}
|
||||
|
||||
async findHomepage() {
|
||||
return this.prisma.testimonial.findMany({
|
||||
where: { isActive: true, showOnHome: true },
|
||||
orderBy: { order: 'asc' },
|
||||
orderBy: { order: "asc" },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user