feat: add template and template group management via GraphQL
- Implemented GraphQL endpoints for importing, querying, and deleting Zabbix templates and template groups. - Added support for full template data import, including items, preprocessing steps, tags, and linked templates. - Implemented dependent item support by deferred creation logic in the template importer. - Added ability to query templates and template groups with name pattern filtering (supporting Zabbix wildcards). - Implemented batch deletion for templates and template groups by ID or name pattern. - Improved error reporting by including detailed Zabbix API error data in GraphQL responses. - Added comprehensive unit and integration tests covering all new functionality. - Provided GraphQL sample queries and mutations in the 'docs' directory for all new endpoints.
This commit is contained in:
parent
e641f8e610
commit
a3ed4886a3
22 changed files with 2450 additions and 20 deletions
162
src/test/template_deleter.test.ts
Normal file
162
src/test/template_deleter.test.ts
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
|
||||
import {TemplateDeleter} from "../execution/template_deleter.js";
|
||||
import {zabbixAPI} from "../datasources/zabbix-api.js";
|
||||
|
||||
// Mocking ZabbixAPI
|
||||
jest.mock("../datasources/zabbix-api.js", () => ({
|
||||
zabbixAPI: {
|
||||
executeRequest: jest.fn(),
|
||||
post: jest.fn()
|
||||
}
|
||||
}));
|
||||
|
||||
describe("TemplateDeleter", () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
test("deleteTemplates - success", async () => {
|
||||
const templateids = [1, 2];
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ templateids: ["1", "2"] });
|
||||
|
||||
const result = await TemplateDeleter.deleteTemplates(templateids, null, "token");
|
||||
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0].id).toBe(1);
|
||||
expect(result[0].message).toContain("deleted successfully");
|
||||
expect(result[1].id).toBe(2);
|
||||
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("template.delete", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
params: [1, 2]
|
||||
})
|
||||
}));
|
||||
});
|
||||
|
||||
test("deleteTemplates - error", async () => {
|
||||
const templateids = [1];
|
||||
const zabbixError = {
|
||||
error: {
|
||||
code: -32602,
|
||||
message: "Invalid params.",
|
||||
data: "Template does not exist"
|
||||
}
|
||||
};
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(zabbixError);
|
||||
|
||||
const result = await TemplateDeleter.deleteTemplates(templateids, null, "token");
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].error).toBeDefined();
|
||||
expect(result[0].message).toContain("Invalid params.");
|
||||
expect(result[0].message).toContain("Template does not exist");
|
||||
});
|
||||
|
||||
test("deleteTemplates - by name_pattern", async () => {
|
||||
// Mock template.get
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([
|
||||
{ templateid: "10", host: "PatternTemplate 1" },
|
||||
{ templateid: "11", host: "PatternTemplate 2" }
|
||||
]);
|
||||
// Mock template.delete
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ templateids: ["10", "11"] });
|
||||
|
||||
const result = await TemplateDeleter.deleteTemplates(null, "PatternTemplate%", "token");
|
||||
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result.map(r => r.id)).toContain(10);
|
||||
expect(result.map(r => r.id)).toContain(11);
|
||||
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("template.get", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
params: expect.objectContaining({
|
||||
search: { name: "PatternTemplate%" }
|
||||
})
|
||||
})
|
||||
}));
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("template.delete", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
params: expect.arrayContaining([10, 11])
|
||||
})
|
||||
}));
|
||||
});
|
||||
|
||||
test("deleteTemplates - merged IDs and name_pattern", async () => {
|
||||
// Mock template.get
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([
|
||||
{ templateid: "10", host: "PatternTemplate 1" },
|
||||
{ templateid: "12", host: "PatternTemplate 3" }
|
||||
]);
|
||||
// Mock template.delete
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ templateids: ["10", "11", "12"] });
|
||||
|
||||
const result = await TemplateDeleter.deleteTemplates([11], "PatternTemplate%", "token");
|
||||
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result.map(r => r.id)).toContain(10);
|
||||
expect(result.map(r => r.id)).toContain(11);
|
||||
expect(result.map(r => r.id)).toContain(12);
|
||||
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("template.delete", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
params: expect.arrayContaining([10, 11, 12])
|
||||
})
|
||||
}));
|
||||
});
|
||||
|
||||
test("deleteTemplateGroups - success", async () => {
|
||||
const groupids = [101];
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ groupids: ["101"] });
|
||||
|
||||
const result = await TemplateDeleter.deleteTemplateGroups(groupids, null, "token");
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].id).toBe(101);
|
||||
expect(result[0].message).toContain("deleted successfully");
|
||||
});
|
||||
|
||||
test("deleteTemplateGroups - error", async () => {
|
||||
const groupids = [101];
|
||||
const zabbixError = {
|
||||
error: {
|
||||
code: -32602,
|
||||
message: "Invalid params.",
|
||||
data: "Group is in use"
|
||||
}
|
||||
};
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(zabbixError);
|
||||
|
||||
const result = await TemplateDeleter.deleteTemplateGroups(groupids, null, "token");
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].error).toBeDefined();
|
||||
expect(result[0].message).toContain("Group is in use");
|
||||
});
|
||||
|
||||
test("deleteTemplateGroups - by name_pattern", async () => {
|
||||
// Mock templategroup.get
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([
|
||||
{ groupid: "201", name: "PatternGroup 1" }
|
||||
]);
|
||||
// Mock templategroup.delete
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ groupids: ["201"] });
|
||||
|
||||
const result = await TemplateDeleter.deleteTemplateGroups(null, "PatternGroup%", "token");
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].id).toBe(201);
|
||||
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("templategroup.get", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
params: expect.objectContaining({
|
||||
search: { name: "PatternGroup%" }
|
||||
})
|
||||
})
|
||||
}));
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("templategroup.delete", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
params: [201]
|
||||
})
|
||||
}));
|
||||
});
|
||||
});
|
||||
176
src/test/template_importer.test.ts
Normal file
176
src/test/template_importer.test.ts
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
|
||||
import {TemplateImporter} from "../execution/template_importer.js";
|
||||
import {zabbixAPI} from "../datasources/zabbix-api.js";
|
||||
import {
|
||||
ZabbixCreateItemRequest,
|
||||
ZabbixCreateTemplateGroupRequest,
|
||||
ZabbixCreateTemplateRequest,
|
||||
ZabbixQueryItemRequest,
|
||||
ZabbixQueryTemplateGroupRequest,
|
||||
ZabbixQueryTemplatesRequest
|
||||
} from "../datasources/zabbix-templates.js";
|
||||
import {ZabbixErrorResult} from "../datasources/zabbix-request.js";
|
||||
|
||||
// Mocking ZabbixAPI.executeRequest
|
||||
jest.mock("../datasources/zabbix-api.js", () => ({
|
||||
zabbixAPI: {
|
||||
executeRequest: jest.fn(),
|
||||
post: jest.fn()
|
||||
}
|
||||
}));
|
||||
|
||||
describe("TemplateImporter", () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
test("importTemplateGroups - create new group", async () => {
|
||||
const templateGroups = [{ groupName: "New Group", uuid: "uuid1" }];
|
||||
|
||||
// Mocking group.get to return empty (group doesn't exist)
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([]);
|
||||
// Mocking group.create
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ groupids: ["101"] });
|
||||
|
||||
const result = await TemplateImporter.importTemplateGroups(templateGroups, "token");
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result![0].groupid).toBe(101);
|
||||
expect(result![0].groupName).toBe("New Group");
|
||||
});
|
||||
|
||||
test("importTemplateGroups - group already exists", async () => {
|
||||
const templateGroups = [{ groupName: "Existing Group" }];
|
||||
|
||||
// Mocking group.get to return existing group
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ groupid: "102", name: "Existing Group" }]);
|
||||
|
||||
const result = await TemplateImporter.importTemplateGroups(templateGroups, "token");
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result![0].groupid).toBe(102);
|
||||
expect(result![0].message).toContain("already exists");
|
||||
});
|
||||
|
||||
test("importTemplates - basic template", async () => {
|
||||
const templates = [{
|
||||
host: "Test Template",
|
||||
groupNames: ["Group1"]
|
||||
}];
|
||||
|
||||
// Mocking group.get for Group1
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ groupid: "201", name: "Group1" }]);
|
||||
// Mocking template.create
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ templateids: ["301"] });
|
||||
|
||||
const result = await TemplateImporter.importTemplates(templates, "token");
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result![0].templateid).toBe("301");
|
||||
expect(result![0].host).toBe("Test Template");
|
||||
});
|
||||
|
||||
test("importTemplates - with items, linked templates and dependent items", async () => {
|
||||
const templates = [{
|
||||
host: "Complex Template",
|
||||
groupNames: ["Group1"],
|
||||
templates: [{ name: "Linked Template" }],
|
||||
items: [
|
||||
{
|
||||
name: "Dependent Item",
|
||||
key: "dependent.key",
|
||||
type: 18,
|
||||
value_type: 3,
|
||||
master_item: { key: "master.key" }
|
||||
},
|
||||
{
|
||||
name: "Master Item",
|
||||
key: "master.key",
|
||||
type: 0,
|
||||
value_type: 3,
|
||||
}
|
||||
]
|
||||
}];
|
||||
|
||||
// Mocking group.get
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ groupid: "201", name: "Group1" }]);
|
||||
// Mocking template.get for linked template
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ templateid: "401", host: "Linked Template" }]);
|
||||
// Mocking template.create
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ templateids: ["501"] });
|
||||
|
||||
// Mocking item.create for Master Item (first pass will pick Master Item because Dependent Item is missing its master)
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ itemids: ["601"] });
|
||||
// Mocking item.create for Dependent Item (second pass)
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ itemids: ["602"] });
|
||||
|
||||
const result = await TemplateImporter.importTemplates(templates, "token");
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result![0].templateid).toBe("501");
|
||||
|
||||
// Check template.create params
|
||||
const templateCreateCall = (zabbixAPI.post as jest.Mock).mock.calls.find(call => call[1].body.method === "template.create");
|
||||
expect(templateCreateCall[1].body.params.templates).toContainEqual({ templateid: "401" });
|
||||
|
||||
// Check item.create calls
|
||||
const itemCreateCalls = (zabbixAPI.post as jest.Mock).mock.calls.filter(call => call[1].body.method === "item.create");
|
||||
expect(itemCreateCalls).toHaveLength(2);
|
||||
|
||||
const masterCall = itemCreateCalls.find(c => c[1].body.params.name === "Master Item");
|
||||
const dependentCall = itemCreateCalls.find(c => c[1].body.params.name === "Dependent Item");
|
||||
|
||||
expect(masterCall[1].body.params.key_).toBe("master.key");
|
||||
expect(dependentCall[1].body.params.key_).toBe("dependent.key");
|
||||
expect(dependentCall[1].body.params.master_itemid).toBe("601");
|
||||
});
|
||||
|
||||
test("importTemplates - template query", async () => {
|
||||
// This tests the template.get functionality used during import
|
||||
const templates = [{
|
||||
host: "Template A",
|
||||
groupNames: ["Group1"],
|
||||
templates: [{ name: "Template B" }]
|
||||
}];
|
||||
|
||||
// Mock Group
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ groupid: "1", name: "Group1" }]);
|
||||
// Mock Template B lookup
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ templateid: "2", host: "Template B" }]);
|
||||
// Mock Template A creation
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ templateids: ["3"] });
|
||||
|
||||
await TemplateImporter.importTemplates(templates, "token");
|
||||
|
||||
// Verify that template.get was called for Template B
|
||||
const templateQueryCall = (zabbixAPI.post as jest.Mock).mock.calls.find(call => call[1].body.method === "template.get");
|
||||
expect(templateQueryCall).toBeDefined();
|
||||
expect(templateQueryCall[1].body.params.filter.host).toContain("Template B");
|
||||
});
|
||||
|
||||
test("importTemplates - error message includes data field", async () => {
|
||||
const templates = [{
|
||||
host: "Error Template",
|
||||
groupNames: ["Group1"]
|
||||
}];
|
||||
|
||||
// Mocking group.get
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ groupid: "201", name: "Group1" }]);
|
||||
|
||||
// Mocking template.create with an error including data
|
||||
const zabbixError = {
|
||||
error: {
|
||||
code: -32602,
|
||||
message: "Invalid params.",
|
||||
data: "Invalid parameter \"/1\": the parameter \"key_\" is missing."
|
||||
}
|
||||
};
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(zabbixError);
|
||||
|
||||
const result = await TemplateImporter.importTemplates(templates, "token");
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result![0].message).toContain("Invalid params.");
|
||||
expect(result![0].message).toContain("the parameter \"key_\" is missing.");
|
||||
});
|
||||
});
|
||||
229
src/test/template_integration.test.ts
Normal file
229
src/test/template_integration.test.ts
Normal file
|
|
@ -0,0 +1,229 @@
|
|||
import { ApolloServer } from '@apollo/server';
|
||||
import { schema_loader } from '../api/schema.js';
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { zabbixAPI } from '../datasources/zabbix-api.js';
|
||||
|
||||
// Mocking ZabbixAPI.post
|
||||
jest.mock("../datasources/zabbix-api.js", () => ({
|
||||
zabbixAPI: {
|
||||
post: jest.fn(),
|
||||
executeRequest: jest.fn(),
|
||||
baseURL: 'http://localhost/zabbix'
|
||||
}
|
||||
}));
|
||||
|
||||
describe("Template Integration Tests", () => {
|
||||
let server: ApolloServer;
|
||||
|
||||
beforeAll(async () => {
|
||||
const schema = await schema_loader();
|
||||
server = new ApolloServer({
|
||||
schema,
|
||||
});
|
||||
});
|
||||
|
||||
test("Import templates using sample query and variables", async () => {
|
||||
const sampleFile = readFileSync(join(process.cwd(), 'docs', 'sample_import_templates_mutation.graphql'), 'utf-8').replace(/\r\n/g, '\n');
|
||||
|
||||
// Extract mutation and variables from the doc file
|
||||
const mutationMatch = sampleFile.match(/```graphql\n([\s\S]*?)\n```/);
|
||||
const variablesMatch = sampleFile.match(/```json\n([\s\S]*?)\n```/);
|
||||
|
||||
const mutation = mutationMatch![1];
|
||||
const variables = JSON.parse(variablesMatch![1]);
|
||||
|
||||
// Mock Zabbix API calls
|
||||
// 1. Group lookup
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ groupid: "201", name: "Templates/Roadwork/Devices" }]);
|
||||
// 2. Linked template lookup
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ templateid: "401", host: "ROADWORK_DEVICE" }]);
|
||||
// 3. Template creation
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ templateids: ["501"] });
|
||||
// 4. Item creation (location)
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ itemids: ["601"] });
|
||||
// 5. Item creation (MQTT_LOCATION)
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ itemids: ["602"] });
|
||||
|
||||
const response = await server.executeOperation({
|
||||
query: mutation,
|
||||
variables: variables,
|
||||
}, {
|
||||
contextValue: {
|
||||
zabbixAuthToken: 'test-token'
|
||||
}
|
||||
});
|
||||
|
||||
expect(response.body.kind).toBe('single');
|
||||
// @ts-ignore
|
||||
const result = response.body.singleResult;
|
||||
expect(result.errors).toBeUndefined();
|
||||
expect(result.data.importTemplates).toHaveLength(1);
|
||||
expect(result.data.importTemplates[0].host).toBe("BT_DEVICE_TRACKER");
|
||||
expect(result.data.importTemplates[0].templateid).toBe("501");
|
||||
});
|
||||
|
||||
test("Import and Export templates comparison", async () => {
|
||||
// 1. Import
|
||||
const importSample = readFileSync(join(process.cwd(), 'docs', 'sample_import_templates_mutation.graphql'), 'utf-8').replace(/\r\n/g, '\n');
|
||||
const importMutation = importSample.match(/```graphql\n([\s\S]*?)\n```/)![1];
|
||||
const importVariables = JSON.parse(importSample.match(/```json\n([\s\S]*?)\n```/)![1]);
|
||||
|
||||
// Mock for import
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ groupid: "201", name: "Templates/Roadwork/Devices" }]);
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ templateid: "401", host: "ROADWORK_DEVICE" }]);
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ templateids: ["501"] });
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ itemids: ["601"] });
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ itemids: ["602"] });
|
||||
|
||||
await server.executeOperation({
|
||||
query: importMutation,
|
||||
variables: importVariables,
|
||||
}, {
|
||||
contextValue: { zabbixAuthToken: 'test-token' }
|
||||
});
|
||||
|
||||
// 2. Export (Query)
|
||||
const querySample = readFileSync(join(process.cwd(), 'docs', 'sample_templates_query.graphql'), 'utf-8').replace(/\r\n/g, '\n');
|
||||
const query = querySample.match(/```graphql\n([\s\S]*?)\n```/)![1];
|
||||
const queryVariables = JSON.parse(querySample.match(/```json\n([\s\S]*?)\n```/)![1]);
|
||||
|
||||
// Mock for query
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ templateid: "501", host: "BT_DEVICE_TRACKER", name: "BT_DEVICE_TRACKER" }]);
|
||||
|
||||
const queryResponse = await server.executeOperation({
|
||||
query: query,
|
||||
variables: queryVariables,
|
||||
}, {
|
||||
contextValue: { zabbixAuthToken: 'test-token' }
|
||||
});
|
||||
|
||||
expect(queryResponse.body.kind).toBe('single');
|
||||
// @ts-ignore
|
||||
const queryResult = queryResponse.body.singleResult;
|
||||
expect(queryResult.errors).toBeUndefined();
|
||||
expect(queryResult.data.templates).toHaveLength(1);
|
||||
expect(queryResult.data.templates[0].name).toBe(importVariables.templates[0].name);
|
||||
expect(queryResult.data.templates[0].templateid).toBe("501");
|
||||
|
||||
// 3. Delete
|
||||
const deleteMutation = `
|
||||
mutation DeleteTemplates($templateids: [Int!], $name_pattern: String) {
|
||||
deleteTemplates(templateids: $templateids, name_pattern: $name_pattern) {
|
||||
id
|
||||
message
|
||||
}
|
||||
}
|
||||
`;
|
||||
// Mock for query (to find ID for name_pattern deletion)
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([{ templateid: "501", host: "BT_DEVICE_TRACKER" }]);
|
||||
// Mock for delete
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ templateids: ["501"] });
|
||||
|
||||
const deleteResponse = await server.executeOperation({
|
||||
query: deleteMutation,
|
||||
variables: { name_pattern: "BT_DEVICE_TRACKER" },
|
||||
}, {
|
||||
contextValue: { zabbixAuthToken: 'test-token' }
|
||||
});
|
||||
|
||||
expect(deleteResponse.body.kind).toBe('single');
|
||||
// @ts-ignore
|
||||
const deleteResult = deleteResponse.body.singleResult;
|
||||
expect(deleteResult.errors).toBeUndefined();
|
||||
expect(deleteResult.data.deleteTemplates).toHaveLength(1);
|
||||
expect(deleteResult.data.deleteTemplates[0].message).toContain("deleted successfully");
|
||||
});
|
||||
|
||||
test("Import and Export template groups comparison", async () => {
|
||||
// 1. Import
|
||||
const importSample = readFileSync(join(process.cwd(), 'docs', 'sample_import_template_groups_mutation.graphql'), 'utf-8').replace(/\r\n/g, '\n');
|
||||
const importMutation = importSample.match(/```graphql\n([\s\S]*?)\n```/)![1];
|
||||
const importVariables = JSON.parse(importSample.match(/```json\n([\s\S]*?)\n```/)![1]);
|
||||
|
||||
// Mock for import (8 groups in sample)
|
||||
for (const group of importVariables.templateGroups) {
|
||||
// Mock lookup (not found)
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([]);
|
||||
// Mock creation
|
||||
const mockGroupId = Math.floor(Math.random() * 1000).toString();
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ groupids: [mockGroupId] });
|
||||
}
|
||||
|
||||
const importResponse = await server.executeOperation({
|
||||
query: importMutation,
|
||||
variables: importVariables,
|
||||
}, {
|
||||
contextValue: { zabbixAuthToken: 'test-token' }
|
||||
});
|
||||
|
||||
expect(importResponse.body.kind).toBe('single');
|
||||
// @ts-ignore
|
||||
const importResult = importResponse.body.singleResult;
|
||||
expect(importResult.errors).toBeUndefined();
|
||||
expect(importResult.data.importTemplateGroups).toHaveLength(importVariables.templateGroups.length);
|
||||
|
||||
// 2. Export (Query)
|
||||
const querySample = readFileSync(join(process.cwd(), 'docs', 'sample_all_template_groups_query.graphql'), 'utf-8').replace(/\r\n/g, '\n');
|
||||
const query = querySample.match(/```graphql\n([\s\S]*?)\n```/)![1];
|
||||
const queryVariables = JSON.parse(querySample.match(/```json\n([\s\S]*?)\n```/)![1]);
|
||||
|
||||
// Mock for query
|
||||
const mockGroupsResponse = importVariables.templateGroups.map((g: any, index: number) => ({
|
||||
groupid: (index + 1000).toString(),
|
||||
name: g.groupName
|
||||
}));
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(mockGroupsResponse);
|
||||
|
||||
const queryResponse = await server.executeOperation({
|
||||
query: query,
|
||||
variables: queryVariables,
|
||||
}, {
|
||||
contextValue: { zabbixAuthToken: 'test-token' }
|
||||
});
|
||||
|
||||
expect(queryResponse.body.kind).toBe('single');
|
||||
// @ts-ignore
|
||||
const queryResult = queryResponse.body.singleResult;
|
||||
expect(queryResult.errors).toBeUndefined();
|
||||
expect(queryResult.data.allTemplateGroups).toHaveLength(importVariables.templateGroups.length);
|
||||
|
||||
// Verify names match
|
||||
const importedNames = importVariables.templateGroups.map((g: any) => g.groupName).sort();
|
||||
const exportedNames = queryResult.data.allTemplateGroups.map((g: any) => g.name).sort();
|
||||
expect(exportedNames).toEqual(importedNames);
|
||||
|
||||
// 3. Delete Template Groups
|
||||
const groupidsToDelete = queryResult.data.allTemplateGroups.map((g: any) => parseInt(g.groupid));
|
||||
// Mock for query (for name_pattern)
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(queryResult.data.allTemplateGroups.map((g: any) => ({ groupid: g.groupid, name: g.name })));
|
||||
// Mock for delete
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ groupids: groupidsToDelete.map((id: number) => id.toString()) });
|
||||
|
||||
const deleteMutation = `
|
||||
mutation DeleteTemplateGroups($groupids: [Int!], $name_pattern: String) {
|
||||
deleteTemplateGroups(groupids: $groupids, name_pattern: $name_pattern) {
|
||||
id
|
||||
message
|
||||
error {
|
||||
message
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const deleteResponse = await server.executeOperation({
|
||||
query: deleteMutation,
|
||||
variables: { name_pattern: "Templates/Roadwork/%" },
|
||||
}, {
|
||||
contextValue: { zabbixAuthToken: 'test-token' }
|
||||
});
|
||||
|
||||
expect(deleteResponse.body.kind).toBe('single');
|
||||
// @ts-ignore
|
||||
const deleteResult = deleteResponse.body.singleResult;
|
||||
expect(deleteResult.errors).toBeUndefined();
|
||||
expect(deleteResult.data.deleteTemplateGroups).toHaveLength(groupidsToDelete.length);
|
||||
expect(deleteResult.data.deleteTemplateGroups[0].message).toContain("deleted successfully");
|
||||
});
|
||||
});
|
||||
112
src/test/template_query.test.ts
Normal file
112
src/test/template_query.test.ts
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
|
||||
import {createResolvers} from "../api/resolvers.js";
|
||||
import {zabbixAPI} from "../datasources/zabbix-api.js";
|
||||
import {QueryTemplatesArgs} from "../schema/generated/graphql.js";
|
||||
|
||||
// Mocking ZabbixAPI
|
||||
jest.mock("../datasources/zabbix-api.js", () => ({
|
||||
zabbixAPI: {
|
||||
executeRequest: jest.fn(),
|
||||
post: jest.fn(),
|
||||
baseURL: "http://mock-zabbix"
|
||||
},
|
||||
ZABBIX_EDGE_DEVICE_BASE_GROUP: "Baustellen-Devices"
|
||||
}));
|
||||
|
||||
describe("Template Resolver", () => {
|
||||
let resolvers: any;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
resolvers = createResolvers();
|
||||
});
|
||||
|
||||
test("templates query - returns all templates", async () => {
|
||||
const mockTemplates = [
|
||||
{ templateid: "1", name: "Template 1", uuid: "uuid1" },
|
||||
{ templateid: "2", name: "Template 2", uuid: "uuid2" }
|
||||
];
|
||||
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(mockTemplates);
|
||||
|
||||
const args: QueryTemplatesArgs = {};
|
||||
const context = { zabbixAuthToken: "test-token" };
|
||||
|
||||
const result = await resolvers.Query.templates(null, args, context);
|
||||
|
||||
expect(result).toEqual(mockTemplates);
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("template.get", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
method: "template.get",
|
||||
params: {}
|
||||
})
|
||||
}));
|
||||
});
|
||||
|
||||
test("templates query - filters by hostids", async () => {
|
||||
const mockTemplates = [{ templateid: "1", name: "Template 1" }];
|
||||
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(mockTemplates);
|
||||
|
||||
const args: QueryTemplatesArgs = { hostids: [1] };
|
||||
const context = { zabbixAuthToken: "test-token" };
|
||||
|
||||
const result = await resolvers.Query.templates(null, args, context);
|
||||
|
||||
expect(result).toEqual(mockTemplates);
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("template.get", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
method: "template.get",
|
||||
params: expect.objectContaining({
|
||||
templateids: [1]
|
||||
})
|
||||
})
|
||||
}));
|
||||
});
|
||||
|
||||
test("templates query - filters by name_pattern", async () => {
|
||||
const mockTemplates = [{ templateid: "1", name: "Template 1" }];
|
||||
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(mockTemplates);
|
||||
|
||||
const args: QueryTemplatesArgs = { name_pattern: "Template" };
|
||||
const context = { zabbixAuthToken: "test-token" };
|
||||
|
||||
const result = await resolvers.Query.templates(null, args, context);
|
||||
|
||||
expect(result).toEqual(mockTemplates);
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("template.get", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
method: "template.get",
|
||||
params: expect.objectContaining({
|
||||
search: {
|
||||
name: "Template"
|
||||
}
|
||||
})
|
||||
})
|
||||
}));
|
||||
});
|
||||
|
||||
test("templates query - filters by name_pattern with % wildcard", async () => {
|
||||
const mockTemplates = [{ templateid: "1", name: "Template 1" }];
|
||||
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(mockTemplates);
|
||||
|
||||
const args: QueryTemplatesArgs = { name_pattern: "Temp%1" };
|
||||
const context = { zabbixAuthToken: "test-token" };
|
||||
|
||||
const result = await resolvers.Query.templates(null, args, context);
|
||||
|
||||
expect(result).toEqual(mockTemplates);
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("template.get", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
method: "template.get",
|
||||
params: expect.objectContaining({
|
||||
search: {
|
||||
name: "Temp%1"
|
||||
}
|
||||
})
|
||||
})
|
||||
}));
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue