-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathconftest.py
More file actions
107 lines (76 loc) · 2.61 KB
/
conftest.py
File metadata and controls
107 lines (76 loc) · 2.61 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""Special pytest fixture configuration file.
This file automatically provides all fixtures defined in it to all
pytest tests in this directory and sub directories.
See https://docs.pytest.org/en/6.2.x/fixture.html#conftest-py-sharing-fixtures-across-multiple-files
"""
import pytest
import pytest_asyncio
import importlib
from typing import Optional
import logging
from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import text
from fastapi import FastAPI, Depends
from fastapi.testclient import TestClient
from pathlib import Path
from arxiv.cloud_auth.userstore import UserStore, UserStoreDB
from arxiv.cloud_auth.domain import User
import arxiv.cloud_auth.fastapi.auth as auth
DB_FILE = "./pytest.db"
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_FILE}"
CONNECT_ARGS = (
{"check_same_thread": False} if "sqlite" in SQLALCHEMY_DATABASE_URL else {}
)
DELETE_DB_FILE_ON_EXIT = True
def escape_bind(stmt):
return stmt.replace(r":0", "\:0")
@pytest.fixture(scope="session")
def engine():
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args=CONNECT_ARGS)
return engine
@pytest.fixture(scope="session")
def user_db(engine):
print("Making tables...")
try:
import arxiv.cloud_auth.userstore_test_tables as tables
tables.metadata.create_all(bind=engine)
print("Done making tables. Starting load of data.")
tables.load_test_data(engine)
print("Done loading test data.")
yield engine
finally: # cleanup
if DELETE_DB_FILE_ON_EXIT:
Path(DB_FILE).unlink(missing_ok=True)
print(f"Cleaning up: Deleted test db file at {DB_FILE}.")
@pytest.fixture(scope="session")
def get_test_db(user_db):
# See https://fastapi.tiangolo.com/advanced/testing-database
engine = user_db
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def override_get_db():
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
return override_get_db
@pytest.fixture
def secret():
return "testing_secret"
@pytest.fixture
def userstore(get_test_db):
_userstore = UserStoreDB()
return UserStore(_userstore, get_test_db)
@pytest.fixture
def api_auth(userstore, secret):
return auth.AuthorizedUser(secret, "testingAudience", userstore)
@pytest_asyncio.fixture
async def fastapi(api_auth):
"""Returns a fast-api app"""
app = FastAPI()
@app.get("/")
async def root(user: Optional[User] = Depends(api_auth)) -> str:
return user
client = TestClient(app)
return client