2022-03-10 11:22:04 +01:00
|
|
|
import base64
|
|
|
|
from types import SimpleNamespace
|
|
|
|
|
|
|
|
pfctl_stdout_lines = b'''
|
|
|
|
block drop quick proto tcp from <f2b-sendmail-auth> to any port = submission
|
|
|
|
block drop quick proto tcp from <f2b-sendmail-auth> to any port = smtps
|
|
|
|
block drop quick proto tcp from <f2b-sendmail-auth> to any port = smtp
|
|
|
|
block drop quick proto tcp from <f2b-sshd> to any port = ssh
|
|
|
|
block drop quick proto tcp from <f2b-recidive> to any
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
def test_request_unauth(client):
|
2022-03-10 14:03:09 +01:00
|
|
|
json_payload = {"port":
|
|
|
|
"any port {pop3,pop3s,imap,imaps,submission,465,sieve}",
|
|
|
|
"name": "dovecot", "protocol": "tcp"}
|
2022-03-10 11:22:04 +01:00
|
|
|
response = client.put("/register", json=json_payload)
|
|
|
|
|
|
|
|
assert response.json['error'] == 'Access Denied'
|
|
|
|
|
|
|
|
|
|
|
|
def test_request_example(client, mocker):
|
|
|
|
def noop():
|
|
|
|
pass
|
|
|
|
run_res = SimpleNamespace()
|
|
|
|
run_res.stdout = pfctl_stdout_lines
|
|
|
|
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")
|
2022-03-10 14:03:09 +01:00
|
|
|
json_payload = {"port":
|
|
|
|
"any port {pop3,pop3s,imap,imaps,submission,465,sieve}",
|
|
|
|
"name": "dovecot", "protocol": "tcp"}
|
|
|
|
|
2022-03-10 11:22:04 +01:00
|
|
|
response = client.put("/register",
|
|
|
|
json=json_payload,
|
|
|
|
headers={"Authorization": "Basic " + valid_credentials})
|
|
|
|
|
|
|
|
assert response.json['remote_user'] == 'test.example.com'
|