2023-11-24 11:26:13 +00:00
|
|
|
import pytest
|
|
|
|
|
2023-12-25 13:49:36 +00:00
|
|
|
import os
|
2023-11-24 11:26:13 +00:00
|
|
|
from os import path
|
|
|
|
from tests.conftest import global_data_dir
|
|
|
|
|
|
|
|
from selfprivacy_api.utils import get_dkim_key, get_domain
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
2023-12-25 13:49:36 +00:00
|
|
|
DKIM_FILE_CONTENT = b'selector._domainkey\tIN\tTXT\t( "v=DKIM1; k=rsa; "\n\t "p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNn/IhEz1SxgHxxxI8vlPYC2dNueiLe1GC4SYz8uHimC8SDkMvAwm7rqi2SimbFgGB5nccCNOqCkrIqJTCB9vufqBnVKAjshHqpOr5hk4JJ1T/AGQKWinstmDbfTLPYTbU8ijZrwwGeqQLlnXR5nSN0GB9GazheA9zaPsT6PV+aQIDAQAB" ) ; ----- DKIM key selector for test-domain.tld\n'
|
2023-11-24 11:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-01-09 18:58:09 +00:00
|
|
|
def dkim_file(mocker, tmpdir, generic_userdata):
|
2023-12-25 13:49:36 +00:00
|
|
|
domain = get_domain()
|
|
|
|
assert domain is not None
|
|
|
|
assert domain != ""
|
2024-07-25 17:21:44 +00:00
|
|
|
# In a separate folder to not interfere with dkim backups
|
|
|
|
dkim_dir = path.join(tmpdir, "dkim")
|
|
|
|
os.mkdir(dkim_dir)
|
2023-11-24 11:26:13 +00:00
|
|
|
|
2023-12-25 13:49:36 +00:00
|
|
|
filename = domain + ".selector.txt"
|
2024-07-25 17:21:44 +00:00
|
|
|
dkim_path = path.join(dkim_dir, filename)
|
2023-11-24 11:26:13 +00:00
|
|
|
|
2023-12-25 13:49:36 +00:00
|
|
|
with open(dkim_path, "wb") as file:
|
|
|
|
file.write(DKIM_FILE_CONTENT)
|
2023-11-24 11:26:13 +00:00
|
|
|
|
2024-07-25 17:21:44 +00:00
|
|
|
mocker.patch("selfprivacy_api.utils.DKIM_DIR", dkim_dir)
|
|
|
|
mocker.patch("selfprivacy_api.services.DKIM_DIR", dkim_dir)
|
2023-12-25 13:49:36 +00:00
|
|
|
return dkim_path
|
2023-11-24 11:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-12-25 13:49:36 +00:00
|
|
|
def no_dkim_file(dkim_file):
|
|
|
|
os.remove(dkim_file)
|
|
|
|
assert path.exists(dkim_file) is False
|
|
|
|
return dkim_file
|
2023-11-24 11:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
2024-01-09 18:58:09 +00:00
|
|
|
def test_get_dkim_key(dkim_file):
|
2023-11-24 11:26:13 +00:00
|
|
|
"""Test DKIM key"""
|
|
|
|
dkim_key = get_dkim_key("test-domain.tld")
|
|
|
|
assert (
|
|
|
|
dkim_key
|
|
|
|
== "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNn/IhEz1SxgHxxxI8vlPYC2dNueiLe1GC4SYz8uHimC8SDkMvAwm7rqi2SimbFgGB5nccCNOqCkrIqJTCB9vufqBnVKAjshHqpOr5hk4JJ1T/AGQKWinstmDbfTLPYTbU8ijZrwwGeqQLlnXR5nSN0GB9GazheA9zaPsT6PV+aQIDAQAB"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-09 18:58:09 +00:00
|
|
|
def test_no_dkim_key(no_dkim_file):
|
2023-11-24 11:26:13 +00:00
|
|
|
"""Test no DKIM key"""
|
|
|
|
dkim_key = get_dkim_key("test-domain.tld")
|
|
|
|
assert dkim_key is None
|