Feature/01 test all the things
This commit is contained in:
77
tests/test_flush.py
Normal file
77
tests/test_flush.py
Normal file
@ -0,0 +1,77 @@
|
||||
from types import SimpleNamespace
|
||||
from subprocess import CalledProcessError
|
||||
|
||||
|
||||
def test_flush(client, mocker, valid_credentials):
|
||||
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}",
|
||||
headers={"Authorization":
|
||||
"Basic " + valid_credentials})
|
||||
|
||||
assert response.json['operation'] == 'flush'
|
||||
|
||||
|
||||
def test_flush_nonexistent(client, mocker, valid_credentials):
|
||||
|
||||
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')
|
||||
|
||||
mocker.patch('jail2ban.pfctl.run',
|
||||
side_effect=side_effect)
|
||||
|
||||
name = 'nonexistent'
|
||||
response = client.get(f"/flush/{name}",
|
||||
headers={"Authorization":
|
||||
"Basic " + valid_credentials})
|
||||
|
||||
assert 'error' in response.json
|
||||
|
||||
|
||||
def test_wrong_method(client, mocker, valid_credentials):
|
||||
|
||||
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')
|
||||
|
||||
mocker.patch('jail2ban.pfctl.run',
|
||||
side_effect=side_effect)
|
||||
|
||||
name = 'nonexistent'
|
||||
response = client.put(f"/flush/{name}",
|
||||
headers={"Authorization":
|
||||
"Basic " + valid_credentials})
|
||||
|
||||
assert response.status_code == 405
|
||||
|
||||
|
||||
def test_filenotfound(app, mocker, valid_credentials):
|
||||
|
||||
app.config.update({
|
||||
"AUTHFILE": '../tests/nonexistent-users-test.txt'
|
||||
})
|
||||
|
||||
client = app.test_client()
|
||||
|
||||
name = 'nonexistent'
|
||||
response = client.get(f"/flush/{name}",
|
||||
headers={"Authorization":
|
||||
"Basic " + valid_credentials})
|
||||
|
||||
assert response.status_code == 500
|
||||
Reference in New Issue
Block a user