2022-03-11 10:38:19 +01:00
|
|
|
from types import SimpleNamespace
|
2022-03-11 21:21:40 +01:00
|
|
|
from subprocess import CalledProcessError
|
2022-03-11 10:38:19 +01:00
|
|
|
|
|
|
|
|
2022-03-14 16:39:24 +01:00
|
|
|
def test_flush(client, mocker, valid_credentials):
|
2022-03-11 10:38:19 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
name = 'sshd'
|
|
|
|
response = client.get(f"/flush/{name}",
|
2022-03-14 16:39:24 +01:00
|
|
|
headers={"Authorization":
|
|
|
|
"Basic " + valid_credentials})
|
2022-03-11 10:38:19 +01:00
|
|
|
|
2022-03-11 13:42:36 +01:00
|
|
|
assert response.json['operation'] == 'flush'
|
2022-03-11 21:21:40 +01:00
|
|
|
|
|
|
|
|
2022-03-14 16:39:24 +01:00
|
|
|
def test_flush_nonexistent(client, mocker, valid_credentials):
|
2022-03-11 21:21:40 +01:00
|
|
|
|
2022-03-14 16:39:24 +01:00
|
|
|
cmd = ['/usr/local/bin/sudo',
|
|
|
|
'/sbin/pfctl', '-a', 'some/anchor',
|
|
|
|
'-t', 'nonexistent', '-T', 'flush']
|
|
|
|
|
|
|
|
side_effect = CalledProcessError(255, cmd, output=b'',
|
|
|
|
stderr=b'pfctl: Table does not exist')
|
2022-03-11 21:21:40 +01:00
|
|
|
|
|
|
|
mocker.patch('jail2ban.pfctl.run',
|
2022-03-14 16:39:24 +01:00
|
|
|
side_effect=side_effect)
|
2022-03-11 21:21:40 +01:00
|
|
|
|
|
|
|
name = 'nonexistent'
|
|
|
|
response = client.get(f"/flush/{name}",
|
2022-03-14 16:39:24 +01:00
|
|
|
headers={"Authorization":
|
|
|
|
"Basic " + valid_credentials})
|
2022-03-11 21:21:40 +01:00
|
|
|
|
|
|
|
assert 'error' in response.json
|
2022-03-11 22:07:04 +01:00
|
|
|
|
|
|
|
|
2022-03-14 16:39:24 +01:00
|
|
|
def test_wrong_method(client, mocker, valid_credentials):
|
|
|
|
|
|
|
|
cmd = ['/usr/local/bin/sudo',
|
|
|
|
'/sbin/pfctl', '-a', 'some/anchor',
|
|
|
|
'-t', 'nonexistent', '-T', 'flush']
|
2022-03-11 22:07:04 +01:00
|
|
|
|
2022-03-14 16:39:24 +01:00
|
|
|
side_effect = CalledProcessError(255, cmd, output=b'',
|
|
|
|
stderr=b'pfctl: Table does not exist')
|
2022-03-11 22:07:04 +01:00
|
|
|
|
|
|
|
mocker.patch('jail2ban.pfctl.run',
|
2022-03-14 16:39:24 +01:00
|
|
|
side_effect=side_effect)
|
2022-03-11 22:07:04 +01:00
|
|
|
|
|
|
|
name = 'nonexistent'
|
|
|
|
response = client.put(f"/flush/{name}",
|
2022-03-14 16:39:24 +01:00
|
|
|
headers={"Authorization":
|
|
|
|
"Basic " + valid_credentials})
|
2022-03-11 22:07:04 +01:00
|
|
|
|
|
|
|
assert response.status_code == 405
|
2022-03-14 15:38:38 +01:00
|
|
|
|
|
|
|
|
2022-03-14 16:39:24 +01:00
|
|
|
def test_filenotfound(app, mocker, valid_credentials):
|
2022-03-14 15:38:38 +01:00
|
|
|
|
|
|
|
app.config.update({
|
|
|
|
"AUTHFILE": '../tests/nonexistent-users-test.txt'
|
|
|
|
})
|
|
|
|
|
|
|
|
client = app.test_client()
|
|
|
|
|
|
|
|
name = 'nonexistent'
|
|
|
|
response = client.get(f"/flush/{name}",
|
2022-03-14 16:39:24 +01:00
|
|
|
headers={"Authorization":
|
|
|
|
"Basic " + valid_credentials})
|
2022-03-14 15:38:38 +01:00
|
|
|
|
|
|
|
assert response.status_code == 500
|