From b8a4512abccad592054cb5c39e621c63b23c016a Mon Sep 17 00:00:00 2001 From: agobrech <45268029+agobrech@users.noreply.github.com> Date: Thu, 4 May 2023 14:34:55 +0200 Subject: [PATCH] fix(AWS Rekognition Node): Fix all different action type (#6136) * Fix broken logic with type action * Unit test base * Add unit test using only nock * Fix failing test * remove console log --------- Co-authored-by: Marcus --- .../Aws/Rekognition/AwsRekognition.node.ts | 96 +++-- .../test/AwsRekognition.node.test.ts | 307 ++++++++++++++++ .../test/AwsRekognitionTestWorkflow.json | 344 ++++++++++++++++++ 3 files changed, 698 insertions(+), 49 deletions(-) create mode 100644 packages/nodes-base/nodes/Aws/Rekognition/test/AwsRekognition.node.test.ts create mode 100644 packages/nodes-base/nodes/Aws/Rekognition/test/AwsRekognitionTestWorkflow.json diff --git a/packages/nodes-base/nodes/Aws/Rekognition/AwsRekognition.node.ts b/packages/nodes-base/nodes/Aws/Rekognition/AwsRekognition.node.ts index a5a94fd2026fe..99345befbfc33 100644 --- a/packages/nodes-base/nodes/Aws/Rekognition/AwsRekognition.node.ts +++ b/packages/nodes-base/nodes/Aws/Rekognition/AwsRekognition.node.ts @@ -389,64 +389,62 @@ export class AwsRekognition implements INodeType { if (type === 'detectText') { action = 'RekognitionService.DetectText'; + } + body.Filters = {}; - body.Filters = {}; - - const box = - ((additionalFields.regionsOfInterestUi as IDataObject) - ?.regionsOfInterestValues as IDataObject[]) || []; + const box = + ((additionalFields.regionsOfInterestUi as IDataObject) + ?.regionsOfInterestValues as IDataObject[]) || []; - if (box.length !== 0) { - //@ts-ignore - body.Filters.RegionsOfInterest = box.map((entry: IDataObject) => { - return { BoundingBox: keysTPascalCase(entry) }; - }); - } + if (box.length !== 0) { + //@ts-ignore + body.Filters.RegionsOfInterest = box.map((entry: IDataObject) => { + return { BoundingBox: keysTPascalCase(entry) }; + }); + } - const wordFilter = (additionalFields.wordFilterUi as IDataObject) || {}; - if (Object.keys(wordFilter).length !== 0) { - //@ts-ignore - body.Filters.WordFilter = keysTPascalCase(wordFilter); - } + const wordFilter = (additionalFields.wordFilterUi as IDataObject) || {}; + if (Object.keys(wordFilter).length !== 0) { + //@ts-ignore + body.Filters.WordFilter = keysTPascalCase(wordFilter); + } - const isBinaryData = this.getNodeParameter('binaryData', i); - if (isBinaryData) { - const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i); - const binaryPropertyData = this.helpers.assertBinaryData(i, binaryPropertyName); - Object.assign(body, { - Image: { - Bytes: binaryPropertyData.data, - }, - }); - } else { - const bucket = this.getNodeParameter('bucket', i) as string; - const name = this.getNodeParameter('name', i) as string; + const isBinaryData = this.getNodeParameter('binaryData', i); + if (isBinaryData) { + const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i); + const binaryPropertyData = this.helpers.assertBinaryData(i, binaryPropertyName); + Object.assign(body, { + Image: { + Bytes: binaryPropertyData.data, + }, + }); + } else { + const bucket = this.getNodeParameter('bucket', i) as string; + const name = this.getNodeParameter('name', i) as string; - Object.assign(body, { - Image: { - S3Object: { - Bucket: bucket, - Name: name, - }, + Object.assign(body, { + Image: { + S3Object: { + Bucket: bucket, + Name: name, }, - }); + }, + }); - if (additionalFields.version) { - //@ts-ignore - body.Image.S3Object.Version = additionalFields.version as string; - } + if (additionalFields.version) { + //@ts-ignore + body.Image.S3Object.Version = additionalFields.version as string; } - - responseData = await awsApiRequestREST.call( - this, - 'rekognition', - 'POST', - '', - JSON.stringify(body), - {}, - { 'X-Amz-Target': action, 'Content-Type': 'application/x-amz-json-1.1' }, - ); } + responseData = await awsApiRequestREST.call( + this, + 'rekognition', + 'POST', + '', + JSON.stringify(body), + {}, + { 'X-Amz-Target': action, 'Content-Type': 'application/x-amz-json-1.1' }, + ); } } diff --git a/packages/nodes-base/nodes/Aws/Rekognition/test/AwsRekognition.node.test.ts b/packages/nodes-base/nodes/Aws/Rekognition/test/AwsRekognition.node.test.ts new file mode 100644 index 0000000000000..a0ce422e4400b --- /dev/null +++ b/packages/nodes-base/nodes/Aws/Rekognition/test/AwsRekognition.node.test.ts @@ -0,0 +1,307 @@ +import { getWorkflowFilenames, testWorkflows } from '../../../../test/nodes/Helpers'; + +import nock from 'nock'; + +const responseLabels = [ + { + LabelModelVersion: '3.0', + Labels: [ + { + Aliases: [], + Categories: [ + { + Name: 'Food and Beverage', + }, + ], + Confidence: 99.81004333496094, + Instances: [], + Name: 'Alcohol', + Parents: [ + { + Name: 'Beverage', + }, + ], + }, + { + Aliases: [ + { + Name: 'Drink', + }, + ], + Categories: [ + { + Name: 'Food and Beverage', + }, + ], + Confidence: 99.81004333496094, + Instances: [], + Name: 'Beverage', + Parents: [], + }, + { + Aliases: [], + Categories: [ + { + Name: 'Food and Beverage', + }, + ], + Confidence: 99.81004333496094, + Instances: [], + Name: 'Liquor', + Parents: [ + { + Name: 'Alcohol', + }, + { + Name: 'Beverage', + }, + ], + }, + { + Aliases: [], + Categories: [ + { + Name: 'Food and Beverage', + }, + ], + Confidence: 99.75448608398438, + Instances: [], + Name: 'Red Wine', + Parents: [ + { + Name: 'Alcohol', + }, + { + Name: 'Beverage', + }, + { + Name: 'Liquor', + }, + { + Name: 'Wine', + }, + ], + }, + { + Aliases: [], + Categories: [ + { + Name: 'Food and Beverage', + }, + ], + Confidence: 99.75448608398438, + Instances: [], + Name: 'Wine', + Parents: [ + { + Name: 'Alcohol', + }, + { + Name: 'Beverage', + }, + { + Name: 'Liquor', + }, + ], + }, + { + Aliases: [], + Categories: [ + { + Name: 'Everyday Objects', + }, + ], + Confidence: 99.52116394042969, + Instances: [], + Name: 'Bottle', + Parents: [], + }, + { + Aliases: [], + Categories: [ + { + Name: 'Food and Beverage', + }, + ], + Confidence: 94.69605255126953, + Instances: [], + Name: 'Wine Bottle', + Parents: [ + { + Name: 'Alcohol', + }, + { + Name: 'Beverage', + }, + { + Name: 'Bottle', + }, + { + Name: 'Liquor', + }, + { + Name: 'Wine', + }, + ], + }, + { + Aliases: [], + Categories: [ + { + Name: 'Food and Beverage', + }, + ], + Confidence: 90.0589370727539, + Instances: [], + Name: 'Food', + Parents: [], + }, + { + Aliases: [], + Categories: [ + { + Name: 'Food and Beverage', + }, + ], + Confidence: 90.0589370727539, + Instances: [ + { + BoundingBox: { + Height: 0.9467026591300964, + Left: 0.23295101523399353, + Top: 0.02573961764574051, + Width: 0.5303559899330139, + }, + Confidence: 90.0589370727539, + }, + ], + Name: 'Ketchup', + Parents: [ + { + Name: 'Food', + }, + ], + }, + { + Aliases: [], + Categories: [ + { + Name: 'Food and Beverage', + }, + ], + Confidence: 65.56095123291016, + Instances: [], + Name: 'Beer', + Parents: [ + { + Name: 'Alcohol', + }, + { + Name: 'Beverage', + }, + ], + }, + { + Aliases: [], + Categories: [ + { + Name: 'Text and Documents', + }, + ], + Confidence: 61.83842468261719, + Instances: [], + Name: 'Document', + Parents: [ + { + Name: 'Text', + }, + ], + }, + { + Aliases: [], + Categories: [ + { + Name: 'Text and Documents', + }, + ], + Confidence: 61.83842468261719, + Instances: [], + Name: 'Id Cards', + Parents: [ + { + Name: 'Document', + }, + { + Name: 'Text', + }, + ], + }, + { + Aliases: [], + Categories: [ + { + Name: 'Text and Documents', + }, + ], + Confidence: 61.83842468261719, + Instances: [ + { + BoundingBox: { + Height: 0.5003108382225037, + Left: 0.2603513300418854, + Top: 0.2912488579750061, + Width: 0.4734913110733032, + }, + Confidence: 61.83842468261719, + }, + ], + Name: 'Passport', + Parents: [ + { + Name: 'Document', + }, + { + Name: 'Id Cards', + }, + { + Name: 'Text', + }, + ], + }, + { + Aliases: [], + Categories: [ + { + Name: 'Text and Documents', + }, + ], + Confidence: 61.83842468261719, + Instances: [], + Name: 'Text', + Parents: [], + }, + ], + }, +]; + +describe('Test AWS Rekogntion Node', () => { + describe('Image Labels Recognition', () => { + const workflows = getWorkflowFilenames(__dirname); + const baseUrl = 'https://rekognition.eu-central-1.amazonaws.com'; + let mock: nock.Scope; + + beforeAll(async () => { + nock.disableNetConnect(); + mock = nock(baseUrl); + }); + + beforeEach(async () => { + mock.post('/').reply(200, responseLabels); + }); + + afterAll(() => { + nock.restore(); + }); + testWorkflows(workflows); + }); +}); diff --git a/packages/nodes-base/nodes/Aws/Rekognition/test/AwsRekognitionTestWorkflow.json b/packages/nodes-base/nodes/Aws/Rekognition/test/AwsRekognitionTestWorkflow.json new file mode 100644 index 0000000000000..4f11f27a45554 --- /dev/null +++ b/packages/nodes-base/nodes/Aws/Rekognition/test/AwsRekognitionTestWorkflow.json @@ -0,0 +1,344 @@ +{ + "name": "node-37-aws-rekognition-empty-response-data", + "nodes": [ + { + "parameters": {}, + "id": "6b79e1a7-8d17-470a-9423-d97042060170", + "name": "When clicking \"Execute Workflow\"", + "type": "n8n-nodes-base.manualTrigger", + "typeVersion": 1, + "position": [ + 40, + 280 + ] + }, + { + "parameters": { + "type": "detectText", + "bucket": "test", + "name": "test", + "additionalFields": {} + }, + "id": "86b957c8-fdec-462e-b55d-40dccf7bc253", + "name": "AWS Rekognition", + "type": "n8n-nodes-base.awsRekognition", + "typeVersion": 1, + "position": [ + 500, + 280 + ], + "credentials": { + "aws": { + "id": "114", + "name": "NodeQA" + } + } + } + ], + "pinData": { + "AWS Rekognition": [ + { + "json": { + "LabelModelVersion": "3.0", + "Labels": [ + { + "Aliases": [], + "Categories": [ + { + "Name": "Food and Beverage" + } + ], + "Confidence": 99.81004333496094, + "Instances": [], + "Name": "Alcohol", + "Parents": [ + { + "Name": "Beverage" + } + ] + }, + { + "Aliases": [ + { + "Name": "Drink" + } + ], + "Categories": [ + { + "Name": "Food and Beverage" + } + ], + "Confidence": 99.81004333496094, + "Instances": [], + "Name": "Beverage", + "Parents": [] + }, + { + "Aliases": [], + "Categories": [ + { + "Name": "Food and Beverage" + } + ], + "Confidence": 99.81004333496094, + "Instances": [], + "Name": "Liquor", + "Parents": [ + { + "Name": "Alcohol" + }, + { + "Name": "Beverage" + } + ] + }, + { + "Aliases": [], + "Categories": [ + { + "Name": "Food and Beverage" + } + ], + "Confidence": 99.75448608398438, + "Instances": [], + "Name": "Red Wine", + "Parents": [ + { + "Name": "Alcohol" + }, + { + "Name": "Beverage" + }, + { + "Name": "Liquor" + }, + { + "Name": "Wine" + } + ] + }, + { + "Aliases": [], + "Categories": [ + { + "Name": "Food and Beverage" + } + ], + "Confidence": 99.75448608398438, + "Instances": [], + "Name": "Wine", + "Parents": [ + { + "Name": "Alcohol" + }, + { + "Name": "Beverage" + }, + { + "Name": "Liquor" + } + ] + }, + { + "Aliases": [], + "Categories": [ + { + "Name": "Everyday Objects" + } + ], + "Confidence": 99.52116394042969, + "Instances": [], + "Name": "Bottle", + "Parents": [] + }, + { + "Aliases": [], + "Categories": [ + { + "Name": "Food and Beverage" + } + ], + "Confidence": 94.69605255126953, + "Instances": [], + "Name": "Wine Bottle", + "Parents": [ + { + "Name": "Alcohol" + }, + { + "Name": "Beverage" + }, + { + "Name": "Bottle" + }, + { + "Name": "Liquor" + }, + { + "Name": "Wine" + } + ] + }, + { + "Aliases": [], + "Categories": [ + { + "Name": "Food and Beverage" + } + ], + "Confidence": 90.0589370727539, + "Instances": [], + "Name": "Food", + "Parents": [] + }, + { + "Aliases": [], + "Categories": [ + { + "Name": "Food and Beverage" + } + ], + "Confidence": 90.0589370727539, + "Instances": [ + { + "BoundingBox": { + "Height": 0.9467026591300964, + "Left": 0.23295101523399353, + "Top": 0.02573961764574051, + "Width": 0.5303559899330139 + }, + "Confidence": 90.0589370727539 + } + ], + "Name": "Ketchup", + "Parents": [ + { + "Name": "Food" + } + ] + }, + { + "Aliases": [], + "Categories": [ + { + "Name": "Food and Beverage" + } + ], + "Confidence": 65.56095123291016, + "Instances": [], + "Name": "Beer", + "Parents": [ + { + "Name": "Alcohol" + }, + { + "Name": "Beverage" + } + ] + }, + { + "Aliases": [], + "Categories": [ + { + "Name": "Text and Documents" + } + ], + "Confidence": 61.83842468261719, + "Instances": [], + "Name": "Document", + "Parents": [ + { + "Name": "Text" + } + ] + }, + { + "Aliases": [], + "Categories": [ + { + "Name": "Text and Documents" + } + ], + "Confidence": 61.83842468261719, + "Instances": [], + "Name": "Id Cards", + "Parents": [ + { + "Name": "Document" + }, + { + "Name": "Text" + } + ] + }, + { + "Aliases": [], + "Categories": [ + { + "Name": "Text and Documents" + } + ], + "Confidence": 61.83842468261719, + "Instances": [ + { + "BoundingBox": { + "Height": 0.5003108382225037, + "Left": 0.2603513300418854, + "Top": 0.2912488579750061, + "Width": 0.4734913110733032 + }, + "Confidence": 61.83842468261719 + } + ], + "Name": "Passport", + "Parents": [ + { + "Name": "Document" + }, + { + "Name": "Id Cards" + }, + { + "Name": "Text" + } + ] + }, + { + "Aliases": [], + "Categories": [ + { + "Name": "Text and Documents" + } + ], + "Confidence": 61.83842468261719, + "Instances": [], + "Name": "Text", + "Parents": [] + } + ] + } + } + ] + }, + "connections": { + "When clicking \"Execute Workflow\"": { + "main": [ + [ + { + "node": "AWS Rekognition", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "active": false, + "settings": {}, + "versionId": "26ee97c0-3122-4281-96ac-ad4ded7b4ee5", + "id": "55", + "meta": { + "instanceId": "8e9416f42a954d0a370d988ac3c0f916f44074a6e45189164b1a8559394a7516" + }, + "tags": [] +} \ No newline at end of file