mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Made some changes on processAttributes function test
This commit is contained in:
parent
27a3ab6969
commit
f8b090b9f6
|
@ -17,11 +17,9 @@ describe('GenericFunctions - awsRequest', () => {
|
|||
});
|
||||
|
||||
test('should make a successful request with correct options', async () => {
|
||||
// Mock credentials and response
|
||||
mockGetCredentials.mockResolvedValueOnce({ region: 'us-west-2' });
|
||||
mockRequestWithAuthentication.mockResolvedValueOnce({ success: true });
|
||||
|
||||
// Define request options with correct type for method
|
||||
const requestOptions = {
|
||||
method: 'POST' as const,
|
||||
url: '/example-endpoint',
|
||||
|
@ -33,7 +31,6 @@ describe('GenericFunctions - awsRequest', () => {
|
|||
|
||||
const result = await awsRequest.call(mockContext, requestOptions);
|
||||
|
||||
// Validate the request call
|
||||
expect(mockGetCredentials).toHaveBeenCalledWith('aws');
|
||||
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'aws',
|
||||
|
@ -53,7 +50,6 @@ describe('GenericFunctions - awsRequest', () => {
|
|||
});
|
||||
|
||||
test('should throw an error if AWS credentials are invalid (403)', async () => {
|
||||
// Mock credentials and response
|
||||
mockGetCredentials.mockResolvedValueOnce({ region: 'us-west-2' });
|
||||
mockRequestWithAuthentication.mockRejectedValueOnce({
|
||||
statusCode: 403,
|
||||
|
@ -76,7 +72,6 @@ describe('GenericFunctions - awsRequest', () => {
|
|||
});
|
||||
|
||||
test('should throw a descriptive error for other AWS errors', async () => {
|
||||
// Mock credentials and response
|
||||
mockGetCredentials.mockResolvedValueOnce({ region: 'us-east-1' });
|
||||
mockRequestWithAuthentication.mockRejectedValueOnce({
|
||||
statusCode: 400,
|
||||
|
@ -97,7 +92,6 @@ describe('GenericFunctions - awsRequest', () => {
|
|||
});
|
||||
|
||||
test('should handle unexpected error structures gracefully', async () => {
|
||||
// Mock credentials and response
|
||||
mockGetCredentials.mockResolvedValueOnce({ region: 'us-east-1' });
|
||||
mockRequestWithAuthentication.mockRejectedValueOnce({
|
||||
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 () => {
|
||||
// Mock credentials and response
|
||||
mockGetCredentials.mockResolvedValueOnce({ region: 'us-east-1' });
|
||||
mockRequestWithAuthentication.mockRejectedValueOnce({
|
||||
statusCode: 500,
|
||||
|
|
|
@ -1,68 +1,81 @@
|
|||
import type { IExecuteSingleFunctions, IHttpRequestOptions } from 'n8n-workflow';
|
||||
|
||||
import { processAttributes } from '../GenericFunctions';
|
||||
import { ApplicationError } from 'n8n-workflow';
|
||||
|
||||
describe('processAttributes', () => {
|
||||
let mockContext: any;
|
||||
let mockGetNodeParameter: jest.Mock;
|
||||
let mockContext: IExecuteSingleFunctions;
|
||||
|
||||
beforeEach(() => {
|
||||
mockGetNodeParameter = jest.fn();
|
||||
mockContext = {
|
||||
getNodeParameter: mockGetNodeParameter,
|
||||
};
|
||||
getNodeParameter: jest.fn(),
|
||||
} as unknown as IExecuteSingleFunctions;
|
||||
});
|
||||
|
||||
test('should process attributes correctly and update the request body', async () => {
|
||||
mockGetNodeParameter.mockReturnValueOnce([
|
||||
it('should process attributes and append them to the request body', async () => {
|
||||
const initialBody = { key: 'value' };
|
||||
|
||||
(mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce([
|
||||
{ Name: 'email', Value: 'test@example.com' },
|
||||
{ Name: 'custom:role', Value: 'admin' },
|
||||
]);
|
||||
|
||||
const requestOptions = {
|
||||
method: 'POST' as const,
|
||||
url: '/example-endpoint',
|
||||
headers: {
|
||||
'X-Amz-Target': 'ExampleService.Action',
|
||||
},
|
||||
body: JSON.stringify({ UserPoolId: 'mockPoolId' }),
|
||||
const requestOptions: IHttpRequestOptions = {
|
||||
body: JSON.stringify(initialBody),
|
||||
url: '',
|
||||
};
|
||||
|
||||
const updatedRequestOptions = await processAttributes.call(mockContext, requestOptions);
|
||||
const result = await processAttributes.call(mockContext, requestOptions);
|
||||
|
||||
const expectedBody = {
|
||||
UserPoolId: 'mockPoolId',
|
||||
expect(result.body).toEqual(
|
||||
JSON.stringify({
|
||||
...initialBody,
|
||||
UserAttributes: [
|
||||
{ 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 () => {
|
||||
const requestOptions = {
|
||||
method: 'POST' as const,
|
||||
url: '/example-endpoint',
|
||||
headers: {},
|
||||
body: undefined,
|
||||
it('should handle an existing object as the request body', async () => {
|
||||
const initialBody = { key: 'value' };
|
||||
|
||||
(mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce([
|
||||
{ Name: 'email', Value: 'user@example.com' },
|
||||
]);
|
||||
|
||||
const requestOptions: IHttpRequestOptions = {
|
||||
body: initialBody,
|
||||
url: '',
|
||||
};
|
||||
|
||||
await expect(processAttributes.call(mockContext, requestOptions)).rejects.toThrow(
|
||||
new ApplicationError('Invalid request body: Expected a JSON string or object.'),
|
||||
const result = await processAttributes.call(mockContext, requestOptions);
|
||||
|
||||
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: [],
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue