Skip to content

Commit

Permalink
create a stub implementation of the cpfValidation lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
dysmento committed Jan 8, 2024
1 parent f11d611 commit 318d1bf
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 48 deletions.
70 changes: 49 additions & 21 deletions api/src/functions/cpfValidation/cpfValidation.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,57 @@
import { mockHttpEvent } from '@redwoodjs/testing/api'
// import { S3EventRecord } from 'aws-lambda'

import { handler } from './cpfValidation'
// import { handler } from './cpfValidation'

// Improve this test with help from the Redwood Testing Doc:
// https://redwoodjs.com/docs/testing#testing-functions

describe('cpfValidation function', () => {
it('Should respond with 200', async () => {
const httpEvent = mockHttpEvent({
queryStringParameters: {
id: '42', // Add parameters here
},
})

const response = await handler(httpEvent, null)
const { data } = JSON.parse(response.body)

expect(response.statusCode).toBe(200)
expect(data).toBe('cpfValidation function')
it('Dummy test', () => {
expect(1 + 1).toBe(2)
})

// You can also use scenarios to test your api functions
// See guide here: https://redwoodjs.com/docs/testing#scenarios
//
// scenario('Scenario test', async () => {
//
// })
// it('Should respond with 200', async () => {
// const record: S3EventRecord = {
// eventVersion: '2.0',
// eventSource: 'aws:s3',
// eventName: 'ObjectCreated:Put',
// eventTime: '1970-01-01T00:00:00.000Z',
// userIdentity: { principalId: 'test-principalId' },
// requestParameters: { sourceIPAddress: 'test-sourceIPAddress' },
// responseElements: {
// 'x-amz-request-id': 'test-x-amz-request-id',
// 'x-amz-id-2': 'test-x-amz-id-2',
// },
// awsRegion: 'us-east-1',
// s3: {
// s3SchemaVersion: '1.0',
// configurationId: 'test-configurationId',
// bucket: {
// name: 'test-bucket',
// arn: 'test-arn',
// ownerIdentity: {
// principalId: 'test-principalId',
// },
// },
// object: {
// key: 'test-key',
// size: 1234,
// eTag: 'test-etag',
// sequencer: 'test-sequencer',
// },
// },
// }
// const s3Event = {
// Records: [record],
// }
// const response = await handler(s3Event, null, null)
// const { data } = JSON.parse(response.body)
// expect(response.statusCode).toBe(200)
// expect(data).toBe('excelToJson function')
})

// You can also use scenarios to test your api functions
// See guide here: https://redwoodjs.com/docs/testing#scenarios
//
// scenario('Scenario test', async () => {
//
// })
48 changes: 21 additions & 27 deletions api/src/functions/cpfValidation/cpfValidation.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
import type { APIGatewayEvent, Context } from 'aws-lambda'
import { https } from 'https'

import { S3Event, S3Handler } from 'aws-lambda'

import { logger } from 'src/lib/logger'

/**
* The handler function is your code that processes http request events.
* You can use return and throw to send a response or error, respectively.
*
* Important: When deployed, a custom serverless function is an open API endpoint and
* is your responsibility to secure appropriately.
*
* @see {@link https://redwoodjs.com/docs/serverless-functions#security-considerations|Serverless Function Considerations}
* in the RedwoodJS documentation for more information.
*
* @typedef { import('aws-lambda').APIGatewayEvent } APIGatewayEvent
* @typedef { import('aws-lambda').Context } Context
* @param { APIGatewayEvent } event - an object which contains information from the invoker.
* @param { Context } context - contains information about the invocation,
* function, and execution environment.
*/
export const handler = async (event: APIGatewayEvent, _context: Context) => {
logger.info(`${event.httpMethod} ${event.path}: cpfValidation function`)
const apiEndpoint = 'https://example.com'

export const handler: S3Handler = async (event: S3Event): Promise<void> => {
try {
const bucket = event.Records[0].s3.bucket.name

Check failure on line 11 in api/src/functions/cpfValidation/cpfValidation.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

'bucket' is assigned a value but never used. Allowed unused vars must match /^_/u

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable bucket.
const key = event.Records[0].s3.object.key

Check failure on line 12 in api/src/functions/cpfValidation/cpfValidation.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

'key' is assigned a value but never used. Allowed unused vars must match /^_/u

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable key.

const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
}

return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: 'cpfValidation function',
}),
// call API endpoint with S3 key
https.request(apiEndpoint, options, (res) => {})

Check failure on line 22 in api/src/functions/cpfValidation/cpfValidation.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

'res' is defined but never used. Allowed unused args must match /^_/u
} catch (error) {
logger.error('Error processing S3 event:', error)
throw error
}
}

0 comments on commit 318d1bf

Please sign in to comment.