Skip to content

Commit

Permalink
chore: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandra shekar Varkala committed Feb 15, 2024
1 parent 9b7c034 commit e5f3aa8
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 31 deletions.
12 changes: 4 additions & 8 deletions src/controllers/__tests__/delivery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ describe('Delivery controller tests', () => {
return mockDestinationService;
});

const data = getData();
const response = await request(server)
.post('/v0/destinations/__rudder_test__/proxy')
.set('Accept', 'application/json')
.send(data);
.send(getData());

expect(response.status).toEqual(200);
expect(response.body).toEqual({ output: testOutput });
Expand All @@ -87,11 +86,10 @@ describe('Delivery controller tests', () => {
return mockDestinationService;
});

const data = getData();
const response = await request(server)
.post('/v0/destinations/__rudder_test__/proxy')
.set('Accept', 'application/json')
.send(data);
.send(getData());

const expectedResp = {
output: {
Expand Down Expand Up @@ -131,11 +129,10 @@ describe('Delivery controller tests', () => {
return mockDestinationService;
});

const data = getData();
const response = await request(server)
.post('/v1/destinations/__rudder_test__/proxy')
.set('Accept', 'application/json')
.send(data);
.send(getData());

expect(response.status).toEqual(200);
expect(response.body).toEqual({ output: testOutput });
Expand All @@ -162,11 +159,10 @@ describe('Delivery controller tests', () => {
return mockDestinationService;
});

const data = getData();
const response = await request(server)
.post('/v1/destinations/__rudder_test__/proxy')
.set('Accept', 'application/json')
.send(data);
.send(getData());

const expectedResp = {
output: {
Expand Down
107 changes: 98 additions & 9 deletions src/controllers/__tests__/destination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { applicationRoutes } from '../../routes';
import { ServiceSelector } from '../../helpers/serviceSelector';
import { DynamicConfigParser } from '../../util/dynamicConfigParser';
import { NativeIntegrationDestinationService } from '../../services/destination/nativeIntegration';
import exp from 'constants';

let server: any;
const OLD_ENV = process.env;
Expand Down Expand Up @@ -85,11 +84,10 @@ describe('Destination controller tests', () => {
return events;
});

const data = getData();
const response = await request(server)
.post('/v0/destinations/__rudder_test__')
.set('Accept', 'application/json')
.send(data);
.send(getData());

expect(response.status).toEqual(200);
expect(response.body).toEqual(expectedOutput);
Expand Down Expand Up @@ -134,11 +132,10 @@ describe('Destination controller tests', () => {
return events;
});

const data = getData();
const response = await request(server)
.post('/v0/destinations/__rudder_test__')
.set('Accept', 'application/json')
.send(data);
.send(getData());

expect(response.status).toEqual(200);
expect(response.body).toEqual(expectedOutput);
Expand Down Expand Up @@ -188,11 +185,10 @@ describe('Destination controller tests', () => {
return events;
});

const data = getRouterTransformInputData();
const response = await request(server)
.post('/routerTransform')
.set('Accept', 'application/json')
.send(data);
.send(getRouterTransformInputData());

expect(response.status).toEqual(200);
expect(response.body).toEqual({ output: expectedOutput });
Expand Down Expand Up @@ -221,11 +217,10 @@ describe('Destination controller tests', () => {
return events;
});

const data = getRouterTransformInputData();
const response = await request(server)
.post('/routerTransform')
.set('Accept', 'application/json')
.send(data);
.send(getRouterTransformInputData());

const expectedOutput = [
{
Expand All @@ -245,4 +240,98 @@ describe('Destination controller tests', () => {
expect(mockDestinationService.doRouterTransformation).toHaveBeenCalledTimes(1);
});
});

describe('Batch transform tests', () => {
test('successful batching at router', async () => {
const mockDestinationService = new NativeIntegrationDestinationService();

const expectedOutput = [
{
message: { a: 'b1' },
destination: {},
metadata: { jobId: 1 },
request: { query: {} },
},
{
message: { a: 'b2' },
destination: {},
metadata: { jobId: 2 },
request: { query: {} },
},
];

mockDestinationService.doBatchTransformation = jest
.fn()
.mockImplementation((events, destinationType, version, requestMetadata) => {
expect(events).toEqual(expectedOutput);
expect(destinationType).toEqual('__rudder_test__');
expect(version).toEqual('v0');

return events;
});
const getDestinationServiceSpy = jest
.spyOn(ServiceSelector, 'getDestinationService')
.mockImplementation(() => {
return mockDestinationService;
});

DynamicConfigParser.process = jest.fn().mockImplementation((events) => {
return events;
});

const response = await request(server)
.post('/batch')
.set('Accept', 'application/json')
.send(getRouterTransformInputData());

expect(response.status).toEqual(200);
expect(response.body).toEqual(expectedOutput);

expect(response.header['apiversion']).toEqual('2');

expect(getDestinationServiceSpy).toHaveBeenCalledTimes(1);
expect(mockDestinationService.doBatchTransformation).toHaveBeenCalledTimes(1);
});

test('batch transformation failure', async () => {
const mockDestinationService = new NativeIntegrationDestinationService();

mockDestinationService.doBatchTransformation = jest
.fn()
.mockImplementation((events, destinationType, version, requestMetadata) => {
throw new Error('Batch transformation failed');
});
const getDestinationServiceSpy = jest
.spyOn(ServiceSelector, 'getDestinationService')
.mockImplementation(() => {
return mockDestinationService;
});

DynamicConfigParser.process = jest.fn().mockImplementation((events) => {
return events;
});

const response = await request(server)
.post('/batch')
.set('Accept', 'application/json')
.send(getRouterTransformInputData());

const expectedOutput = [
{
metadata: [{ jobId: 1 }, { jobId: 2 }],
batched: false,
statusCode: 500,
error: 'Batch transformation failed',
statTags: { errorCategory: 'transformation' },
},
];
expect(response.status).toEqual(200);
expect(response.body).toEqual(expectedOutput);

expect(response.header['apiversion']).toEqual('2');

expect(getDestinationServiceSpy).toHaveBeenCalledTimes(1);
expect(mockDestinationService.doBatchTransformation).toHaveBeenCalledTimes(1);
});
});
});
107 changes: 107 additions & 0 deletions src/controllers/__tests__/regulation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import request from 'supertest';
import { createHttpTerminator } from 'http-terminator';
import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import { applicationRoutes } from '../../routes';
import { ServiceSelector } from '../../helpers/serviceSelector';
import { NativeIntegrationDestinationService } from '../../services/destination/nativeIntegration';

let server: any;
const OLD_ENV = process.env;

beforeAll(async () => {
process.env = { ...OLD_ENV }; // Make a copy
const app = new Koa();
app.use(
bodyParser({
jsonLimit: '200mb',
}),
);
applicationRoutes(app);
server = app.listen(9090);
});

afterAll(async () => {
process.env = OLD_ENV; // Restore old environment
const httpTerminator = createHttpTerminator({
server,
});
await httpTerminator.terminate();
});

afterEach(() => {
jest.clearAllMocks();
});

const getDeletionData = () => {
return [
{ userAttributes: [{ a: 'b1' }], destType: '__rudder_test__' },
{ userAttributes: [{ a: 'b1' }], destType: '__rudder_test__' },
];
};

describe('Regulation controller tests', () => {
describe('Delete users tests', () => {
test('successful delete users request', async () => {
const mockDestinationService = new NativeIntegrationDestinationService();

const expectedOutput = [{ statusCode: 400 }, { statusCode: 200 }];

mockDestinationService.processUserDeletion = jest
.fn()
.mockImplementation((reqs, destInfo) => {
expect(reqs).toEqual(getDeletionData());
expect(destInfo).toEqual({ a: 'test' });

return expectedOutput;
});
const getDestinationServiceSpy = jest
.spyOn(ServiceSelector, 'getNativeDestinationService')
.mockImplementation(() => {
return mockDestinationService;
});

const response = await request(server)
.post('/deleteUsers')
.set('Accept', 'application/json')
.set('x-rudder-dest-info', '{"a": "test"}')
.send(getDeletionData());

expect(response.status).toEqual(400);
expect(response.body).toEqual(expectedOutput);

expect(getDestinationServiceSpy).toHaveBeenCalledTimes(1);
expect(mockDestinationService.processUserDeletion).toHaveBeenCalledTimes(1);
});

test('delete users request failure', async () => {
const mockDestinationService = new NativeIntegrationDestinationService();

mockDestinationService.processUserDeletion = jest
.fn()
.mockImplementation((reqs, destInfo) => {
expect(reqs).toEqual(getDeletionData());
expect(destInfo).toEqual({ a: 'test' });

throw new Error('processUserDeletion error');
});
const getDestinationServiceSpy = jest
.spyOn(ServiceSelector, 'getNativeDestinationService')
.mockImplementation(() => {
return mockDestinationService;
});

const response = await request(server)
.post('/deleteUsers')
.set('Accept', 'application/json')
.set('x-rudder-dest-info', '{"a": "test"}')
.send(getDeletionData());

expect(response.status).toEqual(500);
expect(response.body).toEqual([{ error: {}, statusCode: 500 }]);

expect(getDestinationServiceSpy).toHaveBeenCalledTimes(1);
expect(mockDestinationService.processUserDeletion).toHaveBeenCalledTimes(1);
});
});
});
12 changes: 4 additions & 8 deletions src/controllers/__tests__/source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@ describe('Source controller tests', () => {
return { implementationVersion: version, input: e };
});

const data = getData();
const response = await request(server)
.post('/v0/sources/__rudder_test__')
.set('Accept', 'application/json')
.send(data);
.send(getData());

expect(response.status).toEqual(200);
expect(response.body).toEqual(testOutput);
Expand Down Expand Up @@ -105,11 +104,10 @@ describe('Source controller tests', () => {
throw new Error('test error');
});

const data = getData();
const response = await request(server)
.post('/v0/sources/__rudder_test__')
.set('Accept', 'application/json')
.send(data);
.send(getData());

const expectedResp = [
{
Expand Down Expand Up @@ -161,11 +159,10 @@ describe('Source controller tests', () => {
return { implementationVersion: version, input: e };
});

const data = getData();
const response = await request(server)
.post('/v1/sources/__rudder_test__')
.set('Accept', 'application/json')
.send(data);
.send(getData());

expect(response.status).toEqual(200);
expect(response.body).toEqual(testOutput);
Expand Down Expand Up @@ -196,11 +193,10 @@ describe('Source controller tests', () => {
throw new Error('test error');
});

const data = getData();
const response = await request(server)
.post('/v1/sources/__rudder_test__')
.set('Accept', 'application/json')
.send(data);
.send(getData());

const expectedResp = [
{
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/regulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class RegulationController {
rudderDestInfo,
);
ctx.body = resplist;
ctx.status = resplist[0].statusCode;
ctx.status = resplist[0].statusCode; // TODO: check if this is the right way to set status
} catch (error: CatchErr) {
const metaTO = integrationService.getTags(
userDeletionRequests[0].destType,
Expand All @@ -46,8 +46,8 @@ export class RegulationController {
const errResp = DestinationPostTransformationService.handleUserDeletionFailureEvents(
error,
metaTO,
);
ctx.body = [{ error, statusCode: 500 }] as UserDeletionResponse[];
); // TODO: this is not used. Fix it.
ctx.body = [{ error, statusCode: 500 }] as UserDeletionResponse[]; // TODO: responses array length is always 1. Is that okay?
ctx.status = 500;
}
stats.timing('dest_transform_request_latency', startTime, {
Expand Down
Loading

0 comments on commit e5f3aa8

Please sign in to comment.