selfprivacy-rest-api/tests/conftest.py

82 lines
1.9 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 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-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
2022-01-18 15:20:47 +00:00
def app():
2022-07-05 12:54:21 +00:00
"""Flask application."""
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):
2022-07-05 12:54:21 +00:00
"""Flask unauthorized test client."""
return app.test_client()
2021-11-30 21:53:39 +00:00
class AuthorizedClient(testing.FlaskClient):
2022-07-05 12:54:21 +00:00
"""Flask authorized test client."""
2022-07-07 13:53:19 +00:00
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):
2022-07-05 12:54:21 +00:00
"""Flask client with wrong token"""
2022-07-07 13:53:19 +00:00
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):
2022-07-05 12:54:21 +00:00
"""Authorized test client fixture."""
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):
2022-07-05 12:54:21 +00:00
"""Wrong token test client fixture."""
app.test_client_class = WrongAuthClient
return app.test_client()
@pytest.fixture
2022-01-18 15:20:47 +00:00
def runner(app, tokens_file):
2022-07-05 12:54:21 +00:00
"""Flask test runner."""
2021-11-30 21:53:39 +00:00
return app.test_cli_runner()