From 75ff3c5f0ed95816887b61b571ce9753de5b2d26 Mon Sep 17 00:00:00 2001 From: Arne Gnisa Date: Wed, 24 Jul 2024 13:52:18 +0200 Subject: [PATCH] N21-1918 adjusts api tests --- .../controller/api-test/tool.api.spec.ts | 35 ++++++++++++++++--- .../testing/external-tool-entity.factory.ts | 5 +++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/apps/server/src/modules/tool/external-tool/controller/api-test/tool.api.spec.ts b/apps/server/src/modules/tool/external-tool/controller/api-test/tool.api.spec.ts index 5e304b9bb60..6a646b2d5b6 100644 --- a/apps/server/src/modules/tool/external-tool/controller/api-test/tool.api.spec.ts +++ b/apps/server/src/modules/tool/external-tool/controller/api-test/tool.api.spec.ts @@ -1,5 +1,7 @@ import { Loaded } from '@mikro-orm/core'; import { EntityManager, ObjectId } from '@mikro-orm/mongodb'; +import { FileRecordResponse } from '@modules/files-storage/controller/dto'; +import { instanceEntityFactory } from '@modules/instance/testing'; import { ServerTestModule } from '@modules/server'; import { schoolExternalToolEntityFactory } from '@modules/tool/school-external-tool/testing'; import { HttpStatus, INestApplication } from '@nestjs/common'; @@ -88,23 +90,28 @@ describe('ToolController (API)', () => { logoUrl: 'https://link.to-my-logo.com', url: 'https://link.to-my-tool.com', openNewTab: true, + thumbnailUrl: 'https://link.to-my-thumbnail.com', }; describe('when valid data is given', () => { const setup = async () => { const params: ExternalToolCreateParams = { ...postParams }; + const instance = instanceEntityFactory.build(); const { adminUser, adminAccount } = UserAndAccountTestFactory.buildAdmin({}, [Permission.TOOL_ADMIN]); - await em.persistAndFlush([adminAccount, adminUser]); + await em.persistAndFlush([adminAccount, adminUser, instance]); em.clear(); const base64Logo: string = externalToolFactory.withBase64Logo().build().logo as string; const logoBuffer: Buffer = Buffer.from(base64Logo, 'base64'); axiosMock.onGet(params.logoUrl).reply(HttpStatus.OK, logoBuffer); + const fileRecordResponse: Partial = { id: new ObjectId().toHexString() }; + axiosMock.onPost(/api\/v3\/file\/upload-from-url/).reply(HttpStatus.OK, fileRecordResponse); + const loggedInClient: TestApiClient = await testApiClient.login(adminAccount); - return { loggedInClient, params }; + return { loggedInClient, params, fileRecordResponse }; }; it('should create a tool', async () => { @@ -153,6 +160,7 @@ describe('ToolController (API)', () => { logoUrl: 'https://link.to-my-logo.com', url: 'https://link.to-my-tool.com', openNewTab: true, + thumbnailUrl: postParams.thumbnailUrl, }); }); }); @@ -250,20 +258,26 @@ describe('ToolController (API)', () => { mediumId: 'medium:1', mediaSourceId: 'source:1', }, + thumbnailUrl: 'https://link.to-my-thumbnail.com', }; describe('when valid data is given', () => { const setup = async () => { const params: ExternalToolBulkCreateParams = { data: [{ ...postParams }] }; + const instance = instanceEntityFactory.build(); + const { adminUser, adminAccount } = UserAndAccountTestFactory.buildAdmin({}, [Permission.TOOL_ADMIN]); - await em.persistAndFlush([adminAccount, adminUser]); + await em.persistAndFlush([adminAccount, adminUser, instance]); em.clear(); const base64Logo: string = externalToolFactory.withBase64Logo().build().logo as string; const logoBuffer: Buffer = Buffer.from(base64Logo, 'base64'); axiosMock.onGet(logoUrl).reply(HttpStatus.OK, logoBuffer); + const fileRecordResponse: Partial = { id: new ObjectId().toHexString() }; + axiosMock.onPost(/api\/v3\/file\/upload-from-url/).reply(HttpStatus.OK, fileRecordResponse); + const loggedInClient: TestApiClient = await testApiClient.login(adminAccount); return { @@ -486,6 +500,7 @@ describe('ToolController (API)', () => { isDeactivated: false, logoUrl: 'https://link.to-my-logo.com', url: 'https://link.to-my-tool.com', + thumbnailUrl: 'https://link.to-my-thumbnail2.com', openNewTab: true, medium: { mediumId: 'mediumId', @@ -506,8 +521,14 @@ describe('ToolController (API)', () => { const logoBuffer: Buffer = Buffer.from(base64Logo, 'base64'); axiosMock.onGet(params.logoUrl).reply(HttpStatus.OK, logoBuffer); + const fileRecordResponse: Partial = { id: new ObjectId().toHexString() }; + axiosMock.onDelete(/api\/v3\/file\/delete/).reply(HttpStatus.OK); + axiosMock.onPost(/api\/v3\/file\/upload-from-url/).reply(HttpStatus.OK, fileRecordResponse); + + const instance = instanceEntityFactory.build(); + const { adminUser, adminAccount } = UserAndAccountTestFactory.buildAdmin({}, [Permission.TOOL_ADMIN]); - await em.persistAndFlush([adminAccount, adminUser, externalToolEntity]); + await em.persistAndFlush([adminAccount, adminUser, externalToolEntity, instance]); em.clear(); const loggedInClient: TestApiClient = await testApiClient.login(adminAccount); @@ -565,6 +586,7 @@ describe('ToolController (API)', () => { mediumId: params.medium?.mediumId ?? '', publisher: params.medium?.publisher, }, + thumbnailUrl: 'https://link.to-my-thumbnail2.com', }); }); }); @@ -640,11 +662,14 @@ describe('ToolController (API)', () => { const externalToolEntity: ExternalToolEntity = externalToolEntityFactory.buildWithId(); const { adminUser, adminAccount } = UserAndAccountTestFactory.buildAdmin({}, [Permission.TOOL_ADMIN]); - await em.persistAndFlush([adminAccount, adminUser, externalToolEntity]); + const instance = instanceEntityFactory.build(); + await em.persistAndFlush([adminAccount, adminUser, externalToolEntity, instance]); em.clear(); const loggedInClient: TestApiClient = await testApiClient.login(adminAccount); + axiosMock.onDelete(/api\/v3\/file\/delete/).reply(HttpStatus.OK); + return { loggedInClient, toolId: externalToolEntity.id }; }; diff --git a/apps/server/src/modules/tool/external-tool/testing/external-tool-entity.factory.ts b/apps/server/src/modules/tool/external-tool/testing/external-tool-entity.factory.ts index 909fa436b8d..1c8b4df07a4 100644 --- a/apps/server/src/modules/tool/external-tool/testing/external-tool-entity.factory.ts +++ b/apps/server/src/modules/tool/external-tool/testing/external-tool-entity.factory.ts @@ -16,6 +16,7 @@ import { Lti11ToolConfigEntity, Oauth2ToolConfigEntity, } from '@modules/tool/external-tool/entity'; +import { fileRecordFactory } from '@shared/testing'; import { BaseFactory } from '@shared/testing/factory/base.factory'; import { DeepPartial } from 'fishery'; @@ -121,6 +122,10 @@ export const externalToolEntityFactory = ExternalToolEntityFactory.define( isHidden: false, isDeactivated: false, openNewTab: true, + thumbnail: { + uploadUrl: 'https://uploadurl.com', + fileRecord: fileRecordFactory.build(), + }, }; } );