Skip to content

Commit 8922291

Browse files
committed
Fix a situation when there are no attachments
1 parent cfe85bd commit 8922291

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

tempmail/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Data models for the Temp Mail API."""
22

33
import enum
4+
import typing
45
from dataclasses import dataclass
56
from datetime import datetime
67
from typing import List, Dict, Any
@@ -88,6 +89,7 @@ class EmailMessage:
8889

8990
@classmethod
9091
def from_json(cls, data: Dict[str, Any]) -> "EmailMessage":
92+
attachments: typing.List[Dict[str, Any]] = data["attachments"] or []
9193
return cls(
9294
id=data["id"],
9395
from_addr=data["from"],
@@ -99,7 +101,7 @@ def from_json(cls, data: Dict[str, Any]) -> "EmailMessage":
99101
),
100102
cc=data["cc"],
101103
body_html=data["body_html"],
102-
attachments=[Attachment.from_json(v) for v in data["attachments"]],
104+
attachments=[Attachment.from_json(v) for v in attachments],
103105
)
104106

105107

tests/test_client.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,46 @@ def test_list_email_messages_success(self, mock_request):
155155
],
156156
)
157157

158+
@patch("tempmail.client.requests.Session.request")
159+
def test_list_email_messages_no_attachments(self, mock_request):
160+
mock_response = Mock()
161+
mock_response.status_code = 200
162+
mock_response.json.return_value = {
163+
"messages": [
164+
{
165+
"id": "msg1",
166+
"from": "[email protected]",
167+
168+
"cc": ["[email protected]"],
169+
"subject": "Test Subject",
170+
"body_text": "Test body",
171+
"body_html": "<p>Test body</p>",
172+
"created_at": "2023-01-01T00:00:00Z",
173+
"attachments": None,
174+
}
175+
]
176+
}
177+
mock_response.headers = self._rate_limit_headers
178+
mock_request.return_value = mock_response
179+
180+
client = TempMailClient("test-api-key")
181+
messages: typing.List[EmailMessage] = client.list_email_messages("[email protected]")
182+
183+
assert len(messages) == 1
184+
assert messages[0] == EmailMessage(
185+
id="msg1",
186+
from_addr="[email protected]",
187+
to_addr="[email protected]",
188+
189+
subject="Test Subject",
190+
body_text="Test body",
191+
body_html="<p>Test body</p>",
192+
created_at=datetime.datetime(
193+
2023, 1, 1, 0, 0, tzinfo=datetime.timezone.utc
194+
),
195+
attachments=[],
196+
)
197+
158198
@patch("tempmail.client.requests.Session.request")
159199
def test_list_email_messages_empty(self, mock_request):
160200
mock_response = Mock()

0 commit comments

Comments
 (0)