jail2ban/tests/test_ban.py

62 lines
2.0 KiB
Python
Raw Normal View History

2022-03-10 13:37:34 +01:00
import base64
from types import SimpleNamespace
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'
2022-03-10 14:03:09 +01:00
run_res.returncode = 0
2022-03-10 13:37:34 +01:00
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")
2022-03-10 14:03:09 +01:00
json_payload = {"name": "sshd", "ip": "2001:db8::abad:cafe"}
2022-03-10 13:37:34 +01:00
response = client.put("/ban",
json=json_payload,
headers={"Authorization": "Basic " + valid_credentials})
assert response.json['operation'] == 'add'
2022-03-10 14:03:09 +01:00
2022-03-10 13:37:34 +01:00
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'
2022-03-10 14:03:09 +01:00
run_res.returncode = 0
2022-03-10 13:37:34 +01:00
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")
2022-03-10 14:03:09 +01:00
json_payload = {"name": "sshd", "ip": "192.0.2.42"}
2022-03-10 13:37:34 +01:00
response = client.put("/ban",
json=json_payload,
headers={"Authorization": "Basic " + valid_credentials})
assert response.json['operation'] == 'add'
2022-03-10 20:43:24 +01:00
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"