From 7f59375f76f040e741095fcdad83a6f7a3d58e16 Mon Sep 17 00:00:00 2001 From: Pratik Prajapati <33730817+ppratikcr7@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:24:50 +0530 Subject: [PATCH] adding segmentcontroller unit test cases and resolved other small issues in unit tests (#1729) Co-authored-by: danoswaltCL <97542869+danoswaltCL@users.noreply.github.com> --- .../api/controllers/ExperimentController.ts | 40 ++++++ .../controllers/AnalyticsController.test.ts | 2 +- .../controllers/ExperimentController.test.ts | 6 +- .../controllers/SegmentController.test.ts | 115 ++++++++++++++++++ .../controllers/mocks/SegmentServiceMock.ts | 52 ++++++++ 5 files changed, 212 insertions(+), 3 deletions(-) create mode 100644 backend/packages/Upgrade/test/unit/controllers/SegmentController.test.ts create mode 100644 backend/packages/Upgrade/test/unit/controllers/mocks/SegmentServiceMock.ts diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentController.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentController.ts index 75ddfa8a48..4f81feccc7 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentController.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentController.ts @@ -1232,6 +1232,46 @@ export class ExperimentController { return this.experimentService.importExperiment(experiments, currentUser, request.logger); } + /** + * @swagger + * /experiments/{export}: + * get: + * description: Export Experiment JSON + * parameters: + * - in: body + * name: experiments + * required: true + * schema: + * type: array + * items: + * type: object + * properties: + * fileName: + * type: string + * fileContent: + * type: string + * description: Experiment Files + * tags: + * - Experiments + * produces: + * - application/json + * responses: + * '200': + * description: Experiment is exported + * schema: + * type: array + * items: + * type: object + * properties: + * fileName: + * type: string + * error: + * type: string + * '401': + * description: AuthorizationRequiredError + * '500': + * description: Internal Server Error + */ @Get('/export') public exportExperiment( @QueryParams() diff --git a/backend/packages/Upgrade/test/unit/controllers/AnalyticsController.test.ts b/backend/packages/Upgrade/test/unit/controllers/AnalyticsController.test.ts index 87e6c22fe1..ae0bf9b98a 100644 --- a/backend/packages/Upgrade/test/unit/controllers/AnalyticsController.test.ts +++ b/backend/packages/Upgrade/test/unit/controllers/AnalyticsController.test.ts @@ -56,7 +56,7 @@ describe('Analytics Controller Testing', () => { .expect(200); }); - test('Post request for /api/stats/csv', () => { + test('Get request for /api/stats/csv', () => { return request(app) .get('/api/stats/csv') .query({ diff --git a/backend/packages/Upgrade/test/unit/controllers/ExperimentController.test.ts b/backend/packages/Upgrade/test/unit/controllers/ExperimentController.test.ts index dd5ba9bac2..68a2e4c2df 100644 --- a/backend/packages/Upgrade/test/unit/controllers/ExperimentController.test.ts +++ b/backend/packages/Upgrade/test/unit/controllers/ExperimentController.test.ts @@ -198,8 +198,10 @@ describe('Experiment Controller Testing', () => { test('Get request for /api/experiments/export', () => { return request(app) - .post('/api/experiments/import') - .send([experimentData.id]) + .get('/api/experiments/export') + .query({ + ids: [uuid()], + }) .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200); diff --git a/backend/packages/Upgrade/test/unit/controllers/SegmentController.test.ts b/backend/packages/Upgrade/test/unit/controllers/SegmentController.test.ts new file mode 100644 index 0000000000..ca948d95b8 --- /dev/null +++ b/backend/packages/Upgrade/test/unit/controllers/SegmentController.test.ts @@ -0,0 +1,115 @@ +import app from '../../utils/expressApp'; +import request from 'supertest'; +import { configureLogger } from '../../utils/logger'; +import { useContainer as routingUseContainer } from 'routing-controllers'; +import { Container } from 'typedi'; +import { v4 as uuid } from 'uuid'; +import SegmentServiceMock from './mocks/SegmentServiceMock'; +import { SegmentService } from '../../../src/api/services/SegmentService'; + +import { useContainer as classValidatorUseContainer } from 'class-validator'; +import { useContainer as ormUseContainer } from 'typeorm'; + +describe('Segment Controller Testing', () => { + beforeAll(() => { + configureLogger(); + routingUseContainer(Container); + ormUseContainer(Container); + classValidatorUseContainer(Container); + + // set mock container + Container.set(SegmentService, new SegmentServiceMock()); + }); + + afterAll(() => { + Container.reset(); + }); + + const segmentData = { + id: uuid(), + name: "segment1", + description: "desc", + context: "home", + type: "public", + status: "Unused", + userIds: ["user1"], + groups: { + groupId : "group1", + type: "school" + }, + subSegmentIds: ["seg2"] + }; + + test('Get request for /api/segments', () => { + return request(app) + .get('/api/segments') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200); + }); + + test('Get request for /api/segments/:segmentId', () => { + return request(app) + .get(`/api/segments/${uuid()}`) + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200); + }); + + test('Get request for /api/segments/status/:segmentId', () => { + return request(app) + .get(`/api/segments/status/${uuid()}`) + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200); + }); + + test('Post request for /api/segments', () => { + return request(app) + .post('/api/segments') + .send([segmentData]) + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200); + }); + + test('Delete request for /api/segments/:segmentId', () => { + return request(app).delete(`/api/segments/${uuid()}`).expect('Content-Type', /json/).expect(200); + }); + + test('Post request for /api/segments/import', () => { + return request(app) + .post('/api/segments/import') + .send(segmentData) + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200); + }); + + test('Post request for /api/segments/validation', () => { + return request(app).post('/api/segments/validation').send(segmentData).expect('Content-Type', /json/).expect(200); + }); + + test('Get request for /api/segments/export/json', () => { + return request(app) + .get('/api/segments/export/json') + .query({ + ids: [uuid()], + }) + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200); + }); + + test('Get request for /api/segments/export/csv', () => { + return request(app) + .get('/api/segments/export/csv') + .query({ + ids: [uuid()], + }) + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200); + }); + +}); diff --git a/backend/packages/Upgrade/test/unit/controllers/mocks/SegmentServiceMock.ts b/backend/packages/Upgrade/test/unit/controllers/mocks/SegmentServiceMock.ts new file mode 100644 index 0000000000..f4ee792779 --- /dev/null +++ b/backend/packages/Upgrade/test/unit/controllers/mocks/SegmentServiceMock.ts @@ -0,0 +1,52 @@ +import { Service } from 'typedi'; + +@Service() +export default class ExcludeServiceMock { + public getAllSegments(): Promise<[]> { + return Promise.resolve([]); + } + + public getAllSegmentWithStatus(): Promise<[]> { + return Promise.resolve([]); + } + + public getSegmentById(id: string): Promise<[]> { + return Promise.resolve([]); + } + + public getSegmentWithStatusById(id: string): Promise<[]> { + return Promise.resolve([]); + } + + public getSingleSegmentWithStatus(id: string): Promise<[]> { + return Promise.resolve([]); + } + + public upsertSegment(): Promise<[]> { + return Promise.resolve([]); + } + + public importSegments(): Promise<[]> { + return Promise.resolve([]); + } + + public deleteSegment(id: string): Promise<[]> { + return Promise.resolve([]); + } + + public validateSegments(): Promise<[]> { + return Promise.resolve([]); + } + + public exportSegments(id: string): Promise<[]> { + return Promise.resolve([]); + } + + public exportSegment(id: string): Promise<[]> { + return Promise.resolve([]); + } + + public exportSegmentCSV(id: string): Promise<[]> { + return Promise.resolve([]); + } +}