-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathtest_attachments.py
More file actions
317 lines (263 loc) · 11.6 KB
/
test_attachments.py
File metadata and controls
317 lines (263 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from io import BytesIO
from unittest.mock import Mock
from nylas.models.attachments import Attachment, CreateAttachmentRequest, FindAttachmentQueryParams
from nylas.resources.attachments import Attachments
class TestAttachmentModel:
"""Tests for the Attachment dataclass model."""
def test_attachment_deserialization(self):
"""Test full deserialization of Attachment from dict."""
attach_json = {
"content_type": "image/png",
"filename": "pic.png",
"grant_id": "41009df5-bf11-4c97-aa18-b285b5f2e386",
"id": "185e56cb50e12e82",
"is_inline": True,
"size": 13068,
"content_id": "<ce9b9547-9eeb-43b2-ac4e-58768bdf04e4>",
"content_disposition": "inline",
}
attachment = Attachment.from_dict(attach_json)
assert attachment.content_type == "image/png"
assert attachment.filename == "pic.png"
assert attachment.grant_id == "41009df5-bf11-4c97-aa18-b285b5f2e386"
assert attachment.id == "185e56cb50e12e82"
assert attachment.is_inline is True
assert attachment.size == 13068
assert attachment.content_id == "<ce9b9547-9eeb-43b2-ac4e-58768bdf04e4>"
assert attachment.content_disposition == "inline"
def test_attachment_serialization(self):
"""Test serialization of Attachment to dict."""
attachment = Attachment(
id="185e56cb50e12e82",
grant_id="41009df5-bf11-4c97-aa18-b285b5f2e386",
filename="document.pdf",
content_type="application/pdf",
size=2048,
content_id="<doc-123>",
content_disposition="attachment",
is_inline=False,
)
result = attachment.to_dict()
assert result["id"] == "185e56cb50e12e82"
assert result["grant_id"] == "41009df5-bf11-4c97-aa18-b285b5f2e386"
assert result["filename"] == "document.pdf"
assert result["content_type"] == "application/pdf"
assert result["size"] == 2048
assert result["content_id"] == "<doc-123>"
assert result["content_disposition"] == "attachment"
assert result["is_inline"] is False
def test_attachment_deserialization_partial_fields(self):
"""Test deserialization with only required fields."""
attach_json = {
"id": "abc123",
"filename": "test.txt",
}
attachment = Attachment.from_dict(attach_json)
assert attachment.id == "abc123"
assert attachment.filename == "test.txt"
assert attachment.grant_id is None
assert attachment.content_type is None
assert attachment.size is None
assert attachment.content_id is None
assert attachment.content_disposition is None
assert attachment.is_inline is None
def test_attachment_deserialization_empty_dict(self):
"""Test deserialization from empty dict."""
attachment = Attachment.from_dict({})
assert attachment.id is None
assert attachment.grant_id is None
assert attachment.filename is None
assert attachment.content_type is None
assert attachment.size is None
assert attachment.content_id is None
assert attachment.content_disposition is None
assert attachment.is_inline is None
def test_attachment_default_values(self):
"""Test Attachment instantiation with default values."""
attachment = Attachment()
assert attachment.id is None
assert attachment.grant_id is None
assert attachment.filename is None
assert attachment.content_type is None
assert attachment.size is None
assert attachment.content_id is None
assert attachment.content_disposition is None
assert attachment.is_inline is None
def test_attachment_content_disposition_attachment(self):
"""Test attachment with content_disposition set to 'attachment'."""
attach_json = {
"id": "file-123",
"filename": "report.xlsx",
"content_disposition": "attachment",
"is_inline": False,
}
attachment = Attachment.from_dict(attach_json)
assert attachment.content_disposition == "attachment"
assert attachment.is_inline is False
def test_attachment_content_disposition_inline(self):
"""Test inline attachment with content_disposition."""
attach_json = {
"id": "img-456",
"filename": "logo.png",
"content_disposition": "inline",
"is_inline": True,
"content_id": "<logo@example.com>",
}
attachment = Attachment.from_dict(attach_json)
assert attachment.content_disposition == "inline"
assert attachment.is_inline is True
assert attachment.content_id == "<logo@example.com>"
def test_attachment_roundtrip_serialization(self):
"""Test that serialization and deserialization are inverses."""
original = Attachment(
id="test-id",
grant_id="grant-123",
filename="file.txt",
content_type="text/plain",
size=100,
content_id="<cid123>",
content_disposition="attachment",
is_inline=False,
)
serialized = original.to_dict()
deserialized = Attachment.from_dict(serialized)
assert deserialized.id == original.id
assert deserialized.grant_id == original.grant_id
assert deserialized.filename == original.filename
assert deserialized.content_type == original.content_type
assert deserialized.size == original.size
assert deserialized.content_id == original.content_id
assert deserialized.content_disposition == original.content_disposition
assert deserialized.is_inline == original.is_inline
class TestCreateAttachmentRequest:
"""Tests for the CreateAttachmentRequest TypedDict."""
def test_create_attachment_request_with_base64_content(self):
"""Test creating attachment request with base64 encoded content."""
request: CreateAttachmentRequest = {
"filename": "test.txt",
"content_type": "text/plain",
"content": "SGVsbG8gV29ybGQh", # base64 for "Hello World!"
"size": 12,
}
assert request["filename"] == "test.txt"
assert request["content_type"] == "text/plain"
assert request["content"] == "SGVsbG8gV29ybGQh"
assert request["size"] == 12
def test_create_attachment_request_with_file_object(self):
"""Test creating attachment request with file-like object."""
file_content = BytesIO(b"File content here")
request: CreateAttachmentRequest = {
"filename": "document.pdf",
"content_type": "application/pdf",
"content": file_content,
"size": 17,
}
assert request["filename"] == "document.pdf"
assert request["content_type"] == "application/pdf"
assert request["content"] == file_content
assert request["size"] == 17
def test_create_attachment_request_with_optional_fields(self):
"""Test creating attachment request with all optional fields."""
request: CreateAttachmentRequest = {
"filename": "image.png",
"content_type": "image/png",
"content": "iVBORw0KGgo=",
"size": 1024,
"content_id": "<image001@example.com>",
"content_disposition": "inline",
"is_inline": True,
}
assert request["filename"] == "image.png"
assert request["content_type"] == "image/png"
assert request["content"] == "iVBORw0KGgo="
assert request["size"] == 1024
assert request["content_id"] == "<image001@example.com>"
assert request["content_disposition"] == "inline"
assert request["is_inline"] is True
def test_create_attachment_request_minimal(self):
"""Test creating attachment request with only required fields."""
request: CreateAttachmentRequest = {
"filename": "minimal.txt",
"content_type": "text/plain",
"content": "data",
"size": 4,
}
assert "filename" in request
assert "content_type" in request
assert "content" in request
assert "size" in request
# Optional fields should not be present
assert "content_id" not in request
assert "content_disposition" not in request
assert "is_inline" not in request
class TestFindAttachmentQueryParams:
"""Tests for the FindAttachmentQueryParams TypedDict."""
def test_find_attachment_query_params(self):
"""Test creating find attachment query params."""
params: FindAttachmentQueryParams = {
"message_id": "msg-12345",
}
assert params["message_id"] == "msg-12345"
def test_find_attachment_query_params_various_message_ids(self):
"""Test find attachment query params with various message ID formats."""
# Simple ID
params1: FindAttachmentQueryParams = {"message_id": "abc123"}
assert params1["message_id"] == "abc123"
# UUID format
params2: FindAttachmentQueryParams = {"message_id": "550e8400-e29b-41d4-a716-446655440000"}
assert params2["message_id"] == "550e8400-e29b-41d4-a716-446655440000"
# Complex message ID (email message-id format)
params3: FindAttachmentQueryParams = {"message_id": "<CABcd123@mail.example.com>"}
assert params3["message_id"] == "<CABcd123@mail.example.com>"
class TestAttachments:
"""Tests for the Attachments resource API calls."""
def test_find_attachment(self, http_client_response):
attachments = Attachments(http_client_response)
query_params = FindAttachmentQueryParams(message_id="message-123")
attachments.find(
identifier="abc-123",
attachment_id="attachment-123",
query_params=query_params,
)
http_client_response._execute.assert_called_once_with(
"GET",
"/v3/grants/abc-123/attachments/attachment-123",
None,
query_params,
None,
overrides=None,
)
def test_download_attachment(self):
mock_http_client = Mock()
mock_http_client._execute_download_request.return_value = b"mock data"
attachments = Attachments(mock_http_client)
query_params = FindAttachmentQueryParams(message_id="message-123")
attachments.download(
identifier="abc-123",
attachment_id="attachment-123",
query_params=query_params,
overrides=None,
)
mock_http_client._execute_download_request.assert_called_once_with(
path="/v3/grants/abc-123/attachments/attachment-123/download",
query_params=query_params,
stream=True,
overrides=None,
)
def test_download_bytes(self):
mock_http_client = Mock()
mock_http_client._execute_download_request.return_value = b"mock data"
attachments = Attachments(mock_http_client)
query_params = FindAttachmentQueryParams(message_id="message-123")
attachments.download_bytes(
identifier="abc-123",
attachment_id="attachment-123",
query_params=query_params,
overrides=None,
)
mock_http_client._execute_download_request.assert_called_once_with(
path="/v3/grants/abc-123/attachments/attachment-123/download",
query_params=query_params,
stream=False,
overrides=None,
)