summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-api/conftest.py
blob: 958a5894ada13188e2a29643c46c1b19f121ce69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Configuration file for all unit tests.
"""

from functools import wraps
import pytest
from flask import _request_ctx_stack, g
from opendc.database import Database


def requires_auth_mock(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        _request_ctx_stack.top.current_user = {'sub': 'test'}
        return f(*args, **kwargs)
    return decorated_function


def requires_scope_mock(required_scope):
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            return f(*args, **kwargs)
        return decorated_function
    return decorator


@pytest.fixture
def client():
    """Returns a Flask API client to interact with."""

    # Disable authorization for test API endpoints
    from opendc import exts
    exts.requires_auth = requires_auth_mock
    exts.requires_scope = requires_scope_mock
    exts.has_scope = lambda x: False

    from app import create_app

    app = create_app(testing=True)

    with app.app_context():
        g.db = Database()
        with app.test_client() as client:
            yield client