Skip to content

Commit f9566a6

Browse files
committed
Add tests for changed logic, fix as_bytes DKIM signing bug
New tests: - BaseFile.get_data() with str, bytes, IO, and None - SMTPResponse: defaults, set_status, success, refused_recipients - Message.as_bytes() with DKIM signing The as_bytes test uncovered a bug: as_bytes() called sign_string() (expects str) instead of sign_bytes() (expects bytes), causing TypeError when DKIM signing was enabled. Fixed.
1 parent a7182d0 commit f9566a6

4 files changed

Lines changed: 79 additions & 2 deletions

File tree

emails/message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def as_bytes(self, message_cls: type | None = None) -> bytes:
356356
"""
357357
r = self.build_message(message_cls=message_cls).as_bytes()
358358
if self._signer:
359-
r = self.sign_string(r)
359+
r = self.sign_bytes(r)
360360
return r
361361

362362

emails/testsuite/message/test_dkim.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ def test_dkim_error():
132132
m.as_message()
133133

134134

135+
def test_dkim_as_bytes():
136+
137+
priv_key, pub_key = _generate_key(length=1024)
138+
message = Message(**common_email_data())
139+
message.dkim(key=priv_key, selector='_dkim', domain='somewhere.net')
140+
result = message.as_bytes()
141+
assert isinstance(result, bytes)
142+
assert b'DKIM-Signature: ' in result
143+
144+
135145
def test_dkim_sign_twice():
136146

137147
# Test #44:
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# encoding: utf-8
2+
from emails.backend.response import SMTPResponse
3+
4+
5+
def test_smtp_response_defaults():
6+
r = SMTPResponse()
7+
assert r.status_code is None
8+
assert r.status_text is None
9+
assert r.refused_recipients == {}
10+
assert r.esmtp_opts is None
11+
assert r.rcpt_options is None
12+
assert not r.success
13+
assert r.error is None
14+
15+
16+
def test_smtp_response_set_status():
17+
r = SMTPResponse()
18+
r.set_status('mail', 250, 'OK')
19+
assert r.status_code == 250
20+
assert r.status_text == 'OK'
21+
assert r.last_command == 'mail'
22+
assert len(r.responses) == 1
23+
24+
25+
def test_smtp_response_success():
26+
r = SMTPResponse()
27+
r.set_status('data', 250, 'OK')
28+
assert not r.success # _finished is False
29+
r._finished = True
30+
assert r.success
31+
32+
33+
def test_smtp_response_refused_recipients():
34+
r = SMTPResponse()
35+
r.refused_recipients = {}
36+
r.refused_recipients['bad@example.com'] = (550, b'User unknown')
37+
assert 'bad@example.com' in r.refused_recipients
38+
assert r.refused_recipients['bad@example.com'] == (550, b'User unknown')
39+
40+
41+
def test_smtp_response_exception():
42+
exc = Exception('connection failed')
43+
r = SMTPResponse(exception=exc)
44+
assert r.error is exc
45+
assert not r.success

emails/testsuite/store/test_store.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# encoding: utf-8
2+
from io import BytesIO
3+
24
import pytest
35
import emails
46
import emails.store
5-
from emails.store.file import fix_content_type
7+
from emails.store.file import BaseFile, fix_content_type
68

79

810
def test_fix_content_type():
@@ -47,6 +49,26 @@ def test_store_unique_name():
4749
assert f2.content_id != f3.content_id
4850

4951

52+
def test_get_data_str():
53+
f = BaseFile(data='hello')
54+
assert f.data == 'hello'
55+
56+
57+
def test_get_data_bytes():
58+
f = BaseFile(data=b'hello')
59+
assert f.data == b'hello'
60+
61+
62+
def test_get_data_filelike():
63+
f = BaseFile(data=BytesIO(b'hello'))
64+
assert f.data == b'hello'
65+
66+
67+
def test_get_data_none():
68+
f = BaseFile()
69+
assert f.data is None
70+
71+
5072
def test_store_commons2():
5173
store = emails.store.MemoryFileStore()
5274
f1 = store.add({'uri': '/a/c.gif'})

0 commit comments

Comments
 (0)