Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: api test #2995

Merged
merged 6 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
187 changes: 187 additions & 0 deletions src/controllers/__tests__/delivery.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
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 response = await request(server)
.post('/v0/destinations/__rudder_test__/proxy')
.set('Accept', 'application/json')
.send(getData());

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 response = await request(server)
.post('/v0/destinations/__rudder_test__/proxy')
.set('Accept', 'application/json')
.send(getData());

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 response = await request(server)
.post('/v1/destinations/__rudder_test__/proxy')
.set('Accept', 'application/json')
.send(getData());

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 response = await request(server)
.post('/v1/destinations/__rudder_test__/proxy')
.set('Accept', 'application/json')
.send(getData());

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
Loading