Add exception handler for when pfctl operations fail

This commit is contained in:
2022-03-11 21:21:40 +01:00
parent 34f871ae75
commit 542718b956
2 changed files with 26 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import base64
from types import SimpleNamespace
from subprocess import CalledProcessError
def test_flush(client, mocker):
@ -19,3 +20,19 @@ def test_flush(client, mocker):
headers={"Authorization": "Basic " + valid_credentials})
assert response.json['operation'] == 'flush'
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