selfprivacy-rest-api/tests/conftest.py

82 lines
1.9 KiB
Python
Raw Normal View History

2022-07-05 15:54:21 +03: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 22:01:37 +02:00
2022-01-18 17:20:47 +02:00
@pytest.fixture
def tokens_file(mocker, shared_datadir):
2022-07-05 15:54:21 +03:00
"""Mock tokens file."""
2022-01-24 22:01:37 +02:00
mock = mocker.patch(
"selfprivacy_api.utils.TOKENS_FILE", shared_datadir / "tokens.json"
)
2022-01-18 17:20:47 +02:00
return mock
2022-01-24 22:01:37 +02:00
@pytest.fixture
2022-01-18 17:20:47 +02:00
def app():
2022-07-05 15:54:21 +03:00
"""Flask application."""
2021-11-30 23:53:39 +02:00
app = create_app(
{
2022-01-17 13:29:54 +02:00
"ENABLE_SWAGGER": "1",
2021-11-30 23:53:39 +02:00
}
)
yield app
@pytest.fixture
2022-01-18 17:20:47 +02:00
def client(app, tokens_file):
2022-07-05 15:54:21 +03:00
"""Flask unauthorized test client."""
return app.test_client()
2021-11-30 23:53:39 +02:00
class AuthorizedClient(testing.FlaskClient):
2022-07-05 15:54:21 +03:00
"""Flask authorized test client."""
2022-07-07 16:53:19 +03: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 23:53:39 +02:00
class WrongAuthClient(testing.FlaskClient):
2022-07-05 15:54:21 +03:00
"""Flask client with wrong token"""
2022-07-07 16:53:19 +03: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 17:20:47 +02:00
def authorized_client(app, tokens_file):
2022-07-05 15:54:21 +03:00
"""Authorized test client fixture."""
app.test_client_class = AuthorizedClient
return app.test_client()
2021-11-30 23:53:39 +02:00
@pytest.fixture
2022-01-18 17:20:47 +02:00
def wrong_auth_client(app, tokens_file):
2022-07-05 15:54:21 +03:00
"""Wrong token test client fixture."""
app.test_client_class = WrongAuthClient
return app.test_client()
@pytest.fixture
2022-01-18 17:20:47 +02:00
def runner(app, tokens_file):
2022-07-05 15:54:21 +03:00
"""Flask test runner."""
2021-11-30 23:53:39 +02:00
return app.test_cli_runner()