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,67 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { SettingsController } from './settings.controller';
|
||||
import { SettingsService } from './settings.service';
|
||||
|
||||
describe('SettingsController', () => {
|
||||
let controller: SettingsController;
|
||||
let service: SettingsService;
|
||||
|
||||
const mockSettingsService = {
|
||||
getUiTexts: jest.fn().mockResolvedValue([{ key: 'k', value: 'v' }]),
|
||||
updateUiText: jest.fn().mockResolvedValue({ key: 'k', value: 'v' }),
|
||||
getScientificTerms: jest.fn().mockResolvedValue([{ key: 'k' }]),
|
||||
upsertScientificTerm: jest.fn().mockResolvedValue({ key: 'k' }),
|
||||
deleteScientificTerm: jest.fn().mockResolvedValue({ success: true }),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [SettingsController],
|
||||
providers: [
|
||||
{ provide: SettingsService, useValue: mockSettingsService },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<SettingsController>(SettingsController);
|
||||
service = module.get<SettingsService>(SettingsService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
it('should getUiTexts', async () => {
|
||||
const result = await controller.getUiTexts();
|
||||
expect(service.getUiTexts).toHaveBeenCalled();
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should updateUiText', async () => {
|
||||
const result = await controller.updateUiText('k', 'v');
|
||||
expect(service.updateUiText).toHaveBeenCalledWith('k', 'v');
|
||||
expect(result.key).toBe('k');
|
||||
});
|
||||
|
||||
it('should getScientificTerms', async () => {
|
||||
const result = await controller.getScientificTerms();
|
||||
expect(service.getScientificTerms).toHaveBeenCalled();
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should upsertScientificTerm', async () => {
|
||||
const data = { term: 't', definition: 'd' };
|
||||
const result = await controller.upsertScientificTerm('k', data);
|
||||
expect(service.upsertScientificTerm).toHaveBeenCalledWith('k', data);
|
||||
expect(result.key).toBe('k');
|
||||
});
|
||||
|
||||
it('should deleteScientificTerm', async () => {
|
||||
const result = await controller.deleteScientificTerm('k');
|
||||
expect(service.deleteScientificTerm).toHaveBeenCalledWith('k');
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,16 +1,25 @@
|
||||
import { Controller, Get, Patch, Put, Delete, Body, Param, UseGuards } from '@nestjs/common';
|
||||
import { Controller, Get, Patch, Put, Delete, Body, Param, UseGuards, HttpStatus } from '@nestjs/common';
|
||||
import { SettingsService } from './settings.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth, ApiResponse } from '@nestjs/swagger';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth, ApiResponse, ApiOkResponse, ApiUnauthorizedResponse } from '@nestjs/swagger';
|
||||
|
||||
@ApiTags('Settings')
|
||||
@ApiTags('Settings - تنظیمات متون پویا و واژهنامه علمی')
|
||||
@Controller('settings')
|
||||
export class SettingsController {
|
||||
constructor(private readonly settingsService: SettingsService) {}
|
||||
|
||||
@Get('ui-texts')
|
||||
@ApiOperation({ summary: 'دریافت تمامی متون رابط کاربری' })
|
||||
@ApiResponse({ status: 200, description: 'تمامی متون بازگردانده شدند' })
|
||||
@ApiOperation({ summary: 'دریافت تمامی متون و پیکربندیهای رابط کاربری' })
|
||||
@ApiOkResponse({
|
||||
description: 'یک آبجکت کلید-مقدار حاوی تمامی متون، شعارها و برچسبهای دکمهها',
|
||||
schema: {
|
||||
example: {
|
||||
hero_badge: "تخصص دارویی از آلمان",
|
||||
hero_title: "تخصص آلمانی در خدمت سلامت پتهای خانگی",
|
||||
hero_desc: "بیش از ۴۰ سال تجربه نوآورانه..."
|
||||
}
|
||||
}
|
||||
})
|
||||
getUiTexts() {
|
||||
return this.settingsService.getUiTexts();
|
||||
}
|
||||
@@ -18,15 +27,39 @@ export class SettingsController {
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Patch('ui-texts/:key')
|
||||
@ApiOperation({ summary: 'ویرایش متن یک کلید در رابط کاربری' })
|
||||
@ApiResponse({ status: 200, description: 'متن با موفقیت بهروزرسانی شد' })
|
||||
@ApiOperation({ summary: 'ویرایش متن یک کلید در رابط کاربری (نیازمند توکن)' })
|
||||
@ApiOkResponse({
|
||||
description: 'متن با موفقیت بهروزرسانی شد',
|
||||
schema: {
|
||||
example: {
|
||||
key: 'hero_badge',
|
||||
value: 'تخصص دارویی ممتاز از آلمان'
|
||||
}
|
||||
}
|
||||
})
|
||||
@ApiUnauthorizedResponse({
|
||||
description: 'عدم دسترسی به دلیل عدم احراز هویت',
|
||||
schema: { example: { success: false, message: 'Unauthorized', code: 'UNAUTHORIZED' } }
|
||||
})
|
||||
updateUiText(@Param('key') key: string, @Body('value') value: string) {
|
||||
return this.settingsService.updateUiText(key, value);
|
||||
}
|
||||
|
||||
@Get('scientific-terms')
|
||||
@ApiOperation({ summary: 'دریافت تمامی اصطلاحات علمی' })
|
||||
@ApiResponse({ status: 200, description: 'تمامی اصطلاحات علمی بازگردانده شدند' })
|
||||
@ApiOperation({ summary: 'دریافت تمامی اصطلاحات واژهنامه علمی' })
|
||||
@ApiOkResponse({
|
||||
description: 'لیست کامل اصطلاحات علمی به همراه تعاریف و شناسهها',
|
||||
schema: {
|
||||
example: [
|
||||
{
|
||||
key: 'green-mussel',
|
||||
term: 'صدف لبسبز (Perna Canaliculus)',
|
||||
definition: 'این صدف بومی سواحل بکر نیوزیلند است...',
|
||||
wikiId: 'general'
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
getScientificTerms() {
|
||||
return this.settingsService.getScientificTerms();
|
||||
}
|
||||
@@ -34,8 +67,22 @@ export class SettingsController {
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Put('scientific-terms/:key')
|
||||
@ApiOperation({ summary: 'ثبت یا ویرایش یک اصطلاح علمی' })
|
||||
@ApiResponse({ status: 200, description: 'اصطلاح علمی ثبت یا ویرایش شد' })
|
||||
@ApiOperation({ summary: 'ثبت یا ویرایش یک اصطلاح علمی (نیازمند توکن)' })
|
||||
@ApiOkResponse({
|
||||
description: 'اصطلاح علمی ثبت یا ویرایش شد',
|
||||
schema: {
|
||||
example: {
|
||||
key: 'green-mussel',
|
||||
term: 'صدف لبسبز اصل نیوزیلند',
|
||||
definition: 'این صدف بومی سواحل بکر نیوزیلند است...',
|
||||
wikiId: 'general'
|
||||
}
|
||||
}
|
||||
})
|
||||
@ApiUnauthorizedResponse({
|
||||
description: 'عدم دسترسی',
|
||||
schema: { example: { success: false, message: 'Unauthorized', code: 'UNAUTHORIZED' } }
|
||||
})
|
||||
upsertScientificTerm(@Param('key') key: string, @Body() data: any) {
|
||||
return this.settingsService.upsertScientificTerm(key, data);
|
||||
}
|
||||
@@ -43,8 +90,20 @@ export class SettingsController {
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Delete('scientific-terms/:key')
|
||||
@ApiOperation({ summary: 'حذف یک اصطلاح علمی' })
|
||||
@ApiResponse({ status: 200, description: 'اصطلاح علمی حذف شد' })
|
||||
@ApiOperation({ summary: 'حذف یک اصطلاح علمی (نیازمند توکن)' })
|
||||
@ApiOkResponse({
|
||||
description: 'اصطلاح علمی حذف شد',
|
||||
schema: {
|
||||
example: {
|
||||
success: true,
|
||||
message: 'Scientific term successfully deleted'
|
||||
}
|
||||
}
|
||||
})
|
||||
@ApiUnauthorizedResponse({
|
||||
description: 'عدم دسترسی',
|
||||
schema: { example: { success: false, message: 'Unauthorized', code: 'UNAUTHORIZED' } }
|
||||
})
|
||||
deleteScientificTerm(@Param('key') key: string) {
|
||||
return this.settingsService.deleteScientificTerm(key);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { SettingsService } from './settings.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
describe('SettingsService', () => {
|
||||
let service: SettingsService;
|
||||
let prisma: PrismaService;
|
||||
|
||||
const mockPrisma = {
|
||||
uiText: {
|
||||
findMany: jest.fn(),
|
||||
upsert: jest.fn(),
|
||||
},
|
||||
scientificTerm: {
|
||||
findMany: jest.fn(),
|
||||
upsert: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
SettingsService,
|
||||
{ provide: PrismaService, useValue: mockPrisma },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<SettingsService>(SettingsService);
|
||||
prisma = module.get<PrismaService>(PrismaService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
it('should getUiTexts', async () => {
|
||||
mockPrisma.uiText.findMany.mockResolvedValue([{ key: 'k', value: 'v' }]);
|
||||
const result = await service.getUiTexts();
|
||||
expect(prisma.uiText.findMany).toHaveBeenCalled();
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should updateUiText', async () => {
|
||||
mockPrisma.uiText.upsert.mockResolvedValue({ key: 'k', value: 'v2' });
|
||||
const result = await service.updateUiText('k', 'v2');
|
||||
expect(prisma.uiText.upsert).toHaveBeenCalledWith({
|
||||
where: { key: 'k' },
|
||||
update: { value: 'v2' },
|
||||
create: { key: 'k', value: 'v2' },
|
||||
});
|
||||
expect(result.value).toBe('v2');
|
||||
});
|
||||
|
||||
it('should getScientificTerms', async () => {
|
||||
mockPrisma.scientificTerm.findMany.mockResolvedValue([{ key: 'k' }]);
|
||||
const result = await service.getScientificTerms();
|
||||
expect(prisma.scientificTerm.findMany).toHaveBeenCalled();
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should upsertScientificTerm', async () => {
|
||||
mockPrisma.scientificTerm.upsert.mockResolvedValue({ key: 'k', term: 't' });
|
||||
const data = { term: 't', definition: 'd', wikiId: 'w' };
|
||||
const result = await service.upsertScientificTerm('k', data);
|
||||
expect(prisma.scientificTerm.upsert).toHaveBeenCalledWith({
|
||||
where: { key: 'k' },
|
||||
update: { term: 't', definition: 'd', wikiId: 'w' },
|
||||
create: { key: 'k', term: 't', definition: 'd', wikiId: 'w' },
|
||||
});
|
||||
expect(result.term).toBe('t');
|
||||
});
|
||||
|
||||
it('should deleteScientificTerm', async () => {
|
||||
mockPrisma.scientificTerm.delete.mockResolvedValue({ key: 'k' });
|
||||
const result = await service.deleteScientificTerm('k');
|
||||
expect(prisma.scientificTerm.delete).toHaveBeenCalledWith({
|
||||
where: { key: 'k' },
|
||||
});
|
||||
expect(result.key).toBe('k');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user