|
| 1 | +import type { IExecuteFunctions } from 'n8n-workflow'; |
| 2 | + |
| 3 | +import { apiRequest } from '../index'; |
| 4 | + |
| 5 | +const mockedExecutionContext = { |
| 6 | + getCredentials: jest.fn(), |
| 7 | + helpers: { |
| 8 | + requestWithAuthentication: jest.fn(), |
| 9 | + }, |
| 10 | +}; |
| 11 | + |
| 12 | +describe('apiRequest', () => { |
| 13 | + beforeEach(() => { |
| 14 | + jest.resetAllMocks(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should call requestWithAuthentication with credentials URL if one is provided', async () => { |
| 18 | + mockedExecutionContext.getCredentials.mockResolvedValue({ |
| 19 | + url: 'http://www.test/url/v1', |
| 20 | + }); |
| 21 | + |
| 22 | + // Act |
| 23 | + await apiRequest.call(mockedExecutionContext as unknown as IExecuteFunctions, 'GET', '/test', { |
| 24 | + headers: { 'Content-Type': 'application/json' }, |
| 25 | + }); |
| 26 | + |
| 27 | + // Assert |
| 28 | + |
| 29 | + expect(mockedExecutionContext.getCredentials).toHaveBeenCalledWith('openAiApi'); |
| 30 | + expect(mockedExecutionContext.helpers.requestWithAuthentication).toHaveBeenCalledWith( |
| 31 | + 'openAiApi', |
| 32 | + { |
| 33 | + headers: { 'Content-Type': 'application/json' }, |
| 34 | + method: 'GET', |
| 35 | + uri: 'http://www.test/url/v1/test', |
| 36 | + json: true, |
| 37 | + }, |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should call requestWithAuthentication with default URL if credentials URL is not provided', async () => { |
| 42 | + mockedExecutionContext.getCredentials.mockResolvedValue({}); |
| 43 | + |
| 44 | + // Act |
| 45 | + await apiRequest.call(mockedExecutionContext as unknown as IExecuteFunctions, 'GET', '/test', { |
| 46 | + headers: { 'Content-Type': 'application/json' }, |
| 47 | + }); |
| 48 | + |
| 49 | + // Assert |
| 50 | + |
| 51 | + expect(mockedExecutionContext.getCredentials).toHaveBeenCalledWith('openAiApi'); |
| 52 | + expect(mockedExecutionContext.helpers.requestWithAuthentication).toHaveBeenCalledWith( |
| 53 | + 'openAiApi', |
| 54 | + { |
| 55 | + headers: { 'Content-Type': 'application/json' }, |
| 56 | + method: 'GET', |
| 57 | + uri: 'https://api.openai.com/v1/test', |
| 58 | + json: true, |
| 59 | + }, |
| 60 | + ); |
| 61 | + }); |
| 62 | +}); |
0 commit comments