argos/tests/test_api.py
Alexis Métaireau 43f8aabb2c Refactor server codebase for testing.
- Restructured server module to separate the application creation and configuration.
- Moved code dealing with SQLAlchemy database
  setup and teardown to the main application file.
- Moved functions related to configuration file loading to `argos.server.settings`.
- Fixed SQLAchemy expressions in `argos.server.queries`.
- Implemented a more granular system of setting checks' schedule on the server.
- Introduced frequency scheduling on per-website basis in the YAML config.
- Introduced Pytest fixtures for handling test database and authorized HTTP client in `tests/conftest.py`.
- Included a first test for the api
- Implemented changes to models to accommodate changes to task scheduling.
- Fixed errors concerning database concurrency arising from changes to the application setup.
2023-10-11 23:52:33 +02:00

28 lines
755 B
Python

import pytest
from fastapi.testclient import TestClient
from argos.server import app, models
@pytest.fixture()
def test_db():
models.Base.metadata.create_all(bind=app.engine)
yield
models.Base.metadata.drop_all(bind=app.engine)
def test_read_tasks_requires_auth():
with TestClient(app) as client:
response = client.get("/tasks")
assert response.status_code == 403
def test_read_tasks_returns_tasks():
with TestClient(app) as client:
token = app.state.config.service.secrets[0]
client.headers = {"Authorization": f"Bearer {token}"}
response = client.get("/tasks")
assert response.status_code == 200
# We should have only two tasks
assert len(response.json()) == 2