|
| 1 | +import request from 'supertest'; |
| 2 | +import { createHttpTerminator } from 'http-terminator'; |
| 3 | +import Koa from 'koa'; |
| 4 | +import bodyParser from 'koa-bodyparser'; |
| 5 | +import { applicationRoutes } from '../../routes'; |
| 6 | +import { NativeIntegrationDestinationService } from '../../services/destination/nativeIntegration'; |
| 7 | +import { ServiceSelector } from '../../helpers/serviceSelector'; |
| 8 | + |
| 9 | +let server: any; |
| 10 | +const OLD_ENV = process.env; |
| 11 | + |
| 12 | +beforeAll(async () => { |
| 13 | + process.env = { ...OLD_ENV }; // Make a copy |
| 14 | + const app = new Koa(); |
| 15 | + app.use( |
| 16 | + bodyParser({ |
| 17 | + jsonLimit: '200mb', |
| 18 | + }), |
| 19 | + ); |
| 20 | + applicationRoutes(app); |
| 21 | + server = app.listen(9090); |
| 22 | +}); |
| 23 | + |
| 24 | +afterAll(async () => { |
| 25 | + process.env = OLD_ENV; // Restore old environment |
| 26 | + const httpTerminator = createHttpTerminator({ |
| 27 | + server, |
| 28 | + }); |
| 29 | + await httpTerminator.terminate(); |
| 30 | +}); |
| 31 | + |
| 32 | +afterEach(() => { |
| 33 | + jest.clearAllMocks(); |
| 34 | +}); |
| 35 | + |
| 36 | +const getData = () => { |
| 37 | + return { body: { JSON: { a: 'b' } }, metadata: [{ a1: 'b1' }], destinationConfig: { a2: 'b2' } }; |
| 38 | +}; |
| 39 | + |
| 40 | +describe('Delivery controller tests', () => { |
| 41 | + describe('Delivery V0 tests', () => { |
| 42 | + test('successful delivery', async () => { |
| 43 | + const testOutput = { status: 200, message: 'success' }; |
| 44 | + const mockDestinationService = new NativeIntegrationDestinationService(); |
| 45 | + mockDestinationService.deliver = jest |
| 46 | + .fn() |
| 47 | + .mockImplementation((event, destinationType, requestMetadata, version) => { |
| 48 | + expect(event).toEqual(getData()); |
| 49 | + expect(destinationType).toEqual('__rudder_test__'); |
| 50 | + expect(version).toEqual('v0'); |
| 51 | + return testOutput; |
| 52 | + }); |
| 53 | + const getNativeDestinationServiceSpy = jest |
| 54 | + .spyOn(ServiceSelector, 'getNativeDestinationService') |
| 55 | + .mockImplementation(() => { |
| 56 | + return mockDestinationService; |
| 57 | + }); |
| 58 | + |
| 59 | + const response = await request(server) |
| 60 | + .post('/v0/destinations/__rudder_test__/proxy') |
| 61 | + .set('Accept', 'application/json') |
| 62 | + .send(getData()); |
| 63 | + |
| 64 | + expect(response.status).toEqual(200); |
| 65 | + expect(response.body).toEqual({ output: testOutput }); |
| 66 | + |
| 67 | + expect(response.header['apiversion']).toEqual('2'); |
| 68 | + |
| 69 | + expect(getNativeDestinationServiceSpy).toHaveBeenCalledTimes(1); |
| 70 | + expect(mockDestinationService.deliver).toHaveBeenCalledTimes(1); |
| 71 | + }); |
| 72 | + |
| 73 | + test('delivery failure', async () => { |
| 74 | + const mockDestinationService = new NativeIntegrationDestinationService(); |
| 75 | + mockDestinationService.deliver = jest |
| 76 | + .fn() |
| 77 | + .mockImplementation((event, destinationType, requestMetadata, version) => { |
| 78 | + expect(event).toEqual(getData()); |
| 79 | + expect(destinationType).toEqual('__rudder_test__'); |
| 80 | + expect(version).toEqual('v0'); |
| 81 | + throw new Error('test error'); |
| 82 | + }); |
| 83 | + const getNativeDestinationServiceSpy = jest |
| 84 | + .spyOn(ServiceSelector, 'getNativeDestinationService') |
| 85 | + .mockImplementation(() => { |
| 86 | + return mockDestinationService; |
| 87 | + }); |
| 88 | + |
| 89 | + const response = await request(server) |
| 90 | + .post('/v0/destinations/__rudder_test__/proxy') |
| 91 | + .set('Accept', 'application/json') |
| 92 | + .send(getData()); |
| 93 | + |
| 94 | + const expectedResp = { |
| 95 | + output: { |
| 96 | + message: 'test error', |
| 97 | + statTags: { |
| 98 | + errorCategory: 'transformation', |
| 99 | + }, |
| 100 | + destinationResponse: '', |
| 101 | + status: 500, |
| 102 | + }, |
| 103 | + }; |
| 104 | + expect(response.status).toEqual(500); |
| 105 | + expect(response.body).toEqual(expectedResp); |
| 106 | + |
| 107 | + expect(response.header['apiversion']).toEqual('2'); |
| 108 | + |
| 109 | + expect(getNativeDestinationServiceSpy).toHaveBeenCalledTimes(1); |
| 110 | + expect(mockDestinationService.deliver).toHaveBeenCalledTimes(1); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('Delivery V1 tests', () => { |
| 115 | + test('successful delivery', async () => { |
| 116 | + const testOutput = { status: 200, message: 'success' }; |
| 117 | + const mockDestinationService = new NativeIntegrationDestinationService(); |
| 118 | + mockDestinationService.deliver = jest |
| 119 | + .fn() |
| 120 | + .mockImplementation((event, destinationType, requestMetadata, version) => { |
| 121 | + expect(event).toEqual(getData()); |
| 122 | + expect(destinationType).toEqual('__rudder_test__'); |
| 123 | + expect(version).toEqual('v1'); |
| 124 | + return testOutput; |
| 125 | + }); |
| 126 | + const getNativeDestinationServiceSpy = jest |
| 127 | + .spyOn(ServiceSelector, 'getNativeDestinationService') |
| 128 | + .mockImplementation(() => { |
| 129 | + return mockDestinationService; |
| 130 | + }); |
| 131 | + |
| 132 | + const response = await request(server) |
| 133 | + .post('/v1/destinations/__rudder_test__/proxy') |
| 134 | + .set('Accept', 'application/json') |
| 135 | + .send(getData()); |
| 136 | + |
| 137 | + expect(response.status).toEqual(200); |
| 138 | + expect(response.body).toEqual({ output: testOutput }); |
| 139 | + |
| 140 | + expect(response.header['apiversion']).toEqual('2'); |
| 141 | + |
| 142 | + expect(getNativeDestinationServiceSpy).toHaveBeenCalledTimes(1); |
| 143 | + expect(mockDestinationService.deliver).toHaveBeenCalledTimes(1); |
| 144 | + }); |
| 145 | + |
| 146 | + test('delivery failure', async () => { |
| 147 | + const mockDestinationService = new NativeIntegrationDestinationService(); |
| 148 | + mockDestinationService.deliver = jest |
| 149 | + .fn() |
| 150 | + .mockImplementation((event, destinationType, requestMetadata, version) => { |
| 151 | + expect(event).toEqual(getData()); |
| 152 | + expect(destinationType).toEqual('__rudder_test__'); |
| 153 | + expect(version).toEqual('v1'); |
| 154 | + throw new Error('test error'); |
| 155 | + }); |
| 156 | + const getNativeDestinationServiceSpy = jest |
| 157 | + .spyOn(ServiceSelector, 'getNativeDestinationService') |
| 158 | + .mockImplementation(() => { |
| 159 | + return mockDestinationService; |
| 160 | + }); |
| 161 | + |
| 162 | + const response = await request(server) |
| 163 | + .post('/v1/destinations/__rudder_test__/proxy') |
| 164 | + .set('Accept', 'application/json') |
| 165 | + .send(getData()); |
| 166 | + |
| 167 | + const expectedResp = { |
| 168 | + output: { |
| 169 | + message: 'test error', |
| 170 | + statTags: { |
| 171 | + errorCategory: 'transformation', |
| 172 | + }, |
| 173 | + authErrorCategory: '', |
| 174 | + status: 500, |
| 175 | + response: [{ error: 'test error', metadata: { a1: 'b1' }, statusCode: 500 }], |
| 176 | + }, |
| 177 | + }; |
| 178 | + expect(response.status).toEqual(500); |
| 179 | + expect(response.body).toEqual(expectedResp); |
| 180 | + |
| 181 | + expect(response.header['apiversion']).toEqual('2'); |
| 182 | + |
| 183 | + expect(getNativeDestinationServiceSpy).toHaveBeenCalledTimes(1); |
| 184 | + expect(mockDestinationService.deliver).toHaveBeenCalledTimes(1); |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
0 commit comments