From fd8371b67f9f3be143ecdec3c79f9f2aa0f525a9 Mon Sep 17 00:00:00 2001 From: Vadim Melnik Date: Thu, 28 Sep 2023 11:08:17 +0300 Subject: [PATCH] Testing with pytest, router/api_v1 package tests, work in progress. --- repromon_app/tests/conftest.py | 36 ++++++++++++++++++++++++ repromon_app/tests/router/test_api_v1.py | 20 +++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 repromon_app/tests/router/test_api_v1.py diff --git a/repromon_app/tests/conftest.py b/repromon_app/tests/conftest.py index bfc4362..7d9097a 100644 --- a/repromon_app/tests/conftest.py +++ b/repromon_app/tests/conftest.py @@ -17,6 +17,10 @@ _apikey_tester2: str = None _apikey_tester3: str = None +_token_tester1: str = None +_token_tester2: str = None +_token_tester3: str = None + _fastapi_app: FastAPI = None _test_client: TestClient = None @@ -39,6 +43,11 @@ def init_db(): _apikey_tester1 = svc.get_user_apikey("tester1").apikey _apikey_tester2 = svc.get_user_apikey("tester2").apikey _apikey_tester3 = svc.get_user_apikey("tester3").apikey + + global _token_tester1, _token_tester2, _token_tester3 + _token_tester1 = svc.create_access_token("tester1", 60 * 60).access_token + _token_tester2 = svc.create_access_token("tester2", 60 * 60).access_token + _token_tester3 = svc.create_access_token("tester3", 60 * 60).access_token yield @@ -78,9 +87,36 @@ def fastapi_app() -> FastAPI: return _fastapi_app +@pytest.fixture +def oauth2_tester1_headers() -> dict: + global _token_tester1 + headers = { + "Authorization": f"Bearer {_token_tester1}" + } + return headers + + @pytest.fixture def test_client(fastapi_app) -> str: global _test_client if not _test_client: _test_client = TestClient(fastapi_app) return _test_client + + +@pytest.fixture +def token_tester1() -> str: + global _token_tester1 + return _token_tester1 + + +@pytest.fixture +def token_tester2() -> str: + global _token_tester2 + return _token_tester2 + + +@pytest.fixture +def token_tester3() -> str: + global _token_tester3 + return _token_tester3 diff --git a/repromon_app/tests/router/test_api_v1.py b/repromon_app/tests/router/test_api_v1.py new file mode 100644 index 0000000..2b28b16 --- /dev/null +++ b/repromon_app/tests/router/test_api_v1.py @@ -0,0 +1,20 @@ +import logging + +from fastapi.testclient import TestClient + +logger = logging.getLogger(__name__) +logger.debug(f"name={__name__}") + + +def test_feedback_get_devices( + test_client: TestClient, + oauth2_tester1_headers +): + response = test_client.get( + "/api/1/feedback/get_devices", + headers=oauth2_tester1_headers) + assert response.status_code == 200 + data = response.json() + assert isinstance(data, list) + assert len(data) > 0 + assert data[0]["id"] == 1