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,18 @@
|
||||
{
|
||||
"node": "n8n-nodes-base.mailgun",
|
||||
"nodeVersion": "1.0",
|
||||
"codexVersion": "1.0",
|
||||
"categories": ["Communication", "Development"],
|
||||
"resources": {
|
||||
"credentialDocumentation": [
|
||||
{
|
||||
"url": "https://docs.n8n.io/integrations/builtin/credentials/mailgun/"
|
||||
}
|
||||
],
|
||||
"primaryDocumentation": [
|
||||
{
|
||||
"url": "https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.mailgun/"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
IDataObject,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
JsonObject,
|
||||
IRequestOptions,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeApiError, NodeConnectionTypes } from 'n8n-workflow';
|
||||
import { prepareBinariesDataList } from '../../utils/binary';
|
||||
|
||||
export class Mailgun implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Mailgun',
|
||||
name: 'mailgun',
|
||||
icon: 'file:mailgun.svg',
|
||||
group: ['output'],
|
||||
version: 1,
|
||||
description: 'Sends an email via Mailgun',
|
||||
defaults: {
|
||||
name: 'Mailgun',
|
||||
},
|
||||
usableAsTool: true,
|
||||
inputs: [NodeConnectionTypes.Main],
|
||||
outputs: [NodeConnectionTypes.Main],
|
||||
credentials: [
|
||||
{
|
||||
name: 'mailgunApi',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'From Email',
|
||||
name: 'fromEmail',
|
||||
type: 'string',
|
||||
default: '',
|
||||
required: true,
|
||||
placeholder: 'Admin <admin@example.com>',
|
||||
description: 'Email address of the sender optional with name',
|
||||
},
|
||||
{
|
||||
displayName: 'To Email',
|
||||
name: 'toEmail',
|
||||
type: 'string',
|
||||
default: '',
|
||||
required: true,
|
||||
placeholder: 'info@example.com',
|
||||
description: 'Email address of the recipient. Multiple ones can be separated by comma.',
|
||||
},
|
||||
{
|
||||
displayName: 'Cc Email',
|
||||
name: 'ccEmail',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: '',
|
||||
description: 'Cc Email address of the recipient. Multiple ones can be separated by comma.',
|
||||
},
|
||||
{
|
||||
displayName: 'Bcc Email',
|
||||
name: 'bccEmail',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: '',
|
||||
description: 'Bcc Email address of the recipient. Multiple ones can be separated by comma.',
|
||||
},
|
||||
{
|
||||
displayName: 'Subject',
|
||||
name: 'subject',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'My subject line',
|
||||
description: 'Subject line of the email',
|
||||
},
|
||||
{
|
||||
displayName: 'Text',
|
||||
name: 'text',
|
||||
type: 'string',
|
||||
typeOptions: {
|
||||
rows: 5,
|
||||
},
|
||||
default: '',
|
||||
description: 'Plain text message of email',
|
||||
},
|
||||
{
|
||||
displayName: 'HTML',
|
||||
name: 'html',
|
||||
type: 'string',
|
||||
typeOptions: {
|
||||
rows: 5,
|
||||
editor: 'htmlEditor',
|
||||
},
|
||||
default: '',
|
||||
description: 'HTML text message of email',
|
||||
},
|
||||
{
|
||||
displayName: 'Attachments',
|
||||
name: 'attachments',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description:
|
||||
'Name of the binary properties which contain data which should be added to email as attachment. Multiple ones can be comma-separated.',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
|
||||
const returnData: INodeExecutionData[] = [];
|
||||
const length = items.length;
|
||||
let item: INodeExecutionData;
|
||||
|
||||
for (let itemIndex = 0; itemIndex < length; itemIndex++) {
|
||||
try {
|
||||
item = items[itemIndex];
|
||||
|
||||
const fromEmail = this.getNodeParameter('fromEmail', itemIndex) as string;
|
||||
const toEmail = this.getNodeParameter('toEmail', itemIndex) as string;
|
||||
const ccEmail = this.getNodeParameter('ccEmail', itemIndex) as string;
|
||||
const bccEmail = this.getNodeParameter('bccEmail', itemIndex) as string;
|
||||
const subject = this.getNodeParameter('subject', itemIndex) as string;
|
||||
const text = this.getNodeParameter('text', itemIndex) as string;
|
||||
const html = this.getNodeParameter('html', itemIndex) as string;
|
||||
const attachmentPropertyString = this.getNodeParameter('attachments', itemIndex) as string;
|
||||
|
||||
const credentials = await this.getCredentials('mailgunApi');
|
||||
|
||||
const formData: IDataObject = {
|
||||
from: fromEmail,
|
||||
to: toEmail,
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
};
|
||||
|
||||
if (ccEmail.length !== 0) {
|
||||
formData.cc = ccEmail;
|
||||
}
|
||||
if (bccEmail.length !== 0) {
|
||||
formData.bcc = bccEmail;
|
||||
}
|
||||
|
||||
if (attachmentPropertyString && item.binary) {
|
||||
const attachments = [];
|
||||
const attachmentProperties = prepareBinariesDataList(attachmentPropertyString);
|
||||
|
||||
for (const propertyName of attachmentProperties) {
|
||||
const binaryData = this.helpers.assertBinaryData(itemIndex, propertyName);
|
||||
const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(
|
||||
itemIndex,
|
||||
propertyName,
|
||||
);
|
||||
attachments.push({
|
||||
value: binaryDataBuffer,
|
||||
options: {
|
||||
filename: binaryData.fileName || 'unknown',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (attachments.length) {
|
||||
formData.attachment = attachments;
|
||||
}
|
||||
}
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
formData,
|
||||
uri: `https://${credentials.apiDomain}/v3/${credentials.emailDomain}/messages`,
|
||||
json: true,
|
||||
} satisfies IRequestOptions;
|
||||
|
||||
let responseData;
|
||||
|
||||
try {
|
||||
responseData = await this.helpers.requestWithAuthentication.call(
|
||||
this,
|
||||
'mailgunApi',
|
||||
options,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new NodeApiError(this.getNode(), error as JsonObject);
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
||||
{ itemData: { item: itemIndex } },
|
||||
);
|
||||
|
||||
returnData.push(...executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
const executionErrorData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray({ error: error.message }),
|
||||
{ itemData: { item: itemIndex } },
|
||||
);
|
||||
returnData.push(...executionErrorData);
|
||||
continue;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
return [returnData];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="#fff" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 66 65"><use xlink:href="#a" x=".5" y=".5"/><symbol id="a" overflow="visible"><path fill="#c02126" fill-rule="nonzero" stroke="none" d="M32.066 19.9c-6.7 0-12.13 5.433-12.13 12.1s5.443 12.1 12.13 12.1 12.13-5.433 12.13-12.1-5.443-12.1-12.13-12.1M13.181 32c0-10.408 8.46-18.852 18.885-18.852s18.884 8.454 18.884 18.86c0 .72-.066 1.375-.13 2.03-.13 1.833 1.18 3.207 3.016 3.207 3.082 0 3.4-3.993 3.4-5.302 0-13.943-11.28-25.2-25.246-25.2S6.754 18.001 6.754 31.944s11.279 25.2 25.246 25.2c7.4 0 14.033-3.207 18.7-8.248l5.18 4.32A31.93 31.93 0 0 1 32 63.886c-17.705.002-32-14.334-32-31.942C0 14.27 14.36 0 32 0c17.705 0 32 14.336 32 31.944 0 7.07-3.4 12.83-10.164 12.83-3.016 0-4.787-1.375-5.836-2.88-3.344 5.368-9.246 8.902-16.066 8.902-10.295.065-18.754-8.38-18.754-18.786zm18.885-5.564c3.082 0 5.574 2.487 5.574 5.498 0 3.077-2.492 5.564-5.574 5.564s-5.574-2.477-5.574-5.554c.066-3 2.492-5.498 5.574-5.498z"/></symbol></svg>
|
||||
|
After Width: | Height: | Size: 1.1 KiB |
@@ -0,0 +1,515 @@
|
||||
import type { IExecuteFunctions, IBinaryData } from 'n8n-workflow';
|
||||
|
||||
import { Mailgun } from '../Mailgun.node';
|
||||
import { prepareBinariesDataList } from '../../../utils/binary';
|
||||
|
||||
describe('Test Mailgun node', () => {
|
||||
let mailgunNode: Mailgun;
|
||||
let mockExecuteFunctions: jest.Mocked<IExecuteFunctions>;
|
||||
let mockRequestWithAuthentication: jest.Mock;
|
||||
let mockReturnJsonArray: jest.Mock;
|
||||
let mockConstructExecutionMetaData: jest.Mock;
|
||||
|
||||
beforeEach(() => {
|
||||
mailgunNode = new Mailgun();
|
||||
mockRequestWithAuthentication = jest.fn();
|
||||
mockReturnJsonArray = jest.fn();
|
||||
mockConstructExecutionMetaData = jest.fn();
|
||||
|
||||
mockExecuteFunctions = {
|
||||
getInputData: jest.fn(),
|
||||
getNode: jest.fn(),
|
||||
getCredentials: jest.fn(),
|
||||
getNodeParameter: jest.fn(),
|
||||
continueOnFail: jest.fn(),
|
||||
helpers: {
|
||||
assertBinaryData: jest.fn(),
|
||||
getBinaryDataBuffer: jest.fn(),
|
||||
constructExecutionMetaData: mockConstructExecutionMetaData,
|
||||
returnJsonArray: mockReturnJsonArray,
|
||||
requestWithAuthentication: mockRequestWithAuthentication,
|
||||
},
|
||||
} as unknown as jest.Mocked<IExecuteFunctions>;
|
||||
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('comma-separated attachment strings', () => {
|
||||
it('should process comma-separated attachment names with spaces', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
|
||||
file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
|
||||
file3: { data: 'data3', mimeType: 'text/plain', fileName: 'file3.txt' } as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ type: 'n8n-nodes-base.mailgun' } as any);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
apiDomain: 'api.mailgun.net',
|
||||
emailDomain: 'example.com',
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce('file1, file2, file3');
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
mockRequestWithAuthentication.mockResolvedValue({ id: 'test-message-id' });
|
||||
mockReturnJsonArray.mockImplementation((data: any) => [{ json: data }]);
|
||||
mockConstructExecutionMetaData.mockImplementation((data: any) => data);
|
||||
|
||||
await mailgunNode.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'mailgunApi',
|
||||
expect.objectContaining({
|
||||
formData: expect.objectContaining({
|
||||
attachment: expect.arrayContaining([
|
||||
expect.objectContaining({ options: { filename: 'file1.txt' } }),
|
||||
expect.objectContaining({ options: { filename: 'file2.txt' } }),
|
||||
expect.objectContaining({ options: { filename: 'file3.txt' } }),
|
||||
]),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should process comma-separated attachment names without spaces', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
|
||||
file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ type: 'n8n-nodes-base.mailgun' } as any);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
apiDomain: 'api.mailgun.net',
|
||||
emailDomain: 'example.com',
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce('file1,file2');
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
mockRequestWithAuthentication.mockResolvedValue({ id: 'test-message-id' });
|
||||
mockReturnJsonArray.mockImplementation((data: any) => [{ json: data }]);
|
||||
mockConstructExecutionMetaData.mockImplementation((data: any) => data);
|
||||
|
||||
await mailgunNode.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'mailgunApi',
|
||||
expect.objectContaining({
|
||||
formData: expect.objectContaining({
|
||||
attachment: expect.arrayContaining([
|
||||
expect.objectContaining({ options: { filename: 'file1.txt' } }),
|
||||
expect.objectContaining({ options: { filename: 'file2.txt' } }),
|
||||
]),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should trim extra spaces from attachment names', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
|
||||
file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ type: 'n8n-nodes-base.mailgun' } as any);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
apiDomain: 'api.mailgun.net',
|
||||
emailDomain: 'example.com',
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce(' file1 , file2 ');
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
mockRequestWithAuthentication.mockResolvedValue({ id: 'test-message-id' });
|
||||
mockReturnJsonArray.mockImplementation((data: any) => [{ json: data }]);
|
||||
mockConstructExecutionMetaData.mockImplementation((data: any) => data);
|
||||
|
||||
await mailgunNode.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'mailgunApi',
|
||||
expect.objectContaining({
|
||||
formData: expect.objectContaining({
|
||||
attachment: expect.arrayContaining([
|
||||
expect.objectContaining({ options: { filename: 'file1.txt' } }),
|
||||
expect.objectContaining({ options: { filename: 'file2.txt' } }),
|
||||
]),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should process single attachment name', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
singleFile: {
|
||||
data: 'data1',
|
||||
mimeType: 'text/plain',
|
||||
fileName: 'single.txt',
|
||||
} as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ type: 'n8n-nodes-base.mailgun' } as any);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
apiDomain: 'api.mailgun.net',
|
||||
emailDomain: 'example.com',
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce('singleFile');
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
mockRequestWithAuthentication.mockResolvedValue({ id: 'test-message-id' });
|
||||
mockReturnJsonArray.mockImplementation((data: any) => [{ json: data }]);
|
||||
mockConstructExecutionMetaData.mockImplementation((data: any) => data);
|
||||
|
||||
await mailgunNode.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'mailgunApi',
|
||||
expect.objectContaining({
|
||||
formData: expect.objectContaining({
|
||||
attachment: [expect.objectContaining({ options: { filename: 'single.txt' } })],
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('prepareBinariesDataList helper function', () => {
|
||||
it('should process string attachment names correctly', () => {
|
||||
const result = prepareBinariesDataList('file1, file2');
|
||||
expect(result).toEqual(['file1', 'file2']);
|
||||
});
|
||||
|
||||
it('should wrap IBinaryData object in array', () => {
|
||||
const input = { data: 'data1', mimeType: 'text/plain', fileName: 'file.txt' };
|
||||
const result = prepareBinariesDataList(input);
|
||||
expect(result).toEqual([input]);
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should return IBinaryData array unchanged', () => {
|
||||
const input = [
|
||||
{ data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' },
|
||||
{ data: 'data2', mimeType: 'image/png', fileName: 'file2.png' },
|
||||
];
|
||||
const result = prepareBinariesDataList(input);
|
||||
expect(result).toEqual(input);
|
||||
expect(result).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should return string array unchanged', () => {
|
||||
const input = ['file1', 'file2', 'file3'];
|
||||
const result = prepareBinariesDataList(input);
|
||||
expect(result).toEqual(['file1', 'file2', 'file3']);
|
||||
});
|
||||
|
||||
it('should process IBinaryData object as attachment', async () => {
|
||||
const binaryDataObject = { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' };
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ type: 'n8n-nodes-base.mailgun' } as any);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
apiDomain: 'api.mailgun.net',
|
||||
emailDomain: 'example.com',
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce(binaryDataObject);
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string | IBinaryData) => {
|
||||
if (typeof propertyName === 'object') {
|
||||
return propertyName;
|
||||
}
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string | IBinaryData) => {
|
||||
const binaryData =
|
||||
typeof propertyName === 'object'
|
||||
? propertyName
|
||||
: items[itemIndex].binary![propertyName];
|
||||
return Buffer.from(binaryData.data);
|
||||
},
|
||||
);
|
||||
|
||||
mockRequestWithAuthentication.mockResolvedValue({ id: 'test-message-id' });
|
||||
mockReturnJsonArray.mockImplementation((data: any) => [{ json: data }]);
|
||||
mockConstructExecutionMetaData.mockImplementation((data: any) => data);
|
||||
|
||||
await mailgunNode.execute.call(mockExecuteFunctions);
|
||||
|
||||
const result = prepareBinariesDataList(binaryDataObject);
|
||||
expect(result).toEqual([binaryDataObject]);
|
||||
|
||||
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'mailgunApi',
|
||||
expect.objectContaining({
|
||||
formData: expect.objectContaining({
|
||||
attachment: [expect.objectContaining({ options: { filename: 'file1.txt' } })],
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should process IBinaryData array as attachments', async () => {
|
||||
const binaryDataArray = [
|
||||
{ data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' },
|
||||
{ data: 'data2', mimeType: 'image/png', fileName: 'file2.png' },
|
||||
];
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ type: 'n8n-nodes-base.mailgun' } as any);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
apiDomain: 'api.mailgun.net',
|
||||
emailDomain: 'example.com',
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce(binaryDataArray);
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string | IBinaryData) => {
|
||||
if (typeof propertyName === 'object') {
|
||||
return propertyName;
|
||||
}
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string | IBinaryData) => {
|
||||
const binaryData =
|
||||
typeof propertyName === 'object'
|
||||
? propertyName
|
||||
: items[itemIndex].binary![propertyName];
|
||||
return Buffer.from(binaryData.data);
|
||||
},
|
||||
);
|
||||
|
||||
mockRequestWithAuthentication.mockResolvedValue({ id: 'test-message-id' });
|
||||
mockReturnJsonArray.mockImplementation((data: any) => [{ json: data }]);
|
||||
mockConstructExecutionMetaData.mockImplementation((data: any) => data);
|
||||
|
||||
await mailgunNode.execute.call(mockExecuteFunctions);
|
||||
|
||||
const result = prepareBinariesDataList(binaryDataArray);
|
||||
expect(result).toEqual(binaryDataArray);
|
||||
expect(result).toHaveLength(2);
|
||||
|
||||
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'mailgunApi',
|
||||
expect.objectContaining({
|
||||
formData: expect.objectContaining({
|
||||
attachment: expect.arrayContaining([
|
||||
expect.objectContaining({ options: { filename: 'file1.txt' } }),
|
||||
expect.objectContaining({ options: { filename: 'file2.png' } }),
|
||||
]),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('emails without attachments', () => {
|
||||
it('should send email when no attachments specified', async () => {
|
||||
const items = [{ json: { data: 'test' } }];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ type: 'n8n-nodes-base.mailgun' } as any);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
apiDomain: 'api.mailgun.net',
|
||||
emailDomain: 'example.com',
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce('');
|
||||
|
||||
mockRequestWithAuthentication.mockResolvedValue({ id: 'test-message-id' });
|
||||
mockReturnJsonArray.mockImplementation((data: any) => [{ json: data }]);
|
||||
mockConstructExecutionMetaData.mockImplementation((data: any) => data);
|
||||
|
||||
await mailgunNode.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'mailgunApi',
|
||||
expect.objectContaining({
|
||||
formData: expect.not.objectContaining({
|
||||
attachment: expect.anything(),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should send email when attachments is empty string', async () => {
|
||||
const items = [{ json: { data: 'test' }, binary: {} }];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ type: 'n8n-nodes-base.mailgun' } as any);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
apiDomain: 'api.mailgun.net',
|
||||
emailDomain: 'example.com',
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce('');
|
||||
|
||||
mockRequestWithAuthentication.mockResolvedValue({ id: 'test-message-id' });
|
||||
mockReturnJsonArray.mockImplementation((data: any) => [{ json: data }]);
|
||||
mockConstructExecutionMetaData.mockImplementation((data: any) => data);
|
||||
|
||||
await mailgunNode.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'mailgunApi',
|
||||
expect.objectContaining({
|
||||
formData: expect.not.objectContaining({
|
||||
attachment: expect.anything(),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user