Skip to content

Commit

Permalink
get tools/context-types unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorCapCoder committed Nov 22, 2023
1 parent bc0c5cd commit 2eabe71
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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] });
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ import { ToolContextType } from '../../common/enum';
export class ContextExternalToolService {
constructor(private readonly contextExternalToolRepo: ContextExternalToolRepo) {}

async findContextExternalTools(query: ContextExternalToolQuery): Promise<ContextExternalTool[]> {
public async findContextExternalTools(query: ContextExternalToolQuery): Promise<ContextExternalTool[]> {
const contextExternalTools: ContextExternalTool[] = await this.contextExternalToolRepo.find(query);

return contextExternalTools;
}

async findById(contextExternalToolId: EntityId): Promise<ContextExternalTool> {
public async findById(contextExternalToolId: EntityId): Promise<ContextExternalTool> {
const tool: ContextExternalTool = await this.contextExternalToolRepo.findById(contextExternalToolId);

return tool;
}

async saveContextExternalTool(contextExternalTool: ContextExternalTool): Promise<ContextExternalTool> {
public async saveContextExternalTool(contextExternalTool: ContextExternalTool): Promise<ContextExternalTool> {
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,
Expand All @@ -38,20 +38,20 @@ export class ContextExternalToolService {
await this.contextExternalToolRepo.delete(contextExternalTools);
}

async deleteContextExternalTool(contextExternalTool: ContextExternalTool): Promise<void> {
public async deleteContextExternalTool(contextExternalTool: ContextExternalTool): Promise<void> {
await this.contextExternalToolRepo.delete(contextExternalTool);
}

async findAllByContext(contextRef: ContextRef): Promise<ContextExternalTool[]> {
public async findAllByContext(contextRef: ContextRef): Promise<ContextExternalTool[]> {
const contextExternalTools: ContextExternalTool[] = await this.contextExternalToolRepo.find({
context: contextRef,
});

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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] });
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
});

0 comments on commit 2eabe71

Please sign in to comment.