2022-03-11 10:38:19 +01:00
|
|
|
import base64
|
|
|
|
from types import SimpleNamespace
|
2022-03-11 21:21:40 +01:00
|
|
|
from subprocess import CalledProcessError
|
2022-03-11 10:38:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_flush(client, mocker):
|
|
|
|
def noop():
|
|
|
|
pass
|
|
|
|
run_res = SimpleNamespace()
|
|
|
|
run_res.stdout = b''
|
|
|
|
run_res.stderr = b'1/1 addresses deleted.\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")
|
|
|
|
name = 'sshd'
|
|
|
|
response = client.get(f"/flush/{name}",
|
|
|
|
headers={"Authorization": "Basic " + valid_credentials})
|
|
|
|
|
2022-03-11 13:42:36 +01:00
|
|
|
assert response.json['operation'] == 'flush'
|
2022-03-11 21:21:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_flush_nonexistent(client, mocker):
|
|
|
|
|
|
|
|
cmd = ['/usr/local/bin/sudo', '/sbin/pfctl', '-a', 'some/anchor', '-t', 'nonexistent', '-T', 'flush']
|
|
|
|
|
|
|
|
mocker.patch('jail2ban.pfctl.run',
|
|
|
|
side_effect=CalledProcessError(255, cmd, output=b'',
|
|
|
|
stderr=b'pfctl: Table does not exist'))
|
|
|
|
|
|
|
|
valid_credentials = base64.b64encode(b"test.example.com:testpassword").decode("utf-8")
|
|
|
|
name = 'nonexistent'
|
|
|
|
response = client.get(f"/flush/{name}",
|
|
|
|
headers={"Authorization": "Basic " + valid_credentials})
|
|
|
|
|
|
|
|
assert 'error' in response.json
|
2022-03-11 22:07:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_wrong_method(client, mocker):
|
|
|
|
|
|
|
|
cmd = ['/usr/local/bin/sudo', '/sbin/pfctl', '-a', 'some/anchor', '-t', 'nonexistent', '-T', 'flush']
|
|
|
|
|
|
|
|
mocker.patch('jail2ban.pfctl.run',
|
|
|
|
side_effect=CalledProcessError(255, cmd, output=b'',
|
|
|
|
stderr=b'pfctl: Table does not exist'))
|
|
|
|
|
|
|
|
valid_credentials = base64.b64encode(b"test.example.com:testpassword").decode("utf-8")
|
|
|
|
name = 'nonexistent'
|
|
|
|
response = client.put(f"/flush/{name}",
|
|
|
|
headers={"Authorization": "Basic " + valid_credentials})
|
|
|
|
|
|
|
|
assert response.status_code == 405
|