2022-07-05 12:54:21 +00:00
|
|
|
"""Tests configuration."""
|
|
|
|
# pylint: disable=redefined-outer-name
|
|
|
|
# pylint: disable=unused-argument
|
2022-08-25 17:03:56 +00:00
|
|
|
import os
|
2021-11-29 19:16:08 +00:00
|
|
|
import pytest
|
2022-08-25 17:03:56 +00:00
|
|
|
from fastapi.testclient import TestClient
|
2022-12-28 12:44:51 +00:00
|
|
|
import os.path as path
|
2023-01-02 17:22:18 +00:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from selfprivacy_api.models.tokens.token import Token
|
|
|
|
from selfprivacy_api.repositories.tokens.json_tokens_repository import (
|
|
|
|
JsonTokensRepository,
|
|
|
|
)
|
|
|
|
|
|
|
|
from tests.common import read_json
|
|
|
|
|
|
|
|
EMPTY_TOKENS_JSON = ' {"tokens": []}'
|
|
|
|
|
|
|
|
|
|
|
|
TOKENS_FILE_CONTENTS = {
|
|
|
|
"tokens": [
|
|
|
|
{
|
|
|
|
"token": "TEST_TOKEN",
|
|
|
|
"name": "test_token",
|
|
|
|
"date": datetime.datetime(2022, 1, 14, 8, 31, 10, 789314),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"token": "TEST_TOKEN2",
|
|
|
|
"name": "test_token2",
|
|
|
|
"date": datetime.datetime(2022, 1, 14, 8, 31, 10, 789314),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2023-01-04 12:31:24 +00:00
|
|
|
DEVICE_WE_AUTH_TESTS_WITH = TOKENS_FILE_CONTENTS["tokens"][0]
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
os.environ["TEST_MODE"] = "true"
|
2021-11-29 19:16:08 +00:00
|
|
|
|
2022-01-24 20:01:37 +00:00
|
|
|
|
2022-12-28 12:44:51 +00:00
|
|
|
def global_data_dir():
|
|
|
|
return path.join(path.dirname(__file__), "data")
|
|
|
|
|
|
|
|
|
2022-01-18 15:20:47 +00:00
|
|
|
@pytest.fixture
|
2023-01-02 17:22:18 +00:00
|
|
|
def empty_tokens(mocker, tmpdir):
|
|
|
|
tokenfile = tmpdir / "empty_tokens.json"
|
|
|
|
with open(tokenfile, "w") as file:
|
|
|
|
file.write(EMPTY_TOKENS_JSON)
|
|
|
|
mocker.patch("selfprivacy_api.utils.TOKENS_FILE", new=tokenfile)
|
|
|
|
assert read_json(tokenfile)["tokens"] == []
|
|
|
|
return tmpdir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def empty_json_repo(empty_tokens):
|
|
|
|
repo = JsonTokensRepository()
|
|
|
|
for token in repo.get_tokens():
|
|
|
|
repo.delete_token(token)
|
|
|
|
assert repo.get_tokens() == []
|
|
|
|
return repo
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def tokens_file(empty_json_repo, tmpdir):
|
|
|
|
"""A state with tokens"""
|
|
|
|
for token in TOKENS_FILE_CONTENTS["tokens"]:
|
|
|
|
empty_json_repo._store_token(
|
|
|
|
Token(
|
|
|
|
token=token["token"],
|
|
|
|
device_name=token["name"],
|
|
|
|
created_at=token["date"],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# temporary return for compatibility with older tests
|
|
|
|
|
|
|
|
tokenfile = tmpdir / "empty_tokens.json"
|
|
|
|
assert path.exists(tokenfile)
|
|
|
|
return tokenfile
|
2021-11-29 19:16:08 +00:00
|
|
|
|
2022-01-24 20:01:37 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
@pytest.fixture
|
2022-08-25 17:03:56 +00:00
|
|
|
def jobs_file(mocker, shared_datadir):
|
|
|
|
"""Mock tokens file."""
|
|
|
|
mock = mocker.patch("selfprivacy_api.utils.JOBS_FILE", shared_datadir / "jobs.json")
|
|
|
|
return mock
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2022-08-25 17:03:56 +00:00
|
|
|
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
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def client(tokens_file, huey_database, jobs_file):
|
|
|
|
from selfprivacy_api.app import app
|
2022-01-10 20:35:00 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
return TestClient(app)
|
2022-01-10 20:35:00 +00:00
|
|
|
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
@pytest.fixture
|
2022-08-25 17:03:56 +00:00
|
|
|
def authorized_client(tokens_file, huey_database, jobs_file):
|
2022-07-05 12:54:21 +00:00
|
|
|
"""Authorized test client fixture."""
|
2022-08-25 17:03:56 +00:00
|
|
|
from selfprivacy_api.app import app
|
|
|
|
|
|
|
|
client = TestClient(app)
|
2023-01-04 12:31:24 +00:00
|
|
|
client.headers.update(
|
|
|
|
{"Authorization": "Bearer " + DEVICE_WE_AUTH_TESTS_WITH["token"]}
|
|
|
|
)
|
2022-08-25 17:03:56 +00:00
|
|
|
return client
|
2021-11-29 19:16:08 +00:00
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
@pytest.fixture
|
2022-08-25 17:03:56 +00:00
|
|
|
def wrong_auth_client(tokens_file, huey_database, jobs_file):
|
2022-07-05 12:54:21 +00:00
|
|
|
"""Wrong token test client fixture."""
|
2022-08-25 17:03:56 +00:00
|
|
|
from selfprivacy_api.app import app
|
2022-01-10 20:35:00 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
client = TestClient(app)
|
|
|
|
client.headers.update({"Authorization": "Bearer WRONG_TOKEN"})
|
|
|
|
return client
|