|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from base64 import b64encode |
| 4 | + |
| 5 | +import pytest |
| 6 | +from starlette.testclient import TestClient |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture(autouse=True, scope="session") |
| 10 | +def project_setup(): |
| 11 | + directory = os.path.abspath(os.path.dirname(__file__)) |
| 12 | + project_dir = os.path.join(directory, "data/v3.0") |
| 13 | + sys.path.insert(0, project_dir) |
| 14 | + yield |
| 15 | + sys.path.remove(project_dir) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def app(project_setup, loop): |
| 20 | + from aiohttpproject.__main__ import get_app |
| 21 | + |
| 22 | + return get_app(loop=loop) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +async def client(app, aiohttp_client): |
| 27 | + return await aiohttp_client(app) |
| 28 | + |
| 29 | + |
| 30 | +class BaseTestPetstore: |
| 31 | + api_key = "12345" |
| 32 | + |
| 33 | + @property |
| 34 | + def api_key_encoded(self): |
| 35 | + api_key_bytes = self.api_key.encode("utf8") |
| 36 | + api_key_bytes_enc = b64encode(api_key_bytes) |
| 37 | + return str(api_key_bytes_enc, "utf8") |
| 38 | + |
| 39 | + |
| 40 | +class TestPetPhotoView(BaseTestPetstore): |
| 41 | + @pytest.mark.xfail( |
| 42 | + reason="response binary format not supported", |
| 43 | + strict=True, |
| 44 | + ) |
| 45 | + async def test_get_valid(self, client, data_gif): |
| 46 | + headers = { |
| 47 | + "Authorization": "Basic testuser", |
| 48 | + "Api-Key": self.api_key_encoded, |
| 49 | + "Host": "petstore.swagger.io", |
| 50 | + } |
| 51 | + |
| 52 | + cookies = {"user": "1"} |
| 53 | + response = await client.get( |
| 54 | + "/v1/pets/1/photo", |
| 55 | + headers=headers, |
| 56 | + cookies=cookies, |
| 57 | + ) |
| 58 | + |
| 59 | + assert await response.content.read() == data_gif |
| 60 | + assert response.status == 200 |
| 61 | + |
| 62 | + async def test_post_valid(self, client, data_gif): |
| 63 | + content_type = "image/gif" |
| 64 | + headers = { |
| 65 | + "Authorization": "Basic testuser", |
| 66 | + "Api-Key": self.api_key_encoded, |
| 67 | + "Content-Type": content_type, |
| 68 | + "Host": "petstore.swagger.io", |
| 69 | + } |
| 70 | + data = { |
| 71 | + "file": data_gif, |
| 72 | + } |
| 73 | + |
| 74 | + cookies = {"user": "1"} |
| 75 | + response = await client.post( |
| 76 | + "/v1/pets/1/photo", |
| 77 | + headers=headers, |
| 78 | + data=data, |
| 79 | + cookies=cookies, |
| 80 | + ) |
| 81 | + |
| 82 | + assert not await response.text() |
| 83 | + assert response.status == 201 |
0 commit comments