From 2a96a0f7a24883d679057f8188de1bc1260064d2 Mon Sep 17 00:00:00 2001 From: Dung Nguyen Date: Thu, 12 Sep 2024 22:51:51 +0700 Subject: [PATCH] chore: fix test --- tests/test_api.py | 28 ---------------------------- tests/test_web.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 28 deletions(-) delete mode 100644 tests/test_api.py create mode 100644 tests/test_web.py diff --git a/tests/test_api.py b/tests/test_api.py deleted file mode 100644 index 5af9a88..0000000 --- a/tests/test_api.py +++ /dev/null @@ -1,28 +0,0 @@ -from web import app as flask_app -# from web import asgi_app - -API_TOKEN = 'TEST_TOKEN' -flask_app.config.update({ - "SERVER_AUTH_KEY": API_TOKEN, -}) - - -def test_index_route(): - response = flask_app.test_client().get('/') - - assert response.status_code == 200 - assert response.data.decode('utf-8') == 'Hello world' - - -def test_protected_route(monkeypatch): - headers = {'X-Api-Key': API_TOKEN} - response = flask_app.test_client().get('/protected', headers=headers) - - assert response.status_code == 200 - assert response.data.decode('utf-8') == 'protected' - - -def test_protected_route_unauthenticated(monkeypatch): - response = flask_app.test_client().get('/protected') - - assert response.status_code == 401 diff --git a/tests/test_web.py b/tests/test_web.py new file mode 100644 index 0000000..6c41d96 --- /dev/null +++ b/tests/test_web.py @@ -0,0 +1,37 @@ +import sys +import os +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +import pytest + +from web import app as flask_app +# from web import asgi_app + +API_TOKEN = 'TEST_TOKEN' +flask_app.config.update({"SERVER_AUTH_KEY": API_TOKEN}) + +@pytest.fixture +def client(): + """Set up a test client for the app with setup and teardown logic.""" + with flask_app.test_client() as client: + yield client + +def test_index_route(client): + response = client.get('/') + + assert response.status_code == 200 + assert response.data.decode('utf-8') == 'Hello world' + + +def test_protected_route(client, monkeypatch): + headers = {'X-Api-Key': API_TOKEN} + response = client.get('/protected', headers=headers) + + assert response.status_code == 200 + assert response.data.decode('utf-8') == 'protected' + + +def test_protected_route_unauthenticated(client, monkeypatch): + response = client.get('/protected') + + assert response.status_code == 401