fix searchGroups test

This commit is contained in:
Stamsy 2025-01-11 01:46:01 +02:00
parent 874f0b2ac6
commit e397867749

View file

@ -1,50 +1,57 @@
import type { ILoadOptionsFunctions } from 'n8n-workflow'; import type { ILoadOptionsFunctions } from 'n8n-workflow';
import { searchGroups, awsRequest } from '../GenericFunctions'; import { searchGroups } from '../GenericFunctions';
jest.mock('../GenericFunctions', () => ({ describe('GenericFunctions - searchGroups', () => {
awsRequest: jest.fn(), const mockRequestWithAuthentication = jest.fn();
}));
describe('searchGroups', () => { const mockContext = {
let mockFunctions: ILoadOptionsFunctions; helpers: {
requestWithAuthentication: mockRequestWithAuthentication,
},
getNodeParameter: jest.fn(),
getCredentials: jest.fn(),
} as unknown as ILoadOptionsFunctions;
beforeEach(() => { beforeEach(() => {
mockFunctions = { jest.clearAllMocks();
getNodeParameter: jest.fn(),
} as unknown as ILoadOptionsFunctions;
(awsRequest as jest.Mock).mockReset();
}); });
test('should throw an error if User Pool ID is missing', async () => { it('should throw an error if User Pool ID is missing', async () => {
(mockFunctions.getNodeParameter as jest.Mock).mockReturnValueOnce(undefined); (mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce({});
await expect(searchGroups.call(mockFunctions)).rejects.toThrow( await expect(searchGroups.call(mockContext)).rejects.toThrow(
'User Pool ID is required to search groups', 'User Pool ID is required to search groups',
); );
}); });
test('should make a POST request to search groups and return results', async () => { it('should make a POST request to search groups and return results', async () => {
(mockFunctions.getNodeParameter as jest.Mock).mockReturnValueOnce({ value: 'mockUserPoolId' }); (mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce({ value: 'mockUserPoolId' });
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({ region: 'us-east-1' });
(awsRequest as jest.Mock).mockResolvedValueOnce({ mockRequestWithAuthentication.mockResolvedValueOnce({
Groups: [{ GroupName: 'Admin' }, { GroupName: 'User' }], Groups: [{ GroupName: 'Admin' }, { GroupName: 'User' }],
NextToken: 'nextTokenValue', NextToken: 'nextTokenValue',
}); });
const response = await searchGroups.call(mockFunctions); const response = await searchGroups.call(mockContext);
expect(awsRequest).toHaveBeenCalledWith({ expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
url: '', 'aws',
method: 'POST', expect.objectContaining({
headers: { 'X-Amz-Target': 'AWSCognitoIdentityProviderService.ListGroups' }, baseURL: 'https://cognito-idp.us-east-1.amazonaws.com',
body: JSON.stringify({ method: 'POST',
UserPoolId: 'mockUserPoolId', headers: {
MaxResults: 60, 'Content-Type': 'application/x-amz-json-1.1',
NextToken: undefined, 'X-Amz-Target': 'AWSCognitoIdentityProviderService.ListGroups',
},
body: JSON.stringify({
UserPoolId: 'mockUserPoolId',
MaxResults: 60,
NextToken: undefined,
}),
}), }),
}); );
expect(response).toEqual({ expect(response).toEqual({
results: [ results: [
@ -55,42 +62,51 @@ describe('searchGroups', () => {
}); });
}); });
test('should handle pagination and return all results', async () => { it('should handle pagination and return all results', async () => {
(mockFunctions.getNodeParameter as jest.Mock).mockReturnValueOnce({ value: 'mockUserPoolId' }); (mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce({ value: 'mockUserPoolId' });
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({ region: 'us-east-1' });
(awsRequest as jest.Mock) mockRequestWithAuthentication.mockResolvedValueOnce({
.mockResolvedValueOnce({ Groups: [{ GroupName: 'Admin' }, { GroupName: 'User' }],
Groups: [{ GroupName: 'Admin' }, { GroupName: 'User' }], NextToken: undefined,
NextToken: 'nextTokenValue', });
})
.mockResolvedValueOnce({
Groups: [{ GroupName: 'Manager' }],
NextToken: undefined,
});
const response = await searchGroups.call(mockFunctions, '', 'prevTokenValue'); const response = await searchGroups.call(mockContext, '', 'prevTokenValue');
expect(mockRequestWithAuthentication).toHaveBeenCalledTimes(1);
expect(mockRequestWithAuthentication).toHaveBeenNthCalledWith(
1,
'aws',
expect.objectContaining({
body: JSON.stringify({
UserPoolId: 'mockUserPoolId',
MaxResults: 60,
NextToken: 'prevTokenValue',
}),
}),
);
expect(awsRequest).toHaveBeenCalledTimes(2);
expect(response).toEqual({ expect(response).toEqual({
results: [ results: [
{ name: 'Admin', value: 'Admin' }, { name: 'Admin', value: 'Admin' },
{ name: 'User', value: 'User' }, { name: 'User', value: 'User' },
{ name: 'Manager', value: 'Manager' },
], ],
paginationToken: undefined, paginationToken: undefined,
}); });
}); });
test('should return empty results if no groups are found', async () => { it('should handle empty results', async () => {
(mockFunctions.getNodeParameter as jest.Mock).mockReturnValueOnce({ value: 'mockUserPoolId' }); (mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce({ value: 'mockUserPoolId' });
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({ region: 'us-east-1' });
(awsRequest as jest.Mock).mockResolvedValueOnce({ mockRequestWithAuthentication.mockResolvedValueOnce({
Groups: [], Groups: [],
NextToken: undefined, NextToken: undefined,
}); });
const response = await searchGroups.call(mockFunctions); const response = await searchGroups.call(mockContext);
expect(response).toEqual({ results: [] }); expect(response).toEqual({ results: [], paginationToken: undefined });
}); });
}); });