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,211 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { B2BController } from "./b2b.controller";
|
||||
import { B2BService } from "./b2b.service";
|
||||
|
||||
describe("B2BController", () => {
|
||||
let controller: B2BController;
|
||||
let service: B2BService;
|
||||
|
||||
const mockB2BService = {
|
||||
createInquiry: jest.fn(),
|
||||
findAllInquiries: jest.fn(),
|
||||
updateInquiryStatus: jest.fn(),
|
||||
getPartnerProfile: jest.fn(),
|
||||
createWholesaleOrder: jest.fn(),
|
||||
getAllPartnerAccounts: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [B2BController],
|
||||
providers: [
|
||||
{
|
||||
provide: B2BService,
|
||||
useValue: mockB2BService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<B2BController>(B2BController);
|
||||
service = module.get<B2BService>(B2BService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
describe("createInquiry", () => {
|
||||
it("should submit a new B2B inquiry", async () => {
|
||||
const inquiryData = {
|
||||
companyName: "Pet Pharmacy Aria",
|
||||
contactName: "Dr. Ahmadi",
|
||||
email: "info@aria-pet.com",
|
||||
phone: "09120000000",
|
||||
businessType: "PHARMACY",
|
||||
estimatedVolume: "50-100 units/month",
|
||||
message: "Requesting wholesale catalog for Canina joint supplements",
|
||||
};
|
||||
|
||||
const expectedResponse = {
|
||||
id: "inq-1",
|
||||
...inquiryData,
|
||||
status: "PENDING",
|
||||
createdAt: new Date(),
|
||||
};
|
||||
|
||||
mockB2BService.createInquiry.mockResolvedValue(expectedResponse);
|
||||
|
||||
const result = await controller.createInquiry(inquiryData);
|
||||
|
||||
expect(service.createInquiry).toHaveBeenCalledWith(inquiryData);
|
||||
expect(result).toEqual(expectedResponse);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findAllInquiries", () => {
|
||||
it("should return all B2B inquiries for admin", async () => {
|
||||
const inquiries = [
|
||||
{ id: "inq-1", companyName: "Clinic Alpha", status: "PENDING" },
|
||||
{ id: "inq-2", companyName: "Pet Shop Beta", status: "APPROVED" },
|
||||
];
|
||||
|
||||
mockB2BService.findAllInquiries.mockResolvedValue(inquiries);
|
||||
|
||||
const result = await controller.findAllInquiries();
|
||||
|
||||
expect(service.findAllInquiries).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual(inquiries);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateInquiryStatus", () => {
|
||||
it("should update inquiry status and admin notes", async () => {
|
||||
const id = "inq-1";
|
||||
const body = {
|
||||
status: "CONTACTED",
|
||||
adminNotes: "Contacted via phone, sent wholesale price list",
|
||||
};
|
||||
|
||||
const expectedResponse = {
|
||||
id,
|
||||
companyName: "Pet Pharmacy Aria",
|
||||
status: "CONTACTED",
|
||||
adminNotes: body.adminNotes,
|
||||
};
|
||||
|
||||
mockB2BService.updateInquiryStatus.mockResolvedValue(expectedResponse);
|
||||
|
||||
const result = await controller.updateInquiryStatus(id, body);
|
||||
|
||||
expect(service.updateInquiryStatus).toHaveBeenCalledWith(id, body);
|
||||
expect(result).toEqual(expectedResponse);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getPartnerProfile", () => {
|
||||
it("should fetch partner profile using req.user.id", async () => {
|
||||
const req = { user: { id: "user-partner-1" } };
|
||||
const partnerProfile = {
|
||||
id: "partner-1",
|
||||
userId: "user-partner-1",
|
||||
companyName: "Aria Distribution",
|
||||
status: "ACTIVE",
|
||||
discountRate: 25,
|
||||
};
|
||||
|
||||
mockB2BService.getPartnerProfile.mockResolvedValue(partnerProfile);
|
||||
|
||||
const result = await controller.getPartnerProfile(req);
|
||||
|
||||
expect(service.getPartnerProfile).toHaveBeenCalledWith("user-partner-1");
|
||||
expect(result).toEqual(partnerProfile);
|
||||
});
|
||||
|
||||
it("should fallback to req.user.userId when id is not present", async () => {
|
||||
const req = { user: { userId: "user-partner-2" } };
|
||||
const partnerProfile = {
|
||||
id: "partner-2",
|
||||
userId: "user-partner-2",
|
||||
companyName: "Beta Vet",
|
||||
status: "ACTIVE",
|
||||
};
|
||||
|
||||
mockB2BService.getPartnerProfile.mockResolvedValue(partnerProfile);
|
||||
|
||||
const result = await controller.getPartnerProfile(req);
|
||||
|
||||
expect(service.getPartnerProfile).toHaveBeenCalledWith("user-partner-2");
|
||||
expect(result).toEqual(partnerProfile);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createWholesaleOrder", () => {
|
||||
it("should create wholesale order with userId from req and body", async () => {
|
||||
const req = { user: { id: "user-partner-1" } };
|
||||
const orderBody = {
|
||||
items: [
|
||||
{ productId: "prod-canihydrox-500g", quantity: 10 },
|
||||
{ productId: "prod-gag-tablets", quantity: 5 },
|
||||
],
|
||||
totalAmount: 15000000,
|
||||
};
|
||||
|
||||
const expectedOrder = {
|
||||
id: "order-ws-1",
|
||||
userId: "user-partner-1",
|
||||
totalAmount: 15000000,
|
||||
status: "processing",
|
||||
orderItems: orderBody.items,
|
||||
};
|
||||
|
||||
mockB2BService.createWholesaleOrder.mockResolvedValue(expectedOrder);
|
||||
|
||||
const result = await controller.createWholesaleOrder(req, orderBody);
|
||||
|
||||
expect(service.createWholesaleOrder).toHaveBeenCalledWith(
|
||||
"user-partner-1",
|
||||
orderBody,
|
||||
);
|
||||
expect(result).toEqual(expectedOrder);
|
||||
});
|
||||
|
||||
it("should handle req.user.userId fallback for wholesale order", async () => {
|
||||
const req = { user: { userId: "user-partner-alt" } };
|
||||
const orderBody = {
|
||||
items: [{ productId: "prod-welpenkalk", quantity: 20 }],
|
||||
totalAmount: 8500000,
|
||||
};
|
||||
|
||||
mockB2BService.createWholesaleOrder.mockResolvedValue({
|
||||
id: "order-ws-2",
|
||||
});
|
||||
|
||||
await controller.createWholesaleOrder(req, orderBody);
|
||||
|
||||
expect(service.createWholesaleOrder).toHaveBeenCalledWith(
|
||||
"user-partner-alt",
|
||||
orderBody,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllPartnerAccounts", () => {
|
||||
it("should return all partner accounts for admin", async () => {
|
||||
const partners = [
|
||||
{ id: "p-1", companyName: "Partner 1", status: "ACTIVE" },
|
||||
{ id: "p-2", companyName: "Partner 2", status: "PENDING" },
|
||||
];
|
||||
|
||||
mockB2BService.getAllPartnerAccounts.mockResolvedValue(partners);
|
||||
|
||||
const result = await controller.getAllPartnerAccounts();
|
||||
|
||||
expect(service.getAllPartnerAccounts).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual(partners);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -7,23 +7,23 @@ import {
|
||||
Param,
|
||||
UseGuards,
|
||||
Req,
|
||||
} from '@nestjs/common';
|
||||
import { B2BService, B2BWholesaleOrderItem } from './b2b.service';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from '../auth/roles.guard';
|
||||
import { Roles } from '../auth/roles.decorator';
|
||||
} from "@nestjs/common";
|
||||
import { B2BService, B2BWholesaleOrderItem } from "./b2b.service";
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from "@nestjs/swagger";
|
||||
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";
|
||||
|
||||
@ApiTags('B2B - مدیریت پنل B2B و درخواستها')
|
||||
@Controller('b2b')
|
||||
@ApiTags("B2B - مدیریت پنل B2B و درخواستها")
|
||||
@Controller("b2b")
|
||||
export class B2BController {
|
||||
constructor(private readonly b2bService: B2BService) {}
|
||||
|
||||
@Throttle({ default: { limit: 5, ttl: 60000 } })
|
||||
@Post('inquire')
|
||||
@ApiOperation({ summary: 'ثبت استعلام جدید B2B (فرم ثبت استعلام)' })
|
||||
@Post("inquire")
|
||||
@ApiOperation({ summary: "ثبت استعلام جدید B2B (فرم ثبت استعلام)" })
|
||||
createInquiry(
|
||||
@Body()
|
||||
body: {
|
||||
@@ -40,21 +40,21 @@ export class B2BController {
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('ADMIN')
|
||||
@Roles("ADMIN")
|
||||
@ApiBearerAuth()
|
||||
@Get('inquiries')
|
||||
@ApiOperation({ summary: 'لیست تمام استعلامهای B2B (مخصوص ادمین)' })
|
||||
@Get("inquiries")
|
||||
@ApiOperation({ summary: "لیست تمام استعلامهای B2B (مخصوص ادمین)" })
|
||||
findAllInquiries() {
|
||||
return this.b2bService.findAllInquiries();
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('ADMIN')
|
||||
@Roles("ADMIN")
|
||||
@ApiBearerAuth()
|
||||
@Put('inquiries/:id')
|
||||
@ApiOperation({ summary: 'به روزرسانی وضعیت استعلام B2B' })
|
||||
@Put("inquiries/:id")
|
||||
@ApiOperation({ summary: "به روزرسانی وضعیت استعلام B2B" })
|
||||
updateInquiryStatus(
|
||||
@Param('id') id: string,
|
||||
@Param("id") id: string,
|
||||
@Body() body: { status: string; adminNotes?: string },
|
||||
) {
|
||||
return this.b2bService.updateInquiryStatus(id, body);
|
||||
@@ -62,30 +62,30 @@ export class B2BController {
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Get('partner')
|
||||
@ApiOperation({ summary: 'دریافت اطلاعات حساب همکار تجاری' })
|
||||
@Get("partner")
|
||||
@ApiOperation({ summary: "دریافت اطلاعات حساب همکار تجاری" })
|
||||
getPartnerProfile(@Req() req: { user: { id?: string; userId?: string } }) {
|
||||
const userId = req.user.id || req.user.userId || '';
|
||||
const userId = req.user.id || req.user.userId || "";
|
||||
return this.b2bService.getPartnerProfile(userId);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Post('orders')
|
||||
@ApiOperation({ summary: 'ثبت سفارش عمدهفروشی B2B' })
|
||||
@Post("orders")
|
||||
@ApiOperation({ summary: "ثبت سفارش عمدهفروشی B2B" })
|
||||
createWholesaleOrder(
|
||||
@Req() req: { user: { id?: string; userId?: string } },
|
||||
@Body() body: { items: B2BWholesaleOrderItem[]; totalAmount: number },
|
||||
) {
|
||||
const userId = req.user.id || req.user.userId || '';
|
||||
const userId = req.user.id || req.user.userId || "";
|
||||
return this.b2bService.createWholesaleOrder(userId, body);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('ADMIN')
|
||||
@Roles("ADMIN")
|
||||
@ApiBearerAuth()
|
||||
@Get('partners')
|
||||
@ApiOperation({ summary: 'لیست حسابهای همکاران تجاری (مخصوص ادمین)' })
|
||||
@Get("partners")
|
||||
@ApiOperation({ summary: "لیست حسابهای همکاران تجاری (مخصوص ادمین)" })
|
||||
getAllPartnerAccounts() {
|
||||
return this.b2bService.getAllPartnerAccounts();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { B2BController } from './b2b.controller';
|
||||
import { B2BService } from './b2b.service';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { B2BController } from "./b2b.controller";
|
||||
import { B2BService } from "./b2b.service";
|
||||
import { PrismaModule } from "../prisma/prisma.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { B2BService } from "./b2b.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { ForbiddenException, NotFoundException } from "@nestjs/common";
|
||||
|
||||
describe("B2BService", () => {
|
||||
let service: B2BService;
|
||||
|
||||
const mockPrisma = {
|
||||
b2BInquiry: {
|
||||
create: jest.fn(),
|
||||
findMany: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
update: jest.fn(),
|
||||
},
|
||||
partnerAccount: {
|
||||
findUnique: jest.fn(),
|
||||
findMany: jest.fn(),
|
||||
},
|
||||
order: {
|
||||
create: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [B2BService, { provide: PrismaService, useValue: mockPrisma }],
|
||||
}).compile();
|
||||
|
||||
service = module.get<B2BService>(B2BService);
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("updateInquiryStatus", () => {
|
||||
it("should throw NotFoundException if inquiry does not exist", async () => {
|
||||
mockPrisma.b2BInquiry.findUnique.mockResolvedValue(null);
|
||||
await expect(
|
||||
service.updateInquiryStatus("inq-404", { status: "REVIEWED" }),
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it("should update inquiry status and adminNotes when found", async () => {
|
||||
mockPrisma.b2BInquiry.findUnique.mockResolvedValue({ id: "inq-1" });
|
||||
mockPrisma.b2BInquiry.update.mockResolvedValue({
|
||||
id: "inq-1",
|
||||
status: "APPROVED",
|
||||
adminNotes: "مدارک تایید شد",
|
||||
});
|
||||
|
||||
const res = await service.updateInquiryStatus("inq-1", {
|
||||
status: "APPROVED",
|
||||
adminNotes: "مدارک تایید شد",
|
||||
});
|
||||
expect(res.status).toBe("APPROVED");
|
||||
});
|
||||
});
|
||||
|
||||
describe("createWholesaleOrder", () => {
|
||||
it("should throw ForbiddenException if user is not an active wholesale partner", async () => {
|
||||
mockPrisma.partnerAccount.findUnique.mockResolvedValue({
|
||||
userId: "u-1",
|
||||
status: "PENDING",
|
||||
});
|
||||
|
||||
await expect(
|
||||
service.createWholesaleOrder("u-1", {
|
||||
items: [{ productId: "prod-1", quantity: 20 }],
|
||||
totalAmount: 5000000,
|
||||
}),
|
||||
).rejects.toThrow(ForbiddenException);
|
||||
});
|
||||
|
||||
it("should create order when partner is active", async () => {
|
||||
mockPrisma.partnerAccount.findUnique.mockResolvedValue({
|
||||
userId: "u-partner",
|
||||
status: "ACTIVE",
|
||||
});
|
||||
mockPrisma.order.create.mockResolvedValue({
|
||||
id: "ord-wholesale-1",
|
||||
status: "processing",
|
||||
});
|
||||
|
||||
const res = await service.createWholesaleOrder("u-partner", {
|
||||
items: [{ productId: "prod-1", quantity: 10 }],
|
||||
totalAmount: 15000000,
|
||||
});
|
||||
|
||||
expect(mockPrisma.order.create).toHaveBeenCalledWith({
|
||||
data: expect.objectContaining({
|
||||
userId: "u-partner",
|
||||
totalAmount: 15000000,
|
||||
status: "processing",
|
||||
}),
|
||||
include: { orderItems: true },
|
||||
});
|
||||
expect(res.id).toBe("ord-wholesale-1");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -2,8 +2,8 @@ import {
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
ForbiddenException,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
} from "@nestjs/common";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
|
||||
export interface B2BWholesaleOrderItem {
|
||||
productId: string;
|
||||
@@ -26,14 +26,14 @@ export class B2BService {
|
||||
return this.prisma.b2BInquiry.create({
|
||||
data: {
|
||||
...data,
|
||||
status: 'PENDING',
|
||||
status: "PENDING",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findAllInquiries() {
|
||||
return this.prisma.b2BInquiry.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -71,15 +71,15 @@ export class B2BService {
|
||||
const partner = await this.prisma.partnerAccount.findUnique({
|
||||
where: { userId },
|
||||
});
|
||||
if (!partner || partner.status !== 'ACTIVE') {
|
||||
throw new ForbiddenException('User is not an active wholesale partner');
|
||||
if (!partner || partner.status !== "ACTIVE") {
|
||||
throw new ForbiddenException("User is not an active wholesale partner");
|
||||
}
|
||||
|
||||
return this.prisma.order.create({
|
||||
data: {
|
||||
userId,
|
||||
totalAmount: data.totalAmount,
|
||||
status: 'processing',
|
||||
status: "processing",
|
||||
isRefill: false,
|
||||
orderItems: {
|
||||
create: data.items.map((item) => ({
|
||||
@@ -95,7 +95,7 @@ export class B2BService {
|
||||
async getAllPartnerAccounts() {
|
||||
return this.prisma.partnerAccount.findMany({
|
||||
include: { user: true },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user