diff --git a/Lambda_CRUD.postman_collection.json b/Lambda_CRUD.postman_collection.json index 1bd8f3de892d1fc582c854b0cc2aeea67fb788ae..37f917a187dbfda04356e7a68dd2c5e32ae73321 100644 --- a/Lambda_CRUD.postman_collection.json +++ b/Lambda_CRUD.postman_collection.json @@ -81,6 +81,243 @@ }, "response": [] }, + { + "name": "CREATE no body", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 400\", function () {", + " pm.response.to.have.status(400);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "PUT", + "header": [ + { + "key": "Authorization", + "type": "text", + "value": "SignedHeaders", + "disabled": true + }, + { + "key": "Date", + "type": "text", + "value": "{{$timestamp}}", + "disabled": true + } + ], + "body": { + "mode": "raw", + "raw": "" + }, + "url": { + "raw": "{{endpoint}}/crud", + "host": [ + "{{endpoint}}" + ], + "path": [ + "crud" + ] + } + }, + "response": [] + }, + { + "name": "UPDATE no body", + "event": [ + { + "listen": "prerequest", + "script": { + "exec": [ + "" + ], + "type": "text/javascript" + } + }, + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 400\", function () {", + " pm.response.to.have.status(400);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "" + }, + "url": { + "raw": "{{endpoint}}/crud?id={{id}}", + "host": [ + "{{endpoint}}" + ], + "path": [ + "crud" + ], + "query": [ + { + "key": "id", + "value": "{{id}}" + } + ] + } + }, + "response": [] + }, + { + "name": "UPDATE empty body", + "event": [ + { + "listen": "prerequest", + "script": { + "exec": [ + "" + ], + "type": "text/javascript" + } + }, + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 400\", function () {", + " pm.response.to.have.status(400);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "{}" + }, + "url": { + "raw": "{{endpoint}}/crud?id={{id}}", + "host": [ + "{{endpoint}}" + ], + "path": [ + "crud" + ], + "query": [ + { + "key": "id", + "value": "{{id}}" + } + ] + } + }, + "response": [] + }, + { + "name": "UPDATE no id", + "event": [ + { + "listen": "prerequest", + "script": { + "exec": [ + "" + ], + "type": "text/javascript" + } + }, + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 400\", function () {", + " pm.response.to.have.status(400);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "{\"hello\":\"bonjour\"}" + }, + "url": { + "raw": "{{endpoint}}/crud", + "host": [ + "{{endpoint}}" + ], + "path": [ + "crud" + ] + } + }, + "response": [] + }, + { + "name": "UPDATE inexistant id", + "event": [ + { + "listen": "prerequest", + "script": { + "exec": [ + "" + ], + "type": "text/javascript" + } + }, + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 204\", function () {", + " pm.response.to.have.status(204);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "{\"hello\":\"bonjour\"}" + }, + "url": { + "raw": "{{endpoint}}/crud?id=noid", + "host": [ + "{{endpoint}}" + ], + "path": [ + "crud" + ], + "query": [ + { + "key": "id", + "value": "noid" + } + ] + } + }, + "response": [] + }, { "name": "UPDATE", "event": [ @@ -171,6 +408,179 @@ }, "response": [] }, + { + "name": "GET no id", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 400\", function () {", + " pm.response.to.have.status(400);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{endpoint}}/crud", + "host": [ + "{{endpoint}}" + ], + "path": [ + "crud" + ] + } + }, + "response": [] + }, + { + "name": "GET item created after update", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 200\", function () {", + " pm.response.to.have.status(200);", + "});", + "pm.test(\"response have correct payload\", function () {", + " var jsonData = pm.response.json();", + " pm.expect(jsonData.hello).to.eql(\"bonjour\");", + " pm.expect(jsonData.ID).to.eql(\"noid\");", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{endpoint}}/crud?id=noid", + "host": [ + "{{endpoint}}" + ], + "path": [ + "crud" + ], + "query": [ + { + "key": "id", + "value": "noid" + } + ] + } + }, + "response": [] + }, + { + "name": "DELETE no id", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 400\", function () {", + " pm.response.to.have.status(400);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "{{endpoint}}/crud", + "host": [ + "{{endpoint}}" + ], + "path": [ + "crud" + ] + } + }, + "response": [] + }, + { + "name": "DELETE inexistant id", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 200\", function () {", + " pm.response.to.have.status(200);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "{{endpoint}}/crud?id=noid", + "host": [ + "{{endpoint}}" + ], + "path": [ + "crud" + ], + "query": [ + { + "key": "id", + "value": "noid" + } + ] + } + }, + "response": [] + }, + { + "name": "DELETE item created after update", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 200\", function () {", + " pm.response.to.have.status(200);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "{{endpoint}}/crud?id=noid", + "host": [ + "{{endpoint}}" + ], + "path": [ + "crud" + ], + "query": [ + { + "key": "id", + "value": "noid" + } + ] + } + }, + "response": [] + }, { "name": "DELETE", "event": [ @@ -207,6 +617,46 @@ } }, "response": [] + }, + { + "name": "GET inexistant id", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 200\", function () {", + " pm.response.to.have.status(200);", + "});", + "", + "pm.test(\"response don't have payload\", function () {", + " pm.response.to.not.have.body", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{endpoint}}/crud?id=noid", + "host": [ + "{{endpoint}}" + ], + "path": [ + "crud" + ], + "query": [ + { + "key": "id", + "value": "noid" + } + ] + } + }, + "response": [] } ] } \ No newline at end of file diff --git a/deploy/lambda/src/create-one.js b/deploy/lambda/src/create-one.js index 13a0f6ced4ca9970139d3707225f7c17e2f3b448..ce33e38bc726522e16ee79831029dd215f825b0e 100644 --- a/deploy/lambda/src/create-one.js +++ b/deploy/lambda/src/create-one.js @@ -7,6 +7,7 @@ const RESERVED_RESPONSE = `Error: You're using AWS reserved keywords as attribut exports.handler = async (event = {}) => { console.log(event) if (!event.body) { + console.error("400 invalid request, you are missing the parameter body") return { statusCode: 400, body: 'invalid request, you are missing the parameter body' }; } const item = typeof event.body == 'object' ? event.body : JSON.parse(event.body); @@ -22,6 +23,7 @@ exports.handler = async (event = {}) => { catch (dbError) { const errorResponse = dbError.code === 'ValidationException' && dbError.message.includes('reserved keyword') ? DYNAMODB_EXECUTION_ERROR : RESERVED_RESPONSE; + console.error("500 " + errorResponse) return { statusCode: 500, body: errorResponse }; } return { statusCode: 201, body: JSON.stringify({ id: item[PRIMARY_KEY] }) }; diff --git a/deploy/lambda/src/delete-one.js b/deploy/lambda/src/delete-one.js index 38d898422a6f5df9bc57deb40e04633c795de88a..1e3e5d89af0999900b96c30233c805b29980312a 100644 --- a/deploy/lambda/src/delete-one.js +++ b/deploy/lambda/src/delete-one.js @@ -3,8 +3,13 @@ const db = new AWS.DynamoDB.DocumentClient(); const TABLE_NAME = process.env.TABLE_NAME || ''; const PRIMARY_KEY = process.env.TABLE_KEY || ''; exports.handler = async (event = {}) => { + if (!event.queryStringParameters) { + console.error("400 invalid request, you are missing the path parameter id") + return { statusCode: 400, body: `Error: You are missing the path parameter id` }; + } const requestedItemId = event.queryStringParameters.id; if (!requestedItemId) { + console.error("400 invalid request, you are missing the path parameter id") return { statusCode: 400, body: `Error: You are missing the path parameter id` }; } const params = { @@ -22,6 +27,7 @@ exports.handler = async (event = {}) => { return { statusCode: 200, body: '' }; } catch (dbError) { + console.error("500 "+JSON.stringify(dbError)) return { statusCode: 500, body: JSON.stringify(dbError) }; } }; \ No newline at end of file diff --git a/deploy/lambda/src/get-one.js b/deploy/lambda/src/get-one.js index c8fa4e487284665ffad67c2436b608ca34dcba7c..151028178370cc702bdd29c5571c8855adb27224 100644 --- a/deploy/lambda/src/get-one.js +++ b/deploy/lambda/src/get-one.js @@ -3,8 +3,13 @@ const db = new AWS.DynamoDB.DocumentClient(); const TABLE_NAME = process.env.TABLE_NAME || ''; const PRIMARY_KEY = process.env.TABLE_KEY || ''; exports.handler = async (event = {}) => { + if (!event.queryStringParameters) { + console.error("400 invalid request, you are missing the path parameter id") + return { statusCode: 400, body: `Error: You are missing the path parameter id` }; + } const requestedItemId = event.queryStringParameters.id; if (!requestedItemId) { + console.error("400 invalid request, you are missing the path parameter id") return { statusCode: 400, body: `Error: You are missing the path parameter id` }; } const params = { @@ -19,9 +24,12 @@ exports.handler = async (event = {}) => { }) try { const response = await db.get(params).promise(); + console.log({response:{ statusCode: 200, body: JSON.stringify(response.Item) } + }) return { statusCode: 200, body: JSON.stringify(response.Item) }; } catch (dbError) { + console.error("500 "+JSON.stringify(dbError)) return { statusCode: 500, body: JSON.stringify(dbError) }; } }; \ No newline at end of file diff --git a/deploy/lambda/src/update-one.js b/deploy/lambda/src/update-one.js index 943c4685a139e3d275ba263a96fc8e3e247d5d9f..3c1cbf26810eaeab193c5e6a98bafa4d9210e0ab 100644 --- a/deploy/lambda/src/update-one.js +++ b/deploy/lambda/src/update-one.js @@ -5,15 +5,22 @@ const PRIMARY_KEY = process.env.TABLE_KEY || ''; const RESERVED_RESPONSE = `Error: You're using AWS reserved keywords as attributes`, DYNAMODB_EXECUTION_ERROR = `Error: Execution update, caused a Dynamodb error, please take a look at your CloudWatch Logs.`; exports.handler = async (event = {}) => { if (!event.body) { + console.error("400 invalid request, you are missing the parameter body") return { statusCode: 400, body: 'invalid request, you are missing the parameter body' }; } + if (!event.queryStringParameters) { + console.error("400 invalid request, you are missing the path parameter id") + return { statusCode: 400, body: `Error: You are missing the path parameter id` }; + } const editedItemId = event.queryStringParameters.id; if (!editedItemId) { + console.error("400 invalid request, you are missing the path parameter id") return { statusCode: 400, body: 'invalid request, you are missing the path parameter id' }; } const editedItem = typeof event.body == 'object' ? event.body : JSON.parse(event.body); const editedItemProperties = Object.keys(editedItem); if (!editedItem || editedItemProperties.length < 1) { + console.error("400 invalid request, you are missing the parameter body") return { statusCode: 400, body: 'invalid request, no arguments provided' }; } const firstProperty = editedItemProperties.splice(0, 1); @@ -42,6 +49,7 @@ exports.handler = async (event = {}) => { catch (dbError) { const errorResponse = dbError.code === 'ValidationException' && dbError.message.includes('reserved keyword') ? DYNAMODB_EXECUTION_ERROR : RESERVED_RESPONSE; + console.error("500 "+errorResponse) return { statusCode: 500, body: errorResponse }; } }; \ No newline at end of file