# 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 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""