From 2eabe715d77d3e92ebb9ee7bdc8351c86d10ca2f Mon Sep 17 00:00:00 2001 From: Igor Richter Date: Wed, 22 Nov 2023 14:10:35 +0100 Subject: [PATCH] get tools/context-types unit tests --- .../common/uc/tool-permissions-helper.spec.ts | 20 ++++++++ .../context-external-tool.service.spec.ts | 11 +++++ .../service/context-external-tool.service.ts | 16 +++---- .../api-test/tool-configuration.api.spec.ts | 47 ++++++++++++++++++- .../uc/external-tool-configuration.uc.spec.ts | 26 ++++++++++ 5 files changed, 111 insertions(+), 9 deletions(-) diff --git a/apps/server/src/modules/tool/common/uc/tool-permissions-helper.spec.ts b/apps/server/src/modules/tool/common/uc/tool-permissions-helper.spec.ts index aace35579db..fd9c926ee98 100644 --- a/apps/server/src/modules/tool/common/uc/tool-permissions-helper.spec.ts +++ b/apps/server/src/modules/tool/common/uc/tool-permissions-helper.spec.ts @@ -247,4 +247,24 @@ describe('ToolPermissionHelper', () => { }); }); }); + + describe('ensurePermission', () => { + describe('when it is called', () => { + const setup = () => { + const user = userFactory.buildWithId(); + + authorizationService.getUserWithPermissions.mockResolvedValueOnce(user); + + return { + user, + }; + }; + + it('should check permissions', async () => { + const { user } = setup(); + + await helper.ensurePermission(user.id, Permission.TOOL_ADMIN); + }); + }); + }); }); diff --git a/apps/server/src/modules/tool/context-external-tool/service/context-external-tool.service.spec.ts b/apps/server/src/modules/tool/context-external-tool/service/context-external-tool.service.spec.ts index 60275c71d0d..0693c07e2c7 100644 --- a/apps/server/src/modules/tool/context-external-tool/service/context-external-tool.service.spec.ts +++ b/apps/server/src/modules/tool/context-external-tool/service/context-external-tool.service.spec.ts @@ -12,6 +12,7 @@ import { ToolContextType } from '../../common/enum'; import { SchoolExternalTool } from '../../school-external-tool/domain'; import { ContextExternalTool, ContextRef } from '../domain'; import { ContextExternalToolService } from './context-external-tool.service'; +import { ToolContextTypesList } from '../../external-tool/controller/dto/response/tool-context-types-list'; describe('ContextExternalToolService', () => { let module: TestingModule; @@ -216,4 +217,14 @@ describe('ContextExternalToolService', () => { }); }); }); + + describe('getToolContextTypes', () => { + describe('when it is called', () => { + it('should return ToolContextTypes', () => { + const types: ToolContextTypesList = service.getToolContextTypes(); + + expect(types).toEqual({ data: [ToolContextType.COURSE, ToolContextType.BOARD_ELEMENT] }); + }); + }); + }); }); diff --git a/apps/server/src/modules/tool/context-external-tool/service/context-external-tool.service.ts b/apps/server/src/modules/tool/context-external-tool/service/context-external-tool.service.ts index e40b7f63bec..ea18ced5c86 100644 --- a/apps/server/src/modules/tool/context-external-tool/service/context-external-tool.service.ts +++ b/apps/server/src/modules/tool/context-external-tool/service/context-external-tool.service.ts @@ -10,25 +10,25 @@ import { ToolContextType } from '../../common/enum'; export class ContextExternalToolService { constructor(private readonly contextExternalToolRepo: ContextExternalToolRepo) {} - async findContextExternalTools(query: ContextExternalToolQuery): Promise { + public async findContextExternalTools(query: ContextExternalToolQuery): Promise { const contextExternalTools: ContextExternalTool[] = await this.contextExternalToolRepo.find(query); return contextExternalTools; } - async findById(contextExternalToolId: EntityId): Promise { + public async findById(contextExternalToolId: EntityId): Promise { const tool: ContextExternalTool = await this.contextExternalToolRepo.findById(contextExternalToolId); return tool; } - async saveContextExternalTool(contextExternalTool: ContextExternalTool): Promise { + public async saveContextExternalTool(contextExternalTool: ContextExternalTool): Promise { const savedContextExternalTool: ContextExternalTool = await this.contextExternalToolRepo.save(contextExternalTool); return savedContextExternalTool; } - async deleteBySchoolExternalToolId(schoolExternalToolId: EntityId) { + public async deleteBySchoolExternalToolId(schoolExternalToolId: EntityId) { const contextExternalTools: ContextExternalTool[] = await this.contextExternalToolRepo.find({ schoolToolRef: { schoolToolId: schoolExternalToolId, @@ -38,11 +38,11 @@ export class ContextExternalToolService { await this.contextExternalToolRepo.delete(contextExternalTools); } - async deleteContextExternalTool(contextExternalTool: ContextExternalTool): Promise { + public async deleteContextExternalTool(contextExternalTool: ContextExternalTool): Promise { await this.contextExternalToolRepo.delete(contextExternalTool); } - async findAllByContext(contextRef: ContextRef): Promise { + public async findAllByContext(contextRef: ContextRef): Promise { const contextExternalTools: ContextExternalTool[] = await this.contextExternalToolRepo.find({ context: contextRef, }); @@ -50,8 +50,8 @@ export class ContextExternalToolService { return contextExternalTools; } - getToolContextTypes(): ToolContextTypesList { - const toolContextTypes: ToolContextTypesList = { data: [ToolContextType.COURSE, ToolContextType.BOARD_ELEMENT] }; + public getToolContextTypes(): ToolContextTypesList { + const toolContextTypes: ToolContextTypesList = { data: Object.values(ToolContextType) }; return toolContextTypes; } diff --git a/apps/server/src/modules/tool/external-tool/controller/api-test/tool-configuration.api.spec.ts b/apps/server/src/modules/tool/external-tool/controller/api-test/tool-configuration.api.spec.ts index 68543982174..ee71cab8a0b 100644 --- a/apps/server/src/modules/tool/external-tool/controller/api-test/tool-configuration.api.spec.ts +++ b/apps/server/src/modules/tool/external-tool/controller/api-test/tool-configuration.api.spec.ts @@ -16,7 +16,7 @@ import { userFactory, } from '@shared/testing'; import { ServerTestModule } from '@modules/server'; -import { CustomParameterTypeParams } from '@modules/tool/common/enum'; +import { CustomParameterTypeParams, ToolContextType } from '@modules/tool/common/enum'; import { Response } from 'supertest'; import { CustomParameterLocationParams, CustomParameterScopeTypeParams } from '../../../common/enum'; import { ContextExternalToolEntity, ContextExternalToolType } from '../../../context-external-tool/entity'; @@ -633,8 +633,53 @@ describe('ToolConfigurationController (API)', () => { const response: Response = await loggedInClient.get( `context-external-tools/${contextExternalTool.id}/configuration-template` ); + expect(response.status).toEqual(HttpStatus.NOT_FOUND); }); }); }); + + describe('GET tools/context-types', () => { + describe('when user is not authorized', () => { + const setup = async () => { + const { teacherAccount, teacherUser } = UserAndAccountTestFactory.buildTeacher(); + + await em.persistAndFlush([teacherAccount, teacherUser]); + em.clear(); + + const loggedInClient: TestApiClient = await testApiClient.login(teacherAccount); + + return { loggedInClient }; + }; + + it('should return unauthorized status', async () => { + const { loggedInClient } = await setup(); + + const response = await loggedInClient.get('context-types'); + + expect(response.status).toEqual(HttpStatus.UNAUTHORIZED); + }); + }); + + describe('when user is authorized', () => { + const setup = async () => { + const { adminAccount, adminUser } = UserAndAccountTestFactory.buildAdmin({}, [Permission.TOOL_ADMIN]); + + await em.persistAndFlush([adminAccount, adminUser]); + em.clear(); + + const loggedInClient: TestApiClient = await testApiClient.login(adminAccount); + + return { loggedInClient }; + }; + + it('should return all context types', async () => { + const { loggedInClient } = await setup(); + + const response = await loggedInClient.get('context-types'); + + expect(response.body).toEqual({ data: [ToolContextType.COURSE, ToolContextType.BOARD_ELEMENT] }); + }); + }); + }); }); diff --git a/apps/server/src/modules/tool/external-tool/uc/external-tool-configuration.uc.spec.ts b/apps/server/src/modules/tool/external-tool/uc/external-tool-configuration.uc.spec.ts index 5327c8d4f80..526aba9fabc 100644 --- a/apps/server/src/modules/tool/external-tool/uc/external-tool-configuration.uc.spec.ts +++ b/apps/server/src/modules/tool/external-tool/uc/external-tool-configuration.uc.spec.ts @@ -650,4 +650,30 @@ describe('ExternalToolConfigurationUc', () => { }); }); }); + + describe('getToolContextTypes', () => { + describe('when it is called', () => { + const setup = () => { + const userId: string = new ObjectId().toHexString(); + + return { userId }; + }; + + it('should check Permission', async () => { + const { userId } = setup(); + + await uc.getToolContextTypes(userId); + + expect(toolPermissionHelper.ensurePermission).toHaveBeenCalledWith(userId, 'TOOL_ADMIN'); + }); + + it('should get context types', async () => { + const { userId } = setup(); + + await uc.getToolContextTypes(userId); + + expect(contextExternalToolService.getToolContextTypes).toHaveBeenCalled(); + }); + }); + }); });