Skip to content

Commit

Permalink
chore: api test first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandra shekar Varkala committed Feb 13, 2024
1 parent d522b35 commit bf327c2
Show file tree
Hide file tree
Showing 14 changed files with 1,099 additions and 7 deletions.
70 changes: 70 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@pyroscope/nodejs": "^0.2.6",
"@rudderstack/integrations-lib": "^0.2.2",
"@rudderstack/workflow-engine": "^0.7.2",
"@shopify/jest-koa-mocks": "^5.1.1",
"ajv": "^8.12.0",
"ajv-draft-04": "^1.0.0",
"ajv-formats": "^2.1.1",
Expand Down
191 changes: 191 additions & 0 deletions src/controllers/__tests__/delivery.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import request from 'supertest';
import { createHttpTerminator } from 'http-terminator';
import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import { applicationRoutes } from '../../routes';
import { NativeIntegrationDestinationService } from '../../services/destination/nativeIntegration';
import { ServiceSelector } from '../../helpers/serviceSelector';

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 getData = () => {
return { body: { JSON: { a: 'b' } }, metadata: [{ a1: 'b1' }], destinationConfig: { a2: 'b2' } };
};

describe('Delivery controller tests', () => {
describe('Delivery V0 tests', () => {
test('successful delivery', async () => {
const testOutput = { status: 200, message: 'success' };
const mockDestinationService = new NativeIntegrationDestinationService();
mockDestinationService.deliver = jest
.fn()
.mockImplementation((event, destinationType, requestMetadata, version) => {
expect(event).toEqual(getData());
expect(destinationType).toEqual('__rudder_test__');
expect(version).toEqual('v0');
return testOutput;
});
const getNativeDestinationServiceSpy = jest
.spyOn(ServiceSelector, 'getNativeDestinationService')
.mockImplementation(() => {
return mockDestinationService;
});

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

expect(response.status).toEqual(200);
expect(response.body).toEqual({ output: testOutput });

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

expect(getNativeDestinationServiceSpy).toHaveBeenCalledTimes(1);
expect(mockDestinationService.deliver).toHaveBeenCalledTimes(1);
});

test('delivery failure', async () => {
const mockDestinationService = new NativeIntegrationDestinationService();
mockDestinationService.deliver = jest
.fn()
.mockImplementation((event, destinationType, requestMetadata, version) => {
expect(event).toEqual(getData());
expect(destinationType).toEqual('__rudder_test__');
expect(version).toEqual('v0');
throw new Error('test error');
});
const getNativeDestinationServiceSpy = jest
.spyOn(ServiceSelector, 'getNativeDestinationService')
.mockImplementation(() => {
return mockDestinationService;
});

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

const expectedResp = {
output: {
message: 'test error',
statTags: {
errorCategory: 'transformation',
},
destinationResponse: '',
status: 500,
},
};
expect(response.status).toEqual(500);
expect(response.body).toEqual(expectedResp);

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

expect(getNativeDestinationServiceSpy).toHaveBeenCalledTimes(1);
expect(mockDestinationService.deliver).toHaveBeenCalledTimes(1);
});
});

describe('Delivery V1 tests', () => {
test('successful delivery', async () => {
const testOutput = { status: 200, message: 'success' };
const mockDestinationService = new NativeIntegrationDestinationService();
mockDestinationService.deliver = jest
.fn()
.mockImplementation((event, destinationType, requestMetadata, version) => {
expect(event).toEqual(getData());
expect(destinationType).toEqual('__rudder_test__');
expect(version).toEqual('v1');
return testOutput;
});
const getNativeDestinationServiceSpy = jest
.spyOn(ServiceSelector, 'getNativeDestinationService')
.mockImplementation(() => {
return mockDestinationService;
});

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

expect(response.status).toEqual(200);
expect(response.body).toEqual({ output: testOutput });

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

expect(getNativeDestinationServiceSpy).toHaveBeenCalledTimes(1);
expect(mockDestinationService.deliver).toHaveBeenCalledTimes(1);
});

test('delivery failure', async () => {
const mockDestinationService = new NativeIntegrationDestinationService();
mockDestinationService.deliver = jest
.fn()
.mockImplementation((event, destinationType, requestMetadata, version) => {
expect(event).toEqual(getData());
expect(destinationType).toEqual('__rudder_test__');
expect(version).toEqual('v1');
throw new Error('test error');
});
const getNativeDestinationServiceSpy = jest
.spyOn(ServiceSelector, 'getNativeDestinationService')
.mockImplementation(() => {
return mockDestinationService;
});

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

const expectedResp = {
output: {
message: 'test error',
statTags: {
errorCategory: 'transformation',
},
authErrorCategory: '',
status: 500,
response: [{ error: 'test error', metadata: { a1: 'b1' }, statusCode: 500 }],
},
};
expect(response.status).toEqual(500);
expect(response.body).toEqual(expectedResp);

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

expect(getNativeDestinationServiceSpy).toHaveBeenCalledTimes(1);
expect(mockDestinationService.deliver).toHaveBeenCalledTimes(1);
});
});
});
Loading

0 comments on commit bf327c2

Please sign in to comment.