Add exception handler for when pfctl operations fail
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user