-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathtest_attachment_media.py
More file actions
227 lines (185 loc) · 6.09 KB
/
Copy pathtest_attachment_media.py
File metadata and controls
227 lines (185 loc) · 6.09 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
from io import BytesIO
from zipfile import ZipFile
import pytest
from oss.src.core.sessions.attachments.dtos import AttachmentKind
from oss.src.core.sessions.attachments.media import classify
from oss.src.core.sessions.attachments.types import AttachmentInvalid
def test_inspected_png_type_overrides_declared_text_type():
result = classify(
data=b"\x89PNG\r\n\x1a\n" + (b"\x00" * 32),
declared_media_type="text/plain",
)
assert result.media_type == "image/png"
assert result.kind == AttachmentKind.IMAGE
assert result.native_image is True
def test_unsigned_markdown_keeps_declared_text_subtype():
result = classify(
data=b"# Heading\n\nBody\n",
declared_media_type="text/markdown",
)
assert result.media_type == "text/markdown"
assert result.kind == AttachmentKind.DOCUMENT
def test_unsigned_csv_with_binary_declaration_becomes_plain_text():
result = classify(
data=b"name,value\nalpha,1\n",
declared_media_type="application/octet-stream",
)
assert result.media_type == "text/plain"
assert result.kind == AttachmentKind.DOCUMENT
@pytest.mark.parametrize("data", [b"", b"\x80\x81\x82\x83\x84"])
def test_empty_or_unrecognizable_non_utf8_bytes_are_invalid(data):
with pytest.raises(AttachmentInvalid):
classify(data=data, declared_media_type="application/octet-stream")
def _zip_bytes(*, files):
buffer = BytesIO()
with ZipFile(buffer, "w") as archive:
for path, content in files:
archive.writestr(path, content)
return buffer.getvalue()
def test_plain_zip_is_classified_as_zip():
data = _zip_bytes(files=[("file.txt", "hello")])
result = classify(
data=data,
declared_media_type="application/zip",
)
assert result.media_type == "application/zip"
assert result.kind == AttachmentKind.OTHER
assert result.native_image is False
def test_plain_zip_structure_overrides_declared_docx_type():
result = classify(
data=_zip_bytes(files=[("notes.txt", "hello")]),
declared_media_type=(
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
),
)
assert result.media_type == "application/zip"
assert result.kind == AttachmentKind.OTHER
@pytest.mark.parametrize(
"declared_media_type",
[
"application/zip",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
],
)
def test_docx_structure_is_classified_as_docx(declared_media_type):
result = classify(
data=_zip_bytes(
files=[
("[Content_Types].xml", "<Types />"),
("word/document.xml", "<document />"),
]
),
declared_media_type=declared_media_type,
)
assert result.media_type == (
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
assert result.kind == AttachmentKind.OTHER
def test_malformed_zip_signature_keeps_inspected_type(monkeypatch):
monkeypatch.setattr(
"oss.src.core.sessions.attachments.media.puremagic.from_string",
lambda *_args, **_kwargs: "application/zip",
)
result = classify(
data=b"PK\x03\x04not-a-valid-archive",
declared_media_type="application/zip",
)
assert result.media_type == "application/zip"
assert result.kind == AttachmentKind.OTHER
def test_svg_is_a_workspace_document_not_a_native_image():
result = classify(
data=b'<svg xmlns="http://www.w3.org/2000/svg"></svg>',
declared_media_type="image/svg+xml",
)
assert result.media_type == "image/svg+xml"
assert result.kind == AttachmentKind.DOCUMENT
assert result.native_image is False
_AUDIO_CORPUS = [
(
"wav",
b"RIFF"
+ (36).to_bytes(4, "little")
+ b"WAVEfmt "
+ (16).to_bytes(4, "little")
+ b"\x01\x00\x01\x00\x40\x1f\x00\x00\x80\x3e\x00\x00\x02\x00\x10\x00"
+ b"data\x00\x00\x00\x00",
None,
),
(
"mp3",
b"ID3\x04\x00\x00\x00\x00\x00\x00\xff\xfb\x90\x64" + (b"\x00" * 32),
None,
),
(
"ogg",
b"OggS" + (b"\x00" * 24) + b"\x01vorbis" + (b"\x00" * 32),
"audio/ogg",
),
(
"webm",
b"\x1aE\xdf\xa3" + (b"\x00" * 16) + b"webm" + b"A_OPUS",
"audio/webm",
),
(
"flac",
b"fLaC\x00\x00\x00\x22" + (b"\x00" * 34),
None,
),
(
"m4a",
(24).to_bytes(4, "big")
+ b"ftyp"
+ b"M4A "
+ b"\x00\x00\x00\x00"
+ b"M4A "
+ b"isom",
"audio/mp4",
),
]
@pytest.mark.parametrize(("_name", "data", "expected_media_type"), _AUDIO_CORPUS)
def test_audio_container_corpus_is_classified_as_audio(
_name,
data,
expected_media_type,
):
result = classify(
data=data,
declared_media_type="application/octet-stream",
)
if expected_media_type is not None:
assert result.media_type == expected_media_type
else:
assert result.media_type.startswith("audio/")
assert result.kind == AttachmentKind.AUDIO
assert result.native_image is False
@pytest.mark.parametrize(
("data", "expected_media_type"),
[
(
b"OggS" + (b"\x00" * 24) + b"\x80theora" + b"\x01vorbis" + (b"\x00" * 16),
"video/ogg",
),
(
b"\x1aE\xdf\xa3" + (b"\x00" * 16) + b"webm" + b"V_VP9" + b"A_OPUS",
"video/webm",
),
],
)
def test_video_containers_remain_other_even_with_audio_markers(
data,
expected_media_type,
):
result = classify(data=data, declared_media_type="audio/ogg")
assert result.media_type == expected_media_type
assert result.kind == AttachmentKind.OTHER
assert result.native_image is False
def test_empty_puremagic_result_is_unrecognized(monkeypatch):
monkeypatch.setattr(
"oss.src.core.sessions.attachments.media.puremagic.from_string",
lambda *_args, **_kwargs: "",
)
with pytest.raises(AttachmentInvalid):
classify(
data=b"\x80\x81\x82\x83\x84",
declared_media_type="audio/mp4",
)