mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-27 13:39:44 -08:00
test(Telegram Node): Add some tests for Telegram (no-changelog) (#11043)
Some checks failed
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Benchmark Docker Image CI / build (push) Has been cancelled
Some checks failed
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Benchmark Docker Image CI / build (push) Has been cancelled
This commit is contained in:
parent
0a8a57e4ec
commit
7ad4badd2d
|
@ -276,7 +276,7 @@ export class TelegramTrigger implements INodeType {
|
|||
) as IDataObject;
|
||||
|
||||
// When the image is sent from the desktop app telegram does not resize the image
|
||||
// So return the only image avaiable
|
||||
// So return the only image available
|
||||
// Basically the Image Size parameter would work just when the images comes from the mobile app
|
||||
if (image === undefined) {
|
||||
image = bodyData[key]!.photo![0];
|
||||
|
|
|
@ -0,0 +1,335 @@
|
|||
import {
|
||||
NodeApiError,
|
||||
type IDataObject,
|
||||
type IExecuteFunctions,
|
||||
type IHookFunctions,
|
||||
type IHttpRequestMethods,
|
||||
type ILoadOptionsFunctions,
|
||||
type IWebhookFunctions,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
addAdditionalFields,
|
||||
apiRequest,
|
||||
getPropertyName,
|
||||
getSecretToken,
|
||||
} from '../GenericFunctions';
|
||||
|
||||
describe('Telegram > GenericFunctions', () => {
|
||||
describe('apiRequest', () => {
|
||||
let mockThis: IHookFunctions & IExecuteFunctions & ILoadOptionsFunctions & IWebhookFunctions;
|
||||
const credentials = { baseUrl: 'https://api.telegram.org', accessToken: 'testToken' };
|
||||
beforeEach(() => {
|
||||
mockThis = {
|
||||
getCredentials: jest.fn(),
|
||||
helpers: {
|
||||
request: jest.fn(),
|
||||
},
|
||||
getNode: jest.fn(),
|
||||
} as unknown as IHookFunctions &
|
||||
IExecuteFunctions &
|
||||
ILoadOptionsFunctions &
|
||||
IWebhookFunctions;
|
||||
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should make a successful API request', async () => {
|
||||
const method: IHttpRequestMethods = 'POST';
|
||||
const endpoint = 'sendMessage';
|
||||
const body: IDataObject = { text: 'Hello, world!' };
|
||||
const query: IDataObject = { chat_id: '12345' };
|
||||
const option: IDataObject = { headers: { 'Custom-Header': 'value' } };
|
||||
|
||||
(mockThis.getCredentials as jest.Mock).mockResolvedValue(credentials);
|
||||
(mockThis.helpers.request as jest.Mock).mockResolvedValue({ success: true });
|
||||
|
||||
const result = await apiRequest.call(mockThis, method, endpoint, body, query, option);
|
||||
|
||||
expect(mockThis.getCredentials).toHaveBeenCalledWith('telegramApi');
|
||||
expect(mockThis.helpers.request).toHaveBeenCalledWith({
|
||||
headers: { 'Custom-Header': 'value' },
|
||||
method: 'POST',
|
||||
uri: 'https://api.telegram.org/bottestToken/sendMessage',
|
||||
body: { text: 'Hello, world!' },
|
||||
qs: { chat_id: '12345' },
|
||||
json: true,
|
||||
});
|
||||
expect(result).toEqual({ success: true });
|
||||
});
|
||||
|
||||
it('should handle an API request with no body and query', async () => {
|
||||
const method: IHttpRequestMethods = 'GET';
|
||||
const endpoint = 'getMe';
|
||||
const body: IDataObject = {};
|
||||
const query: IDataObject = {};
|
||||
|
||||
(mockThis.getCredentials as jest.Mock).mockResolvedValue(credentials);
|
||||
(mockThis.helpers.request as jest.Mock).mockResolvedValue({ success: true });
|
||||
|
||||
const result = await apiRequest.call(mockThis, method, endpoint, body, query);
|
||||
|
||||
expect(mockThis.getCredentials).toHaveBeenCalledWith('telegramApi');
|
||||
expect(mockThis.helpers.request).toHaveBeenCalledWith({
|
||||
headers: {},
|
||||
method: 'GET',
|
||||
uri: 'https://api.telegram.org/bottestToken/getMe',
|
||||
json: true,
|
||||
});
|
||||
expect(result).toEqual({ success: true });
|
||||
});
|
||||
|
||||
it('should handle an API request with no additional options', async () => {
|
||||
const method: IHttpRequestMethods = 'POST';
|
||||
const endpoint = 'sendMessage';
|
||||
const body: IDataObject = { text: 'Hello, world!' };
|
||||
|
||||
(mockThis.getCredentials as jest.Mock).mockResolvedValue(credentials);
|
||||
(mockThis.helpers.request as jest.Mock).mockResolvedValue({ success: true });
|
||||
|
||||
const result = await apiRequest.call(mockThis, method, endpoint, body);
|
||||
|
||||
expect(mockThis.getCredentials).toHaveBeenCalledWith('telegramApi');
|
||||
expect(mockThis.helpers.request).toHaveBeenCalledWith({
|
||||
headers: {},
|
||||
method: 'POST',
|
||||
uri: 'https://api.telegram.org/bottestToken/sendMessage',
|
||||
body: { text: 'Hello, world!' },
|
||||
json: true,
|
||||
});
|
||||
expect(result).toEqual({ success: true });
|
||||
});
|
||||
|
||||
it('should throw a NodeApiError on request failure', async () => {
|
||||
const method: IHttpRequestMethods = 'POST';
|
||||
const endpoint = 'sendMessage';
|
||||
const body: IDataObject = { text: 'Hello, world!' };
|
||||
|
||||
(mockThis.getCredentials as jest.Mock).mockResolvedValue(credentials);
|
||||
(mockThis.helpers.request as jest.Mock).mockRejectedValue(new Error('Request failed'));
|
||||
|
||||
await expect(apiRequest.call(mockThis, method, endpoint, body)).rejects.toThrow(NodeApiError);
|
||||
|
||||
expect(mockThis.getCredentials).toHaveBeenCalledWith('telegramApi');
|
||||
expect(mockThis.helpers.request).toHaveBeenCalledWith({
|
||||
headers: {},
|
||||
method: 'POST',
|
||||
uri: 'https://api.telegram.org/bottestToken/sendMessage',
|
||||
body: { text: 'Hello, world!' },
|
||||
json: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('addAdditionalFields', () => {
|
||||
let mockThis: IExecuteFunctions;
|
||||
|
||||
beforeEach(() => {
|
||||
mockThis = {
|
||||
getNodeParameter: jest.fn(),
|
||||
} as unknown as IExecuteFunctions;
|
||||
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should add additional fields and attribution for sendMessage operation', () => {
|
||||
const body: IDataObject = { text: 'Hello, world!' };
|
||||
const index = 0;
|
||||
const nodeVersion = 1.1;
|
||||
const instanceId = '45';
|
||||
|
||||
(mockThis.getNodeParameter as jest.Mock).mockImplementation((paramName: string) => {
|
||||
switch (paramName) {
|
||||
case 'operation':
|
||||
return 'sendMessage';
|
||||
case 'additionalFields':
|
||||
return { appendAttribution: true };
|
||||
case 'replyMarkup':
|
||||
return 'none';
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
});
|
||||
|
||||
addAdditionalFields.call(mockThis, body, index, nodeVersion, instanceId);
|
||||
|
||||
expect(body).toEqual({
|
||||
text: 'Hello, world!\n\n_This message was sent automatically with _[n8n](https://n8n.io/?utm_source=n8n-internal&utm_medium=powered_by&utm_campaign=n8n-nodes-base.telegram_45)',
|
||||
parse_mode: 'Markdown',
|
||||
disable_web_page_preview: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('should add reply markup for inlineKeyboard', () => {
|
||||
const body: IDataObject = { text: 'Hello, world!' };
|
||||
const index = 0;
|
||||
|
||||
(mockThis.getNodeParameter as jest.Mock).mockImplementation((paramName: string) => {
|
||||
switch (paramName) {
|
||||
case 'operation':
|
||||
return 'sendMessage';
|
||||
case 'additionalFields':
|
||||
return {};
|
||||
case 'replyMarkup':
|
||||
return 'inlineKeyboard';
|
||||
case 'inlineKeyboard':
|
||||
return {
|
||||
rows: [
|
||||
{
|
||||
row: {
|
||||
buttons: [
|
||||
{ text: 'Button 1', additionalFields: { url: 'https://example.com' } },
|
||||
{ text: 'Button 2' },
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
});
|
||||
|
||||
addAdditionalFields.call(mockThis, body, index);
|
||||
|
||||
expect(body).toEqual({
|
||||
text: 'Hello, world!',
|
||||
disable_web_page_preview: true,
|
||||
parse_mode: 'Markdown',
|
||||
reply_markup: {
|
||||
inline_keyboard: [
|
||||
[{ text: 'Button 1', url: 'https://example.com' }, { text: 'Button 2' }],
|
||||
],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should add reply markup for forceReply', () => {
|
||||
const body: IDataObject = { text: 'Hello, world!' };
|
||||
const index = 0;
|
||||
|
||||
(mockThis.getNodeParameter as jest.Mock).mockImplementation((paramName: string) => {
|
||||
switch (paramName) {
|
||||
case 'operation':
|
||||
return 'sendMessage';
|
||||
case 'additionalFields':
|
||||
return {};
|
||||
case 'replyMarkup':
|
||||
return 'forceReply';
|
||||
case 'forceReply':
|
||||
return { force_reply: true };
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
});
|
||||
|
||||
addAdditionalFields.call(mockThis, body, index);
|
||||
|
||||
expect(body).toEqual({
|
||||
text: 'Hello, world!',
|
||||
disable_web_page_preview: true,
|
||||
parse_mode: 'Markdown',
|
||||
reply_markup: { force_reply: true },
|
||||
});
|
||||
});
|
||||
|
||||
it('should add reply markup for replyKeyboardRemove', () => {
|
||||
const body: IDataObject = { text: 'Hello, world!' };
|
||||
const index = 0;
|
||||
|
||||
(mockThis.getNodeParameter as jest.Mock).mockImplementation((paramName: string) => {
|
||||
switch (paramName) {
|
||||
case 'operation':
|
||||
return 'sendMessage';
|
||||
case 'additionalFields':
|
||||
return {};
|
||||
case 'replyMarkup':
|
||||
return 'replyKeyboardRemove';
|
||||
case 'replyKeyboardRemove':
|
||||
return { remove_keyboard: true };
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
});
|
||||
|
||||
addAdditionalFields.call(mockThis, body, index);
|
||||
|
||||
expect(body).toEqual({
|
||||
text: 'Hello, world!',
|
||||
disable_web_page_preview: true,
|
||||
parse_mode: 'Markdown',
|
||||
reply_markup: { remove_keyboard: true },
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle nodeVersion 1.2 and set disable_web_page_preview', () => {
|
||||
const body: IDataObject = { text: 'Hello, world!' };
|
||||
const index = 0;
|
||||
const nodeVersion = 1.2;
|
||||
|
||||
(mockThis.getNodeParameter as jest.Mock).mockImplementation((paramName: string) => {
|
||||
switch (paramName) {
|
||||
case 'operation':
|
||||
return 'sendMessage';
|
||||
case 'additionalFields':
|
||||
return {};
|
||||
case 'replyMarkup':
|
||||
return 'none';
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
});
|
||||
|
||||
addAdditionalFields.call(mockThis, body, index, nodeVersion);
|
||||
|
||||
expect(body).toEqual({
|
||||
disable_web_page_preview: true,
|
||||
parse_mode: 'Markdown',
|
||||
text: 'Hello, world!\n\n_This message was sent automatically with _[n8n](https://n8n.io/?utm_source=n8n-internal&utm_medium=powered_by&utm_campaign=n8n-nodes-base.telegram)',
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('getPropertyName', () => {
|
||||
it('should return the property name by removing "send" and converting to lowercase', () => {
|
||||
expect(getPropertyName('sendMessage')).toBe('message');
|
||||
expect(getPropertyName('sendEmail')).toBe('email');
|
||||
expect(getPropertyName('sendNotification')).toBe('notification');
|
||||
});
|
||||
|
||||
it('should return the original string in lowercase if it does not contain "send"', () => {
|
||||
expect(getPropertyName('receiveMessage')).toBe('receivemessage');
|
||||
expect(getPropertyName('fetchData')).toBe('fetchdata');
|
||||
});
|
||||
|
||||
it('should return an empty string if the input is "send"', () => {
|
||||
expect(getPropertyName('send')).toBe('');
|
||||
});
|
||||
|
||||
it('should handle empty strings', () => {
|
||||
expect(getPropertyName('')).toBe('');
|
||||
});
|
||||
});
|
||||
describe('getSecretToken', () => {
|
||||
const mockThis = {
|
||||
getWorkflow: jest.fn().mockReturnValue({ id: 'workflow123' }),
|
||||
getNode: jest.fn().mockReturnValue({ id: 'node123' }),
|
||||
} as unknown as IHookFunctions & IWebhookFunctions;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should return a valid secret token', () => {
|
||||
const secretToken = getSecretToken.call(mockThis);
|
||||
|
||||
expect(secretToken).toBe('workflow123_node123');
|
||||
});
|
||||
|
||||
it('should remove invalid characters from the secret token', () => {
|
||||
mockThis.getNode().id = 'node@123';
|
||||
mockThis.getWorkflow().id = 'workflow#123';
|
||||
|
||||
const secretToken = getSecretToken.call(mockThis);
|
||||
expect(secretToken).toBe('workflow123_node123');
|
||||
});
|
||||
});
|
||||
});
|
37
packages/nodes-base/nodes/Telegram/tests/Helpers.ts
Normal file
37
packages/nodes-base/nodes/Telegram/tests/Helpers.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
import { get } from 'lodash';
|
||||
import type { IDataObject, IExecuteFunctions, IGetNodeParameterOptions, INode } from 'n8n-workflow';
|
||||
|
||||
export const telegramNode: INode = {
|
||||
id: 'b3039263-29ad-4476-9894-51dfcc5a706d',
|
||||
name: 'Telegram node',
|
||||
typeVersion: 1.2,
|
||||
type: 'n8n-nodes-base.telegram',
|
||||
position: [0, 0],
|
||||
parameters: {
|
||||
resource: 'callback',
|
||||
operation: 'answerQuery',
|
||||
},
|
||||
};
|
||||
|
||||
export const createMockExecuteFunction = (nodeParameters: IDataObject) => {
|
||||
const fakeExecuteFunction = {
|
||||
getInputData() {
|
||||
return [{ json: {} }];
|
||||
},
|
||||
getNodeParameter(
|
||||
parameterName: string,
|
||||
_itemIndex: number,
|
||||
fallbackValue?: IDataObject | undefined,
|
||||
options?: IGetNodeParameterOptions | undefined,
|
||||
) {
|
||||
const parameter = options?.extractValue ? `${parameterName}.value` : parameterName;
|
||||
return get(nodeParameters, parameter, fallbackValue);
|
||||
},
|
||||
getNode() {
|
||||
return telegramNode;
|
||||
},
|
||||
helpers: {},
|
||||
continueOnFail: () => false,
|
||||
} as unknown as IExecuteFunctions;
|
||||
return fakeExecuteFunction;
|
||||
};
|
|
@ -0,0 +1,399 @@
|
|||
export const getChatResponse = {
|
||||
ok: true,
|
||||
result: {
|
||||
id: 123456789,
|
||||
first_name: 'Nathan',
|
||||
last_name: 'W',
|
||||
username: 'n8n',
|
||||
type: 'private',
|
||||
active_usernames: ['n8n'],
|
||||
bio: 'Automation',
|
||||
has_private_forwards: true,
|
||||
max_reaction_count: 11,
|
||||
accent_color_id: 3,
|
||||
},
|
||||
};
|
||||
|
||||
export const sendMessageResponse = {
|
||||
ok: true,
|
||||
result: {
|
||||
message_id: 40,
|
||||
from: {
|
||||
id: 9876543210,
|
||||
is_bot: true,
|
||||
first_name: '@n8n',
|
||||
username: 'n8n_test_bot',
|
||||
},
|
||||
chat: {
|
||||
id: 123456789,
|
||||
first_name: 'Nathan',
|
||||
last_name: 'W',
|
||||
username: 'n8n',
|
||||
type: 'private',
|
||||
},
|
||||
date: 1732960606,
|
||||
text: 'a\n\nThis message was sent automatically with n8n',
|
||||
entities: [
|
||||
{
|
||||
offset: 3,
|
||||
length: 41,
|
||||
type: 'italic',
|
||||
},
|
||||
{
|
||||
offset: 44,
|
||||
length: 3,
|
||||
type: 'text_link',
|
||||
url: 'https://n8n.io/?utm_source=n8n-internal&utm_medium=powered_by&utm_campaign=n8n-nodes-base.telegram_8c8c5237b8e37b006a7adce87f4369350c58e41f3ca9de16196d3197f69eabcd',
|
||||
},
|
||||
],
|
||||
link_preview_options: {
|
||||
is_disabled: true,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const sendMediaGroupResponse = {
|
||||
ok: true,
|
||||
result: [
|
||||
{
|
||||
message_id: 41,
|
||||
from: {
|
||||
id: 9876543210,
|
||||
is_bot: true,
|
||||
first_name: '@n8n',
|
||||
username: 'n8n_test_bot',
|
||||
},
|
||||
chat: {
|
||||
id: 123456789,
|
||||
first_name: 'Nathan',
|
||||
last_name: 'W',
|
||||
username: 'n8n',
|
||||
type: 'private',
|
||||
},
|
||||
date: 1732963445,
|
||||
photo: [
|
||||
{
|
||||
file_id:
|
||||
'AgACAgQAAxkDAAMpZ0rsde8lw0E3xttFxGpPdwkExZIAAv21MRvcM11S26tCdFbflv4BAAMCAANzAAM2BA',
|
||||
file_unique_id: 'AQAD_bUxG9wzXVJ4',
|
||||
file_size: 919,
|
||||
width: 90,
|
||||
height: 24,
|
||||
},
|
||||
{
|
||||
file_id:
|
||||
'AgACAgQAAxkDAAMpZ0rsde8lw0E3xttFxGpPdwkExZIAAv21MRvcM11S26tCdFbflv4BAAMCAANtAAM2BA',
|
||||
file_unique_id: 'AQAD_bUxG9wzXVJy',
|
||||
file_size: 6571,
|
||||
width: 320,
|
||||
height: 87,
|
||||
},
|
||||
{
|
||||
file_id:
|
||||
'AgACAgQAAxkDAAMpZ0rsde8lw0E3xttFxGpPdwkExZIAAv21MRvcM11S26tCdFbflv4BAAMCAAN4AAM2BA',
|
||||
file_unique_id: 'AQAD_bUxG9wzXVJ9',
|
||||
file_size: 9639,
|
||||
width: 458,
|
||||
height: 124,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export const sendLocationMessageResponse = {
|
||||
ok: true,
|
||||
result: {
|
||||
message_id: 42,
|
||||
from: {
|
||||
id: 9876543210,
|
||||
is_bot: true,
|
||||
first_name: '@n8n',
|
||||
username: 'n8n_test_bot',
|
||||
},
|
||||
chat: {
|
||||
id: 123456789,
|
||||
first_name: 'Nathan',
|
||||
last_name: 'W',
|
||||
username: 'n8n',
|
||||
type: 'private',
|
||||
},
|
||||
date: 1732963630,
|
||||
reply_to_message: {
|
||||
message_id: 40,
|
||||
from: {
|
||||
id: 9876543210,
|
||||
is_bot: true,
|
||||
first_name: '@n8n',
|
||||
username: 'n8n_test_bot',
|
||||
},
|
||||
chat: {
|
||||
id: 123456789,
|
||||
first_name: 'Nathan',
|
||||
last_name: 'W',
|
||||
username: 'n8n',
|
||||
type: 'private',
|
||||
},
|
||||
date: 1732960606,
|
||||
text: 'a\n\nThis message was sent automatically with n8n',
|
||||
entities: [
|
||||
{
|
||||
offset: 3,
|
||||
length: 41,
|
||||
type: 'italic',
|
||||
},
|
||||
{
|
||||
offset: 44,
|
||||
length: 3,
|
||||
type: 'text_link',
|
||||
url: 'https://n8n.io/?utm_source=n8n-internal&utm_medium=powered_by&utm_campaign=n8n-nodes-base.telegram_8c8c5237b8e37b006a7adce87f4369350c58e41f3ca9de16196d3197f69eabcd',
|
||||
},
|
||||
],
|
||||
link_preview_options: {
|
||||
is_disabled: true,
|
||||
},
|
||||
},
|
||||
location: {
|
||||
latitude: 0.00001,
|
||||
longitude: 0.000003,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const okTrueResponse = {
|
||||
ok: true,
|
||||
result: true,
|
||||
};
|
||||
|
||||
export const sendStickerResponse = {
|
||||
ok: true,
|
||||
result: {
|
||||
message_id: 44,
|
||||
from: {
|
||||
id: 9876543210,
|
||||
is_bot: true,
|
||||
first_name: '@n8n',
|
||||
username: 'n8n_test_bot',
|
||||
},
|
||||
chat: {
|
||||
id: 123456789,
|
||||
first_name: 'Nathan',
|
||||
last_name: 'W',
|
||||
username: 'n8n',
|
||||
type: 'private',
|
||||
},
|
||||
date: 1732965815,
|
||||
document: {
|
||||
file_name: '1_webp_ll.png',
|
||||
mime_type: 'image/png',
|
||||
thumbnail: {
|
||||
file_id: 'AAMCBAADGQMAAyxnSvW31uMAAWa2AAFl0vD1zqc_3xXeAAIbBwACJ95cUvzVqVKE_cXTAQAHbQADNgQ',
|
||||
file_unique_id: 'AQADGwcAAifeXFJy',
|
||||
file_size: 12534,
|
||||
width: 320,
|
||||
height: 241,
|
||||
},
|
||||
thumb: {
|
||||
file_id: 'AAMCBAADGQMAAyxnSvW31uMAAWa2AAFl0vD1zqc_3xXeAAIbBwACJ95cUvzVqVKE_cXTAQAHbQADNgQ',
|
||||
file_unique_id: 'AQADGwcAAifeXFJy',
|
||||
file_size: 12534,
|
||||
width: 320,
|
||||
height: 241,
|
||||
},
|
||||
file_id: 'BQACAgQAAxkDAAMsZ0r1t9bjAAFmtgABZdLw9c6nP98V3gACGwcAAifeXFL81alShP3F0zYE',
|
||||
file_unique_id: 'AgADGwcAAifeXFI',
|
||||
file_size: 122750,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const editMessageTextResponse = {
|
||||
ok: true,
|
||||
result: {
|
||||
message_id: 40,
|
||||
from: {
|
||||
id: 9876543210,
|
||||
is_bot: true,
|
||||
first_name: '@n8n',
|
||||
username: 'n8n_test_bot',
|
||||
},
|
||||
chat: {
|
||||
id: 123456789,
|
||||
first_name: 'Nathan',
|
||||
last_name: 'W',
|
||||
username: 'n8n',
|
||||
type: 'private',
|
||||
},
|
||||
date: 1732960606,
|
||||
edit_date: 1732967008,
|
||||
text: 'test',
|
||||
reply_markup: {
|
||||
inline_keyboard: [
|
||||
[
|
||||
{
|
||||
text: 'foo',
|
||||
callback_data: 'callback',
|
||||
},
|
||||
{
|
||||
text: 'n8n',
|
||||
url: 'https://n8n.io/',
|
||||
},
|
||||
],
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const chatAdministratorsResponse = {
|
||||
ok: true,
|
||||
result: [
|
||||
{
|
||||
user: {
|
||||
id: 9876543210,
|
||||
is_bot: true,
|
||||
first_name: '@n8n',
|
||||
username: 'n8n_test_bot',
|
||||
},
|
||||
status: 'administrator',
|
||||
can_be_edited: false,
|
||||
can_manage_chat: true,
|
||||
can_change_info: true,
|
||||
can_post_messages: true,
|
||||
can_edit_messages: true,
|
||||
can_delete_messages: true,
|
||||
can_invite_users: true,
|
||||
can_restrict_members: true,
|
||||
can_promote_members: false,
|
||||
can_manage_video_chats: true,
|
||||
can_post_stories: true,
|
||||
can_edit_stories: true,
|
||||
can_delete_stories: true,
|
||||
is_anonymous: false,
|
||||
can_manage_voice_chats: true,
|
||||
},
|
||||
{
|
||||
user: {
|
||||
id: 123456789,
|
||||
is_bot: false,
|
||||
first_name: 'Nathan',
|
||||
last_name: 'W',
|
||||
username: 'n8n',
|
||||
language_code: 'en',
|
||||
},
|
||||
status: 'creator',
|
||||
is_anonymous: false,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export const sendAnimationMessageResponse = {
|
||||
ok: true,
|
||||
result: {
|
||||
message_id: 45,
|
||||
from: {
|
||||
id: 9876543210,
|
||||
is_bot: true,
|
||||
first_name: '@n8n',
|
||||
username: 'n8n_test_bot',
|
||||
},
|
||||
chat: {
|
||||
id: 123456789,
|
||||
first_name: 'Nathan',
|
||||
last_name: 'W',
|
||||
username: 'n8n',
|
||||
type: 'private',
|
||||
},
|
||||
date: 1732968868,
|
||||
animation: {
|
||||
file_name: 'Telegram---Opening-Image.gif.mp4',
|
||||
mime_type: 'video/mp4',
|
||||
duration: 6,
|
||||
width: 320,
|
||||
height: 320,
|
||||
thumbnail: {
|
||||
file_id: 'AAMCBAADGQMAAy1nSwGkq99SDYaaS1VR0EMAAUrOw1cAAiYEAALzCVxR7jIYS8d3HycBAAdtAAM2BA',
|
||||
file_unique_id: 'AQADJgQAAvMJXFFy',
|
||||
file_size: 36480,
|
||||
width: 320,
|
||||
height: 320,
|
||||
},
|
||||
thumb: {
|
||||
file_id: 'AAMCBAADGQMAAy1nSwGkq99SDYaaS1VR0EMAAUrOw1cAAiYEAALzCVxR7jIYS8d3HycBAAdtAAM2BA',
|
||||
file_unique_id: 'AQADJgQAAvMJXFFy',
|
||||
file_size: 36480,
|
||||
width: 320,
|
||||
height: 320,
|
||||
},
|
||||
file_id: 'CgACAgQAAxkDAAMtZ0sBpKvfUg2GmktVUdBDAAFKzsNXAAImBAAC8wlcUe4yGEvHdx8nNgQ',
|
||||
file_unique_id: 'AgADJgQAAvMJXFE',
|
||||
file_size: 309245,
|
||||
},
|
||||
document: {
|
||||
file_name: 'Telegram---Opening-Image.gif.mp4',
|
||||
mime_type: 'video/mp4',
|
||||
thumbnail: {
|
||||
file_id: 'AAMCBAADGQMAAy1nSwGkq99SDYaaS1VR0EMAAUrOw1cAAiYEAALzCVxR7jIYS8d3HycBAAdtAAM2BA',
|
||||
file_unique_id: 'AQADJgQAAvMJXFFy',
|
||||
file_size: 36480,
|
||||
width: 320,
|
||||
height: 320,
|
||||
},
|
||||
thumb: {
|
||||
file_id: 'AAMCBAADGQMAAy1nSwGkq99SDYaaS1VR0EMAAUrOw1cAAiYEAALzCVxR7jIYS8d3HycBAAdtAAM2BA',
|
||||
file_unique_id: 'AQADJgQAAvMJXFFy',
|
||||
file_size: 36480,
|
||||
width: 320,
|
||||
height: 320,
|
||||
},
|
||||
file_id: 'CgACAgQAAxkDAAMtZ0sBpKvfUg2GmktVUdBDAAFKzsNXAAImBAAC8wlcUe4yGEvHdx8nNgQ',
|
||||
file_unique_id: 'AgADJgQAAvMJXFE',
|
||||
file_size: 309245,
|
||||
},
|
||||
caption: 'Animation',
|
||||
},
|
||||
};
|
||||
|
||||
export const sendAudioResponse = {
|
||||
ok: true,
|
||||
result: {
|
||||
message_id: 46,
|
||||
from: {
|
||||
id: 9876543210,
|
||||
is_bot: true,
|
||||
first_name: '@n8n',
|
||||
username: 'n8n_test_bot',
|
||||
},
|
||||
chat: {
|
||||
id: 123456789,
|
||||
first_name: 'Nathan',
|
||||
last_name: 'W',
|
||||
username: 'n8n',
|
||||
type: 'private',
|
||||
},
|
||||
date: 1732969291,
|
||||
audio: {
|
||||
duration: 3,
|
||||
file_name: 'sample-3s.mp3',
|
||||
mime_type: 'audio/mpeg',
|
||||
file_id: 'CQACAgQAAxkDAAMuZ0sDSxCh3hW89NQa-eTpxKioqGAAAjsEAAIBCU1SGtsPA4N9TSo2BA',
|
||||
file_unique_id: 'AgADOwQAAgEJTVI',
|
||||
file_size: 52079,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const getMemberResponse = {
|
||||
ok: true,
|
||||
result: {
|
||||
user: {
|
||||
id: 123456789,
|
||||
is_bot: false,
|
||||
first_name: 'Nathan',
|
||||
last_name: 'W',
|
||||
username: 'n8n',
|
||||
language_code: 'en',
|
||||
},
|
||||
status: 'creator',
|
||||
is_anonymous: false,
|
||||
},
|
||||
};
|
1326
packages/nodes-base/nodes/Telegram/tests/Workflow/workflow.json
Normal file
1326
packages/nodes-base/nodes/Telegram/tests/Workflow/workflow.json
Normal file
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,54 @@
|
|||
import nock from 'nock';
|
||||
import { FAKE_CREDENTIALS_DATA } from '../../../../test/nodes/FakeCredentialsMap';
|
||||
|
||||
import { getWorkflowFilenames, testWorkflows } from '../../../../test/nodes/Helpers';
|
||||
|
||||
import {
|
||||
getChatResponse,
|
||||
sendMediaGroupResponse,
|
||||
sendMessageResponse,
|
||||
sendLocationMessageResponse,
|
||||
okTrueResponse,
|
||||
sendStickerResponse,
|
||||
editMessageTextResponse,
|
||||
chatAdministratorsResponse,
|
||||
sendAnimationMessageResponse,
|
||||
sendAudioResponse,
|
||||
getMemberResponse,
|
||||
} from './apiResponses';
|
||||
|
||||
describe('Telegram', () => {
|
||||
describe('Run Telegram workflow', () => {
|
||||
beforeAll(() => {
|
||||
nock.disableNetConnect();
|
||||
|
||||
const { baseUrl } = FAKE_CREDENTIALS_DATA.telegramApi;
|
||||
const mock = nock(baseUrl);
|
||||
|
||||
mock.post('/bottestToken/getChat').reply(200, getChatResponse);
|
||||
mock.post('/bottestToken/sendMessage').reply(200, sendMessageResponse);
|
||||
mock.post('/bottestToken/sendMediaGroup').reply(200, sendMediaGroupResponse);
|
||||
mock.post('/bottestToken/sendLocation').reply(200, sendLocationMessageResponse);
|
||||
mock.post('/bottestToken/deleteMessage').reply(200, okTrueResponse);
|
||||
mock.post('/bottestToken/pinChatMessage').reply(200, okTrueResponse);
|
||||
mock.post('/bottestToken/setChatDescription').reply(200, okTrueResponse);
|
||||
mock.post('/bottestToken/setChatTitle').reply(200, okTrueResponse);
|
||||
mock.post('/bottestToken/unpinChatMessage').reply(200, okTrueResponse);
|
||||
mock.post('/bottestToken/sendChatAction').reply(200, okTrueResponse);
|
||||
mock.post('/bottestToken/leaveChat').reply(200, okTrueResponse);
|
||||
mock.post('/bottestToken/sendSticker').reply(200, sendStickerResponse);
|
||||
mock.post('/bottestToken/editMessageText').reply(200, editMessageTextResponse);
|
||||
mock.post('/bottestToken/getChatAdministrators').reply(200, chatAdministratorsResponse);
|
||||
mock.post('/bottestToken/sendAnimation').reply(200, sendAnimationMessageResponse);
|
||||
mock.post('/bottestToken/sendAudio').reply(200, sendAudioResponse);
|
||||
mock.post('/bottestToken/getChatMember').reply(200, getMemberResponse);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
nock.restore();
|
||||
});
|
||||
|
||||
const workflows = getWorkflowFilenames(__dirname);
|
||||
testWorkflows(workflows);
|
||||
});
|
||||
});
|
|
@ -121,4 +121,8 @@ BQIDAQAB
|
|||
secret: 'baz',
|
||||
algorithm: 'HS256',
|
||||
},
|
||||
telegramApi: {
|
||||
accessToken: 'testToken',
|
||||
baseUrl: 'https://api.telegram.org',
|
||||
},
|
||||
} as const;
|
||||
|
|
Loading…
Reference in a new issue