From 64523ae8b53a8058e498c956971e7395b564ad03 Mon Sep 17 00:00:00 2001 From: Ruben van Staveren Date: Thu, 10 Mar 2022 13:37:34 +0100 Subject: [PATCH] Add tests for /ban --- tests/test_ban.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/test_ban.py diff --git a/tests/test_ban.py b/tests/test_ban.py new file mode 100644 index 0000000..61c6584 --- /dev/null +++ b/tests/test_ban.py @@ -0,0 +1,51 @@ +import base64 +from types import SimpleNamespace + +pfctl_stdout_lines = b''' +block drop quick proto tcp from to any port = submission +block drop quick proto tcp from to any port = smtps +block drop quick proto tcp from to any port = smtp +block drop quick proto tcp from to any port = ssh +block drop quick proto tcp from to any +''' + + +def test_ban_ipv6(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":"2001:db8::abad:cafe"} + response = client.put("/ban", + json=json_payload, + headers={"Authorization": "Basic " + valid_credentials}) + + + assert response.json['operation'] == 'add' + +def test_ban_ipv4(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":"192.0.2.42"} + response = client.put("/ban", + json=json_payload, + headers={"Authorization": "Basic " + valid_credentials}) + + + assert response.json['operation'] == 'add'