-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconftest.py
More file actions
91 lines (65 loc) · 2.12 KB
/
conftest.py
File metadata and controls
91 lines (65 loc) · 2.12 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
from datetime import datetime
import pytest
import pytz
from fastapi.testclient import TestClient
os.environ["CONFIG_PATH"] = "test_config.toml"
@pytest.fixture
def config():
from pingpong.config import config
return config
@pytest.fixture
async def db(config):
from pingpong.models import Base
await config.db.driver.init(Base, drop_first=True)
yield config.db.driver
@pytest.fixture
def now(request):
default_now = datetime(2024, 1, 1, 0, 0, 0)
dt = getattr(request, "param", default_now)
# Make sure to use a timezone-aware datetime. By default, the timezone is UTC.
# If we don't do this, the tests will fail when run in a different timezone.
if not dt.tzinfo:
dt = pytz.utc.localize(dt)
return lambda: dt
@pytest.fixture
async def authz(request, config):
from pingpong.authz.mock import MockFgaAuthzServer
params = getattr(request, "param", None)
async with MockFgaAuthzServer(config.authz.driver, params) as server:
yield server
@pytest.fixture
async def api(config, db, user, now, authz):
from pingpong.server import app, v1
api = TestClient(app)
api.app.state["now"] = now
v1.state["now"] = now
await config.authz.driver.init()
yield api
@pytest.fixture
async def user(request, config, db):
if not hasattr(request, "param"):
yield None
else:
from pingpong.models import User
async with db.async_session() as session:
u = User(**request.param)
session.add(u)
await session.commit()
yield u
@pytest.fixture
async def institution(request, config, db):
if not hasattr(request, "param"):
yield None
else:
from pingpong.models import Institution
async with db.async_session() as session:
i = Institution(**request.param)
session.add(i)
await session.commit()
yield i
@pytest.fixture
async def valid_user_token(user, now):
from pingpong.auth import encode_session_token
from pingpong.now import offset
return encode_session_token(user.id, nowfn=offset(now, seconds=-60))