From e52e29444f04e4708692e2da4a5687069562d508 Mon Sep 17 00:00:00 2001 From: Tony Fernandez Date: Tue, 29 Oct 2024 08:54:25 -0400 Subject: [PATCH] added user/pass auth test --- tests/unit/hooks/test_zabbix.py | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/unit/hooks/test_zabbix.py b/tests/unit/hooks/test_zabbix.py index 7647b80c..5e3c7afd 100644 --- a/tests/unit/hooks/test_zabbix.py +++ b/tests/unit/hooks/test_zabbix.py @@ -21,6 +21,14 @@ DATA_HOST_KEY = { "id": 1, } +DATA_HOST_KEY_WITH_TOKEN = { + "jsonrpc": "2.0", + "method": "history.push", + "params": {"host": HOST, "key": KEY, "value": VALUE}, + "id": 1, + "auth": "3fe6ed01a69ebd79907a120bcd04e494" +} + DATA_ITEMID = { "jsonrpc": "2.0", "method": "history.push", @@ -200,6 +208,43 @@ def test_ping_monitor_config_host_and_key_with_api_key_auth_data_successful(): ).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_host_and_key_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, + 'host': HOST, + 'key': KEY, + '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, {},