Skip to content

Commit b09d636

Browse files
Merge pull request #116 from Promptly-Technologies-LLC/112-in-the-test-suite-use-the-appurl_path_for-method-for-testing-redirect-destinations
Use app.url_path_for instead of hardcoded paths in the test suite to make tests more robust and maintainable
2 parents 03ee2fe + 8109d7a commit b09d636

File tree

4 files changed

+72
-69
lines changed

4 files changed

+72
-69
lines changed

tests/routers/test_account.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def test_request_email_update_success(auth_client: TestClient, test_account: Acc
285285
)
286286

287287
assert response.status_code == 303
288-
assert "profile?email_update_requested=true" in response.headers["location"]
288+
assert f"{app.url_path_for('read_profile')}?email_update_requested=true" in response.headers["location"]
289289

290290
# Verify email was "sent"
291291
mock_resend_send.assert_called_once()
@@ -350,7 +350,7 @@ def test_confirm_email_update_success(unauth_client: TestClient, session: Sessio
350350
)
351351

352352
assert response.status_code == 303
353-
assert "profile?email_updated=true" in response.headers["location"]
353+
assert f"{app.url_path_for('read_profile')}?email_updated=true" in response.headers["location"]
354354

355355
# Verify email was updated
356356
session.refresh(test_account)

tests/routers/test_organization.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def test_create_organization_success(auth_client, session, test_user):
1010
"""Test successful organization creation"""
1111
response = auth_client.post(
12-
"/organizations/create",
12+
app.url_path_for("create_organization"),
1313
data={"name": "New Test Organization"},
1414
follow_redirects=False
1515
)
@@ -66,7 +66,7 @@ def test_create_organization_success(auth_client, session, test_user):
6666
def test_create_organization_empty_name(auth_client):
6767
"""Test organization creation with empty name"""
6868
response = auth_client.post(
69-
"/organizations/create",
69+
app.url_path_for("create_organization"),
7070
data={"name": " "},
7171
follow_redirects=True
7272
)
@@ -82,7 +82,7 @@ def test_create_organization_duplicate_name(auth_client, session, test_organizat
8282
org_count_before = len(session.exec(select(Organization)).all())
8383

8484
response = auth_client.post(
85-
"/organizations/create",
85+
app.url_path_for("create_organization"),
8686
data={"name": test_organization.name},
8787
follow_redirects=False
8888
)
@@ -105,7 +105,7 @@ def test_create_organization_duplicate_name(auth_client, session, test_organizat
105105
def test_create_organization_unauthenticated(unauth_client):
106106
"""Test organization creation without authentication"""
107107
response = unauth_client.post(
108-
"/organizations/create",
108+
app.url_path_for("create_organization"),
109109
data={"name": "Unauthorized Org"},
110110
follow_redirects=False
111111
)
@@ -130,7 +130,7 @@ def test_update_organization_success(
130130

131131
new_name = "Updated Organization Name"
132132
response = auth_client.post(
133-
f"/organizations/update/{test_organization.id}",
133+
app.url_path_for("update_organization", org_id=test_organization.id),
134134
data={"id": str(test_organization.id), "name": new_name},
135135
follow_redirects=False
136136
)
@@ -156,7 +156,7 @@ def test_update_organization_unauthorized(auth_client, session, test_organizatio
156156
session.commit()
157157

158158
response = auth_client.post(
159-
f"/organizations/update/{test_organization.id}",
159+
app.url_path_for("update_organization", org_id=test_organization.id),
160160
data={
161161
"id": test_organization.id,
162162
"name": "Unauthorized Update"
@@ -183,7 +183,7 @@ def test_update_organization_duplicate_name(auth_client, session, test_organizat
183183
session.commit()
184184

185185
response = auth_client.post(
186-
f"/organizations/update/{test_organization.id}",
186+
app.url_path_for("update_organization", org_id=test_organization.id),
187187
data={
188188
"id": test_organization.id,
189189
"name": "Existing Org"
@@ -206,7 +206,7 @@ def test_update_organization_empty_name(auth_client, session, test_organization,
206206
session.commit()
207207

208208
response = auth_client.post(
209-
f"/organizations/update/{test_organization.id}",
209+
app.url_path_for("update_organization", org_id=test_organization.id),
210210
data={
211211
"id": test_organization.id,
212212
"name": " "
@@ -221,7 +221,7 @@ def test_update_organization_empty_name(auth_client, session, test_organization,
221221
def test_update_organization_unauthenticated(unauth_client, test_organization):
222222
"""Test organization update without authentication"""
223223
response = unauth_client.post(
224-
f"/organizations/update/{test_organization.id}",
224+
app.url_path_for("update_organization", org_id=test_organization.id),
225225
data={
226226
"id": test_organization.id,
227227
"name": "Unauthorized Update"
@@ -246,12 +246,12 @@ def test_delete_organization_success(auth_client, session, test_organization, te
246246
session.commit()
247247

248248
response = auth_client.post(
249-
f"/organizations/delete/{org_id}",
249+
app.url_path_for("delete_organization", org_id=org_id),
250250
follow_redirects=False
251251
)
252252

253253
assert response.status_code == 303 # Redirect status code
254-
assert "/profile" in response.headers["location"]
254+
assert app.url_path_for("read_profile") in response.headers["location"]
255255

256256
# Expire all objects in the session to force a refresh from the database
257257
session.expire_all()
@@ -266,7 +266,7 @@ def test_delete_organization_unauthorized(auth_client_member, session, test_orga
266266
"""Test organization deletion without proper permissions"""
267267
# Use auth_client_member, who belongs to the org but has no delete permission
268268
response = auth_client_member.post(
269-
f"/organizations/delete/{test_organization.id}",
269+
app.url_path_for("delete_organization", org_id=test_organization.id),
270270
follow_redirects=False
271271
)
272272

@@ -280,7 +280,7 @@ def test_delete_organization_unauthorized(auth_client_member, session, test_orga
280280
def test_delete_organization_not_member(auth_client_non_member, session, test_organization):
281281
"""Test organization deletion by non-member"""
282282
response = auth_client_non_member.post(
283-
f"/organizations/delete/{test_organization.id}",
283+
app.url_path_for("delete_organization", org_id=test_organization.id),
284284
follow_redirects=False
285285
)
286286

@@ -294,7 +294,7 @@ def test_delete_organization_not_member(auth_client_non_member, session, test_or
294294
def test_delete_organization_unauthenticated(unauth_client, test_organization):
295295
"""Test organization deletion without authentication"""
296296
response = unauth_client.post(
297-
f"/organizations/delete/{test_organization.id}",
297+
app.url_path_for("delete_organization", org_id=test_organization.id),
298298
follow_redirects=False
299299
)
300300

@@ -320,7 +320,7 @@ def test_delete_organization_cascade(auth_client, session, test_organization, te
320320
session.commit()
321321

322322
response = auth_client.post(
323-
f"/organizations/delete/{org_id}",
323+
app.url_path_for("delete_organization", org_id=org_id),
324324
follow_redirects=False
325325
)
326326

@@ -341,7 +341,7 @@ def test_delete_organization_cascade(auth_client, session, test_organization, te
341341
def test_read_organization_as_owner(auth_client_owner, test_organization):
342342
"""Test accessing organization page as an owner"""
343343
response = auth_client_owner.get(
344-
f"/organizations/{test_organization.id}",
344+
app.url_path_for("read_organization", org_id=test_organization.id),
345345
follow_redirects=False
346346
)
347347

@@ -360,7 +360,7 @@ def test_read_organization_as_owner(auth_client_owner, test_organization):
360360
def test_read_organization_as_admin(auth_client_admin, test_organization):
361361
"""Test accessing organization page as an admin"""
362362
response = auth_client_admin.get(
363-
f"/organizations/{test_organization.id}",
363+
app.url_path_for("read_organization", org_id=test_organization.id),
364364
follow_redirects=False
365365
)
366366

@@ -379,7 +379,7 @@ def test_read_organization_as_admin(auth_client_admin, test_organization):
379379
def test_read_organization_as_member(auth_client_member, test_organization):
380380
"""Test accessing organization page as a regular member"""
381381
response = auth_client_member.get(
382-
f"/organizations/{test_organization.id}",
382+
app.url_path_for("read_organization", org_id=test_organization.id),
383383
follow_redirects=False
384384
)
385385

@@ -398,7 +398,7 @@ def test_read_organization_as_member(auth_client_member, test_organization):
398398
def test_read_organization_as_non_member(auth_client_non_member, test_organization):
399399
"""Test accessing organization page as a non-member"""
400400
response = auth_client_non_member.get(
401-
f"/organizations/{test_organization.id}",
401+
app.url_path_for("read_organization", org_id=test_organization.id),
402402
follow_redirects=False
403403
)
404404

@@ -410,7 +410,7 @@ def test_read_organization_as_non_member(auth_client_non_member, test_organizati
410410
def test_organization_page_displays_members_correctly(auth_client_owner, org_admin_user, org_member_user, test_organization):
411411
"""Test that members and their roles are displayed correctly"""
412412
response = auth_client_owner.get(
413-
f"/organizations/{test_organization.id}",
413+
app.url_path_for("read_organization", org_id=test_organization.id),
414414
follow_redirects=False
415415
)
416416

@@ -463,7 +463,7 @@ def test_empty_organization_displays_no_members_message(auth_client_owner, sessi
463463
session.commit()
464464

465465
response = auth_client_owner.get(
466-
f"/organizations/{empty_org.id}",
466+
app.url_path_for("read_organization", org_id=empty_org.id),
467467
follow_redirects=False
468468
)
469469

@@ -481,14 +481,14 @@ def test_invite_user_success(auth_client_owner, session, test_organization, non_
481481

482482
# Send invite
483483
response = auth_client_owner.post(
484-
f"/organizations/invite/{test_organization.id}",
484+
app.url_path_for("invite_member", org_id=test_organization.id),
485485
data={"email": non_member_user.account.email},
486486
follow_redirects=False
487487
)
488488

489489
# Should redirect back to organization page
490490
assert response.status_code == 303
491-
assert f"/organizations/{test_organization.id}" in response.headers["location"]
491+
assert app.url_path_for("read_organization", org_id=test_organization.id) in response.headers["location"]
492492

493493
# Verify database state - user should now have the Member role
494494
session.refresh(non_member_user)
@@ -508,7 +508,7 @@ def test_invite_user_success(auth_client_owner, session, test_organization, non_
508508
def test_invite_nonexistent_user(auth_client_owner, test_organization):
509509
"""Test inviting a user that doesn't exist in the system"""
510510
response = auth_client_owner.post(
511-
f"/organizations/invite/{test_organization.id}",
511+
app.url_path_for("invite_member", org_id=test_organization.id),
512512
data={"email": "[email protected]"},
513513
follow_redirects=True
514514
)
@@ -521,7 +521,7 @@ def test_invite_nonexistent_user(auth_client_owner, test_organization):
521521
def test_invite_existing_member(auth_client_owner, test_organization, org_member_user):
522522
"""Test inviting a user who is already a member"""
523523
response = auth_client_owner.post(
524-
f"/organizations/invite/{test_organization.id}",
524+
app.url_path_for("invite_member", org_id=test_organization.id),
525525
data={"email": org_member_user.account.email},
526526
follow_redirects=True
527527
)
@@ -534,7 +534,7 @@ def test_invite_existing_member(auth_client_owner, test_organization, org_member
534534
def test_invite_without_permission(auth_client_member, test_organization, non_member_user):
535535
"""Test inviting a user without having the INVITE_USER permission"""
536536
response = auth_client_member.post(
537-
f"/organizations/invite/{test_organization.id}",
537+
app.url_path_for("invite_member", org_id=test_organization.id),
538538
data={"email": non_member_user.account.email},
539539
follow_redirects=True
540540
)

0 commit comments

Comments
 (0)