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,73 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { PetsController } from './pets.controller';
|
||||
import { PetsService } from './pets.service';
|
||||
|
||||
describe('PetsController', () => {
|
||||
let controller: PetsController;
|
||||
let service: PetsService;
|
||||
|
||||
const mockPetsService = {
|
||||
create: jest.fn().mockResolvedValue({ id: 'pet-id', name: 'Buddy' }),
|
||||
findAllByUser: jest.fn().mockResolvedValue([{ id: 'pet-id', name: 'Buddy' }]),
|
||||
findOne: jest.fn().mockResolvedValue({ id: 'pet-id', name: 'Buddy' }),
|
||||
update: jest.fn().mockResolvedValue({ id: 'pet-id', name: 'Buddy New' }),
|
||||
remove: jest.fn().mockResolvedValue({ success: true }),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [PetsController],
|
||||
providers: [
|
||||
{ provide: PetsService, useValue: mockPetsService },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<PetsController>(PetsController);
|
||||
service = module.get<PetsService>(PetsService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
it('should create pet', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const dto = { name: 'Buddy', type: 'Dog' };
|
||||
const result = await controller.create(req, dto);
|
||||
expect(service.create).toHaveBeenCalledWith('user-id', dto);
|
||||
expect(result.id).toBe('pet-id');
|
||||
});
|
||||
|
||||
it('should findAll pets', 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 findOne pet', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const result = await controller.findOne(req, 'pet-id');
|
||||
expect(service.findOne).toHaveBeenCalledWith('pet-id', 'user-id');
|
||||
expect(result.id).toBe('pet-id');
|
||||
});
|
||||
|
||||
it('should update pet', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const dto = { name: 'Buddy New' };
|
||||
const result = await controller.update(req, 'pet-id', dto);
|
||||
expect(service.update).toHaveBeenCalledWith('pet-id', 'user-id', dto);
|
||||
expect(result.name).toBe('Buddy New');
|
||||
});
|
||||
|
||||
it('should remove pet', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const result = await controller.remove(req, 'pet-id');
|
||||
expect(service.remove).toHaveBeenCalledWith('pet-id', 'user-id');
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,43 +1,178 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Req } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Req, HttpStatus } from '@nestjs/common';
|
||||
import { PetsService } from './pets.service';
|
||||
import { CreatePetDto } from './dto/create-pet.dto';
|
||||
import { UpdatePetDto } from './dto/update-pet.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('Pets')
|
||||
@ApiTags('Pets - مدیریت حیوانات خانگی')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Controller('pets')
|
||||
@ApiResponse({
|
||||
status: HttpStatus.UNAUTHORIZED,
|
||||
description: 'عدم احراز هویت - توکن معتبر نیست یا ارسال نشده است',
|
||||
schema: {
|
||||
example: {
|
||||
success: false,
|
||||
message: 'Unauthorized',
|
||||
code: 'UNAUTHORIZED',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
})
|
||||
export class PetsController {
|
||||
constructor(private readonly petsService: PetsService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'ثبت حیوان خانگی جدید' })
|
||||
@ApiCreatedResponse({
|
||||
description: 'حیوان خانگی جدید با موفقیت ثبت شد',
|
||||
schema: {
|
||||
example: {
|
||||
id: 'c7b8d9e0-1234-5678-abcd-ef1234567890',
|
||||
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
||||
name: 'بادی',
|
||||
type: 'سگ',
|
||||
breed: 'ژرمن شپرد',
|
||||
age: 3,
|
||||
weight: '25.50',
|
||||
activityLevel: 'متوسط',
|
||||
imageUrl: null,
|
||||
createdAt: '2026-05-26T18:10:00.000Z'
|
||||
}
|
||||
}
|
||||
})
|
||||
@ApiBadRequestResponse({
|
||||
description: 'خطا در صحتسنجی فیلدهای ورودی',
|
||||
schema: {
|
||||
example: {
|
||||
success: false,
|
||||
message: 'نوع حیوان خانگی اجباری است',
|
||||
code: 'BAD_REQUEST',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
})
|
||||
create(@Req() req: any, @Body() createPetDto: CreatePetDto) {
|
||||
return this.petsService.create(req.user.id, createPetDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'لیست حیوانات خانگی کاربر' })
|
||||
@ApiOperation({ summary: 'لیست حیوانات خانگی کاربر فعلی' })
|
||||
@ApiOkResponse({
|
||||
description: 'آرایهای از حیوانات خانگی ثبت شده کاربر',
|
||||
schema: {
|
||||
example: [
|
||||
{
|
||||
id: 'c7b8d9e0-1234-5678-abcd-ef1234567890',
|
||||
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
||||
name: 'بادی',
|
||||
type: 'سگ',
|
||||
breed: 'ژرمن شپرد',
|
||||
age: 3,
|
||||
weight: '25.50',
|
||||
activityLevel: 'متوسط',
|
||||
imageUrl: null,
|
||||
createdAt: '2026-05-26T18:10:00.000Z'
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
findAll(@Req() req: any) {
|
||||
return this.petsService.findAllByUser(req.user.id);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'دریافت جزئیات یک حیوان خانگی' })
|
||||
@ApiOperation({ summary: 'دریافت جزئیات کامل یک حیوان خانگی' })
|
||||
@ApiOkResponse({
|
||||
description: 'اطلاعات کامل حیوان خانگی مشخص شده با ID',
|
||||
schema: {
|
||||
example: {
|
||||
id: 'c7b8d9e0-1234-5678-abcd-ef1234567890',
|
||||
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
||||
name: 'بادی',
|
||||
type: 'سگ',
|
||||
breed: 'ژرمن شپرد',
|
||||
age: 3,
|
||||
weight: '25.50',
|
||||
activityLevel: 'متوسط',
|
||||
imageUrl: null,
|
||||
medicalConditions: [],
|
||||
reminders: [],
|
||||
healthLogs: [],
|
||||
createdAt: '2026-05-26T18:10:00.000Z'
|
||||
}
|
||||
}
|
||||
})
|
||||
@ApiNotFoundResponse({
|
||||
description: 'حیوان خانگی پیدا نشد یا متعلق به کاربر جاری نیست',
|
||||
schema: {
|
||||
example: {
|
||||
success: false,
|
||||
message: 'Pet not found or unauthorized',
|
||||
code: 'NOT_FOUND',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
})
|
||||
findOne(@Req() req: any, @Param('id') id: string) {
|
||||
return this.petsService.findOne(id, req.user.id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'بروزرسانی اطلاعات حیوان خانگی' })
|
||||
@ApiOkResponse({
|
||||
description: 'اطلاعات حیوان خانگی با موفقیت بهروزرسانی شد',
|
||||
schema: {
|
||||
example: {
|
||||
id: 'c7b8d9e0-1234-5678-abcd-ef1234567890',
|
||||
name: 'بادی قهرمان',
|
||||
type: 'سگ',
|
||||
breed: 'ژرمن شپرد',
|
||||
age: 4,
|
||||
weight: '26.00',
|
||||
activityLevel: 'زیاد'
|
||||
}
|
||||
}
|
||||
})
|
||||
@ApiNotFoundResponse({
|
||||
description: 'حیوان خانگی یافت نشد',
|
||||
schema: {
|
||||
example: {
|
||||
success: false,
|
||||
message: 'Pet not found',
|
||||
code: 'NOT_FOUND',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
})
|
||||
update(@Req() req: any, @Param('id') id: string, @Body() updatePetDto: UpdatePetDto) {
|
||||
return this.petsService.update(id, req.user.id, updatePetDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@ApiOperation({ summary: 'حذف حیوان خانگی' })
|
||||
@ApiOkResponse({
|
||||
description: 'حیوان خانگی با موفقیت حذف شد',
|
||||
schema: {
|
||||
example: {
|
||||
success: true,
|
||||
message: 'حیوان خانگی با موفقیت حذف شد'
|
||||
}
|
||||
}
|
||||
})
|
||||
@ApiNotFoundResponse({
|
||||
description: 'حیوان خانگی یافت نشد',
|
||||
schema: {
|
||||
example: {
|
||||
success: false,
|
||||
message: 'Pet not found',
|
||||
code: 'NOT_FOUND',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
})
|
||||
remove(@Req() req: any, @Param('id') id: string) {
|
||||
return this.petsService.remove(id, req.user.id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { PetsService } from './pets.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { NotFoundException } from '@nestjs/common';
|
||||
|
||||
describe('PetsService', () => {
|
||||
let service: PetsService;
|
||||
let prisma: PrismaService;
|
||||
|
||||
const mockPrisma = {
|
||||
pet: {
|
||||
create: jest.fn(),
|
||||
findMany: jest.fn(),
|
||||
findFirst: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
PetsService,
|
||||
{ provide: PrismaService, useValue: mockPrisma },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<PetsService>(PetsService);
|
||||
prisma = module.get<PrismaService>(PrismaService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
it('should create pet', async () => {
|
||||
const dto = { name: 'Buddy', type: 'Dog', weight: 12.5 };
|
||||
mockPrisma.pet.create.mockResolvedValue({ id: 'pet-id', ...dto });
|
||||
|
||||
const result = await service.create('user-id', dto);
|
||||
expect(prisma.pet.create).toHaveBeenCalled();
|
||||
expect(result.name).toBe('Buddy');
|
||||
});
|
||||
|
||||
it('should find all pets by user', async () => {
|
||||
mockPrisma.pet.findMany.mockResolvedValue([{ id: 'pet-1' }]);
|
||||
const result = await service.findAllByUser('user-id');
|
||||
expect(prisma.pet.findMany).toHaveBeenCalledWith({
|
||||
where: { userId: 'user-id' },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
describe('findOne', () => {
|
||||
it('should throw NotFoundException if pet not found', async () => {
|
||||
mockPrisma.pet.findFirst.mockResolvedValue(null);
|
||||
await expect(service.findOne('pet-id', 'user-id')).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it('should return pet if found', async () => {
|
||||
const pet = { id: 'pet-id', userId: 'user-id' };
|
||||
mockPrisma.pet.findFirst.mockResolvedValue(pet);
|
||||
const result = await service.findOne('pet-id', 'user-id');
|
||||
expect(result).toEqual(pet);
|
||||
});
|
||||
});
|
||||
|
||||
it('should update pet', async () => {
|
||||
const pet = { id: 'pet-id', userId: 'user-id' };
|
||||
mockPrisma.pet.findFirst.mockResolvedValue(pet);
|
||||
const dto = { name: 'Buddy New', type: 'Dog' };
|
||||
mockPrisma.pet.update.mockResolvedValue({ ...pet, ...dto });
|
||||
|
||||
const result = await service.update('pet-id', 'user-id', dto);
|
||||
expect(prisma.pet.update).toHaveBeenCalled();
|
||||
expect(result.name).toBe('Buddy New');
|
||||
});
|
||||
|
||||
it('should remove pet', async () => {
|
||||
const pet = { id: 'pet-id', userId: 'user-id' };
|
||||
mockPrisma.pet.findFirst.mockResolvedValue(pet);
|
||||
mockPrisma.pet.delete.mockResolvedValue(pet);
|
||||
|
||||
const result = await service.remove('pet-id', 'user-id');
|
||||
expect(prisma.pet.delete).toHaveBeenCalled();
|
||||
expect(result.id).toBe('pet-id');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user