|
|
@ -6,6 +6,8 @@ import tempfile |
|
|
|
from typing import Tuple, Any |
|
|
|
|
|
|
|
import pytest |
|
|
|
from flask import Flask |
|
|
|
from flask.testing import FlaskClient, FlaskCliRunner |
|
|
|
from werkzeug.test import Client |
|
|
|
|
|
|
|
from atheneum import create_app, init_db, register_blueprints |
|
|
@ -23,7 +25,7 @@ def add_test_user() -> Tuple[str, str]: |
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
def app(): |
|
|
|
def app() -> Flask: |
|
|
|
"""Create and configure a new app instance for each test.""" |
|
|
|
# create a temporary file to isolate the database for each test |
|
|
|
db_fd, db_path = tempfile.mkstemp() |
|
|
@ -50,30 +52,33 @@ def app(): |
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
def client(app): |
|
|
|
def client(app: Flask) -> FlaskClient: |
|
|
|
"""A test client for the app.""" |
|
|
|
return app.test_client() |
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
def runner(app): |
|
|
|
def runner(app: Flask) -> FlaskCliRunner: |
|
|
|
"""A test runner for the app's Click commands.""" |
|
|
|
return app.test_cli_runner() |
|
|
|
|
|
|
|
|
|
|
|
class AuthActions(object): |
|
|
|
def __init__(self, client: Client, username: str = "", password: str = ""): |
|
|
|
def __init__(self, |
|
|
|
client: Client, |
|
|
|
username: str = "", |
|
|
|
password: str = "") -> None: |
|
|
|
self._client = client |
|
|
|
self.username: str = username |
|
|
|
self.password: str = password |
|
|
|
self.token: str = "" |
|
|
|
|
|
|
|
def configure(self, username, password) -> Any: |
|
|
|
def configure(self, username: str, password: str) -> Any: |
|
|
|
self.username = username |
|
|
|
self.password = password |
|
|
|
return self |
|
|
|
|
|
|
|
def login(self): |
|
|
|
def login(self) -> Any: |
|
|
|
auth_header = self.get_authorization_header_basic() |
|
|
|
result = self._client.post( |
|
|
|
'/auth/login', |
|
|
@ -84,7 +89,7 @@ class AuthActions(object): |
|
|
|
self.token = result.json['token'] |
|
|
|
return result |
|
|
|
|
|
|
|
def bump(self): |
|
|
|
def bump(self) -> Any: |
|
|
|
auth_header = self.get_authorization_header_token() |
|
|
|
return self._client.post( |
|
|
|
'/auth/bump', |
|
|
@ -93,7 +98,7 @@ class AuthActions(object): |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
def logout(self): |
|
|
|
def logout(self) -> Any: |
|
|
|
auth_header = self.get_authorization_header_token() |
|
|
|
return self._client.post( |
|
|
|
'/auth/logout', |
|
|
@ -116,7 +121,7 @@ class AuthActions(object): |
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
def auth(client: Client): |
|
|
|
def auth(client: Client) -> AuthActions: |
|
|
|
return AuthActions(client, |
|
|
|
client.application.config.get('test_username'), |
|
|
|
client.application.config.get('test_password')) |