Skip to content

Commit

Permalink
N21-1918 adjusts api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arnegns committed Jul 24, 2024
1 parent db2ecd0 commit 75ff3c5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<FileRecordResponse> = { 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 () => {
Expand Down Expand Up @@ -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,
});
});
});
Expand Down Expand Up @@ -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<FileRecordResponse> = { id: new ObjectId().toHexString() };
axiosMock.onPost(/api\/v3\/file\/upload-from-url/).reply(HttpStatus.OK, fileRecordResponse);

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

return {
Expand Down Expand Up @@ -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',
Expand All @@ -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<FileRecordResponse> = { 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);
Expand Down Expand Up @@ -565,6 +586,7 @@ describe('ToolController (API)', () => {
mediumId: params.medium?.mediumId ?? '',
publisher: params.medium?.publisher,
},
thumbnailUrl: 'https://link.to-my-thumbnail2.com',
});
});
});
Expand Down Expand Up @@ -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 };
};

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

Expand Down Expand Up @@ -121,6 +122,10 @@ export const externalToolEntityFactory = ExternalToolEntityFactory.define(
isHidden: false,
isDeactivated: false,
openNewTab: true,
thumbnail: {
uploadUrl: 'https://uploadurl.com',
fileRecord: fileRecordFactory.build(),
},
};
}
);

0 comments on commit 75ff3c5

Please sign in to comment.