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,98 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { UsersController } from './users.controller';
|
||||
import { UsersService } from './users.service';
|
||||
|
||||
describe('UsersController', () => {
|
||||
let controller: UsersController;
|
||||
let service: UsersService;
|
||||
|
||||
const mockUsersService = {
|
||||
findById: jest.fn().mockResolvedValue({ id: 'user-id', firstName: 'Test' }),
|
||||
update: jest.fn().mockResolvedValue({ id: 'user-id', firstName: 'Updated' }),
|
||||
addAddress: jest.fn().mockResolvedValue({ id: 'addr-id', title: 'Home' }),
|
||||
updateAddress: jest.fn().mockResolvedValue({ id: 'addr-id', title: 'Work' }),
|
||||
deleteAddress: jest.fn().mockResolvedValue({ success: true }),
|
||||
setDefaultAddress: jest.fn().mockResolvedValue({ success: true }),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [UsersController],
|
||||
providers: [
|
||||
{ provide: UsersService, useValue: mockUsersService },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<UsersController>(UsersController);
|
||||
service = module.get<UsersService>(UsersService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
it('should getProfile', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const result = await controller.getProfile(req);
|
||||
expect(service.findById).toHaveBeenCalledWith('user-id');
|
||||
expect(result.id).toBe('user-id');
|
||||
});
|
||||
|
||||
it('should updateProfile', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const dto = { firstName: 'Updated' };
|
||||
const result = await controller.updateProfile(req, dto);
|
||||
expect(service.update).toHaveBeenCalledWith('user-id', dto);
|
||||
expect(result.firstName).toBe('Updated');
|
||||
});
|
||||
|
||||
it('should addAddress', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const dto = {
|
||||
title: 'Home',
|
||||
receptorName: 'Ali',
|
||||
phone: '0912',
|
||||
province: 'Teh',
|
||||
city: 'Teh',
|
||||
detail: 'Det',
|
||||
zipCode: '123',
|
||||
};
|
||||
const result = await controller.addAddress(req, dto);
|
||||
expect(service.addAddress).toHaveBeenCalledWith('user-id', dto);
|
||||
expect(result.id).toBe('addr-id');
|
||||
});
|
||||
|
||||
it('should updateAddress', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const dto = {
|
||||
title: 'Work',
|
||||
receptorName: 'Ali',
|
||||
phone: '0912',
|
||||
province: 'Teh',
|
||||
city: 'Teh',
|
||||
detail: 'Det',
|
||||
zipCode: '123',
|
||||
};
|
||||
const result = await controller.updateAddress(req, 'addr-id', dto);
|
||||
expect(service.updateAddress).toHaveBeenCalledWith('user-id', 'addr-id', dto);
|
||||
expect(result.title).toBe('Work');
|
||||
});
|
||||
|
||||
it('should deleteAddress', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const result = await controller.deleteAddress(req, 'addr-id');
|
||||
expect(service.deleteAddress).toHaveBeenCalledWith('user-id', 'addr-id');
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('should setDefaultAddress', async () => {
|
||||
const req = { user: { id: 'user-id' } };
|
||||
const result = await controller.setDefaultAddress(req, 'addr-id');
|
||||
expect(service.setDefaultAddress).toHaveBeenCalledWith('user-id', 'addr-id');
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,19 +1,50 @@
|
||||
import { Controller, Get, Patch, Post, Delete, Body, Param, UseGuards, Req } from '@nestjs/common';
|
||||
import { Controller, Get, Patch, Post, Delete, Body, Param, UseGuards, Req, HttpStatus } from '@nestjs/common';
|
||||
import { UsersService } from './users.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse } from '@nestjs/swagger';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse, ApiOkResponse, ApiCreatedResponse, ApiBadRequestResponse } from '@nestjs/swagger';
|
||||
import { UpdateProfileDto } from './dto/update-profile.dto';
|
||||
import { AddressDto } from './dto/address.dto';
|
||||
|
||||
@ApiTags('Users')
|
||||
@ApiTags('Users - مدیریت کاربران و آدرسها')
|
||||
@ApiBearerAuth()
|
||||
@Controller('users')
|
||||
@ApiResponse({
|
||||
status: HttpStatus.UNAUTHORIZED,
|
||||
description: 'عدم احراز هویت - توکن معتبر نیست یا ارسال نشده است',
|
||||
schema: {
|
||||
example: {
|
||||
success: false,
|
||||
message: 'Unauthorized',
|
||||
code: 'UNAUTHORIZED',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
})
|
||||
export class UsersController {
|
||||
constructor(private readonly usersService: UsersService) {}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('profile')
|
||||
@ApiOperation({ summary: 'دریافت پروفایل کاربر فعلی' })
|
||||
@ApiOkResponse({
|
||||
description: 'مشخصات کامل کاربر به همراه آدرسها و اطلاعات حیوانات خانگی',
|
||||
schema: {
|
||||
example: {
|
||||
id: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
||||
firstName: 'کاربر',
|
||||
lastName: 'جدید',
|
||||
email: '09123456789@temp.local',
|
||||
mobile: '09123456789',
|
||||
role: 'User_PetOwner',
|
||||
walletBalance: '1500000.00',
|
||||
charityDonationTotal: '25000.00',
|
||||
addresses: [],
|
||||
pets: [],
|
||||
createdAt: '2026-05-26T15:20:00.000Z',
|
||||
updatedAt: '2026-05-26T15:20:00.000Z'
|
||||
}
|
||||
}
|
||||
})
|
||||
getProfile(@Req() req: any) {
|
||||
return this.usersService.findById(req.user.id);
|
||||
}
|
||||
@@ -21,7 +52,34 @@ export class UsersController {
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Patch('profile')
|
||||
@ApiOperation({ summary: 'ویرایش پروفایل کاربر فعلی' })
|
||||
@ApiResponse({ status: 200, description: 'پروفایل با موفقیت ویرایش شد' })
|
||||
@ApiOkResponse({
|
||||
description: 'پروفایل با موفقیت ویرایش شد',
|
||||
schema: {
|
||||
example: {
|
||||
id: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
||||
firstName: 'علی',
|
||||
lastName: 'احمدی',
|
||||
email: 'ali@example.com',
|
||||
mobile: '09123456789',
|
||||
role: 'User_PetOwner',
|
||||
walletBalance: '1500000.00',
|
||||
charityDonationTotal: '25000.00'
|
||||
}
|
||||
}
|
||||
})
|
||||
@ApiBadRequestResponse({
|
||||
description: 'خطا در صحتسنجی فیلدهای ورودی',
|
||||
schema: {
|
||||
example: {
|
||||
success: false,
|
||||
message: 'ایمیل نامعتبر است',
|
||||
code: 'BAD_REQUEST',
|
||||
details: {
|
||||
message: ['ایمیل نامعتبر است']
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
updateProfile(@Req() req: any, @Body() updateProfileDto: UpdateProfileDto) {
|
||||
return this.usersService.update(req.user.id, updateProfileDto);
|
||||
}
|
||||
@@ -29,7 +87,24 @@ export class UsersController {
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('addresses')
|
||||
@ApiOperation({ summary: 'ایجاد آدرس جدید برای کاربر' })
|
||||
@ApiResponse({ status: 201, description: 'آدرس جدید با موفقیت ایجاد شد' })
|
||||
@ApiCreatedResponse({
|
||||
description: 'آدرس جدید با موفقیت ایجاد شد',
|
||||
schema: {
|
||||
example: {
|
||||
id: 'b5a6c7d8-1234-5678-abcd-ef1234567890',
|
||||
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
||||
title: 'خانه',
|
||||
receptorName: 'علی احمدی',
|
||||
phone: '09123456789',
|
||||
province: 'تهران',
|
||||
city: 'تهران',
|
||||
detail: 'خیابان آزادی، کوچه مریم، پلاک ۱۰',
|
||||
zipCode: '1456789012',
|
||||
isDefault: false,
|
||||
createdAt: '2026-05-26T18:00:00.000Z'
|
||||
}
|
||||
}
|
||||
})
|
||||
addAddress(@Req() req: any, @Body() addressDto: AddressDto) {
|
||||
return this.usersService.addAddress(req.user.id, addressDto);
|
||||
}
|
||||
@@ -37,7 +112,24 @@ export class UsersController {
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Patch('addresses/:addressId')
|
||||
@ApiOperation({ summary: 'ویرایش آدرس کاربر' })
|
||||
@ApiResponse({ status: 200, description: 'آدرس با موفقیت ویرایش شد' })
|
||||
@ApiOkResponse({
|
||||
description: 'آدرس با موفقیت ویرایش شد',
|
||||
schema: {
|
||||
example: {
|
||||
id: 'b5a6c7d8-1234-5678-abcd-ef1234567890',
|
||||
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
||||
title: 'دفتر کار',
|
||||
receptorName: 'علی احمدی',
|
||||
phone: '09123456789',
|
||||
province: 'تهران',
|
||||
city: 'تهران',
|
||||
detail: 'خیابان ولیعصر، برج سپهر، طبقه ۴',
|
||||
zipCode: '1456789012',
|
||||
isDefault: false,
|
||||
createdAt: '2026-05-26T18:00:00.000Z'
|
||||
}
|
||||
}
|
||||
})
|
||||
updateAddress(
|
||||
@Req() req: any,
|
||||
@Param('addressId') addressId: string,
|
||||
@@ -49,7 +141,15 @@ export class UsersController {
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Delete('addresses/:addressId')
|
||||
@ApiOperation({ summary: 'حذف آدرس کاربر' })
|
||||
@ApiResponse({ status: 200, description: 'آدرس با موفقیت حذف شد' })
|
||||
@ApiOkResponse({
|
||||
description: 'آدرس با موفقیت حذف شد',
|
||||
schema: {
|
||||
example: {
|
||||
success: true,
|
||||
message: 'آدرس با موفقیت حذف شد'
|
||||
}
|
||||
}
|
||||
})
|
||||
deleteAddress(@Req() req: any, @Param('addressId') addressId: string) {
|
||||
return this.usersService.deleteAddress(req.user.id, addressId);
|
||||
}
|
||||
@@ -57,7 +157,15 @@ export class UsersController {
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Patch('addresses/:addressId/default')
|
||||
@ApiOperation({ summary: 'انتخاب آدرس به عنوان پیشفرض' })
|
||||
@ApiResponse({ status: 200, description: 'آدرس به عنوان پیشفرض ثبت شد' })
|
||||
@ApiOkResponse({
|
||||
description: 'آدرس به عنوان پیشفرض ثبت شد',
|
||||
schema: {
|
||||
example: {
|
||||
success: true,
|
||||
message: 'آدرس پیشفرض با موفقیت تغییر کرد'
|
||||
}
|
||||
}
|
||||
})
|
||||
setDefaultAddress(@Req() req: any, @Param('addressId') addressId: string) {
|
||||
return this.usersService.setDefaultAddress(req.user.id, addressId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { UsersService } from './users.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
describe('UsersService', () => {
|
||||
let service: UsersService;
|
||||
let prisma: PrismaService;
|
||||
|
||||
const mockPrisma = {
|
||||
user: {
|
||||
findUnique: jest.fn(),
|
||||
update: jest.fn(),
|
||||
},
|
||||
userAddress: {
|
||||
updateMany: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
UsersService,
|
||||
{ provide: PrismaService, useValue: mockPrisma },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<UsersService>(UsersService);
|
||||
prisma = module.get<PrismaService>(PrismaService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
it('should call prisma findUnique in findById', async () => {
|
||||
const mockUser = { id: 'user-id' };
|
||||
mockPrisma.user.findUnique.mockResolvedValue(mockUser);
|
||||
const result = await service.findById('user-id');
|
||||
expect(prisma.user.findUnique).toHaveBeenCalled();
|
||||
expect(result).toEqual(mockUser);
|
||||
});
|
||||
|
||||
it('should call prisma update in update', async () => {
|
||||
const data = { firstName: 'Ali' };
|
||||
mockPrisma.user.update.mockResolvedValue({ id: 'user-id', ...data });
|
||||
const result = await service.update('user-id', data);
|
||||
expect(prisma.user.update).toHaveBeenCalled();
|
||||
expect(result.firstName).toBe('Ali');
|
||||
});
|
||||
|
||||
it('should addAddress', async () => {
|
||||
const addressData = { title: 'Home', isDefault: true };
|
||||
mockPrisma.userAddress.create.mockResolvedValue({ id: 'addr-id', ...addressData });
|
||||
|
||||
const result = await service.addAddress('user-id', addressData);
|
||||
expect(prisma.userAddress.updateMany).toHaveBeenCalledWith({
|
||||
where: { userId: 'user-id' },
|
||||
data: { isDefault: false },
|
||||
});
|
||||
expect(prisma.userAddress.create).toHaveBeenCalled();
|
||||
expect(result.id).toBe('addr-id');
|
||||
});
|
||||
|
||||
it('should updateAddress', async () => {
|
||||
const addressData = { title: 'Work', isDefault: true };
|
||||
mockPrisma.userAddress.update.mockResolvedValue({ id: 'addr-id', ...addressData });
|
||||
|
||||
const result = await service.updateAddress('user-id', 'addr-id', addressData);
|
||||
expect(prisma.userAddress.updateMany).toHaveBeenCalled();
|
||||
expect(prisma.userAddress.update).toHaveBeenCalled();
|
||||
expect(result.title).toBe('Work');
|
||||
});
|
||||
|
||||
it('should deleteAddress', async () => {
|
||||
mockPrisma.userAddress.delete.mockResolvedValue({ id: 'addr-id' });
|
||||
const result = await service.deleteAddress('user-id', 'addr-id');
|
||||
expect(prisma.userAddress.delete).toHaveBeenCalled();
|
||||
expect(result.id).toBe('addr-id');
|
||||
});
|
||||
|
||||
it('should setDefaultAddress', async () => {
|
||||
mockPrisma.userAddress.update.mockResolvedValue({ id: 'addr-id', isDefault: true });
|
||||
const result = await service.setDefaultAddress('user-id', 'addr-id');
|
||||
expect(prisma.userAddress.updateMany).toHaveBeenCalledWith({
|
||||
where: { userId: 'user-id' },
|
||||
data: { isDefault: false },
|
||||
});
|
||||
expect(prisma.userAddress.update).toHaveBeenCalledWith({
|
||||
where: { id: 'addr-id', userId: 'user-id' },
|
||||
data: { isDefault: true },
|
||||
});
|
||||
expect(result.isDefault).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user