first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
import type { MockProxy } from 'jest-mock-extended';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { type IExecuteFunctions } from 'n8n-workflow';
|
||||
|
||||
import * as googleHelpers from '../../GenericFunctions';
|
||||
import { googleApiRequest } from '../GenericFunctions';
|
||||
|
||||
jest.mock('../../GenericFunctions', () => ({
|
||||
...jest.requireActual('../../GenericFunctions'),
|
||||
getGoogleAccessToken: jest.fn().mockResolvedValue({ access_token: 'mock-access-token' }),
|
||||
}));
|
||||
|
||||
describe('Test GoogleChat, googleApiRequest', () => {
|
||||
let mockExecuteFunctions: MockProxy<IExecuteFunctions>;
|
||||
|
||||
beforeEach(() => {
|
||||
mockExecuteFunctions = mock<IExecuteFunctions>();
|
||||
|
||||
mockExecuteFunctions.helpers = {
|
||||
requestWithAuthentication: jest.fn().mockResolvedValue({}),
|
||||
request: jest.fn().mockResolvedValue({}),
|
||||
} as any;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should call requestWithAuthentication when authentication set to OAuth2', async () => {
|
||||
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('oAuth2'); // authentication
|
||||
|
||||
const result = await googleApiRequest.call(mockExecuteFunctions, 'POST', '/test-resource', {
|
||||
text: 'test',
|
||||
});
|
||||
|
||||
expect(result).toEqual({ success: true });
|
||||
|
||||
expect(mockExecuteFunctions.helpers.requestWithAuthentication).toHaveBeenCalledTimes(1);
|
||||
expect(mockExecuteFunctions.helpers.requestWithAuthentication).toHaveBeenCalledWith(
|
||||
'googleChatOAuth2Api',
|
||||
{
|
||||
body: { text: 'test' },
|
||||
headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
|
||||
json: true,
|
||||
method: 'POST',
|
||||
qs: {},
|
||||
qsStringifyOptions: { arrayFormat: 'repeat' },
|
||||
uri: 'https://chat.googleapis.com/test-resource',
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('should call request when authentication set to serviceAccount', async () => {
|
||||
const mockCredentials = {
|
||||
email: 'test@example.com',
|
||||
privateKey: 'private-key',
|
||||
};
|
||||
|
||||
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('serviceAccount');
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValueOnce(mockCredentials);
|
||||
|
||||
const result = await googleApiRequest.call(mockExecuteFunctions, 'GET', '/test-resource');
|
||||
|
||||
expect(googleHelpers.getGoogleAccessToken).toHaveBeenCalledWith(mockCredentials, 'chat');
|
||||
expect(mockExecuteFunctions.helpers.request).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
headers: expect.objectContaining({
|
||||
Authorization: 'Bearer mock-access-token',
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(result).toEqual({ success: true });
|
||||
});
|
||||
|
||||
it('should call request when noCredentials equals true', async () => {
|
||||
const result = await googleApiRequest.call(
|
||||
mockExecuteFunctions,
|
||||
'GET',
|
||||
'/test-resource',
|
||||
{},
|
||||
{},
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
|
||||
expect(mockExecuteFunctions.helpers.request).toHaveBeenCalledTimes(1);
|
||||
expect(mockExecuteFunctions.helpers.request).toHaveBeenCalledWith({
|
||||
headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
|
||||
json: true,
|
||||
method: 'GET',
|
||||
qs: {},
|
||||
qsStringifyOptions: { arrayFormat: 'repeat' },
|
||||
uri: 'https://chat.googleapis.com/test-resource',
|
||||
});
|
||||
expect(result).toEqual({ success: true });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user