feat: complete testing, security, dynamic content, and devops overhaul
This commit is contained in:
1 parent
467532713c
commit
40ff169dc2
52 files changed
+3918
-104
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { OrdersController } from './orders.controller';
|
||||
import { OrdersService } from './orders.service';
|
||||
|
||||
describe('OrdersController', () => {
|
||||
let controller: OrdersController;
|
||||
let service: OrdersService;
|
||||
|
||||
const mockOrdersService = {
|
||||
create: jest.fn().mockResolvedValue({ id: 'order-id', totalAmount: 1000 }),
|
||||
findAllByUser: jest.fn().mockResolvedValue([{ id: 'order-id', totalAmount: 1000 }]),
|
||||
findOne: jest.fn().mockResolvedValue({ id: 'order-id', totalAmount: 1000 }),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [OrdersController],
|
||||
providers: [
|
||||
{ provide: OrdersService, useValue: mockOrdersService },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<OrdersController>(OrdersController);
|
||||
service = module.get<OrdersService>(OrdersService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
it('should create order', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const dto = { items: [{ productId: 'prod-1', quantity: 2 }] };
|
||||
const result = await controller.create(req, dto);
|
||||
expect(service.create).toHaveBeenCalledWith('user-id', dto);
|
||||
expect(result.id).toBe('order-id');
|
||||
});
|
||||
|
||||
it('should list orders of user', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const result = await controller.findAll(req);
|
||||
expect(service.findAllByUser).toHaveBeenCalledWith('user-id');
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should find one order', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const result = await controller.findOne(req, 'order-id');
|
||||
expect(service.findOne).toHaveBeenCalledWith('order-id', 'user-id');
|
||||
expect(result.id).toBe('order-id');
|
||||
});
|
||||
});
|
||||
@@ -1,30 +1,120 @@
|
||||
import { Controller, Get, Post, Body, Param, UseGuards, Req } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Body, Param, UseGuards, Req, HttpStatus } from '@nestjs/common';
|
||||
import { OrdersService } from './orders.service';
|
||||
import { CreateOrderDto } from './dto/create-order.dto';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse, ApiOkResponse, ApiCreatedResponse, ApiBadRequestResponse, ApiNotFoundResponse } from '@nestjs/swagger';
|
||||
|
||||
@ApiTags('Orders')
|
||||
@ApiTags('Orders - مدیریت سفارشها')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Controller('orders')
|
||||
@ApiResponse({
|
||||
status: HttpStatus.UNAUTHORIZED,
|
||||
description: 'عدم احراز هویت - توکن معتبر نیست یا ارسال نشده است',
|
||||
schema: {
|
||||
example: {
|
||||
success: false,
|
||||
message: 'Unauthorized',
|
||||
code: 'UNAUTHORIZED',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
})
|
||||
export class OrdersController {
|
||||
constructor(private readonly ordersService: OrdersService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'ثبت سفارش جدید' })
|
||||
@ApiCreatedResponse({
|
||||
description: 'سفارش با موفقیت ثبت شد',
|
||||
schema: {
|
||||
example: {
|
||||
id: 'e1d2c3b4-1234-5678-abcd-ef1234567890',
|
||||
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
||||
couponId: null,
|
||||
totalAmount: '3500000.00',
|
||||
charityDonation: '10000.00',
|
||||
status: 'processing',
|
||||
trackingNumber: null,
|
||||
createdAt: '2026-05-26T18:10:00.000Z'
|
||||
}
|
||||
}
|
||||
})
|
||||
@ApiBadRequestResponse({
|
||||
description: 'اعتبارسنجی اقلام سبد خرید با خطا مواجه شد',
|
||||
schema: {
|
||||
example: {
|
||||
success: false,
|
||||
message: 'سبد خرید نمیتواند خالی باشد',
|
||||
code: 'BAD_REQUEST',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
})
|
||||
create(@Req() req: any, @Body() createOrderDto: CreateOrderDto) {
|
||||
return this.ordersService.create(req.user.id, createOrderDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'لیست سفارشهای کاربر' })
|
||||
@ApiOperation({ summary: 'لیست سفارشهای کاربر فعلی' })
|
||||
@ApiOkResponse({
|
||||
description: 'آرایهای از سفارشهای ثبت شده کاربر',
|
||||
schema: {
|
||||
example: [
|
||||
{
|
||||
id: 'e1d2c3b4-1234-5678-abcd-ef1234567890',
|
||||
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
||||
totalAmount: '3500000.00',
|
||||
charityDonation: '10000.00',
|
||||
status: 'processing',
|
||||
createdAt: '2026-05-26T18:10:00.000Z'
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
findAll(@Req() req: any) {
|
||||
return this.ordersService.findAllByUser(req.user.id);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'جزئیات یک سفارش' })
|
||||
@ApiOperation({ summary: 'دریافت جزئیات یک سفارش خاص' })
|
||||
@ApiOkResponse({
|
||||
description: 'اطلاعات کامل سفارش مشخص شده به همراه اقلام سفارش',
|
||||
schema: {
|
||||
example: {
|
||||
id: 'e1d2c3b4-1234-5678-abcd-ef1234567890',
|
||||
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
||||
totalAmount: '3500000.00',
|
||||
charityDonation: '10000.00',
|
||||
status: 'processing',
|
||||
trackingNumber: 'TRK-987654321',
|
||||
createdAt: '2026-05-26T18:10:00.000Z',
|
||||
orderItems: [
|
||||
{
|
||||
id: 'item-1',
|
||||
quantity: 2,
|
||||
product: {
|
||||
id: 'a1b2c3d4-1234-5678-abcd-ef1234567890',
|
||||
name: 'Canhydrox GAG (کنهیدروکس)',
|
||||
priceDisplay: '۱,۷۵۰,۰۰۰ تومان',
|
||||
imageUrl: 'https://example.com/canhydrox.png'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
})
|
||||
@ApiNotFoundResponse({
|
||||
description: 'سفارش یافت نشد یا متعلق به کاربر فعلی نیست',
|
||||
schema: {
|
||||
example: {
|
||||
success: false,
|
||||
message: 'Order not found',
|
||||
code: 'NOT_FOUND',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
})
|
||||
findOne(@Req() req: any, @Param('id') id: string) {
|
||||
return this.ordersService.findOne(id, req.user.id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { OrdersService } from './orders.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { NotFoundException, BadRequestException } from '@nestjs/common';
|
||||
|
||||
describe('OrdersService', () => {
|
||||
let service: OrdersService;
|
||||
let prisma: PrismaService;
|
||||
|
||||
const mockPrisma = {
|
||||
product: {
|
||||
findUnique: jest.fn(),
|
||||
},
|
||||
order: {
|
||||
create: jest.fn(),
|
||||
findMany: jest.fn(),
|
||||
findFirst: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
OrdersService,
|
||||
{ provide: PrismaService, useValue: mockPrisma },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<OrdersService>(OrdersService);
|
||||
prisma = module.get<PrismaService>(PrismaService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should throw NotFoundException if product does not exist', async () => {
|
||||
mockPrisma.product.findUnique.mockResolvedValue(null);
|
||||
const dto = { items: [{ productId: 'invalid-prod', quantity: 2 }] };
|
||||
await expect(service.create('user-id', dto)).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it('should throw BadRequestException if items are empty', async () => {
|
||||
const dto = { items: [] };
|
||||
await expect(service.create('user-id', dto)).rejects.toThrow(BadRequestException);
|
||||
});
|
||||
|
||||
it('should successfully create order and sum amounts', async () => {
|
||||
const prod = { id: 'prod-1', priceValue: 1000 };
|
||||
mockPrisma.product.findUnique.mockResolvedValue(prod);
|
||||
mockPrisma.order.create.mockResolvedValue({ id: 'order-1', totalAmount: 2000 });
|
||||
|
||||
const dto = { items: [{ productId: 'prod-1', quantity: 2 }] };
|
||||
const result = await service.create('user-id', dto);
|
||||
|
||||
expect(prisma.product.findUnique).toHaveBeenCalledWith({ where: { id: 'prod-1' } });
|
||||
expect(prisma.order.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
userId: 'user-id',
|
||||
totalAmount: 2000,
|
||||
status: 'processing',
|
||||
orderItems: {
|
||||
create: [{ productId: 'prod-1', quantity: 2 }],
|
||||
},
|
||||
},
|
||||
include: {
|
||||
orderItems: {
|
||||
include: { product: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(result.id).toBe('order-1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findAllByUser', () => {
|
||||
it('should find all orders of a user', async () => {
|
||||
mockPrisma.order.findMany.mockResolvedValue([{ id: 'order-1' }]);
|
||||
const result = await service.findAllByUser('user-id');
|
||||
expect(prisma.order.findMany).toHaveBeenCalled();
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findOne', () => {
|
||||
it('should throw NotFoundException if order does not exist', async () => {
|
||||
mockPrisma.order.findFirst.mockResolvedValue(null);
|
||||
await expect(service.findOne('order-id', 'user-id')).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it('should return order if found', async () => {
|
||||
const order = { id: 'order-1', userId: 'user-id' };
|
||||
mockPrisma.order.findFirst.mockResolvedValue(order);
|
||||
const result = await service.findOne('order-1', 'user-id');
|
||||
expect(result).toEqual(order);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user