zabbix-graphql-api/src/test/user_rights.test.ts
Andreas Hilbig fb5e9cbe81 feat: improve Zabbix multi-version compatibility and introduce local development environment
This update enhances compatibility across multiple Zabbix versions and introduces tools for easier local development and testing.

Key improvements and verified version support:
- Verified Zabbix version support: 6.2, 6.4, 7.0, and 7.4.
- Version-specific feature handling:
  - `history.push` is enabled only for Zabbix 7.0+; older versions skip it with a clear error or notice.
  - Conditional JSON-RPC authentication: the `auth` field is automatically added to the request body for versions older than 6.4.
  - Implemented static Zabbix version caching in the datasource to minimize redundant API calls.
- Query optimization refinements:
  - Added mapping for implied fields (e.g., `state` -> `items`, `deviceType` -> `tags`).
  - Automatically prune unnecessary Zabbix parameters (like `selectItems` or `selectTags`) when not requested.
- Local development environment:
  - Added a new `zabbix-local` Docker Compose profile that includes PostgreSQL, Zabbix Server, and Zabbix Web.
  - Supports testing different versions by passing the `ZABBIX_VERSION` environment variable (e.g., 6.2, 6.4, 7.0, 7.4).
  - Provided a sample environment file at `samples/zabbix-local.env`.
- Documentation and Roadmap:
  - Updated README with a comprehensive version compatibility matrix and local environment instructions.
  - Created a new guide: `docs/howtos/local_development.md`.
  - Updated maintenance guides and added "Local Development Environment" as an achieved milestone in the roadmap.
- Test suite enhancements:
  - Improved Smoketest and RegressionTest executors with more reliable resource cleanup and error reporting.
  - Made tests version-aware to prevent failures on older Zabbix instances.

BREAKING CHANGE: Dropped Zabbix 6.0 specific workarounds; the minimum supported version is now 6.2.
2026-02-04 14:01:53 +01:00

130 lines
5.4 KiB
TypeScript

import {createResolvers} from "../api/resolvers.js";
import {zabbixAPI} from "../datasources/zabbix-api.js";
// Mocking ZabbixAPI
jest.mock("../datasources/zabbix-api.js", () => ({
zabbixAPI: {
executeRequest: jest.fn(),
post: jest.fn(),
getVersion: jest.fn().mockResolvedValue("7.0.0"),
baseURL: "http://mock-zabbix"
}
}));
// Mocking Config
jest.mock("../common_utils.js", () => ({
Config: {
ZABBIX_PERMISSION_TEMPLATE_GROUP_NAME_PREFIX: "CustomPerms"
}
}));
describe("User Rights and Permissions Resolvers", () => {
let resolvers: any;
beforeEach(() => {
jest.clearAllMocks();
resolvers = createResolvers();
});
test("exportUserRights query", async () => {
// Mocks for exportUserRights
(zabbixAPI.post as jest.Mock).mockImplementation((path: string) => {
if (path === "templategroup.get") return Promise.resolve([]);
if (path === "hostgroup.get") return Promise.resolve([]);
if (path === "usergroup.get.withuuids") return Promise.resolve([{ usrgrpid: "1", name: "UserGroup1", hostgroup_rights: [], templategroup_rights: [] }]);
if (path === "module.get") return Promise.resolve([{ moduleid: "10", id: "mod1" }]);
if (path === "role.get") return Promise.resolve([{ roleid: "2", name: "UserRole1" }]);
return Promise.resolve([]);
});
const args = { name_pattern: "User" };
const context = { zabbixAuthToken: "test-token" };
const result = await resolvers.Query.exportUserRights(null, args, context);
expect(result.userGroups).toBeDefined();
expect(result.userRoles).toBeDefined();
expect(zabbixAPI.post).toHaveBeenCalledWith("usergroup.get.withuuids", expect.anything());
expect(zabbixAPI.post).toHaveBeenCalledWith("module.get", expect.anything());
expect(zabbixAPI.post).toHaveBeenCalledWith("role.get", expect.anything());
});
test("userPermissions query", async () => {
// Mock for userPermissions
(zabbixAPI.post as jest.Mock).mockImplementation((path: string) => {
if (path === "usergroup.get.permissions") return Promise.resolve([
{
usrgrpid: "1",
name: "Group 1",
templategroup_rights: [{ id: "1001", permission: "3" }]
}
]);
if (path === "templategroup.get.permissions") return Promise.resolve([
{ groupid: "1001", name: "CustomPerms/Hostgroup/1001" }
]);
return Promise.resolve([]);
});
const args = { objectNames: ["Hostgroup/1001"] };
const context = { zabbixAuthToken: "test-token" };
const result = await resolvers.Query.userPermissions(null, args, context);
expect(result).toBeDefined();
expect(result).toHaveLength(1);
expect(result[0].objectName).toBe("Hostgroup/1001");
expect(result[0].permission).toBe("3"); // Zabbix value "3" (READ_WRITE)
});
test("hasPermissions query", async () => {
// Mock for hasPermissions
(zabbixAPI.post as jest.Mock).mockImplementation((path: string) => {
if (path === "usergroup.get.permissions") return Promise.resolve([
{
usrgrpid: "1",
templategroup_rights: [{ id: "1002", permission: "3" }]
}
]);
if (path === "templategroup.get.permissions") return Promise.resolve([
{ groupid: "1002", name: "CustomPerms/Hostgroup/1002" }
]);
return Promise.resolve([]);
});
const args = { permissions: [{ objectName: "Hostgroup/1002", permission: "READ" }] };
const context = { zabbixAuthToken: "test-token" };
const result = await resolvers.Query.hasPermissions(null, args, context);
expect(result).toBe(true);
});
test("importUserRights mutation", async () => {
// Mocks for importUserRights
(zabbixAPI.post as jest.Mock).mockImplementation((path: string) => {
if (path === "module.get") return Promise.resolve([{ moduleid: "10", id: "mod1" }]);
if (path === "role.get") return Promise.resolve([{ roleid: "2", name: "NewRole" }]);
if (path === "role.update") return Promise.resolve({ roleids: ["2"] });
if (path === "templategroup.get") return Promise.resolve([{ groupid: "101", name: "Group1", uuid: "uuid1" }]);
if (path === "hostgroup.get") return Promise.resolve([{ groupid: "201", name: "HostGroup1", uuid: "uuid2" }]);
if (path === "usergroup.get") return Promise.resolve([{ usrgrpid: "1", name: "NewGroup" }]);
if (path === "usergroup.update") return Promise.resolve({ usrgrpids: ["1"] });
if (path === "hostgroup.propagate") return Promise.resolve(true);
return Promise.resolve([]);
});
const args = {
input: {
userRoles: [{ name: "NewRole", type: 1 }],
userGroups: [{ name: "NewGroup" }]
},
dryRun: false
};
const context = { zabbixAuthToken: "test-token" };
const result = await resolvers.Mutation.importUserRights(null, args, context);
expect(result.userRoles).toHaveLength(1);
expect(result.userGroups).toHaveLength(1);
});
});