selfprivacy-rest-api/tests/conftest.py

83 lines
2 KiB
Python
Raw Normal View History

2022-07-05 12:54:21 +00:00
"""Tests configuration."""
# pylint: disable=redefined-outer-name
# pylint: disable=unused-argument
import os
import pytest
2023-03-10 14:14:41 +00:00
from os import path
from fastapi.testclient import TestClient
def pytest_generate_tests(metafunc):
os.environ["TEST_MODE"] = "true"
2022-01-24 20:01:37 +00:00
2023-03-10 14:14:41 +00:00
def global_data_dir():
return path.join(path.dirname(__file__), "data")
2022-01-18 15:20:47 +00:00
@pytest.fixture
def tokens_file(mocker, shared_datadir):
2022-07-05 12:54:21 +00:00
"""Mock tokens file."""
2022-01-24 20:01:37 +00:00
mock = mocker.patch(
"selfprivacy_api.utils.TOKENS_FILE", shared_datadir / "tokens.json"
)
2022-01-18 15:20:47 +00:00
return mock
2022-01-24 20:01:37 +00:00
@pytest.fixture
def jobs_file(mocker, shared_datadir):
"""Mock tokens file."""
mock = mocker.patch("selfprivacy_api.utils.JOBS_FILE", shared_datadir / "jobs.json")
return mock
2023-03-10 14:14:41 +00:00
@pytest.fixture
def generic_userdata(mocker, tmpdir):
filename = "turned_on.json"
source_path = path.join(global_data_dir(), filename)
userdata_path = path.join(tmpdir, filename)
with open(userdata_path, "w") as file:
with open(source_path, "r") as source:
file.write(source.read())
mock = mocker.patch("selfprivacy_api.utils.USERDATA_FILE", new=userdata_path)
return mock
@pytest.fixture
def huey_database(mocker, shared_datadir):
"""Mock huey database."""
mock = mocker.patch(
"selfprivacy_api.utils.huey.HUEY_DATABASE", shared_datadir / "huey.db"
)
return mock
2021-11-30 21:53:39 +00:00
2022-07-07 13:53:19 +00:00
@pytest.fixture
def client(tokens_file, huey_database, jobs_file):
from selfprivacy_api.app import app
return TestClient(app)
@pytest.fixture
def authorized_client(tokens_file, huey_database, jobs_file):
2022-07-05 12:54:21 +00:00
"""Authorized test client fixture."""
from selfprivacy_api.app import app
client = TestClient(app)
client.headers.update({"Authorization": "Bearer TEST_TOKEN"})
return client
2021-11-30 21:53:39 +00:00
@pytest.fixture
def wrong_auth_client(tokens_file, huey_database, jobs_file):
2022-07-05 12:54:21 +00:00
"""Wrong token test client fixture."""
from selfprivacy_api.app import app
client = TestClient(app)
client.headers.update({"Authorization": "Bearer WRONG_TOKEN"})
return client