-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chandra shekar Varkala
committed
Feb 13, 2024
1 parent
d522b35
commit bf327c2
Showing
14 changed files
with
1,099 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.