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,203 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { ContactController } from "./contact.controller";
|
||||
import { ContactService } from "./contact.service";
|
||||
|
||||
describe("ContactController", () => {
|
||||
let controller: ContactController;
|
||||
let service: ContactService;
|
||||
|
||||
const mockContactService = {
|
||||
submitContactForm: jest.fn(),
|
||||
getContactInfo: jest.fn(),
|
||||
getAllSubmissions: jest.fn(),
|
||||
updateSubmissionStatus: jest.fn(),
|
||||
updateContactInfoItems: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [ContactController],
|
||||
providers: [
|
||||
{
|
||||
provide: ContactService,
|
||||
useValue: mockContactService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<ContactController>(ContactController);
|
||||
service = module.get<ContactService>(ContactService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(controller).toBeDefined();
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("submitContact", () => {
|
||||
it("should submit a contact message successfully", async () => {
|
||||
const dto = {
|
||||
name: "رضا محمدی",
|
||||
phone: "09123456789",
|
||||
email: "reza@example.com",
|
||||
subject: "مشاوره مصرف مکمل کنین هیدروکس",
|
||||
message:
|
||||
"لطفا در مورد نحوه مصرف کانهیدروکس برای سگ روتوایلر راهنمایی کنید.",
|
||||
};
|
||||
|
||||
const expectedResponse = {
|
||||
success: true,
|
||||
message:
|
||||
"پیام شما با موفقیت ثبت شد و بهزودی کارشناسان ما با شما تماس خواهند گرفت.",
|
||||
submissionId: "sub-12345",
|
||||
};
|
||||
|
||||
mockContactService.submitContactForm.mockResolvedValue(expectedResponse);
|
||||
|
||||
const result = await controller.submitContact(dto);
|
||||
|
||||
expect(service.submitContactForm).toHaveBeenCalledWith(dto);
|
||||
expect(result).toEqual(expectedResponse);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getContactInfo", () => {
|
||||
it("should return the configured contact info cards", async () => {
|
||||
const mockContactInfo = [
|
||||
{
|
||||
id: "info-1",
|
||||
key: "branch_info",
|
||||
title: "اطلاعات نمایندگی",
|
||||
value: "تلفنهای تماس ۰۲۱-۸۸۸۸۴۴۴۴",
|
||||
icon: "phone",
|
||||
order: 1,
|
||||
},
|
||||
{
|
||||
id: "info-2",
|
||||
key: "email_info",
|
||||
title: "پست الکترونیک",
|
||||
value: "info@canina.ir",
|
||||
icon: "mail",
|
||||
order: 2,
|
||||
},
|
||||
];
|
||||
|
||||
mockContactService.getContactInfo.mockResolvedValue(mockContactInfo);
|
||||
|
||||
const result = await controller.getContactInfo();
|
||||
|
||||
expect(service.getContactInfo).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual(mockContactInfo);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllSubmissions", () => {
|
||||
it("should return paginated submissions with default page 1 and limit 20", async () => {
|
||||
const mockResult = {
|
||||
items: [
|
||||
{
|
||||
id: "sub-1",
|
||||
name: "سارا احمدی",
|
||||
phone: "09901112233",
|
||||
status: "PENDING",
|
||||
createdAt: new Date(),
|
||||
},
|
||||
],
|
||||
total: 1,
|
||||
page: 1,
|
||||
limit: 20,
|
||||
totalPages: 1,
|
||||
};
|
||||
|
||||
mockContactService.getAllSubmissions.mockResolvedValue(mockResult);
|
||||
|
||||
const result = await controller.getAllSubmissions();
|
||||
|
||||
expect(service.getAllSubmissions).toHaveBeenCalledWith(1, 20, undefined);
|
||||
expect(result).toEqual(mockResult);
|
||||
});
|
||||
|
||||
it("should parse string query parameters for page, limit, and status", async () => {
|
||||
const mockResult = {
|
||||
items: [],
|
||||
total: 0,
|
||||
page: 2,
|
||||
limit: 10,
|
||||
totalPages: 0,
|
||||
};
|
||||
|
||||
mockContactService.getAllSubmissions.mockResolvedValue(mockResult);
|
||||
|
||||
const result = await controller.getAllSubmissions("2", "10", "ANSWERED");
|
||||
|
||||
expect(service.getAllSubmissions).toHaveBeenCalledWith(2, 10, "ANSWERED");
|
||||
expect(result).toEqual(mockResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateSubmissionStatus", () => {
|
||||
it("should update submission status and admin notes", async () => {
|
||||
const subId = "sub-1";
|
||||
const updatePayload = {
|
||||
status: "RESOLVED",
|
||||
adminNotes: "با مشتری تماس گرفته شد و مشاوره ارائه گردید.",
|
||||
};
|
||||
|
||||
const updatedRecord = {
|
||||
id: subId,
|
||||
name: "علی کریمی",
|
||||
phone: "09121111111",
|
||||
status: "RESOLVED",
|
||||
adminNotes: "با مشتری تماس گرفته شد و مشاوره ارائه گردید.",
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
mockContactService.updateSubmissionStatus.mockResolvedValue(
|
||||
updatedRecord,
|
||||
);
|
||||
|
||||
const result = await controller.updateSubmissionStatus(
|
||||
subId,
|
||||
updatePayload,
|
||||
);
|
||||
|
||||
expect(service.updateSubmissionStatus).toHaveBeenCalledWith(
|
||||
subId,
|
||||
updatePayload.status,
|
||||
updatePayload.adminNotes,
|
||||
);
|
||||
expect(result).toEqual(updatedRecord);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateContactInfo", () => {
|
||||
it("should update contact info items list", async () => {
|
||||
const itemsPayload = {
|
||||
items: [
|
||||
{
|
||||
key: "address_info",
|
||||
title: "نشانی جدید دفتر مرکزی",
|
||||
value: "تهران، خیابان ولیعصر",
|
||||
icon: "map-pin",
|
||||
order: 1,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
mockContactService.updateContactInfoItems.mockResolvedValue(
|
||||
itemsPayload.items,
|
||||
);
|
||||
|
||||
const result = await controller.updateContactInfo(itemsPayload);
|
||||
|
||||
expect(service.updateContactInfoItems).toHaveBeenCalledWith(
|
||||
itemsPayload.items,
|
||||
);
|
||||
expect(result).toEqual(itemsPayload.items);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -7,15 +7,15 @@ import {
|
||||
Param,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ContactService } from './contact.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from '../auth/roles.guard';
|
||||
import { Roles } from '../auth/roles.decorator';
|
||||
} from "@nestjs/common";
|
||||
import { ContactService } from "./contact.service";
|
||||
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
|
||||
import { RolesGuard } from "../auth/roles.guard";
|
||||
import { Roles } from "../auth/roles.decorator";
|
||||
|
||||
import { Throttle } from '@nestjs/throttler';
|
||||
import { Throttle } from "@nestjs/throttler";
|
||||
|
||||
@Controller('contact')
|
||||
@Controller("contact")
|
||||
export class ContactController {
|
||||
constructor(private readonly contactService: ContactService) {}
|
||||
|
||||
@@ -34,18 +34,18 @@ export class ContactController {
|
||||
return this.contactService.submitContactForm(body);
|
||||
}
|
||||
|
||||
@Get('info')
|
||||
@Get("info")
|
||||
async getContactInfo() {
|
||||
return this.contactService.getContactInfo();
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('ADMIN', 'Admin', 'SUPERADMIN', 'SuperAdmin', 'ADMIN_EXPERT')
|
||||
@Get('submissions')
|
||||
@Roles("ADMIN", "Admin", "SUPERADMIN", "SuperAdmin", "ADMIN_EXPERT")
|
||||
@Get("submissions")
|
||||
async getAllSubmissions(
|
||||
@Query('page') page?: string,
|
||||
@Query('limit') limit?: string,
|
||||
@Query('status') status?: string,
|
||||
@Query("page") page?: string,
|
||||
@Query("limit") limit?: string,
|
||||
@Query("status") status?: string,
|
||||
) {
|
||||
return this.contactService.getAllSubmissions(
|
||||
page ? parseInt(page, 10) : 1,
|
||||
@@ -55,10 +55,10 @@ export class ContactController {
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('ADMIN', 'Admin', 'SUPERADMIN', 'SuperAdmin', 'ADMIN_EXPERT')
|
||||
@Put('submissions/:id')
|
||||
@Roles("ADMIN", "Admin", "SUPERADMIN", "SuperAdmin", "ADMIN_EXPERT")
|
||||
@Put("submissions/:id")
|
||||
async updateSubmissionStatus(
|
||||
@Param('id') id: string,
|
||||
@Param("id") id: string,
|
||||
@Body() body: { status: string; adminNotes?: string },
|
||||
) {
|
||||
return this.contactService.updateSubmissionStatus(
|
||||
@@ -69,8 +69,8 @@ export class ContactController {
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('ADMIN', 'Admin', 'SUPERADMIN', 'SuperAdmin', 'ADMIN_EXPERT')
|
||||
@Put('info')
|
||||
@Roles("ADMIN", "Admin", "SUPERADMIN", "SuperAdmin", "ADMIN_EXPERT")
|
||||
@Put("info")
|
||||
async updateContactInfo(
|
||||
@Body()
|
||||
body: {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ContactController } from './contact.controller';
|
||||
import { ContactService } from './contact.service';
|
||||
import { SmsModule } from '../common/sms.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { ContactController } from "./contact.controller";
|
||||
import { ContactService } from "./contact.service";
|
||||
import { SmsModule } from "../common/sms.module";
|
||||
|
||||
@Module({
|
||||
imports: [SmsModule],
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { ContactService, CreateContactSubmissionDto } from "./contact.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { SmsService } from "../common/services/sms.service";
|
||||
|
||||
describe("ContactService", () => {
|
||||
let service: ContactService;
|
||||
|
||||
const mockPrisma = {
|
||||
contactSubmission: {
|
||||
create: jest.fn(),
|
||||
findMany: jest.fn(),
|
||||
count: jest.fn(),
|
||||
update: jest.fn(),
|
||||
},
|
||||
contactInfo: {
|
||||
findMany: jest.fn(),
|
||||
upsert: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
const mockSmsService = {
|
||||
triggerEvent: jest.fn().mockResolvedValue(true),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
ContactService,
|
||||
{
|
||||
provide: PrismaService,
|
||||
useValue: mockPrisma,
|
||||
},
|
||||
{
|
||||
provide: SmsService,
|
||||
useValue: mockSmsService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<ContactService>(ContactService);
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("submitContactForm", () => {
|
||||
it("should create contact submission and trigger SMS event", async () => {
|
||||
const dto: CreateContactSubmissionDto = {
|
||||
name: "علی حسینی",
|
||||
phone: "09121112233",
|
||||
message: "درخواست مشاوره مکمل سگ",
|
||||
};
|
||||
mockPrisma.contactSubmission.create.mockResolvedValue({
|
||||
id: "sub-1",
|
||||
...dto,
|
||||
});
|
||||
|
||||
const result = await service.submitContactForm(dto);
|
||||
|
||||
expect(mockPrisma.contactSubmission.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
name: dto.name,
|
||||
phone: dto.phone,
|
||||
email: undefined,
|
||||
subject: undefined,
|
||||
message: dto.message,
|
||||
},
|
||||
});
|
||||
expect(mockSmsService.triggerEvent).toHaveBeenCalledWith(
|
||||
"CONTACT_SUBMITTED",
|
||||
expect.objectContaining({
|
||||
customerName: dto.name,
|
||||
customerPhone: dto.phone,
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.submissionId).toBe("sub-1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getContactInfo", () => {
|
||||
it("should return contact items if they already exist", async () => {
|
||||
const items = [
|
||||
{ id: "1", key: "branch_info", title: "تلفن", value: "021" },
|
||||
];
|
||||
mockPrisma.contactInfo.findMany.mockResolvedValue(items);
|
||||
|
||||
const result = await service.getContactInfo();
|
||||
expect(result).toEqual(items);
|
||||
expect(mockPrisma.contactInfo.upsert).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should seed default items if empty", async () => {
|
||||
mockPrisma.contactInfo.findMany
|
||||
.mockResolvedValueOnce([])
|
||||
.mockResolvedValueOnce([{ key: "branch_info" }]);
|
||||
|
||||
const result = await service.getContactInfo();
|
||||
expect(mockPrisma.contactInfo.upsert).toHaveBeenCalledTimes(3);
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllSubmissions", () => {
|
||||
it("should return paginated submissions", async () => {
|
||||
mockPrisma.contactSubmission.findMany.mockResolvedValue([
|
||||
{ id: "sub-1" },
|
||||
]);
|
||||
mockPrisma.contactSubmission.count.mockResolvedValue(1);
|
||||
|
||||
const result = await service.getAllSubmissions(1, 10);
|
||||
expect(result.total).toBe(1);
|
||||
expect(result.items).toHaveLength(1);
|
||||
expect(result.totalPages).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateSubmissionStatus", () => {
|
||||
it("should update submission status and admin notes", async () => {
|
||||
mockPrisma.contactSubmission.update.mockResolvedValue({
|
||||
id: "sub-1",
|
||||
status: "RESOLVED",
|
||||
adminNotes: "تماس گرفته شد",
|
||||
});
|
||||
|
||||
const result = await service.updateSubmissionStatus(
|
||||
"sub-1",
|
||||
"RESOLVED",
|
||||
"تماس گرفته شد",
|
||||
);
|
||||
expect(mockPrisma.contactSubmission.update).toHaveBeenCalledWith({
|
||||
where: { id: "sub-1" },
|
||||
data: { status: "RESOLVED", adminNotes: "تماس گرفته شد" },
|
||||
});
|
||||
expect(result.status).toBe("RESOLVED");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { SmsService } from '../common/services/sms.service';
|
||||
import { Injectable, Logger } from "@nestjs/common";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { SmsService } from "../common/services/sms.service";
|
||||
|
||||
export interface CreateContactSubmissionDto {
|
||||
name: string;
|
||||
@@ -40,15 +40,15 @@ export class ContactService {
|
||||
|
||||
// Send SMS confirmation & notifications via triggerEvent
|
||||
try {
|
||||
await this.smsService.triggerEvent('CONTACT_SUBMITTED', {
|
||||
await this.smsService.triggerEvent("CONTACT_SUBMITTED", {
|
||||
senderName: dto.name,
|
||||
customerName: dto.name,
|
||||
senderPhone: dto.phone,
|
||||
customerPhone: dto.phone,
|
||||
email: dto.email || '',
|
||||
subject: dto.subject || 'پیام جدید',
|
||||
message: dto.message || '',
|
||||
date: new Date().toLocaleDateString('fa-IR'),
|
||||
email: dto.email || "",
|
||||
subject: dto.subject || "پیام جدید",
|
||||
message: dto.message || "",
|
||||
date: new Date().toLocaleDateString("fa-IR"),
|
||||
});
|
||||
} catch (err) {
|
||||
this.logger.error(
|
||||
@@ -59,39 +59,39 @@ export class ContactService {
|
||||
return {
|
||||
success: true,
|
||||
message:
|
||||
'پیام شما با موفقیت ثبت شد و بهزودی کارشناسان ما با شما تماس خواهند گرفت.',
|
||||
"پیام شما با موفقیت ثبت شد و بهزودی کارشناسان ما با شما تماس خواهند گرفت.",
|
||||
submissionId: submission.id,
|
||||
};
|
||||
}
|
||||
|
||||
async getContactInfo() {
|
||||
let items = await this.prisma.contactInfo.findMany({
|
||||
orderBy: { order: 'asc' },
|
||||
orderBy: { order: "asc" },
|
||||
});
|
||||
|
||||
if (items.length === 0) {
|
||||
// Seed default items if empty
|
||||
const defaultItems = [
|
||||
{
|
||||
key: 'branch_info',
|
||||
title: 'اطلاعات نمایندگی',
|
||||
key: "branch_info",
|
||||
title: "اطلاعات نمایندگی",
|
||||
value:
|
||||
'تلفنهای تماس\n۰۲۱-۸۸۸۸ ۴۴۴۴\n\nشنبه تا چهارشنبه ۹:۰۰ الی ۱۸:۰۰',
|
||||
icon: 'phone',
|
||||
"تلفنهای تماس\n۰۲۱-۸۸۸۸ ۴۴۴۴\n\nشنبه تا چهارشنبه ۹:۰۰ الی ۱۸:۰۰",
|
||||
icon: "phone",
|
||||
order: 1,
|
||||
},
|
||||
{
|
||||
key: 'email_info',
|
||||
title: 'پست الکترونیک',
|
||||
value: 'info@canina.ir\n\nپاسخگویی در کمتر از ۲۴ ساعت کاری',
|
||||
icon: 'mail',
|
||||
key: "email_info",
|
||||
title: "پست الکترونیک",
|
||||
value: "info@canina.ir\n\nپاسخگویی در کمتر از ۲۴ ساعت کاری",
|
||||
icon: "mail",
|
||||
order: 2,
|
||||
},
|
||||
{
|
||||
key: 'address_info',
|
||||
title: 'نشانی دفتر مرکزی',
|
||||
value: 'تهران، جردن، خیابان سعیدی، ساختمان کنینا، طبقه ۵، واحد ۱۹',
|
||||
icon: 'map-pin',
|
||||
key: "address_info",
|
||||
title: "نشانی دفتر مرکزی",
|
||||
value: "تهران، جردن، خیابان سعیدی، ساختمان کنینا، طبقه ۵، واحد ۱۹",
|
||||
icon: "map-pin",
|
||||
order: 3,
|
||||
},
|
||||
];
|
||||
@@ -105,7 +105,7 @@ export class ContactService {
|
||||
}
|
||||
|
||||
items = await this.prisma.contactInfo.findMany({
|
||||
orderBy: { order: 'asc' },
|
||||
orderBy: { order: "asc" },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ export class ContactService {
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
orderBy: { createdAt: "desc" },
|
||||
}),
|
||||
this.prisma.contactSubmission.count({ where }),
|
||||
]);
|
||||
|
||||
Reference in new issue
Block a user