Made some changes on processAttributes function test

This commit is contained in:
Adina Totorean 2025-01-13 12:22:20 +02:00
parent 27a3ab6969
commit f8b090b9f6
2 changed files with 58 additions and 52 deletions

View file

@ -17,11 +17,9 @@ describe('GenericFunctions - awsRequest', () => {
}); });
test('should make a successful request with correct options', async () => { test('should make a successful request with correct options', async () => {
// Mock credentials and response
mockGetCredentials.mockResolvedValueOnce({ region: 'us-west-2' }); mockGetCredentials.mockResolvedValueOnce({ region: 'us-west-2' });
mockRequestWithAuthentication.mockResolvedValueOnce({ success: true }); mockRequestWithAuthentication.mockResolvedValueOnce({ success: true });
// Define request options with correct type for method
const requestOptions = { const requestOptions = {
method: 'POST' as const, method: 'POST' as const,
url: '/example-endpoint', url: '/example-endpoint',
@ -33,7 +31,6 @@ describe('GenericFunctions - awsRequest', () => {
const result = await awsRequest.call(mockContext, requestOptions); const result = await awsRequest.call(mockContext, requestOptions);
// Validate the request call
expect(mockGetCredentials).toHaveBeenCalledWith('aws'); expect(mockGetCredentials).toHaveBeenCalledWith('aws');
expect(mockRequestWithAuthentication).toHaveBeenCalledWith( expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
'aws', 'aws',
@ -53,7 +50,6 @@ describe('GenericFunctions - awsRequest', () => {
}); });
test('should throw an error if AWS credentials are invalid (403)', async () => { test('should throw an error if AWS credentials are invalid (403)', async () => {
// Mock credentials and response
mockGetCredentials.mockResolvedValueOnce({ region: 'us-west-2' }); mockGetCredentials.mockResolvedValueOnce({ region: 'us-west-2' });
mockRequestWithAuthentication.mockRejectedValueOnce({ mockRequestWithAuthentication.mockRejectedValueOnce({
statusCode: 403, statusCode: 403,
@ -76,7 +72,6 @@ describe('GenericFunctions - awsRequest', () => {
}); });
test('should throw a descriptive error for other AWS errors', async () => { test('should throw a descriptive error for other AWS errors', async () => {
// Mock credentials and response
mockGetCredentials.mockResolvedValueOnce({ region: 'us-east-1' }); mockGetCredentials.mockResolvedValueOnce({ region: 'us-east-1' });
mockRequestWithAuthentication.mockRejectedValueOnce({ mockRequestWithAuthentication.mockRejectedValueOnce({
statusCode: 400, statusCode: 400,
@ -97,7 +92,6 @@ describe('GenericFunctions - awsRequest', () => {
}); });
test('should handle unexpected error structures gracefully', async () => { test('should handle unexpected error structures gracefully', async () => {
// Mock credentials and response
mockGetCredentials.mockResolvedValueOnce({ region: 'us-east-1' }); mockGetCredentials.mockResolvedValueOnce({ region: 'us-east-1' });
mockRequestWithAuthentication.mockRejectedValueOnce({ mockRequestWithAuthentication.mockRejectedValueOnce({
cause: { error: { message: 'Something went wrong' } }, cause: { error: { message: 'Something went wrong' } },
@ -115,7 +109,6 @@ describe('GenericFunctions - awsRequest', () => {
}); });
test('should throw a generic error if no meaningful information is provided', async () => { test('should throw a generic error if no meaningful information is provided', async () => {
// Mock credentials and response
mockGetCredentials.mockResolvedValueOnce({ region: 'us-east-1' }); mockGetCredentials.mockResolvedValueOnce({ region: 'us-east-1' });
mockRequestWithAuthentication.mockRejectedValueOnce({ mockRequestWithAuthentication.mockRejectedValueOnce({
statusCode: 500, statusCode: 500,

View file

@ -1,68 +1,81 @@
import type { IExecuteSingleFunctions, IHttpRequestOptions } from 'n8n-workflow';
import { processAttributes } from '../GenericFunctions'; import { processAttributes } from '../GenericFunctions';
import { ApplicationError } from 'n8n-workflow';
describe('processAttributes', () => { describe('processAttributes', () => {
let mockContext: any; let mockContext: IExecuteSingleFunctions;
let mockGetNodeParameter: jest.Mock;
beforeEach(() => { beforeEach(() => {
mockGetNodeParameter = jest.fn();
mockContext = { mockContext = {
getNodeParameter: mockGetNodeParameter, getNodeParameter: jest.fn(),
}; } as unknown as IExecuteSingleFunctions;
}); });
test('should process attributes correctly and update the request body', async () => { it('should process attributes and append them to the request body', async () => {
mockGetNodeParameter.mockReturnValueOnce([ const initialBody = { key: 'value' };
(mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce([
{ Name: 'email', Value: 'test@example.com' }, { Name: 'email', Value: 'test@example.com' },
{ Name: 'custom:role', Value: 'admin' }, { Name: 'custom:role', Value: 'admin' },
]); ]);
const requestOptions = { const requestOptions: IHttpRequestOptions = {
method: 'POST' as const, body: JSON.stringify(initialBody),
url: '/example-endpoint', url: '',
headers: {
'X-Amz-Target': 'ExampleService.Action',
},
body: JSON.stringify({ UserPoolId: 'mockPoolId' }),
}; };
const updatedRequestOptions = await processAttributes.call(mockContext, requestOptions); const result = await processAttributes.call(mockContext, requestOptions);
const expectedBody = { expect(result.body).toEqual(
UserPoolId: 'mockPoolId', JSON.stringify({
UserAttributes: [ ...initialBody,
{ Name: 'email', Value: 'test@example.com' }, UserAttributes: [
{ Name: 'custom:role', Value: 'admin' }, { Name: 'email', Value: 'test@example.com' },
], { Name: 'custom:role', Value: 'admin' },
}; ],
}),
expect(updatedRequestOptions.body).toBe(JSON.stringify(expectedBody));
});
test('should throw an error if the body cannot be parsed as JSON', async () => {
const requestOptions = {
method: 'POST' as const,
url: '/example-endpoint',
headers: {},
body: 'invalid json body',
};
await expect(processAttributes.call(mockContext, requestOptions)).rejects.toThrow(
new ApplicationError('Invalid JSON body: Unable to parse.'),
); );
}); });
test('should throw an error if the body is not a string or object', async () => { it('should handle an existing object as the request body', async () => {
const requestOptions = { const initialBody = { key: 'value' };
method: 'POST' as const,
url: '/example-endpoint', (mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce([
headers: {}, { Name: 'email', Value: 'user@example.com' },
body: undefined, ]);
const requestOptions: IHttpRequestOptions = {
body: initialBody,
url: '',
}; };
await expect(processAttributes.call(mockContext, requestOptions)).rejects.toThrow( const result = await processAttributes.call(mockContext, requestOptions);
new ApplicationError('Invalid request body: Expected a JSON string or object.'),
expect(result.body).toEqual(
JSON.stringify({
...initialBody,
UserAttributes: [{ Name: 'email', Value: 'user@example.com' }],
}),
);
});
it('should handle an empty attributes array gracefully', async () => {
const initialBody = { key: 'value' };
(mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce([]);
const requestOptions: IHttpRequestOptions = {
body: JSON.stringify(initialBody),
url: '',
};
const result = await processAttributes.call(mockContext, requestOptions);
expect(result.body).toEqual(
JSON.stringify({
...initialBody,
UserAttributes: [],
}),
); );
}); });
}); });