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,160 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { BannersController } from "./banners.controller";
|
||||
import { BannersService } from "./banners.service";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
describe("BannersController", () => {
|
||||
let controller: BannersController;
|
||||
let service: BannersService;
|
||||
|
||||
const mockBannersService = {
|
||||
findAll: jest.fn(),
|
||||
create: jest.fn(),
|
||||
reorder: jest.fn(),
|
||||
update: jest.fn(),
|
||||
remove: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [BannersController],
|
||||
providers: [
|
||||
{
|
||||
provide: BannersService,
|
||||
useValue: mockBannersService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<BannersController>(BannersController);
|
||||
service = module.get<BannersService>(BannersService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return all banners ordered", async () => {
|
||||
const banners = [
|
||||
{
|
||||
id: "banner-1",
|
||||
title: "جشنواره مکملهای مفاصل کانینا آلمان",
|
||||
imageUrl: "https://canina.ir/banners/joints.jpg",
|
||||
linkUrl: "/products?category=joints",
|
||||
order: 1,
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
id: "banner-2",
|
||||
title: "مشاوره رایگان دامپزشک آنلاین",
|
||||
imageUrl: "https://canina.ir/banners/consultation.jpg",
|
||||
linkUrl: "/tickets",
|
||||
order: 2,
|
||||
isActive: true,
|
||||
},
|
||||
];
|
||||
|
||||
mockBannersService.findAll.mockResolvedValue(banners);
|
||||
|
||||
const result = await controller.findAll();
|
||||
|
||||
expect(service.findAll).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual(banners);
|
||||
});
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a new banner via service.create", async () => {
|
||||
const createInput: Prisma.BannerCreateInput = {
|
||||
title: "تخفیف ویژه پایان فصل محصولات سگ و گربه",
|
||||
imageUrl: "https://canina.ir/banners/seasonal.jpg",
|
||||
linkUrl: "/campaigns/seasonal",
|
||||
order: 3,
|
||||
isActive: true,
|
||||
};
|
||||
|
||||
const createdBanner = {
|
||||
id: "banner-3",
|
||||
...createInput,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
mockBannersService.create.mockResolvedValue(createdBanner);
|
||||
|
||||
const result = await controller.create(createInput);
|
||||
|
||||
expect(service.create).toHaveBeenCalledWith(createInput);
|
||||
expect(result).toEqual(createdBanner);
|
||||
});
|
||||
});
|
||||
|
||||
describe("reorder", () => {
|
||||
it("should update banner display orders via service.reorder", async () => {
|
||||
const reorderItems = [
|
||||
{ id: "banner-2", order: 1 },
|
||||
{ id: "banner-1", order: 2 },
|
||||
];
|
||||
|
||||
const expectedResponse = {
|
||||
success: true,
|
||||
message: "Banners reordered successfully",
|
||||
};
|
||||
|
||||
mockBannersService.reorder.mockResolvedValue(expectedResponse);
|
||||
|
||||
const result = await controller.reorder(reorderItems);
|
||||
|
||||
expect(service.reorder).toHaveBeenCalledWith(reorderItems);
|
||||
expect(result).toEqual(expectedResponse);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update existing banner attributes by id", async () => {
|
||||
const bannerId = "banner-1";
|
||||
const updateInput: Prisma.BannerUpdateInput = {
|
||||
title: "عنوان بهروزرسانی شده بنر اسلایدر اصلی",
|
||||
isActive: false,
|
||||
};
|
||||
|
||||
const updatedBanner = {
|
||||
id: bannerId,
|
||||
title: "عنوان بهروزرسانی شده بنر اسلایدر اصلی",
|
||||
imageUrl: "https://canina.ir/banners/joints.jpg",
|
||||
linkUrl: "/products?category=joints",
|
||||
order: 1,
|
||||
isActive: false,
|
||||
};
|
||||
|
||||
mockBannersService.update.mockResolvedValue(updatedBanner);
|
||||
|
||||
const result = await controller.update(bannerId, updateInput);
|
||||
|
||||
expect(service.update).toHaveBeenCalledWith(bannerId, updateInput);
|
||||
expect(result).toEqual(updatedBanner);
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should delete banner by id via service.remove", async () => {
|
||||
const bannerId = "banner-2";
|
||||
const removedBanner = {
|
||||
id: bannerId,
|
||||
title: "مشاوره رایگان دامپزشک آنلاین",
|
||||
};
|
||||
|
||||
mockBannersService.remove.mockResolvedValue(removedBanner);
|
||||
|
||||
const result = await controller.remove(bannerId);
|
||||
|
||||
expect(service.remove).toHaveBeenCalledWith(bannerId);
|
||||
expect(result).toEqual(removedBanner);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -8,58 +8,58 @@ import {
|
||||
Body,
|
||||
Param,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { BannersService } from './banners.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 { BannersService } from "./banners.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('Banners - بنرهای تبلیغاتی و اسلایدر')
|
||||
@Controller('banners')
|
||||
@ApiTags("Banners - بنرهای تبلیغاتی و اسلایدر")
|
||||
@Controller("banners")
|
||||
export class BannersController {
|
||||
constructor(private readonly bannersService: BannersService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'لیست تمامی بنرها' })
|
||||
@ApiOperation({ summary: "لیست تمامی بنرها" })
|
||||
findAll() {
|
||||
return this.bannersService.findAll();
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Roles("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'ایجاد بنر جدید (نیازمند ادمین)' })
|
||||
@ApiOperation({ summary: "ایجاد بنر جدید (نیازمند ادمین)" })
|
||||
create(@Body() body: Prisma.BannerCreateInput) {
|
||||
return this.bannersService.create(body);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Roles("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Put('reorder')
|
||||
@ApiOperation({ summary: 'تغییر ترتیب نمایش بنرها (نیازمند ادمین)' })
|
||||
@Put("reorder")
|
||||
@ApiOperation({ summary: "تغییر ترتیب نمایش بنرها (نیازمند ادمین)" })
|
||||
reorder(@Body() body: { id: string; order: number }[]) {
|
||||
return this.bannersService.reorder(body);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Roles("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'ویرایش بنر (نیازمند ادمین)' })
|
||||
update(@Param('id') id: string, @Body() body: Prisma.BannerUpdateInput) {
|
||||
@Patch(":id")
|
||||
@ApiOperation({ summary: "ویرایش بنر (نیازمند ادمین)" })
|
||||
update(@Param("id") id: string, @Body() body: Prisma.BannerUpdateInput) {
|
||||
return this.bannersService.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.bannersService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { BannersController } from './banners.controller';
|
||||
import { BannersService } from './banners.service';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { BannersController } from "./banners.controller";
|
||||
import { BannersService } from "./banners.service";
|
||||
import { PrismaModule } from "../prisma/prisma.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { BannersService } from "./banners.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
|
||||
describe("BannersService", () => {
|
||||
let service: BannersService;
|
||||
|
||||
const mockPrisma = {
|
||||
banner: {
|
||||
findMany: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
$transaction: jest
|
||||
.fn()
|
||||
.mockImplementation((promises) => Promise.all(promises)),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
BannersService,
|
||||
{
|
||||
provide: PrismaService,
|
||||
useValue: mockPrisma,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<BannersService>(BannersService);
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return banners ordered by order asc", async () => {
|
||||
const banners = [{ id: "b1", order: 1 }];
|
||||
mockPrisma.banner.findMany.mockResolvedValue(banners);
|
||||
|
||||
const result = await service.findAll();
|
||||
expect(mockPrisma.banner.findMany).toHaveBeenCalledWith({
|
||||
orderBy: { order: "asc" },
|
||||
});
|
||||
expect(result).toEqual(banners);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should throw NotFoundException if banner does not exist", async () => {
|
||||
mockPrisma.banner.findUnique.mockResolvedValue(null);
|
||||
await expect(service.update("b1", {})).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it("should update banner successfully", async () => {
|
||||
mockPrisma.banner.findUnique.mockResolvedValue({ id: "b1" });
|
||||
mockPrisma.banner.update.mockResolvedValue({
|
||||
id: "b1",
|
||||
title: "Banner 1",
|
||||
});
|
||||
|
||||
const result = await service.update("b1", { title: "Banner 1" });
|
||||
expect(result.title).toBe("Banner 1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should throw NotFoundException if banner does not exist", async () => {
|
||||
mockPrisma.banner.findUnique.mockResolvedValue(null);
|
||||
await expect(service.remove("b1")).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it("should delete banner successfully", async () => {
|
||||
mockPrisma.banner.findUnique.mockResolvedValue({ id: "b1" });
|
||||
mockPrisma.banner.delete.mockResolvedValue({ id: "b1" });
|
||||
|
||||
const result = await service.remove("b1");
|
||||
expect(result.id).toBe("b1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("reorder", () => {
|
||||
it("should run transaction to reorder banners", async () => {
|
||||
mockPrisma.banner.update.mockResolvedValue({});
|
||||
const items = [
|
||||
{ id: "b1", order: 2 },
|
||||
{ id: "b2", order: 1 },
|
||||
];
|
||||
|
||||
const result = await service.reorder(items);
|
||||
expect(mockPrisma.banner.update).toHaveBeenCalledTimes(2);
|
||||
expect(mockPrisma.$transaction).toHaveBeenCalled();
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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 BannersService {
|
||||
@@ -8,7 +8,7 @@ export class BannersService {
|
||||
|
||||
async findAll() {
|
||||
return this.prisma.banner.findMany({
|
||||
orderBy: { order: 'asc' },
|
||||
orderBy: { order: "asc" },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -45,6 +45,6 @@ export class BannersService {
|
||||
}),
|
||||
);
|
||||
await this.prisma.$transaction(updates);
|
||||
return { success: true, message: 'Banners reordered successfully' };
|
||||
return { success: true, message: "Banners reordered successfully" };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user