2022-07-05 15:54:21 +03:00
|
|
|
"""Tests configuration."""
|
|
|
|
# pylint: disable=redefined-outer-name
|
|
|
|
# pylint: disable=unused-argument
|
2021-11-29 22:16:08 +03:00
|
|
|
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
|
2021-11-29 22:16:08 +03:00
|
|
|
|
2022-01-24 22:01:37 +02:00
|
|
|
|
2021-11-29 22:16:08 +03:00
|
|
|
@pytest.fixture
|
2022-01-18 17:20:47 +02:00
|
|
|
def app():
|
2022-07-05 15:54:21 +03:00
|
|
|
"""Flask application."""
|
2021-12-01 00:53:39 +03:00
|
|
|
app = create_app(
|
|
|
|
{
|
2022-01-17 13:29:54 +02:00
|
|
|
"ENABLE_SWAGGER": "1",
|
2021-12-01 00:53:39 +03:00
|
|
|
}
|
|
|
|
)
|
2021-11-29 22:16:08 +03: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."""
|
2021-11-29 22:16:08 +03:00
|
|
|
return app.test_client()
|
|
|
|
|
2021-12-01 00:53:39 +03:00
|
|
|
|
2021-11-29 22:16:08 +03:00
|
|
|
class AuthorizedClient(testing.FlaskClient):
|
2022-07-05 15:54:21 +03:00
|
|
|
"""Flask authorized test client."""
|
2022-07-07 15:53:19 +02:00
|
|
|
|
2021-11-29 22:16:08 +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-12-01 00:53:39 +03:00
|
|
|
|
2022-01-10 22:35:00 +02:00
|
|
|
class WrongAuthClient(testing.FlaskClient):
|
2022-07-05 15:54:21 +03:00
|
|
|
"""Flask client with wrong token"""
|
2022-07-07 15:53:19 +02:00
|
|
|
|
2022-01-10 22:35:00 +02: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)
|
|
|
|
|
|
|
|
|
2021-11-29 22:16:08 +03:00
|
|
|
@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."""
|
2021-11-29 22:16:08 +03:00
|
|
|
app.test_client_class = AuthorizedClient
|
|
|
|
return app.test_client()
|
|
|
|
|
2021-12-01 00:53:39 +03:00
|
|
|
|
2022-01-10 22:35:00 +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."""
|
2022-01-10 22:35:00 +02:00
|
|
|
app.test_client_class = WrongAuthClient
|
|
|
|
return app.test_client()
|
|
|
|
|
|
|
|
|
2021-11-29 22:16:08 +03:00
|
|
|
@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-12-01 00:53:39 +03:00
|
|
|
return app.test_cli_runner()
|