|
1 | | -describe('Temporary Placeholder Test', () => { |
2 | | - it('should pass without any assertions', () => { |
3 | | - // This is a temporary placeholder test that will pass without any assertions |
| 1 | +const { S3Client, GetObjectCommand, CopyObjectCommand, DeleteObjectCommand } = require('@aws-sdk/client-s3'); |
| 2 | +const csvParser = require('csv-parser'); |
| 3 | +const { getCorsHeaders } = require('../lambdas/cors'); |
| 4 | +const { HTTP_STATUS, MESSAGES } = require('../lambdas/constants'); |
| 5 | +const { handler } = require('../lambdas/importFileParser'); |
| 6 | + |
| 7 | +jest.mock('@aws-sdk/client-s3'); |
| 8 | +jest.mock('csv-parser'); |
| 9 | + |
| 10 | +describe('Lambda handler', () => { |
| 11 | + let sendMock; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + sendMock = jest.fn(); |
| 15 | + S3Client.mockImplementation(() => ({ |
| 16 | + send: sendMock |
| 17 | + })); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + jest.resetAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + test('should process CSV and return success response', async () => { |
| 25 | + const mockEvent = { |
| 26 | + headers: { |
| 27 | + origin: 'http://example.com' |
| 28 | + }, |
| 29 | + Records: [{ |
| 30 | + s3: { |
| 31 | + bucket: { |
| 32 | + name: 'my-bucket' |
| 33 | + }, |
| 34 | + object: { |
| 35 | + key: 'path/to/myfile.csv' |
| 36 | + } |
| 37 | + } |
| 38 | + }] |
| 39 | + }; |
| 40 | + |
| 41 | + // Mock the S3 getObject response to return a readable stream |
| 42 | + const { PassThrough } = require('stream'); |
| 43 | + const mockStream = new PassThrough(); |
| 44 | + mockStream.end('name,age\nAlice,30\nBob,25\n'); |
| 45 | + |
| 46 | + sendMock |
| 47 | + .mockResolvedValueOnce({ Body: mockStream }) // getObjectCommand |
| 48 | + .mockResolvedValueOnce({}) // copyObjectCommand |
| 49 | + .mockResolvedValueOnce({}); // deleteObjectCommand |
| 50 | + |
| 51 | + // Mock the csvParser to return a readable stream of parsed CSV objects |
| 52 | + const mockCsvStream = new PassThrough({ objectMode: true }); |
| 53 | + setImmediate(() => { |
| 54 | + mockCsvStream.write({ name: 'Alice', age: '30' }); |
| 55 | + mockCsvStream.write({ name: 'Bob', age: '25' }); |
| 56 | + mockCsvStream.end(); |
4 | 57 | }); |
| 58 | + |
| 59 | + csvParser.mockReturnValue(mockCsvStream); |
| 60 | + |
| 61 | + const response = await handler(mockEvent); |
| 62 | + |
| 63 | + expect(sendMock).toHaveBeenCalledTimes(3); |
| 64 | + expect(sendMock).toHaveBeenNthCalledWith(1, expect.any(GetObjectCommand)); |
| 65 | + expect(sendMock).toHaveBeenNthCalledWith(2, expect.any(CopyObjectCommand)); |
| 66 | + expect(sendMock).toHaveBeenNthCalledWith(3, expect.any(DeleteObjectCommand)); |
| 67 | + |
| 68 | + expect(response).toEqual({ |
| 69 | + statusCode: 200, |
| 70 | + headers: { |
| 71 | + 'Access-Control-Allow-Origin': '*', |
| 72 | + 'Access-Control-Allow-Headers': 'Content-Type', |
| 73 | + 'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS', |
| 74 | + 'Content-Type': 'application/json' |
| 75 | + }, |
| 76 | + body: JSON.stringify({ message: 'CSV File was processed' }) |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + test('should handle errors and return error response', async () => { |
| 81 | + const mockEvent = { |
| 82 | + headers: { |
| 83 | + origin: 'http://example.com' |
| 84 | + }, |
| 85 | + Records: [{ |
| 86 | + s3: { |
| 87 | + bucket: { |
| 88 | + name: 'my-bucket' |
| 89 | + }, |
| 90 | + object: { |
| 91 | + key: 'path/to/myfile.csv' |
| 92 | + } |
| 93 | + } |
| 94 | + }] |
| 95 | + }; |
| 96 | + |
| 97 | + const errorMessage = 'Something went wrong'; |
| 98 | + |
| 99 | + sendMock.mockRejectedValue(new Error(errorMessage)); |
| 100 | + |
| 101 | + const response = await handler(mockEvent); |
| 102 | + |
| 103 | + expect(sendMock).toHaveBeenCalledTimes(1); |
| 104 | + expect(response.statusCode).toBe(HTTP_STATUS.INTERNAL_SERVER_ERROR); |
| 105 | + expect(response).toEqual({ |
| 106 | + statusCode: HTTP_STATUS.INTERNAL_SERVER_ERROR, |
| 107 | + headers: { |
| 108 | + 'Access-Control-Allow-Origin': '*', |
| 109 | + 'Access-Control-Allow-Headers': 'Content-Type', |
| 110 | + 'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS', |
| 111 | + 'Content-Type': 'application/json' |
| 112 | + }, |
| 113 | + body: JSON.stringify({ message: MESSAGES.INTERNAL_SERVER_ERROR }) |
| 114 | + }); |
| 115 | + }); |
5 | 116 | }); |
0 commit comments