From b5f16f161c19ff38ed84aceba1cd19ad457bfb94 Mon Sep 17 00:00:00 2001 From: Ruben van Staveren Date: Thu, 10 Mar 2022 20:43:24 +0100 Subject: [PATCH] Test for invalid address handling --- tests/test_ban.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_ban.py b/tests/test_ban.py index edf3266..fc26c44 100644 --- a/tests/test_ban.py +++ b/tests/test_ban.py @@ -40,3 +40,22 @@ def test_ban_ipv4(client, mocker): headers={"Authorization": "Basic " + valid_credentials}) assert response.json['operation'] == 'add' + +def test_ban_invalid(client, mocker): + def noop(): + pass + run_res = SimpleNamespace() + run_res.stdout = b'' + run_res.stderr = b'1/1 addresses added.\n' + run_res.returncode = 0 + run_res.check_returncode = noop + + mocker.patch('jail2ban.pfctl.run', return_value=run_res) + + valid_credentials = base64.b64encode(b"test.example.com:testpassword").decode("utf-8") + json_payload = {"name": "sshd", "ip": "not:an::addr:ess"} + response = client.put("/ban", + json=json_payload, + headers={"Authorization": "Basic " + valid_credentials}) + + assert response.json['error'] == "'not:an::addr:ess' does not appear to be an IPv4 or IPv6 address"