2021-11-29 19:16:08 +00:00
|
|
|
import base64
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
|
2023-01-16 15:41:54 +00:00
|
|
|
from selfprivacy_api.utils import get_dkim_key
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
###############################################################################
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
|
|
|
class ProcessMock:
|
2021-11-29 19:16:08 +00:00
|
|
|
"""Mock subprocess.Popen"""
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def __init__(self, args, **kwargs):
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
|
|
|
|
def communicate():
|
2023-01-16 15:41:54 +00:00
|
|
|
return (
|
|
|
|
b'selector._domainkey\tIN\tTXT\t( "v=DKIM1; k=rsa; "\n\t "p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNn/IhEz1SxgHxxxI8vlPYC2dNueiLe1GC4SYz8uHimC8SDkMvAwm7rqi2SimbFgGB5nccCNOqCkrIqJTCB9vufqBnVKAjshHqpOr5hk4JJ1T/AGQKWinstmDbfTLPYTbU8ijZrwwGeqQLlnXR5nSN0GB9GazheA9zaPsT6PV+aQIDAQAB" ) ; ----- DKIM key selector for example.com\n',
|
|
|
|
None,
|
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
class NoFileMock(ProcessMock):
|
|
|
|
def communicate():
|
|
|
|
return (b"", None)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_subproccess_popen(mocker):
|
|
|
|
mock = mocker.patch("subprocess.Popen", autospec=True, return_value=ProcessMock)
|
2021-11-30 21:53:39 +00:00
|
|
|
mocker.patch(
|
2022-08-25 17:03:56 +00:00
|
|
|
"selfprivacy_api.rest.services.get_domain",
|
2021-11-30 21:53:39 +00:00
|
|
|
autospec=True,
|
|
|
|
return_value="example.com",
|
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
mocker.patch("os.path.exists", autospec=True, return_value=True)
|
2021-11-30 21:53:39 +00:00
|
|
|
return mock
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_no_file(mocker):
|
|
|
|
mock = mocker.patch("subprocess.Popen", autospec=True, return_value=NoFileMock)
|
2021-11-30 21:53:39 +00:00
|
|
|
mocker.patch(
|
2022-08-25 17:03:56 +00:00
|
|
|
"selfprivacy_api.rest.services.get_domain",
|
2021-11-30 21:53:39 +00:00
|
|
|
autospec=True,
|
|
|
|
return_value="example.com",
|
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
mocker.patch("os.path.exists", autospec=True, return_value=False)
|
|
|
|
return mock
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
###############################################################################
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_unauthorized(client, mock_subproccess_popen):
|
|
|
|
"""Test unauthorized"""
|
|
|
|
response = client.get("/services/mailserver/dkim")
|
|
|
|
assert response.status_code == 401
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_illegal_methods(authorized_client, mock_subproccess_popen):
|
|
|
|
response = authorized_client.post("/services/mailserver/dkim")
|
|
|
|
assert response.status_code == 405
|
|
|
|
response = authorized_client.put("/services/mailserver/dkim")
|
|
|
|
assert response.status_code == 405
|
|
|
|
response = authorized_client.delete("/services/mailserver/dkim")
|
|
|
|
assert response.status_code == 405
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2023-01-16 15:41:54 +00:00
|
|
|
def test_get_dkim_key(mock_subproccess_popen):
|
2021-11-29 19:16:08 +00:00
|
|
|
"""Test DKIM key"""
|
2023-01-16 15:41:54 +00:00
|
|
|
dkim_key = get_dkim_key("example.com")
|
|
|
|
assert (
|
|
|
|
dkim_key
|
|
|
|
== "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNn/IhEz1SxgHxxxI8vlPYC2dNueiLe1GC4SYz8uHimC8SDkMvAwm7rqi2SimbFgGB5nccCNOqCkrIqJTCB9vufqBnVKAjshHqpOr5hk4JJ1T/AGQKWinstmDbfTLPYTbU8ijZrwwGeqQLlnXR5nSN0GB9GazheA9zaPsT6PV+aQIDAQAB"
|
|
|
|
)
|
|
|
|
assert mock_subproccess_popen.call_args[0][0] == [
|
|
|
|
"cat",
|
|
|
|
"/var/dkim/example.com.selector.txt",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_dkim_key(authorized_client, mock_subproccess_popen):
|
|
|
|
"""Test old REST DKIM key endpoint"""
|
2021-11-29 19:16:08 +00:00
|
|
|
response = authorized_client.get("/services/mailserver/dkim")
|
|
|
|
assert response.status_code == 200
|
2023-01-16 15:41:54 +00:00
|
|
|
assert (
|
|
|
|
base64.b64decode(response.text)
|
|
|
|
== b'selector._domainkey\tIN\tTXT\t( "v=DKIM1; k=rsa; "\n\t "p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNn/IhEz1SxgHxxxI8vlPYC2dNueiLe1GC4SYz8uHimC8SDkMvAwm7rqi2SimbFgGB5nccCNOqCkrIqJTCB9vufqBnVKAjshHqpOr5hk4JJ1T/AGQKWinstmDbfTLPYTbU8ijZrwwGeqQLlnXR5nSN0GB9GazheA9zaPsT6PV+aQIDAQAB" ) ; ----- DKIM key selector for example.com\n'
|
|
|
|
)
|
2021-11-30 21:53:39 +00:00
|
|
|
assert mock_subproccess_popen.call_args[0][0] == [
|
|
|
|
"cat",
|
|
|
|
"/var/dkim/example.com.selector.txt",
|
|
|
|
]
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
def test_no_dkim_key(authorized_client, mock_no_file):
|
|
|
|
"""Test no DKIM key"""
|
|
|
|
response = authorized_client.get("/services/mailserver/dkim")
|
|
|
|
assert response.status_code == 404
|
2021-11-30 21:53:39 +00:00
|
|
|
assert mock_no_file.called == False
|