forked from pgorecki/python-ddd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
63 lines (48 loc) · 1.48 KB
/
Copy pathconftest.py
File metadata and controls
63 lines (48 loc) · 1.48 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
import uuid
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from api.main import app as fastapi_instance
from config.api_config import ApiConfig
from modules.iam.application.services import IamService
from seedwork.infrastructure.database import Base
@pytest.fixture
def engine():
config = ApiConfig()
eng = create_engine(config.DATABASE_URL, echo=config.DATABASE_ECHO)
with eng.begin() as connection:
Base.metadata.drop_all(connection)
Base.metadata.create_all(connection)
return eng
@pytest.fixture
def db_session(engine):
with Session(engine) as session:
yield session
@pytest.fixture
def api():
return fastapi_instance
@pytest.fixture
def api_client(api, app):
client = TestClient(api)
return client
@pytest.fixture
def authenticated_api_client(api, app):
access_token = uuid.uuid4()
with app.transaction_context() as ctx:
iam: IamService = ctx[IamService]
current_user = iam.create_user(
uuid.UUID(int=1),
email="user1@example.com",
password="password",
access_token=str(access_token),
is_superuser=False,
)
headers = {"Authorization": f"bearer {access_token}"}
client = TestClient(api, headers=headers)
client.current_user = current_user
return client
@pytest.fixture
def app(api, db_session):
app = api.container.application()
return app