From 2a680d86db342a47819a8ce8bb7797b21e179711 Mon Sep 17 00:00:00 2001 From: Adina Totorean Date: Wed, 8 Jan 2025 16:25:56 +0200 Subject: [PATCH] Fixed tests --- .../nodes/Aws/Cognito/GenericFunctions.ts | 4 + ...est.ts => PresendAdditionalFields.test.ts} | 15 ++-- .../Aws/Cognito/test/PresendFilter.test.ts | 89 ++++++++----------- .../Aws/Cognito/test/PresendPath.test.ts | 15 ++-- 4 files changed, 59 insertions(+), 64 deletions(-) rename packages/nodes-base/nodes/Aws/Cognito/test/{PresendOptions.test.ts => PresendAdditionalFields.test.ts} (68%) diff --git a/packages/nodes-base/nodes/Aws/Cognito/GenericFunctions.ts b/packages/nodes-base/nodes/Aws/Cognito/GenericFunctions.ts index 64a9869952..cb5f1d5e12 100644 --- a/packages/nodes-base/nodes/Aws/Cognito/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Aws/Cognito/GenericFunctions.ts @@ -62,6 +62,10 @@ export async function presendFilters( const filterAttribute = filterToSend?.attribute as string; const filterValue = filterToSend?.value as string; + if (!filterValue) { + throw new NodeOperationError(this.getNode(), 'Please provide Value to use filtering.'); + } + let body: IDataObject; if (typeof requestOptions.body === 'string') { try { diff --git a/packages/nodes-base/nodes/Aws/Cognito/test/PresendOptions.test.ts b/packages/nodes-base/nodes/Aws/Cognito/test/PresendAdditionalFields.test.ts similarity index 68% rename from packages/nodes-base/nodes/Aws/Cognito/test/PresendOptions.test.ts rename to packages/nodes-base/nodes/Aws/Cognito/test/PresendAdditionalFields.test.ts index fb16bcf416..dfa83c01b7 100644 --- a/packages/nodes-base/nodes/Aws/Cognito/test/PresendOptions.test.ts +++ b/packages/nodes-base/nodes/Aws/Cognito/test/PresendAdditionalFields.test.ts @@ -1,7 +1,8 @@ -import { presendOptions } from '../GenericFunctions'; import { NodeOperationError } from 'n8n-workflow'; -describe('presendOptions', () => { +import { presendAdditionalFields } from '../GenericFunctions'; + +describe('presendAdditionalFields', () => { let mockContext: any; beforeEach(() => { @@ -25,7 +26,7 @@ describe('presendOptions', () => { headers: {}, }; - const result = await presendOptions.call(mockContext, requestOptions); + const result = await presendAdditionalFields.call(mockContext, requestOptions); expect(result).toEqual(requestOptions); }); @@ -40,10 +41,10 @@ describe('presendOptions', () => { headers: {}, }; - await expect(presendOptions.call(mockContext, requestOptions)).rejects.toThrow( + await expect(presendAdditionalFields.call(mockContext, requestOptions)).rejects.toThrow( new NodeOperationError( mockContext.getNode(), - 'At least one of the options (Description, Precedence, Path, or RoleArn) must be provided to update the group.', + 'At least one of the additional fields must be provided to update the group.', ), ); }); @@ -58,10 +59,10 @@ describe('presendOptions', () => { headers: {}, }; - await expect(presendOptions.call(mockContext, requestOptions)).rejects.toThrow( + await expect(presendAdditionalFields.call(mockContext, requestOptions)).rejects.toThrow( new NodeOperationError( mockContext.getNode(), - 'At least one of the options (Description, Precedence, Path, or RoleArn) must be provided to update the group.', + 'At least one of the additional fields must be provided to update the group.', ), ); }); diff --git a/packages/nodes-base/nodes/Aws/Cognito/test/PresendFilter.test.ts b/packages/nodes-base/nodes/Aws/Cognito/test/PresendFilter.test.ts index 4ebc281f6d..9dd1afdd24 100644 --- a/packages/nodes-base/nodes/Aws/Cognito/test/PresendFilter.test.ts +++ b/packages/nodes-base/nodes/Aws/Cognito/test/PresendFilter.test.ts @@ -1,7 +1,8 @@ -import { presendFilter } from '../GenericFunctions'; import { NodeOperationError } from 'n8n-workflow'; -describe('presendFilter', () => { +import { presendFilters } from '../GenericFunctions'; + +describe('presendFilters', () => { let mockContext: any; beforeEach(() => { @@ -15,8 +16,8 @@ describe('presendFilter', () => { test('should return request options with applied filter when all parameters are provided', async () => { mockContext.getNodeParameter.mockImplementation((param: string) => { - if (param === 'additionalFields') { - return { filterAttribute: 'name', filterType: 'exactMatch', filterValue: 'John' }; + if (param === 'filters') { + return { filter: { attribute: 'name', value: 'John' } }; } return {}; }); @@ -28,19 +29,19 @@ describe('presendFilter', () => { headers: {}, }; - const result = await presendFilter.call(mockContext, requestOptions); + const result = await presendFilters.call(mockContext, requestOptions); expect(result.body).toBe( JSON.stringify({ - Filter: 'name = "John"', + Filter: '"name"^="John"', }), ); }); test('should throw an error if any filter parameter is missing', async () => { mockContext.getNodeParameter.mockImplementation((param: string) => { - if (param === 'additionalFields') { - return { filterAttribute: 'name', filterType: 'exactMatch' }; + if (param === 'filters') { + return { filter: { attribute: 'name' } }; // Missing value } return {}; }); @@ -52,18 +53,15 @@ describe('presendFilter', () => { headers: {}, }; - await expect(presendFilter.call(mockContext, requestOptions)).rejects.toThrow( - new NodeOperationError( - mockContext.getNode(), - 'Please provide Filter Attribute, Filter Type, and Filter Value to use filtering.', - ), + await expect(presendFilters.call(mockContext, requestOptions)).rejects.toThrow( + new NodeOperationError(mockContext.getNode(), 'Please provide Value to use filtering.'), ); }); test('should parse requestOptions.body if it is a string', async () => { mockContext.getNodeParameter.mockImplementation((param: string) => { - if (param === 'additionalFields') { - return { filterAttribute: 'name', filterType: 'startsWith', filterValue: 'Jo' }; + if (param === 'filters') { + return { filter: { attribute: 'name', value: 'Jo' } }; } return {}; }); @@ -75,24 +73,40 @@ describe('presendFilter', () => { headers: {}, }; - const result = await presendFilter.call(mockContext, requestOptions); + const result = await presendFilters.call(mockContext, requestOptions); expect(result.body).toBe( JSON.stringify({ key: 'value', - Filter: 'name ^= "Jo"', + Filter: '"name"^="Jo"', }), ); }); + test('should throw an error if requestOptions.body is an invalid JSON string', async () => { + mockContext.getNodeParameter.mockImplementation((param: string) => { + if (param === 'filters') { + return { filter: { attribute: 'name', value: 'John' } }; + } + return {}; + }); + + const requestOptions = { + method: 'POST' as const, + url: '/example-endpoint', + body: '{invalidJson}', + headers: {}, + }; + + await expect(presendFilters.call(mockContext, requestOptions)).rejects.toThrow( + new NodeOperationError(mockContext.getNode(), 'Failed to parse requestOptions body'), + ); + }); + test('should not parse requestOptions.body if it is already an object', async () => { mockContext.getNodeParameter.mockImplementation((param: string) => { - if (param === 'additionalFields') { - return { - filterAttribute: 'email', - filterType: 'exactMatch', - filterValue: 'test@example.com', - }; + if (param === 'filters') { + return { filter: { attribute: 'email', value: 'test@example.com' } }; } return {}; }); @@ -104,37 +118,12 @@ describe('presendFilter', () => { headers: {}, }; - const result = await presendFilter.call(mockContext, requestOptions); + const result = await presendFilters.call(mockContext, requestOptions); expect(result.body).toBe( JSON.stringify({ key: 'value', - Filter: 'email = "test@example.com"', - }), - ); - }); - - test('should handle unsupported filterType values by defaulting to the provided filterType', async () => { - mockContext.getNodeParameter.mockImplementation((param: string) => { - if (param === 'additionalFields') { - return { filterAttribute: 'name', filterType: 'unknownType', filterValue: 'John' }; - } - return {}; - }); - - const requestOptions = { - method: 'POST' as const, - url: '/example-endpoint', - body: { key: 'value' }, - headers: {}, - }; - - const result = await presendFilter.call(mockContext, requestOptions); - - expect(result.body).toBe( - JSON.stringify({ - key: 'value', - Filter: 'name unknownType "John"', + Filter: '"email"^="test@example.com"', }), ); }); diff --git a/packages/nodes-base/nodes/Aws/Cognito/test/PresendPath.test.ts b/packages/nodes-base/nodes/Aws/Cognito/test/PresendPath.test.ts index 33c33c22f5..acb4f85b29 100644 --- a/packages/nodes-base/nodes/Aws/Cognito/test/PresendPath.test.ts +++ b/packages/nodes-base/nodes/Aws/Cognito/test/PresendPath.test.ts @@ -1,7 +1,8 @@ -import { presendPath } from '../GenericFunctions'; import { NodeOperationError } from 'n8n-workflow'; -describe('presendPath', () => { +import { presendVerifyPath } from '../GenericFunctions'; + +describe('presendVerifyPath', () => { let mockContext: any; let mockGetNodeParameter: jest.Mock; @@ -23,7 +24,7 @@ describe('presendPath', () => { body: {}, }; - const result = await presendPath.call(mockContext, requestOptions); + const result = await presendVerifyPath.call(mockContext, requestOptions); expect(result).toEqual(requestOptions); }); @@ -38,7 +39,7 @@ describe('presendPath', () => { body: {}, }; - await expect(presendPath.call(mockContext, requestOptions)).rejects.toThrow( + await expect(presendVerifyPath.call(mockContext, requestOptions)).rejects.toThrow( new NodeOperationError(mockContext.getNode(), 'Path must be between 1 and 512 characters.'), ); }); @@ -53,7 +54,7 @@ describe('presendPath', () => { body: {}, }; - await expect(presendPath.call(mockContext, requestOptions)).rejects.toThrow( + await expect(presendVerifyPath.call(mockContext, requestOptions)).rejects.toThrow( new NodeOperationError(mockContext.getNode(), 'Path must be between 1 and 512 characters.'), ); }); @@ -69,7 +70,7 @@ describe('presendPath', () => { body: {}, }; - await expect(presendPath.call(mockContext, requestOptions)).rejects.toThrow( + await expect(presendVerifyPath.call(mockContext, requestOptions)).rejects.toThrow( new NodeOperationError( mockContext.getNode(), 'Path must begin and end with a forward slash and contain valid ASCII characters.', @@ -87,7 +88,7 @@ describe('presendPath', () => { body: {}, }; - await expect(presendPath.call(mockContext, requestOptions)).rejects.toThrow( + await expect(presendVerifyPath.call(mockContext, requestOptions)).rejects.toThrow( new NodeOperationError( mockContext.getNode(), 'Path must begin and end with a forward slash and contain valid ASCII characters.',