Newer
Older
# copyright 2023-2023 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact https://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import json
import webtest
from test.util import ApiBaseTC, check_missing_custom_header_response
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
class ApiBinaryTC(ApiBaseTC):
def upload_file(self) -> int:
self.login_request()
queries = [
{
"query": "Insert EntityWithBinary X: X binary %(binary_ref)s",
"params": {
"binary_ref": {"type": "binary_reference", "ref": "test_file"}
},
}
]
response = self.webapp.post(
self.get_api_path("rql"),
params={
"queries": json.dumps(queries),
"test_file": webtest.Upload("filename.txt", b"content"),
},
content_type="multipart/form-data",
headers=self.custom_headers,
)
return response.json[0][0][0]
def test_download_binary(self):
eid = self.upload_file()
response = self.webapp.get(
self.get_api_path("binary"),
params={"eid": eid, "attribute": "binary"},
headers=self.custom_headers,
)
assert response.body, b"content"
def test_unknown_eid(self):
response = self.webapp.get(
self.get_api_path("binary"),
params={"eid": 1227, "attribute": "binary"},
headers=self.custom_headers,
status=400,
)
assert response.json == {
"title": "KeyError",
"message": "No entity with eid 1227 in the repository",
"data": None,
}
def test_unknown_attribute(self):
eid = self.upload_file()
response = self.webapp.get(
self.get_api_path("binary"),
params={"eid": eid, "attribute": "unknown"},
headers=self.custom_headers,
status=400,
)
assert response.json == {
"title": "KeyError",
"message": "'No relation named unknown in schema'",
"data": None,
}
def test_attribute_not_binary(self):
eid = self.upload_file()
response = self.webapp.get(
self.get_api_path("binary"),
params={"eid": eid, "attribute": "name"},
headers=self.custom_headers,
status=400,
)
assert response.json == {
"title": "KeyError",
"message": "Attribute 'name' of entity 'EntityWithBinary' is not of type Bytes",
"data": None,
}
def test_no_file_present(self):
self.login_request()
response = self.webapp.post(
self.get_api_path("rql"),
params=json.dumps(
[
{
"query": "Insert EntityWithBinary X",
"params": {},
}
]
),
content_type="application/json",
headers=self.custom_headers,
)
eid = response.json[0][0][0]
response = self.webapp.get(
self.get_api_path("binary"),
params={"eid": eid, "attribute": "binary"},
headers=self.custom_headers,
status=204,
)
assert response.body == b""
def test_missing_custom_headers_returns_400(self):
response = self.webapp.get(
self.get_api_path("binary"),
status=400,
).json
check_missing_custom_header_response(response, 2)
response = self.webapp.get(
self.get_api_path("binary"),
params={"eid": 50, "attribute": "binary"},
status=400,
).json
check_missing_custom_header_response(response)