From fa6a4734d4375c01cb0a61315db7874145afd993 Mon Sep 17 00:00:00 2001 From: Tony Fernandez Date: Tue, 29 Oct 2024 09:00:06 -0400 Subject: [PATCH] added auth tests with item id --- tests/unit/hooks/test_zabbix.py | 71 ++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/tests/unit/hooks/test_zabbix.py b/tests/unit/hooks/test_zabbix.py index 5e3c7afd..2fe28cca 100644 --- a/tests/unit/hooks/test_zabbix.py +++ b/tests/unit/hooks/test_zabbix.py @@ -36,6 +36,14 @@ DATA_ITEMID = { "id": 1, } +DATA_HOST_KEY_WITH_TOKEN = { + "jsonrpc": "2.0", + "method": "history.push", + "params": {"itemid": ITEMID, "value": VALUE}, + "id": 1, + "auth": "3fe6ed01a69ebd79907a120bcd04e494" +} + DATA_USER_LOGIN = { "jsonrpc": "2.0", "method": "user.login", @@ -252,4 +260,65 @@ def test_ping_monitor_config_host_and_key_with_username_password_auth_data_succe borgmatic.hooks.monitor.State.FAIL, monitoring_log_level=1, dry_run=False, - ) \ No newline at end of file + ) + +def test_ping_monitor_config_itemid_with_api_key_auth_data_successful(): + # This test should simulate a successful POST to a Zabbix server. This test uses API_KEY + # to authenticate and HOST/KEY to know which item to populate in Zabbix. + hook_config = { + 'server': SERVER, + 'itemid': ITEMID, + 'api_key': API_KEY + } + flexmock(module.requests).should_receive('post').with_args( + f'{SERVER}', + headers=AUTH_HEADERS_API_KEY, + json=DATA_ITEMID, + ).and_return(flexmock(ok=True)).once() + flexmock(module.logger).should_receive('warning').never() + + module.ping_monitor( + hook_config, + {}, + 'config.yaml', + borgmatic.hooks.monitor.State.FAIL, + monitoring_log_level=1, + dry_run=False, + ) + +def test_ping_monitor_config_itemid_with_username_password_auth_data_successful(): + # This test should simulate a successful POST to a Zabbix server. This test uses USERNAME/PASSWORD + # to authenticate and HOST/KEY to know which item to populate in Zabbix. + hook_config = { + 'server': SERVER, + 'itemid': ITEMID, + 'username': USERNAME, + 'password': PASSWORD + } + + auth_response = flexmock(ok=True) + auth_response.should_receive('json').and_return({"jsonrpc":"2.0","result":"3fe6ed01a69ebd79907a120bcd04e494","id":1}) + + flexmock(module.requests).should_receive('post').with_args( + f'{SERVER}', + headers=AUTH_HEADERS_USERNAME_PASSWORD, + json=DATA_USER_LOGIN, + ).and_return(auth_response).once() + + flexmock(module.logger).should_receive('warning').never() + + flexmock(module.requests).should_receive('post').with_args( + f'{SERVER}', + headers=AUTH_HEADERS_USERNAME_PASSWORD, + json=DATA_HOST_KEY_WITH_TOKEN, + ).and_return(flexmock(ok=True)).once() + + module.ping_monitor( + hook_config, + {}, + 'config.yaml', + borgmatic.hooks.monitor.State.FAIL, + monitoring_log_level=1, + dry_run=False, + ) +test_ping_monitor_config_itemid_with_username_password_auth_data_successful() \ No newline at end of file