@@ -86,9 +86,9 @@ def test_invalid_token_type():
86
86
# --- API Endpoint Tests ---
87
87
88
88
89
- def test_register_endpoint (client : TestClient , session : Session ):
90
- response = client .post (
91
- "/auth/ register" ,
89
+ def test_register_endpoint (unauth_client : TestClient , session : Session ):
90
+ response = unauth_client .post (
91
+ app . url_path_for ( " register") ,
92
92
data = {
93
93
"name" : "New User" ,
94
94
@@ -107,9 +107,9 @@ def test_register_endpoint(client: TestClient, session: Session):
107
107
assert verify_password ("NewPass123!@#" , user .hashed_password )
108
108
109
109
110
- def test_login_endpoint (client : TestClient , test_user : User ):
111
- response = client .post (
112
- "/auth/ login" ,
110
+ def test_login_endpoint (unauth_client : TestClient , test_user : User ):
111
+ response = unauth_client .post (
112
+ app . url_path_for ( " login") ,
113
113
data = {
114
114
"email" : test_user .email ,
115
115
"password" : "Test123!@#"
@@ -124,18 +124,18 @@ def test_login_endpoint(client: TestClient, test_user: User):
124
124
assert "refresh_token" in cookies
125
125
126
126
127
- def test_refresh_token_endpoint (client : TestClient , test_user : User ):
128
- # Create expired access token and valid refresh token
129
- access_token = create_access_token (
127
+ def test_refresh_token_endpoint (auth_client : TestClient , test_user : User ):
128
+ # Override just the access token to be expired, keeping the valid refresh token
129
+ expired_access_token = create_access_token (
130
130
{"sub" : test_user .email },
131
131
timedelta (minutes = - 10 )
132
132
)
133
- refresh_token = create_refresh_token ({ "sub" : test_user . email } )
133
+ auth_client . cookies . set ( "access_token" , expired_access_token )
134
134
135
- client . cookies . set ( "access_token" , access_token )
136
- client . cookies . set ("refresh_token" , refresh_token )
137
-
138
- response = client . post ( "/auth/refresh" , follow_redirects = False )
135
+ response = auth_client . post (
136
+ app . url_path_for ("refresh_token" ),
137
+ follow_redirects = False
138
+ )
139
139
assert response .status_code == 303
140
140
141
141
# Check for new tokens in headers
@@ -155,10 +155,10 @@ def test_refresh_token_endpoint(client: TestClient, test_user: User):
155
155
assert decoded ["sub" ] == test_user .email
156
156
157
157
158
- def test_password_reset_flow (client : TestClient , session : Session , test_user : User , mock_resend_send ):
158
+ def test_password_reset_flow (unauth_client : TestClient , session : Session , test_user : User , mock_resend_send ):
159
159
# Test forgot password request
160
- response = client .post (
161
- "/auth/ forgot_password" ,
160
+ response = unauth_client .post (
161
+ app . url_path_for ( " forgot_password") ,
162
162
data = {"email" : test_user .email },
163
163
follow_redirects = False
164
164
)
@@ -188,8 +188,8 @@ def test_password_reset_flow(client: TestClient, session: Session, test_user: Us
188
188
assert not reset_token .used
189
189
190
190
# Test password reset
191
- response = client .post (
192
- "/auth/ reset_password" ,
191
+ response = unauth_client .post (
192
+ app . url_path_for ( " reset_password") ,
193
193
data = {
194
194
"email" : test_user .email ,
195
195
"token" : reset_token .token ,
@@ -207,12 +207,11 @@ def test_password_reset_flow(client: TestClient, session: Session, test_user: Us
207
207
assert reset_token .used
208
208
209
209
210
- def test_logout_endpoint (client : TestClient ):
211
- # First set some cookies
212
- client .cookies .set ("access_token" , "some_access_token" )
213
- client .cookies .set ("refresh_token" , "some_refresh_token" )
214
-
215
- response = client .get ("/auth/logout" , follow_redirects = False )
210
+ def test_logout_endpoint (auth_client : TestClient ):
211
+ response = auth_client .get (
212
+ app .url_path_for ("logout" ),
213
+ follow_redirects = False
214
+ )
216
215
assert response .status_code == 303
217
216
218
217
# Check for cookie deletion in headers
@@ -226,9 +225,9 @@ def test_logout_endpoint(client: TestClient):
226
225
# --- Error Case Tests ---
227
226
228
227
229
- def test_register_with_existing_email (client : TestClient , test_user : User ):
230
- response = client .post (
231
- "/auth/ register" ,
228
+ def test_register_with_existing_email (unauth_client : TestClient , test_user : User ):
229
+ response = unauth_client .post (
230
+ app . url_path_for ( " register") ,
232
231
data = {
233
232
"name" : "Another User" ,
234
233
"email" : test_user .email ,
@@ -239,9 +238,9 @@ def test_register_with_existing_email(client: TestClient, test_user: User):
239
238
assert response .status_code == 400
240
239
241
240
242
- def test_login_with_invalid_credentials (client : TestClient , test_user : User ):
243
- response = client .post (
244
- "/auth/ login" ,
241
+ def test_login_with_invalid_credentials (unauth_client : TestClient , test_user : User ):
242
+ response = unauth_client .post (
243
+ app . url_path_for ( " login") ,
245
244
data = {
246
245
"email" : test_user .email ,
247
246
"password" : "WrongPass123!@#"
@@ -250,9 +249,9 @@ def test_login_with_invalid_credentials(client: TestClient, test_user: User):
250
249
assert response .status_code == 400
251
250
252
251
253
- def test_password_reset_with_invalid_token (client : TestClient , test_user : User ):
254
- response = client .post (
255
- "/auth/ reset_password" ,
252
+ def test_password_reset_with_invalid_token (unauth_client : TestClient , test_user : User ):
253
+ response = unauth_client .post (
254
+ app . url_path_for ( " reset_password") ,
256
255
data = {
257
256
"email" : test_user .email ,
258
257
"token" : "invalid_token" ,
@@ -263,7 +262,7 @@ def test_password_reset_with_invalid_token(client: TestClient, test_user: User):
263
262
assert response .status_code == 400
264
263
265
264
266
- def test_password_reset_url_generation (client : TestClient ):
265
+ def test_password_reset_url_generation (unauth_client : TestClient ):
267
266
"""
268
267
Tests that the password reset URL is correctly formatted and contains
269
268
the required query parameters.
@@ -290,12 +289,12 @@ def test_password_reset_url_generation(client: TestClient):
290
289
assert query_params ["token" ][0 ] == test_token
291
290
292
291
293
- def test_password_reset_email_url (client : TestClient , session : Session , test_user : User , mock_resend_send ):
292
+ def test_password_reset_email_url (unauth_client : TestClient , session : Session , test_user : User , mock_resend_send ):
294
293
"""
295
294
Tests that the password reset email contains a properly formatted reset URL.
296
295
"""
297
- response = client .post (
298
- "/auth/ forgot_password" ,
296
+ response = unauth_client .post (
297
+ app . url_path_for ( " forgot_password") ,
299
298
data = {"email" : test_user .email },
300
299
follow_redirects = False
301
300
)
0 commit comments