mirror of
https://seed.flo-the.dev/z3gWc1qgaeZaoGwL4WTstLNoqjayM.git
synced 2025-12-06 04:47:35 +01:00
add a configurable list of recipients/cert dict
Signed-off-by: Florian Brandes <florian.brandes@posteo.de>
This commit is contained in:
@@ -42,15 +42,22 @@ def config() -> smtprd.ClientConfig:
|
||||
use_tls=False,
|
||||
start_tls=False,
|
||||
sender="sender",
|
||||
recipients=["recipient"],
|
||||
set_reply_to="",
|
||||
smime_to_cert=Path("tests", "signer.pem"),
|
||||
smime_cert=Path("tests", "signer.pem"),
|
||||
smime_cert_private=Path("tests", "privkey.pem"),
|
||||
)
|
||||
return cfg
|
||||
|
||||
|
||||
def emails() -> smtprd.EmailConfig:
|
||||
"""Return the email dict"""
|
||||
|
||||
cfg = smtprd.EmailConfig(
|
||||
email_certs={"recipient": Path("tests", "signer.pem")},
|
||||
)
|
||||
return cfg
|
||||
|
||||
|
||||
def test_config_from_config():
|
||||
"""Test opening and reading the config file"""
|
||||
config_parser: configparser.ConfigParser = configparser.ConfigParser()
|
||||
@@ -73,17 +80,24 @@ def test_config_from_ini():
|
||||
assert cfg.client.use_tls is True
|
||||
|
||||
|
||||
def test_config_emailcerts():
|
||||
"""test retrieval of email/cert pair"""
|
||||
email_list = [("email1", "cert1"), ("email2", "cert2")]
|
||||
cfg = smtprd.Config._get_emails_and_certs(email_list)
|
||||
assert cfg["email2"] == "cert2"
|
||||
|
||||
|
||||
def test_client_encrypt():
|
||||
"""Test encryption. Keys were generated with
|
||||
openssl req -newkey rsa:1024 -nodes -x509 -days 365 -out signer.pem
|
||||
"""
|
||||
|
||||
client = smtprd.SMTPClient(config())
|
||||
encrypted = client._encrypt(b"abc", "subject")
|
||||
client = smtprd.SMTPClient(config(), emails())
|
||||
encrypted = client._encrypt(b"abc", "subject", "recipient")
|
||||
lines = encrypted.decode().splitlines()
|
||||
# Test format of header
|
||||
assert lines[0] == "From: " + str(config().sender)
|
||||
assert lines[1] == "To: " + str(config().recipients[0])
|
||||
assert lines[1] == "To: recipient"
|
||||
assert lines[2] == "Subject: " + "subject"
|
||||
assert lines[3] == "MIME-Version: 1.0"
|
||||
assert lines[4] == 'Content-Disposition: attachment; filename="smime.p7m"'
|
||||
@@ -98,8 +112,9 @@ def test_client_encrypt():
|
||||
lines[8] == "MIIBdgYJKoZIhvcNAQcDoIIBZzCCAWMCAQAxggEeMIIBGgIBADCBgjBqMQswCQYD"
|
||||
)
|
||||
|
||||
# test decryption
|
||||
s = SMIME.SMIME()
|
||||
s.load_key(config().smime_cert_private, config().smime_cert)
|
||||
s.load_key(config().smime_cert_private, emails().email_certs["recipient"])
|
||||
buf = BIO.MemoryBuffer(encrypted)
|
||||
p7, _ = SMIME.smime_load_pkcs7_bio(buf)
|
||||
out = s.decrypt(p7)
|
||||
@@ -108,13 +123,13 @@ def test_client_encrypt():
|
||||
|
||||
def test_client_sign():
|
||||
"""Test signing"""
|
||||
client = smtprd.SMTPClient(config())
|
||||
client = smtprd.SMTPClient(config(), emails())
|
||||
message = email.message.EmailMessage()
|
||||
message.set_content("Test to sign")
|
||||
# header are needed, because email.message.Emailmessage will add a
|
||||
# newline between the boundary and the content if no header is found
|
||||
message.add_header("From", config().sender)
|
||||
message.add_header("To", config().recipients[0])
|
||||
message.add_header("To", "recipient")
|
||||
message.add_header("Subject", "Test")
|
||||
signed = client._sign(message)
|
||||
assert "This is an S/MIME signed message" in signed.decode()
|
||||
|
||||
Reference in New Issue
Block a user