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,153 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { FaqController } from "./faq.controller";
|
||||
import { FaqService } from "./faq.service";
|
||||
|
||||
describe("FaqController", () => {
|
||||
let controller: FaqController;
|
||||
let service: FaqService;
|
||||
|
||||
const mockFaqService = {
|
||||
findActive: jest.fn(),
|
||||
findAll: jest.fn(),
|
||||
create: jest.fn(),
|
||||
reorder: jest.fn(),
|
||||
update: jest.fn(),
|
||||
remove: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [FaqController],
|
||||
providers: [
|
||||
{
|
||||
provide: FaqService,
|
||||
useValue: mockFaqService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<FaqController>(FaqController);
|
||||
service = module.get<FaqService>(FaqService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
describe("findActive", () => {
|
||||
it("should return only active FAQs for public view", async () => {
|
||||
const activeFaqs = [
|
||||
{
|
||||
id: "faq-1",
|
||||
question: "زمان ارسال سفارش؟",
|
||||
answer: "۲۴ الی ۴۸ ساعت",
|
||||
order: 1,
|
||||
isActive: true,
|
||||
},
|
||||
];
|
||||
mockFaqService.findActive.mockResolvedValue(activeFaqs);
|
||||
|
||||
const result = await controller.findActive();
|
||||
|
||||
expect(service.findActive).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual(activeFaqs);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return all FAQs for admin view", async () => {
|
||||
const allFaqs = [
|
||||
{ id: "faq-1", question: "Q1", answer: "A1", order: 1, isActive: true },
|
||||
{
|
||||
id: "faq-2",
|
||||
question: "Q2",
|
||||
answer: "A2",
|
||||
order: 2,
|
||||
isActive: false,
|
||||
},
|
||||
];
|
||||
mockFaqService.findAll.mockResolvedValue(allFaqs);
|
||||
|
||||
const result = await controller.findAll();
|
||||
|
||||
expect(service.findAll).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual(allFaqs);
|
||||
});
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a new FAQ entry", async () => {
|
||||
const body = {
|
||||
question: "آیا مکملها گرید دارویی آلمان هستند؟",
|
||||
answer:
|
||||
"بله، تمام محصولات مستقیماً از شرکت Canina Pharma آلمان تهیه میشوند.",
|
||||
order: 1,
|
||||
isActive: true,
|
||||
};
|
||||
const createdFaq = { id: "faq-created", ...body };
|
||||
mockFaqService.create.mockResolvedValue(createdFaq);
|
||||
|
||||
const result = await controller.create(body);
|
||||
|
||||
expect(service.create).toHaveBeenCalledWith(body);
|
||||
expect(result).toEqual(createdFaq);
|
||||
});
|
||||
});
|
||||
|
||||
describe("reorder", () => {
|
||||
it("should reorder FAQs using provided list of ids and order ranks", async () => {
|
||||
const body = {
|
||||
items: [
|
||||
{ id: "faq-1", order: 2 },
|
||||
{ id: "faq-2", order: 1 },
|
||||
],
|
||||
};
|
||||
mockFaqService.reorder.mockResolvedValue({ success: true });
|
||||
|
||||
const result = await controller.reorder(body);
|
||||
|
||||
expect(service.reorder).toHaveBeenCalledWith(body.items);
|
||||
expect(result).toEqual({ success: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update an existing FAQ", async () => {
|
||||
const id = "faq-1";
|
||||
const updateBody = {
|
||||
question: "سوال ویرایش شده",
|
||||
isActive: false,
|
||||
};
|
||||
const updatedResult = {
|
||||
id,
|
||||
question: "سوال ویرایش شده",
|
||||
answer: "پاسخ قبلی",
|
||||
order: 1,
|
||||
isActive: false,
|
||||
};
|
||||
mockFaqService.update.mockResolvedValue(updatedResult);
|
||||
|
||||
const result = await controller.update(id, updateBody);
|
||||
|
||||
expect(service.update).toHaveBeenCalledWith(id, updateBody);
|
||||
expect(result).toEqual(updatedResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should delete an existing FAQ", async () => {
|
||||
const id = "faq-1";
|
||||
const deletedFaq = { id, question: "حذف شده" };
|
||||
mockFaqService.remove.mockResolvedValue(deletedFaq);
|
||||
|
||||
const result = await controller.remove(id);
|
||||
|
||||
expect(service.remove).toHaveBeenCalledWith(id);
|
||||
expect(result).toEqual(deletedFaq);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -7,38 +7,38 @@ import {
|
||||
Body,
|
||||
Param,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { FaqService } from './faq.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 { FaqService } from "./faq.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('FAQ - سوالات متداول')
|
||||
@Controller('faq')
|
||||
@ApiTags("FAQ - سوالات متداول")
|
||||
@Controller("faq")
|
||||
export class FaqController {
|
||||
constructor(private readonly faqService: FaqService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'دریافت سوالات متداول فعال برای صفحه اصلی' })
|
||||
@ApiOperation({ summary: "دریافت سوالات متداول فعال برای صفحه اصلی" })
|
||||
findActive() {
|
||||
return this.faqService.findActive();
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Roles("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Get('admin')
|
||||
@ApiOperation({ summary: 'دریافت تمام سوالات (ادمین)' })
|
||||
@Get("admin")
|
||||
@ApiOperation({ summary: "دریافت تمام سوالات (ادمین)" })
|
||||
findAll() {
|
||||
return this.faqService.findAll();
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Roles("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'ایجاد سوال متداول جدید' })
|
||||
@ApiOperation({ summary: "ایجاد سوال متداول جدید" })
|
||||
create(
|
||||
@Body()
|
||||
body: {
|
||||
@@ -52,21 +52,21 @@ export class FaqController {
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Roles("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Put('reorder')
|
||||
@ApiOperation({ summary: 'مرتبسازی سوالات' })
|
||||
@Put("reorder")
|
||||
@ApiOperation({ summary: "مرتبسازی سوالات" })
|
||||
reorder(@Body() body: { items: { id: string; order: number }[] }) {
|
||||
return this.faqService.reorder(body.items);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Roles("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Put(':id')
|
||||
@ApiOperation({ summary: 'ویرایش سوال متداول' })
|
||||
@Put(":id")
|
||||
@ApiOperation({ summary: "ویرایش سوال متداول" })
|
||||
update(
|
||||
@Param('id') id: string,
|
||||
@Param("id") id: string,
|
||||
@Body()
|
||||
body: Partial<{
|
||||
question: string;
|
||||
@@ -79,11 +79,11 @@ export class FaqController {
|
||||
}
|
||||
|
||||
@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.faqService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { FaqController } from './faq.controller';
|
||||
import { FaqService } from './faq.service';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { FaqController } from "./faq.controller";
|
||||
import { FaqService } from "./faq.service";
|
||||
|
||||
@Module({
|
||||
controllers: [FaqController],
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { FaqService } from "./faq.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
|
||||
describe("FaqService", () => {
|
||||
let service: FaqService;
|
||||
|
||||
const mockPrisma = {
|
||||
fAQ: {
|
||||
findMany: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
FaqService,
|
||||
{
|
||||
provide: PrismaService,
|
||||
useValue: mockPrisma,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<FaqService>(FaqService);
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("findActive", () => {
|
||||
it("should return only active faqs ordered by order asc", async () => {
|
||||
const activeFaqs = [
|
||||
{ id: "1", question: "Q1", answer: "A1", isActive: true, order: 0 },
|
||||
];
|
||||
mockPrisma.fAQ.findMany.mockResolvedValue(activeFaqs);
|
||||
|
||||
const result = await service.findActive();
|
||||
expect(mockPrisma.fAQ.findMany).toHaveBeenCalledWith({
|
||||
where: { isActive: true },
|
||||
orderBy: { order: "asc" },
|
||||
});
|
||||
expect(result).toEqual(activeFaqs);
|
||||
});
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create new FAQ", async () => {
|
||||
const dto = {
|
||||
question: "New Q",
|
||||
answer: "New A",
|
||||
order: 1,
|
||||
isActive: true,
|
||||
};
|
||||
mockPrisma.fAQ.create.mockResolvedValue({ id: "faq-1", ...dto });
|
||||
|
||||
const result = await service.create(dto);
|
||||
expect(mockPrisma.fAQ.create).toHaveBeenCalledWith({ data: dto });
|
||||
expect(result.id).toBe("faq-1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should throw NotFoundException if FAQ not found", async () => {
|
||||
mockPrisma.fAQ.findUnique.mockResolvedValue(null);
|
||||
await expect(
|
||||
service.update("invalid-id", { question: "updated" }),
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it("should update FAQ if found", async () => {
|
||||
mockPrisma.fAQ.findUnique.mockResolvedValue({
|
||||
id: "faq-1",
|
||||
question: "Old",
|
||||
});
|
||||
mockPrisma.fAQ.update.mockResolvedValue({
|
||||
id: "faq-1",
|
||||
question: "Updated",
|
||||
});
|
||||
|
||||
const result = await service.update("faq-1", { question: "Updated" });
|
||||
expect(result.question).toBe("Updated");
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should throw NotFoundException if FAQ not found", async () => {
|
||||
mockPrisma.fAQ.findUnique.mockResolvedValue(null);
|
||||
await expect(service.remove("invalid-id")).rejects.toThrow(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it("should delete FAQ if found", async () => {
|
||||
mockPrisma.fAQ.findUnique.mockResolvedValue({ id: "faq-1" });
|
||||
mockPrisma.fAQ.delete.mockResolvedValue({ id: "faq-1" });
|
||||
|
||||
const result = await service.remove("faq-1");
|
||||
expect(result.id).toBe("faq-1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("reorder", () => {
|
||||
it("should update orders for each item", async () => {
|
||||
mockPrisma.fAQ.update.mockResolvedValue({});
|
||||
const items = [
|
||||
{ id: "1", order: 2 },
|
||||
{ id: "2", order: 1 },
|
||||
];
|
||||
const result = await service.reorder(items);
|
||||
expect(mockPrisma.fAQ.update).toHaveBeenCalledTimes(2);
|
||||
expect(result).toEqual({ success: true });
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
|
||||
@Injectable()
|
||||
export class FaqService {
|
||||
@@ -9,14 +9,14 @@ export class FaqService {
|
||||
async findActive() {
|
||||
return this.prisma.fAQ.findMany({
|
||||
where: { isActive: true },
|
||||
orderBy: { order: 'asc' },
|
||||
orderBy: { order: "asc" },
|
||||
});
|
||||
}
|
||||
|
||||
// Admin: all FAQs
|
||||
async findAll() {
|
||||
return this.prisma.fAQ.findMany({
|
||||
orderBy: { order: 'asc' },
|
||||
orderBy: { order: "asc" },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -39,13 +39,13 @@ export class FaqService {
|
||||
}>,
|
||||
) {
|
||||
const faq = await this.prisma.fAQ.findUnique({ where: { id } });
|
||||
if (!faq) throw new NotFoundException('سوال یافت نشد');
|
||||
if (!faq) throw new NotFoundException("سوال یافت نشد");
|
||||
return this.prisma.fAQ.update({ where: { id }, data });
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const faq = await this.prisma.fAQ.findUnique({ where: { id } });
|
||||
if (!faq) throw new NotFoundException('سوال یافت نشد');
|
||||
if (!faq) throw new NotFoundException("سوال یافت نشد");
|
||||
return this.prisma.fAQ.delete({ where: { id } });
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user