selfprivacy-rest-api/tests/conftest.py

69 lines
1.5 KiB
Python
Raw Normal View History

import pytest
from flask import testing
from selfprivacy_api.app import create_app
2022-01-24 20:01:37 +00:00
2022-01-18 15:20:47 +00:00
@pytest.fixture
def tokens_file(mocker, shared_datadir):
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
2022-01-18 15:20:47 +00:00
def app():
2021-11-30 21:53:39 +00:00
app = create_app(
{
2022-01-17 11:29:54 +00:00
"ENABLE_SWAGGER": "1",
2021-11-30 21:53:39 +00:00
}
)
yield app
@pytest.fixture
2022-01-18 15:20:47 +00:00
def client(app, tokens_file):
return app.test_client()
2021-11-30 21:53:39 +00:00
class AuthorizedClient(testing.FlaskClient):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.token = "TEST_TOKEN"
def open(self, *args, **kwargs):
if "headers" not in kwargs:
kwargs["headers"] = {}
kwargs["headers"]["Authorization"] = f"Bearer {self.token}"
return super().open(*args, **kwargs)
2021-11-30 21:53:39 +00:00
class WrongAuthClient(testing.FlaskClient):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.token = "WRONG_TOKEN"
def open(self, *args, **kwargs):
if "headers" not in kwargs:
kwargs["headers"] = {}
kwargs["headers"]["Authorization"] = f"Bearer {self.token}"
return super().open(*args, **kwargs)
@pytest.fixture
2022-01-18 15:20:47 +00:00
def authorized_client(app, tokens_file):
app.test_client_class = AuthorizedClient
return app.test_client()
2021-11-30 21:53:39 +00:00
@pytest.fixture
2022-01-18 15:20:47 +00:00
def wrong_auth_client(app, tokens_file):
app.test_client_class = WrongAuthClient
return app.test_client()
@pytest.fixture
2022-01-18 15:20:47 +00:00
def runner(app, tokens_file):
2021-11-30 21:53:39 +00:00
return app.test_cli_runner()