@panoptic-it-solutions/n8n-nodes-datto-rmm
Version:
n8n node for Datto RMM integration
216 lines • 12.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const n8n_workflow_1 = require("n8n-workflow");
const DattoRmm_node_1 = require("../DattoRmm.node");
jest.mock('../resources/account/execute', () => ({
executeAccountOperation: jest.fn(),
}));
jest.mock('../resources/activity-log/execute', () => ({
executeActivityLogOperation: jest.fn(),
}));
jest.mock('../helpers/resourceMapper', () => ({
getResourceMapperFields: jest.fn(),
}));
const execute_1 = require("../resources/account/execute");
const resourceMapper_1 = require("../helpers/resourceMapper");
describe('DattoRmm Node', () => {
let dattoRmm;
let mockExecuteFunctions;
let mockLoadOptionsFunctions;
beforeEach(() => {
dattoRmm = new DattoRmm_node_1.DattoRmm();
mockExecuteFunctions = {
getNodeParameter: jest.fn(),
getNode: jest.fn(() => ({ name: 'DattoRmm', type: 'dattoRmm' })),
};
mockLoadOptionsFunctions = {
getNodeParameter: jest.fn(),
getNode: jest.fn(() => ({ name: 'DattoRmm', type: 'dattoRmm' })),
};
jest.clearAllMocks();
});
describe('Node Description', () => {
it('should have correct basic properties', () => {
expect(dattoRmm.description.displayName).toBe('Datto RMM');
expect(dattoRmm.description.name).toBe('dattoRmm');
expect(dattoRmm.description.version).toBe(1);
expect(dattoRmm.description.usableAsTool).toBe(true);
});
it('should have proper node configuration', () => {
expect(dattoRmm.description.group).toEqual(['transform']);
expect(dattoRmm.description.inputs).toHaveLength(1);
expect(dattoRmm.description.outputs).toHaveLength(1);
});
it('should require credentials', () => {
expect(dattoRmm.description.credentials).toEqual([
{
name: 'dattoRmmApi',
required: true,
},
]);
});
it('should have proper request defaults', () => {
expect(dattoRmm.description.requestDefaults).toEqual({
baseURL: '={{$credentials.apiUrl}}',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
});
});
});
describe('Properties Configuration', () => {
it('should have resource property as first property', () => {
const resourceProperty = dattoRmm.description.properties[0];
expect(resourceProperty.name).toBe('resource');
expect(resourceProperty.type).toBe('options');
expect(resourceProperty.required).toBe(true);
expect(resourceProperty.default).toBe('account');
});
it('should include account properties', () => {
const properties = dattoRmm.description.properties;
expect(properties.length).toBeGreaterThan(1);
const accountOperationProperty = properties.find((prop) => { var _a, _b, _c; return prop.name === 'operation' && ((_c = (_b = (_a = prop.displayOptions) === null || _a === void 0 ? void 0 : _a.show) === null || _b === void 0 ? void 0 : _b.resource) === null || _c === void 0 ? void 0 : _c.includes('account')); });
expect(accountOperationProperty).toBeDefined();
});
it('should have resourceMapper field for account operations', () => {
var _a, _b;
const properties = dattoRmm.description.properties;
const resourceMapperProperty = properties.find((prop) => prop.name === 'resourceMapper' && prop.type === 'resourceMapper');
expect(resourceMapperProperty).toBeDefined();
expect((_b = (_a = resourceMapperProperty === null || resourceMapperProperty === void 0 ? void 0 : resourceMapperProperty.displayOptions) === null || _a === void 0 ? void 0 : _a.show) === null || _b === void 0 ? void 0 : _b.resource).toContain('account');
});
});
describe('Execute Method - Switch Routing', () => {
it('should call executeAccountOperation for account resource', async () => {
const mockResult = [[]];
execute_1.executeAccountOperation.mockResolvedValue(mockResult);
mockExecuteFunctions.getNodeParameter.mockReturnValue('account');
const result = await dattoRmm.execute.call(mockExecuteFunctions);
expect(mockExecuteFunctions.getNodeParameter).toHaveBeenCalledWith('resource', 0);
expect(execute_1.executeAccountOperation).toHaveBeenCalledWith();
expect(result).toBe(mockResult);
});
it('should throw error for unsupported resource', async () => {
mockExecuteFunctions.getNodeParameter.mockReturnValue('unsupported');
await expect(dattoRmm.execute.call(mockExecuteFunctions)).rejects.toThrow(n8n_workflow_1.NodeOperationError);
expect(mockExecuteFunctions.getNodeParameter).toHaveBeenCalledWith('resource', 0);
expect(execute_1.executeAccountOperation).not.toHaveBeenCalled();
});
it('should propagate errors from resource execute functions', async () => {
const testError = new Error('Test execution error');
execute_1.executeAccountOperation.mockRejectedValue(testError);
mockExecuteFunctions.getNodeParameter.mockReturnValue('account');
await expect(dattoRmm.execute.call(mockExecuteFunctions)).rejects.toThrow('Test execution error');
});
});
describe('Methods Section', () => {
describe('resourceMapping', () => {
it('should have getFields method', () => {
var _a, _b, _c;
expect(dattoRmm.methods).toBeDefined();
expect((_a = dattoRmm.methods) === null || _a === void 0 ? void 0 : _a.resourceMapping).toBeDefined();
expect((_c = (_b = dattoRmm.methods) === null || _b === void 0 ? void 0 : _b.resourceMapping) === null || _c === void 0 ? void 0 : _c.getFields).toBeDefined();
});
it('should call getResourceMapperFields with correct resource', async () => {
var _a, _b, _c;
const mockFields = { fields: [] };
resourceMapper_1.getResourceMapperFields.mockResolvedValue(mockFields);
mockLoadOptionsFunctions.getNodeParameter.mockReturnValue('account');
const result = await ((_c = (_b = (_a = dattoRmm.methods) === null || _a === void 0 ? void 0 : _a.resourceMapping) === null || _b === void 0 ? void 0 : _b.getFields) === null || _c === void 0 ? void 0 : _c.call(mockLoadOptionsFunctions));
expect(mockLoadOptionsFunctions.getNodeParameter).toHaveBeenCalledWith('resource', 0);
expect(resourceMapper_1.getResourceMapperFields).toHaveBeenCalledWith('account');
expect(result).toBe(mockFields);
});
it('should handle errors in getFields', async () => {
var _a, _b, _c;
const testError = new Error('ResourceMapper error');
resourceMapper_1.getResourceMapperFields.mockRejectedValue(testError);
mockLoadOptionsFunctions.getNodeParameter.mockReturnValue('account');
await expect((_c = (_b = (_a = dattoRmm.methods) === null || _a === void 0 ? void 0 : _a.resourceMapping) === null || _b === void 0 ? void 0 : _b.getFields) === null || _c === void 0 ? void 0 : _c.call(mockLoadOptionsFunctions)).rejects.toThrow('ResourceMapper error');
});
});
describe('loadOptions', () => {
it('should have getResources method', () => {
var _a, _b;
expect((_b = (_a = dattoRmm.methods) === null || _a === void 0 ? void 0 : _a.loadOptions) === null || _b === void 0 ? void 0 : _b.getResources).toBeDefined();
});
it('should return available resources', async () => {
var _a, _b, _c;
const result = await ((_c = (_b = (_a = dattoRmm.methods) === null || _a === void 0 ? void 0 : _a.loadOptions) === null || _b === void 0 ? void 0 : _b.getResources) === null || _c === void 0 ? void 0 : _c.call(mockLoadOptionsFunctions));
expect(result).toEqual([
{ name: 'Account', value: 'account' },
{ name: 'Device', value: 'device' },
{ name: 'Site', value: 'site' },
{ name: 'Alert', value: 'alert' },
{ name: 'Job', value: 'job' },
{ name: 'Audit', value: 'audit' },
{ name: 'System', value: 'system' },
{ name: 'Filter', value: 'filter' },
]);
});
it('should have getSelectColumns method', () => {
var _a, _b;
expect((_b = (_a = dattoRmm.methods) === null || _a === void 0 ? void 0 : _a.loadOptions) === null || _b === void 0 ? void 0 : _b.getSelectColumns).toBeDefined();
});
it('should return columns from resourceMapper for getSelectColumns', async () => {
var _a, _b, _c;
const mockFields = {
fields: [
{ id: 'id', displayName: 'ID' },
{ id: 'name', displayName: 'Name' },
{ id: 'email', displayName: 'Email' },
],
};
resourceMapper_1.getResourceMapperFields.mockResolvedValue(mockFields);
mockLoadOptionsFunctions.getNodeParameter.mockReturnValue('account');
const result = await ((_c = (_b = (_a = dattoRmm.methods) === null || _a === void 0 ? void 0 : _a.loadOptions) === null || _b === void 0 ? void 0 : _b.getSelectColumns) === null || _c === void 0 ? void 0 : _c.call(mockLoadOptionsFunctions));
expect(result).toEqual([
{ name: 'ID', value: 'id' },
{ name: 'Name', value: 'name' },
{ name: 'Email', value: 'email' },
]);
});
it('should handle errors in getSelectColumns gracefully', async () => {
var _a, _b, _c;
const testError = new Error('Load options error');
resourceMapper_1.getResourceMapperFields.mockRejectedValue(testError);
mockLoadOptionsFunctions.getNodeParameter.mockReturnValue('account');
const result = await ((_c = (_b = (_a = dattoRmm.methods) === null || _a === void 0 ? void 0 : _a.loadOptions) === null || _b === void 0 ? void 0 : _b.getSelectColumns) === null || _c === void 0 ? void 0 : _c.call(mockLoadOptionsFunctions));
expect(result).toEqual([]);
});
});
});
describe('Integration Tests', () => {
it('should handle complete workflow for account resource', async () => {
const mockExecutionData = [
[{ json: { id: 1, name: 'Test Account' } }],
];
execute_1.executeAccountOperation.mockResolvedValue(mockExecutionData);
mockExecuteFunctions.getNodeParameter.mockReturnValue('account');
const result = await dattoRmm.execute.call(mockExecuteFunctions);
expect(mockExecuteFunctions.getNodeParameter).toHaveBeenCalledWith('resource', 0);
expect(execute_1.executeAccountOperation).toHaveBeenCalled();
expect(result).toEqual(mockExecutionData);
});
it('should integrate resourceMapping with loadOptions', async () => {
var _a, _b, _c, _d, _e, _f;
const mockFields = {
fields: [
{ id: 'id', displayName: 'ID' },
{ id: 'name', displayName: 'Account Name' },
],
};
resourceMapper_1.getResourceMapperFields.mockResolvedValue(mockFields);
mockLoadOptionsFunctions.getNodeParameter.mockReturnValue('account');
const mapperResult = await ((_c = (_b = (_a = dattoRmm.methods) === null || _a === void 0 ? void 0 : _a.resourceMapping) === null || _b === void 0 ? void 0 : _b.getFields) === null || _c === void 0 ? void 0 : _c.call(mockLoadOptionsFunctions));
expect(mapperResult).toBe(mockFields);
const columnsResult = await ((_f = (_e = (_d = dattoRmm.methods) === null || _d === void 0 ? void 0 : _d.loadOptions) === null || _e === void 0 ? void 0 : _e.getSelectColumns) === null || _f === void 0 ? void 0 : _f.call(mockLoadOptionsFunctions));
expect(columnsResult).toEqual([
{ name: 'ID', value: 'id' },
{ name: 'Account Name', value: 'name' },
]);
});
});
});
//# sourceMappingURL=DattoRmm.node.test.js.map