Skip to content

Commit

Permalink
N21-1319 review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrikallab committed Nov 15, 2023
1 parent 7b597f3 commit 280f3c9
Show file tree
Hide file tree
Showing 27 changed files with 238 additions and 354 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum ToolContextType {
COURSE = 'course',
BOARD_ELEMENT = 'boardElement',
BOARD_ELEMENT = 'board-element',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ToolContextType } from '../enum';
import { ContextExternalToolType } from '../../context-external-tool/entity';

export class ToolContextMapper {
static contextMapping: Record<ToolContextType, ContextExternalToolType> = {
[ToolContextType.COURSE]: ContextExternalToolType.COURSE,
[ToolContextType.BOARD_ELEMENT]: ContextExternalToolType.BOARD_ELEMENT,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { EntityManager, ObjectId } from '@mikro-orm/mongodb';
import { ServerTestModule } from '@modules/server';
import { HttpStatus, INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { Permission } from '@shared/domain';
import { Permission, SchoolEntity } from '@shared/domain';
import {
cleanupCollections,
contextExternalToolEntityFactory,
externalToolEntityFactory,
externalToolFactory,
schoolExternalToolEntityFactory,
schoolFactory,
TestApiClient,
UserAndAccountTestFactory,
} from '@shared/testing';
Expand All @@ -21,14 +22,17 @@ import {
CustomParameterScopeTypeParams,
CustomParameterTypeParams,
ToolConfigType,
ToolContextType,
} from '../../../common/enum';
import { ContextExternalToolEntity, ContextExternalToolType } from '../../../context-external-tool/entity';
import { SchoolExternalToolEntity } from '../../../school-external-tool/entity';
import { ExternalToolMetadata } from '../../domain';
import { ExternalToolEntity } from '../../entity';
import { ExternalToolCreateParams, ExternalToolResponse, ExternalToolSearchListResponse } from '../dto';
import { ExternalToolMetadataResponse } from '../dto/response/external-tool-metadata.response';
import {
ExternalToolCreateParams,
ExternalToolResponse,
ExternalToolSearchListResponse,
ExternalToolMetadataResponse,
} from '../dto';

describe('ToolController (API)', () => {
let app: INestApplication;
Expand Down Expand Up @@ -627,31 +631,16 @@ describe('ToolController (API)', () => {

describe('[GET] tools/external-tools/:externalToolId/metadata', () => {
describe('when user is not authenticated', () => {
const setup = async () => {
const setup = () => {
const toolId: string = new ObjectId().toHexString();
const externalToolEntity: ExternalToolEntity = externalToolEntityFactory.buildWithId(undefined, toolId);

const contextExternalToolCount = new Map<ToolContextType, number>();
contextExternalToolCount.set(ToolContextType.COURSE, 3);
contextExternalToolCount.set(ToolContextType.BOARD_ELEMENT, 2);
const externalToolMetadata: ExternalToolMetadata = new ExternalToolMetadata({
schoolExternalToolCount: 2,
contextExternalToolCountPerContext: contextExternalToolCount,
});

const { adminUser, adminAccount } = UserAndAccountTestFactory.buildAdmin({}, [Permission.TOOL_ADMIN]);
await em.persistAndFlush([adminAccount, adminUser, externalToolEntity]);
em.clear();

const loggedInClient: TestApiClient = await testApiClient.login(adminAccount);

return { loggedInClient, toolId, externalToolEntity, externalToolMetadata };
return { toolId };
};

it('should return unauthorized', async () => {
const { externalToolEntity } = await setup();
const { toolId } = setup();

const response: Response = await testApiClient.get(`${externalToolEntity.id}/metadata`);
const response: Response = await testApiClient.get(`${toolId}/metadata`);

expect(response.statusCode).toEqual(HttpStatus.UNAUTHORIZED);
});
Expand All @@ -662,64 +651,37 @@ describe('ToolController (API)', () => {
const toolId: string = new ObjectId().toHexString();
const externalToolEntity: ExternalToolEntity = externalToolEntityFactory.buildWithId(undefined, toolId);

const schoolToolId: string = new ObjectId().toHexString();
const schoolExternalToolEntity: SchoolExternalToolEntity = schoolExternalToolEntityFactory.buildWithId(
{ tool: externalToolEntity },
schoolToolId
);
const schoolToolId1: string = new ObjectId().toHexString();
const schoolExternalToolEntity1: SchoolExternalToolEntity = schoolExternalToolEntityFactory.buildWithId(
{ tool: externalToolEntity },
schoolToolId1
);

const courseExternalToolEntity: ContextExternalToolEntity = contextExternalToolEntityFactory.buildWithId({
schoolTool: schoolExternalToolEntity,
contextType: ContextExternalToolType.COURSE,
contextId: new ObjectId().toHexString(),
const school: SchoolEntity = schoolFactory.buildWithId();
const schoolExternalToolEntitys: SchoolExternalToolEntity[] = schoolExternalToolEntityFactory.buildList(2, {
tool: externalToolEntity,
school,
});
const courseExternalToolEntity1: ContextExternalToolEntity = contextExternalToolEntityFactory.buildWithId({
schoolTool: schoolExternalToolEntity1,
contextType: ContextExternalToolType.COURSE,
contextId: new ObjectId().toHexString(),
});
const courseExternalToolEntity2: ContextExternalToolEntity = contextExternalToolEntityFactory.buildWithId({
schoolTool: schoolExternalToolEntity,

const courseTools: ContextExternalToolEntity[] = contextExternalToolEntityFactory.buildList(3, {
schoolTool: schoolExternalToolEntitys[0],
contextType: ContextExternalToolType.COURSE,
contextId: new ObjectId().toHexString(),
});

const boardExternalToolEntity: ContextExternalToolEntity = contextExternalToolEntityFactory.buildWithId({
schoolTool: schoolExternalToolEntity,
contextType: ContextExternalToolType.BOARD_ELEMENT,
contextId: new ObjectId().toHexString(),
});
const boardExternalToolEntity1: ContextExternalToolEntity = contextExternalToolEntityFactory.buildWithId({
schoolTool: schoolExternalToolEntity1,
const boardTools: ContextExternalToolEntity[] = contextExternalToolEntityFactory.buildList(2, {
schoolTool: schoolExternalToolEntitys[1],
contextType: ContextExternalToolType.BOARD_ELEMENT,
contextId: new ObjectId().toHexString(),
});

const contextExternalToolCount = new Map<ToolContextType, number>();
contextExternalToolCount.set(ToolContextType.COURSE, 3);
contextExternalToolCount.set(ToolContextType.BOARD_ELEMENT, 2);
const externalToolMetadata: ExternalToolMetadata = new ExternalToolMetadata({
schoolExternalToolCount: 2,
contextExternalToolCountPerContext: contextExternalToolCount,
contextExternalToolCountPerContext: { course: 3, boardElement: 2 },
});

const { adminUser, adminAccount } = UserAndAccountTestFactory.buildAdmin({}, [Permission.TOOL_ADMIN]);
await em.persistAndFlush([
adminAccount,
adminUser,
school,
externalToolEntity,
schoolExternalToolEntity,
schoolExternalToolEntity1,
courseExternalToolEntity,
courseExternalToolEntity1,
courseExternalToolEntity2,
boardExternalToolEntity,
boardExternalToolEntity1,
...schoolExternalToolEntitys,
...courseTools,
...boardTools,
]);
em.clear();

Expand All @@ -728,19 +690,21 @@ describe('ToolController (API)', () => {
return { loggedInClient, toolId, externalToolEntity, externalToolMetadata };
};

it('should return the metadata of externaltool', async () => {
const { loggedInClient, externalToolEntity, externalToolMetadata } = await setup();
it('should return the metadata of externalTool', async () => {
const { loggedInClient, externalToolEntity } = await setup();

const response: Response = await loggedInClient.get(`${externalToolEntity.id}/metadata`);

const body: ExternalToolMetadataResponse = response.body as ExternalToolMetadataResponse;

expect(response.statusCode).toEqual(HttpStatus.OK);
expect(body).toBeDefined();
expect(body).toMatchObject<ExternalToolMetadataResponse>(externalToolMetadata);
expect(body.schoolExternalToolCount).toEqual<number>(2);
expect(body.contextExternalToolCountPerContext).toHaveProperty('course', 3);
expect(body.contextExternalToolCountPerContext).toHaveProperty('boardElement', 2);
expect(body).toEqual<ExternalToolMetadataResponse>({
schoolExternalToolCount: 2,
contextExternalToolCountPerContext: {
course: 3,
boardElement: 2,
},
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { ApiProperty } from '@nestjs/swagger';
import { ToolContextType } from '../../../../common/enum';
import { ContextExternalToolType } from '../../../../context-external-tool/entity';

export class ExternalToolMetadataResponse {
@ApiProperty()
schoolExternalToolCount: number;

@ApiProperty()
contextExternalToolCountPerContext: Map<ToolContextType, number>;
@ApiProperty({
type: 'object',
properties: Object.fromEntries(
Object.values(ContextExternalToolType).map((key: ContextExternalToolType) => [key, { type: 'number' }])
),
})
contextExternalToolCountPerContext: Record<ContextExternalToolType, number>;

constructor(externalToolMetadataResponse: ExternalToolMetadataResponse) {
this.schoolExternalToolCount = externalToolMetadataResponse.schoolExternalToolCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './context-external-tool-configuration-template.response';
export * from './context-external-tool-configuration-template-list.response';
export * from './school-external-tool-configuration-template.response';
export * from './school-external-tool-configuration-template-list.response';
export * from './external-tool-metadata.response';
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import {
ExternalToolSearchParams,
ExternalToolUpdateParams,
SortExternalToolParams,
ExternalToolMetadataResponse,
} from './dto';
import { ExternalToolMetadataResponse } from './dto/response/external-tool-metadata.response';

@ApiTags('Tool')
@Authenticate('jwt')
Expand Down Expand Up @@ -172,6 +172,7 @@ export class ToolController {
@ApiOperation({ summary: 'Gets the metadata of an external tool.' })
@ApiOkResponse({
description: 'Metadata of external tool fetched successfully.',
type: ExternalToolMetadataResponse,
})
@ApiUnauthorizedResponse({ description: 'User is not logged in.' })
async getMetaDataForExternalTool(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ToolContextType } from '../../common/enum';

export class ExternalToolMetadata {
schoolExternalToolCount: number;

contextExternalToolCountPerContext: Map<ToolContextType, number>;
contextExternalToolCountPerContext: Record<string, number>;

constructor(externalToolMetadata: ExternalToolMetadata) {
this.schoolExternalToolCount = externalToolMetadata.schoolExternalToolCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { OauthProviderServiceModule } from '@infra/oauth-provider';
import { EncryptionModule } from '@infra/encryption';
import { ExternalToolRepo } from '@shared/repo';
import { ToolConfigModule } from '../tool-config.module';
import { ExternalToolMetadataMapper } from './mapper';
import { ToolContextMapper } from '../common/mapper/tool-context.mapper';
import {
ExternalToolConfigurationService,
ExternalToolLogoService,
Expand All @@ -29,6 +31,8 @@ import { CommonToolModule } from '../common';
ExternalToolLogoService,
ExternalToolRepo,
ExternalToolMetadataService,
ExternalToolMetadataMapper,
ToolContextMapper,
],
exports: [
ExternalToolService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,10 @@ import { ExternalToolMetadataLoggable } from './external-tool-metadata-loggable'

describe('ExternalToolMetadataLoggable', () => {
describe('constructor', () => {
const setup = () => {
const msg = 'message';

return { msg };
};

it('should create an instance of ExternalToolLogoFetchedLoggable', () => {
const { msg } = setup();

const loggable = new ExternalToolMetadataLoggable(msg);
const loggable = new ExternalToolMetadataLoggable();

expect(loggable).toBeInstanceOf(ExternalToolMetadataLoggable);
});
});

describe('getLogMessage', () => {
const setup = () => {
const msg = 'message';
const loggable = new ExternalToolMetadataLoggable(msg);

return { loggable, msg };
};

it('should return a loggable message', () => {
const { loggable, msg } = setup();

const message = loggable.getLogMessage();

expect(message).toEqual({
type: 'EXTERNAL_TOOL_METADATA',
message: 'No related tools found, return empty external tool metadata',
data: {
msg,
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { ErrorLogMessage, Loggable, LogMessage, ValidationErrorLogMessage } from '@src/core/logger';

export class ExternalToolMetadataLoggable implements Loggable {
constructor(private readonly msg: string) {}

getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage {
return {
type: 'EXTERNAL_TOOL_METADATA',
message: 'No related tools found, return empty external tool metadata',
data: {
msg: this.msg,
},
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ExternalToolMetadataResponse } from '../controller/dto/response/external-tool-metadata.response';
import { ExternalToolMetadata } from '../domain/external-tool-metadata';
import { ToolContextType } from '../../common/enum';
import { ContextExternalToolType } from '../../context-external-tool/entity';
import { ExternalToolMetadataResponse } from '../controller/dto';
import { ExternalToolMetadata } from '../domain';

export class ExternalToolMetadataMapper {
static mapToExternalToolMetadataResponse(externalToolMetadata: ExternalToolMetadata): ExternalToolMetadataResponse {
Expand Down
Loading

0 comments on commit 280f3c9

Please sign in to comment.