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
|
import os
import sys
from base64 import b64encode
from io import BytesIO
import pytest
@pytest.fixture(autouse=True, scope="session")
def project_setup():
directory = os.path.abspath(os.path.dirname(__file__))
project_dir = os.path.join(directory, "data/v3.0")
sys.path.insert(0, project_dir)
yield
sys.path.remove(project_dir)
@pytest.fixture
def app(project_setup):
from aiohttpproject.__main__ import get_app
return get_app()
@pytest.fixture
async def client(app, aiohttp_client):
return await aiohttp_client(app)
class BaseTestPetstore:
api_key = "12345"
@property
def api_key_encoded(self):
api_key_bytes = self.api_key.encode("utf8")
api_key_bytes_enc = b64encode(api_key_bytes)
return str(api_key_bytes_enc, "utf8")
class TestPetPhotoView(BaseTestPetstore):
async def test_get_valid(self, client, data_gif):
headers = {
"Authorization": "Basic testuser",
"Api-Key": self.api_key_encoded,
"Host": "petstore.swagger.io",
}
cookies = {"user": "1"}
response = await client.get(
"/v1/pets/1/photo",
headers=headers,
cookies=cookies,
)
assert await response.content.read() == data_gif
assert response.status == 200
async def test_post_valid(self, client, data_gif):
content_type = "image/gif"
headers = {
"Authorization": "Basic testuser",
"Api-Key": self.api_key_encoded,
"Content-Type": content_type,
"Host": "petstore.swagger.io",
}
data = {
"file": BytesIO(data_gif),
}
cookies = {"user": "1"}
response = await client.post(
"/v1/pets/1/photo",
headers=headers,
data=data,
cookies=cookies,
)
assert not await response.text()
assert response.status == 201
|