1
1
import pytest
2
2
from typing import Generator
3
- from dotenv import load_dotenv
4
- from sqlmodel import create_engine , Session , select
3
+ from sqlmodel import create_engine , Session , select , SQLModel
5
4
from sqlalchemy import Engine
6
5
from fastapi .testclient import TestClient
7
- from utils .db import get_connection_url , set_up_db , tear_down_db , get_session
8
- from utils .models import User , PasswordResetToken , Organization , Role , UserPassword
6
+ from utils .db import get_session
7
+ from utils .models import User , PasswordResetToken , Organization , Role , Account , Permission
9
8
from utils .auth import get_password_hash , create_access_token , create_refresh_token
9
+ from utils .enums import ValidPermissions
10
10
from main import app
11
11
12
- load_dotenv ()
13
-
14
12
15
13
# Define a custom exception for test setup errors
16
14
class SetupError (Exception ):
@@ -26,9 +24,8 @@ def engine() -> Engine:
26
24
Create a new SQLModel engine for the test database.
27
25
Use an in-memory SQLite database for testing.
28
26
"""
29
- engine = create_engine (
30
- get_connection_url ()
31
- )
27
+ # Use in-memory SQLite for testing
28
+ engine = create_engine ("sqlite:///:memory:" )
32
29
return engine
33
30
34
31
@@ -38,9 +35,22 @@ def set_up_database(engine) -> Generator[None, None, None]:
38
35
Set up the test database before running the test suite.
39
36
Drop all tables and recreate them to ensure a clean state.
40
37
"""
41
- set_up_db (drop = True )
38
+ # Create all tables in the in-memory database
39
+ SQLModel .metadata .create_all (engine )
40
+
41
+ # Create permissions
42
+ with Session (engine ) as session :
43
+ # Check if permissions already exist
44
+ existing_permissions = session .exec (select (Permission )).all ()
45
+ if not existing_permissions :
46
+ # Create all permissions from the ValidPermissions enum
47
+ for permission in ValidPermissions :
48
+ session .add (Permission (name = permission ))
49
+ session .commit ()
50
+
42
51
yield
43
- tear_down_db ()
52
+ # Drop all tables
53
+ SQLModel .metadata .drop_all (engine )
44
54
45
55
46
56
@pytest .fixture
@@ -57,22 +67,37 @@ def clean_db(session: Session) -> None:
57
67
"""
58
68
Cleans up the database tables before each test.
59
69
"""
60
- for model in (PasswordResetToken , User , Role , Organization ):
70
+ # Don't delete permissions as they are required for tests
71
+ for model in (PasswordResetToken , User , Role , Organization , Account ):
61
72
for record in session .exec (select (model )).all ():
62
73
session .delete (record )
63
74
64
75
session .commit ()
65
76
66
77
67
78
@pytest .fixture ()
68
- def test_user (session : Session ) -> User :
79
+ def test_account (session : Session ) -> Account :
80
+ """
81
+ Creates a test account in the database.
82
+ """
83
+ account = Account (
84
+
85
+ hashed_password = get_password_hash ("Test123!@#" )
86
+ )
87
+ session .add (account )
88
+ session .commit ()
89
+ session .refresh (account )
90
+ return account
91
+
92
+
93
+ @pytest .fixture ()
94
+ def test_user (session : Session , test_account : Account ) -> User :
69
95
"""
70
96
Creates a test user in the database.
71
97
"""
72
98
user = User (
73
99
name = "Test User" ,
74
-
75
- password = UserPassword (hashed_password = get_password_hash ("Test123!@#" ))
100
+ account_id = test_account .id
76
101
)
77
102
session .add (user )
78
103
session .commit ()
@@ -95,7 +120,7 @@ def get_session_override():
95
120
96
121
97
122
@pytest .fixture ()
98
- def auth_client (session : Session , test_user : User ) -> Generator [TestClient , None , None ]:
123
+ def auth_client (session : Session , test_account : Account ) -> Generator [TestClient , None , None ]:
99
124
"""
100
125
Provides a TestClient instance with valid authentication tokens.
101
126
"""
@@ -106,8 +131,8 @@ def get_session_override():
106
131
client = TestClient (app )
107
132
108
133
# Create and set valid tokens
109
- access_token = create_access_token ({"sub" : test_user .email })
110
- refresh_token = create_refresh_token ({"sub" : test_user .email })
134
+ access_token = create_access_token ({"sub" : test_account .email })
135
+ refresh_token = create_refresh_token ({"sub" : test_account .email })
111
136
112
137
client .cookies .set ("access_token" , access_token )
113
138
client .cookies .set ("refresh_token" , refresh_token )
0 commit comments