Skip to content

Commit b37deb0

Browse files
committed
Hoist awaited calls out of asserts
1 parent 869c20c commit b37deb0

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

tests/test_change_password.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ async def test_change_password_success_rotates_evicts_and_fires_hook(
9999
assert r.status_code == 200
100100

101101
assert len(fired) == 1 # on_after_password_changed fired once
102-
assert (await b.get("/me")).status_code == 401 # other session evicted
103-
assert (await a.get("/me")).status_code == 200 # caller's session kept
102+
resp = await b.get("/me")
103+
assert resp.status_code == 401 # other session evicted
104+
resp = await a.get("/me")
105+
assert resp.status_code == 200 # caller's session kept
104106

105107
repo = UserRepository(UserModel)
106108
async with sessionmaker() as db:
@@ -109,12 +111,10 @@ async def test_change_password_success_rotates_evicts_and_fires_hook(
109111

110112
# the hash actually rotated: new password logs in, old does not
111113
async with _client(app) as fresh:
112-
assert (
113-
await fresh.post("/login", data={"username": "alice", "password": "new-strong-1"})
114-
).status_code == 200
115-
assert (
116-
await fresh.post("/login", data={"username": "alice", "password": "pw123456"})
117-
).status_code == 401
114+
resp = await fresh.post("/login", data={"username": "alice", "password": "new-strong-1"})
115+
assert resp.status_code == 200
116+
resp = await fresh.post("/login", data={"username": "alice", "password": "pw123456"})
117+
assert resp.status_code == 401
118118
await auth.shutdown()
119119

120120

tests/transports/test_management_routes.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ async def test_routes_gated_off_by_default(get_session, UserModel) -> None:
5151
await auth.initialize()
5252
async with _client(app) as c:
5353
await _register_login(c)
54-
assert (await c.get("/sessions")).status_code == 404 # not mounted
54+
resp = await c.get("/sessions")
55+
assert resp.status_code == 404 # not mounted
5556
await auth.shutdown()
5657

5758

@@ -63,7 +64,8 @@ async def test_logout_all_revokes_and_clears(get_session, UserModel) -> None:
6364
r = await c.post("/logout-all", headers={"X-CSRF-Token": csrf})
6465
assert r.status_code == 200
6566
assert r.json()["revoked"] >= 1
66-
assert (await c.get("/me")).status_code == 401 # cookie cleared, session gone
67+
resp = await c.get("/me")
68+
assert resp.status_code == 401 # cookie cleared, session gone
6769
await auth.shutdown()
6870

6971

@@ -75,8 +77,10 @@ async def test_logout_all_keep_current(get_session, UserModel) -> None:
7577
await _register_login(b) # second session for the same user
7678
r = await a.post("/logout-all?keep_current=true", headers={"X-CSRF-Token": csrf_a})
7779
assert r.status_code == 200 and r.json()["revoked"] == 1 # revoked B, kept A
78-
assert (await a.get("/me")).status_code == 200 # caller's session survives
79-
assert (await b.get("/me")).status_code == 401 # the other session is gone
80+
resp = await a.get("/me")
81+
assert resp.status_code == 200 # caller's session survives
82+
resp = await b.get("/me")
83+
assert resp.status_code == 401 # the other session is gone
8084
await auth.shutdown()
8185

8286

@@ -108,23 +112,24 @@ async def test_delete_own_other_unknown_and_cross_user(get_session, UserModel) -
108112
# revoke alice's other session (B)
109113
r = await a.delete(f"/sessions/{b_id}", headers={"X-CSRF-Token": csrf_a})
110114
assert r.status_code == 200
111-
assert (await b.get("/me")).status_code == 401
115+
resp = await b.get("/me")
116+
assert resp.status_code == 401
112117

113118
# unknown id -> 404
114-
assert (
115-
await a.delete("/sessions/nope", headers={"X-CSRF-Token": csrf_a})
116-
).status_code == 404
119+
resp = await a.delete("/sessions/nope", headers={"X-CSRF-Token": csrf_a})
120+
assert resp.status_code == 404
117121

118122
# another user's session id -> 404 (ownership check, no leak)
119-
assert (
120-
await other.delete(f"/sessions/{a_id}", headers={"X-CSRF-Token": csrf_other})
121-
).status_code == 404
122-
assert (await a.get("/me")).status_code == 200 # untouched
123+
resp = await other.delete(f"/sessions/{a_id}", headers={"X-CSRF-Token": csrf_other})
124+
assert resp.status_code == 404
125+
resp = await a.get("/me")
126+
assert resp.status_code == 200 # untouched
123127

124128
# revoke own current session -> 200 + cookie cleared
125129
r = await a.delete(f"/sessions/{a_id}", headers={"X-CSRF-Token": csrf_a})
126130
assert r.status_code == 200
127-
assert (await a.get("/me")).status_code == 401
131+
resp = await a.get("/me")
132+
assert resp.status_code == 401
128133
await auth.shutdown()
129134

130135

@@ -152,7 +157,8 @@ async def test_csrf_refresh_no_session_401(get_session, UserModel) -> None:
152157
app, auth = _build(get_session, UserModel)
153158
await auth.initialize()
154159
async with _client(app) as c:
155-
assert (await c.post("/csrf/refresh")).status_code == 401 # no session cookie
160+
resp = await c.post("/csrf/refresh")
161+
assert resp.status_code == 401 # no session cookie
156162
await auth.shutdown()
157163

158164

@@ -161,5 +167,6 @@ async def test_csrf_refresh_disabled_400(get_session, UserModel) -> None:
161167
await auth.initialize()
162168
async with _client(app) as c:
163169
await _register_login(c)
164-
assert (await c.post("/csrf/refresh")).status_code == 400 # CSRF disabled
170+
resp = await c.post("/csrf/refresh")
171+
assert resp.status_code == 400 # CSRF disabled
165172
await auth.shutdown()

0 commit comments

Comments
 (0)