Skip to content

Commit

Permalink
Testing with pytest, router/api_v1 package tests, work in progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmdocua committed Sep 28, 2023
1 parent 3bc9dd4 commit fd8371b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
36 changes: 36 additions & 0 deletions repromon_app/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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


Expand Down Expand Up @@ -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
20 changes: 20 additions & 0 deletions repromon_app/tests/router/test_api_v1.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fd8371b

Please sign in to comment.