Skip to main content

max / makenotwork

Give admin tests one harness call for an admin session Eight tests in tests/workflows/admin.rs hand-rolled the same user INSERT plus /_test/login preamble so the session user_id would match the platform_admin_id given to new_with_admin, and admin_can_see_dashboard patched a random login_as user with an UPDATE first. TestHarness::login_as_id logs in with a caller-chosen UUID, and new_with_admin_session pairs it with new_with_admin.
Co-Authored-By
Claude Opus 5 (1M context) <noreply@anthropic.com>
Author: Max Johnson <me@maxj.phd> · 2026-08-23 20:49 UTC
Signed with PGP, not checked
Commit: 7df1af2270aa22c8e64303cf81c64fc1eb590c9d
Parent: 38d7f56
2 files changed, +26 insertions, -108 deletions
@@ -149,8 +149,16 @@
149 149
150 150 /// Log in as a user by username. Creates the user if needed. Returns the user's UUID.
151 151 pub(crate) async fn login_as(&mut self, username: &str) -> Uuid {
152 - let user_id = Uuid::new_v4();
152 + self.login_as_id(Uuid::new_v4(), username).await
153 + }
153 154
155 + /// Log in as a user with a caller-chosen UUID. Creates the user if needed.
156 + ///
157 + /// Tests that need the session `user_id` to match an id the app already
158 + /// knows about, such as the platform admin id handed to
159 + /// [`TestHarness::new_with_admin`], use this rather than `login_as`, which
160 + /// picks a random UUID.
161 + pub(crate) async fn login_as_id(&mut self, user_id: Uuid, username: &str) -> Uuid {
154 162 sqlx::query(
155 163 "INSERT INTO users (mnw_account_id, username, display_name)
156 164 VALUES ($1, $2, $3)
@@ -233,6 +241,15 @@
233 241 .await
234 242 }
235 243
244 + /// Create a harness with a specific platform admin user ID and a session
245 + /// already logged in as that admin. One call covers what admin route tests
246 + /// need before they can touch `/_admin`.
247 + pub(crate) async fn new_with_admin_session(admin_id: Uuid) -> Self {
248 + let mut h = Self::new_with_admin(admin_id).await;
249 + h.login_as_id(admin_id, "admin").await;
250 + h
251 + }
252 +
236 253 /// Ban a user in a community via direct SQL.
237 254 pub(crate) async fn ban_user(
238 255 &self,
@@ -15,22 +15,7 @@
15 15 #[sqlx::test]
16 16 async fn admin_can_see_dashboard(_pool: sqlx::PgPool) {
17 17 let admin_id = Uuid::new_v4();
18 - let mut h = TestHarness::new_with_admin(admin_id).await;
19 - let _admin = h.login_as("admin").await;
20 -
21 - // Re-login with the correct admin_id since login_as generates a random UUID
22 - sqlx::query("UPDATE users SET mnw_account_id = $1 WHERE username = 'admin'")
23 - .bind(admin_id)
24 - .execute(&h.db)
25 - .await
26 - .unwrap();
27 -
28 - h.client.get("/").await;
29 - let body = serde_json::json!({
30 - "user_id": admin_id.to_string(),
31 - "username": "admin",
32 - });
33 - h.client.post_json("/_test/login", &body.to_string()).await;
18 + let mut h = TestHarness::new_with_admin_session(admin_id).await;
34 19
35 20 let resp = h.client.get("/_admin").await;
36 21 assert_eq!(resp.status, axum::http::StatusCode::OK);
@@ -40,19 +25,7 @@
40 25 #[sqlx::test]
41 26 async fn admin_can_suspend_community(_pool: sqlx::PgPool) {
42 27 let admin_id = Uuid::new_v4();
43 - let mut h = TestHarness::new_with_admin(admin_id).await;
44 -
45 - sqlx::query(
46 - "INSERT INTO users (mnw_account_id, username, display_name)
47 - VALUES ($1, 'admin', 'Admin') ON CONFLICT DO NOTHING",
48 - )
49 - .bind(admin_id)
50 - .execute(&h.db)
51 - .await
52 - .unwrap();
53 - h.client.get("/").await;
54 - let body = serde_json::json!({ "user_id": admin_id.to_string(), "username": "admin" });
55 - h.client.post_json("/_test/login", &body.to_string()).await;
28 + let mut h = TestHarness::new_with_admin_session(admin_id).await;
56 29
57 30 let community_id = h.create_community("Test Community", "test").await;
58 31
@@ -77,19 +50,7 @@
77 50 #[sqlx::test]
78 51 async fn admin_can_unsuspend_community(_pool: sqlx::PgPool) {
79 52 let admin_id = Uuid::new_v4();
80 - let mut h = TestHarness::new_with_admin(admin_id).await;
81 -
82 - sqlx::query(
83 - "INSERT INTO users (mnw_account_id, username, display_name)
84 - VALUES ($1, 'admin', 'Admin') ON CONFLICT DO NOTHING",
85 - )
86 - .bind(admin_id)
87 - .execute(&h.db)
88 - .await
89 - .unwrap();
90 - h.client.get("/").await;
91 - let body = serde_json::json!({ "user_id": admin_id.to_string(), "username": "admin" });
92 - h.client.post_json("/_test/login", &body.to_string()).await;
53 + let mut h = TestHarness::new_with_admin_session(admin_id).await;
93 54
94 55 let community_id = h.create_community("Test", "test").await;
95 56
@@ -119,19 +80,7 @@
119 80 #[sqlx::test]
120 81 async fn admin_can_suspend_user(_pool: sqlx::PgPool) {
121 82 let admin_id = Uuid::new_v4();
122 - let mut h = TestHarness::new_with_admin(admin_id).await;
123 -
124 - sqlx::query(
125 - "INSERT INTO users (mnw_account_id, username, display_name)
126 - VALUES ($1, 'admin', 'Admin') ON CONFLICT DO NOTHING",
127 - )
128 - .bind(admin_id)
129 - .execute(&h.db)
130 - .await
131 - .unwrap();
132 - h.client.get("/").await;
133 - let body = serde_json::json!({ "user_id": admin_id.to_string(), "username": "admin" });
134 - h.client.post_json("/_test/login", &body.to_string()).await;
83 + let mut h = TestHarness::new_with_admin_session(admin_id).await;
135 84
136 85 let target_id = Uuid::new_v4();
137 86 sqlx::query(
@@ -163,19 +112,7 @@
163 112 #[sqlx::test]
164 113 async fn admin_can_unsuspend_user(_pool: sqlx::PgPool) {
165 114 let admin_id = Uuid::new_v4();
166 - let mut h = TestHarness::new_with_admin(admin_id).await;
167 -
168 - sqlx::query(
169 - "INSERT INTO users (mnw_account_id, username, display_name)
170 - VALUES ($1, 'admin', 'Admin') ON CONFLICT DO NOTHING",
171 - )
172 - .bind(admin_id)
173 - .execute(&h.db)
174 - .await
175 - .unwrap();
176 - h.client.get("/").await;
177 - let body = serde_json::json!({ "user_id": admin_id.to_string(), "username": "admin" });
178 - h.client.post_json("/_test/login", &body.to_string()).await;
115 + let mut h = TestHarness::new_with_admin_session(admin_id).await;
179 116
180 117 let target_id = Uuid::new_v4();
181 118 sqlx::query(
@@ -205,19 +142,7 @@
205 142 #[sqlx::test]
206 143 async fn admin_search_finds_users(_pool: sqlx::PgPool) {
207 144 let admin_id = Uuid::new_v4();
208 - let mut h = TestHarness::new_with_admin(admin_id).await;
209 -
210 - sqlx::query(
211 - "INSERT INTO users (mnw_account_id, username, display_name)
212 - VALUES ($1, 'admin', 'Admin') ON CONFLICT DO NOTHING",
213 - )
214 - .bind(admin_id)
215 - .execute(&h.db)
216 - .await
217 - .unwrap();
218 - h.client.get("/").await;
219 - let body = serde_json::json!({ "user_id": admin_id.to_string(), "username": "admin" });
220 - h.client.post_json("/_test/login", &body.to_string()).await;
145 + let mut h = TestHarness::new_with_admin_session(admin_id).await;
221 146
222 147 let target_id = Uuid::new_v4();
223 148 sqlx::query(
@@ -240,19 +165,7 @@
240 165 #[sqlx::test]
241 166 async fn admin_invalid_uuid_returns_404(_pool: sqlx::PgPool) {
242 167 let admin_id = Uuid::new_v4();
243 - let mut h = TestHarness::new_with_admin(admin_id).await;
244 -
245 - sqlx::query(
246 - "INSERT INTO users (mnw_account_id, username, display_name)
247 - VALUES ($1, 'admin', 'Admin') ON CONFLICT DO NOTHING",
248 - )
249 - .bind(admin_id)
250 - .execute(&h.db)
251 - .await
252 - .unwrap();
253 - h.client.get("/").await;
254 - let body = serde_json::json!({ "user_id": admin_id.to_string(), "username": "admin" });
255 - h.client.post_json("/_test/login", &body.to_string()).await;
168 + let mut h = TestHarness::new_with_admin_session(admin_id).await;
256 169
257 170 let resp = h
258 171 .client
@@ -265,19 +178,7 @@
265 178 #[sqlx::test]
266 179 async fn admin_suspend_creates_mod_log_entry(_pool: sqlx::PgPool) {
267 180 let admin_id = Uuid::new_v4();
268 - let mut h = TestHarness::new_with_admin(admin_id).await;
269 -
270 - sqlx::query(
271 - "INSERT INTO users (mnw_account_id, username, display_name)
272 - VALUES ($1, 'admin', 'Admin') ON CONFLICT DO NOTHING",
273 - )
274 - .bind(admin_id)
275 - .execute(&h.db)
276 - .await
277 - .unwrap();
278 - h.client.get("/").await;
279 - let body = serde_json::json!({ "user_id": admin_id.to_string(), "username": "admin" });
280 - h.client.post_json("/_test/login", &body.to_string()).await;
181 + let mut h = TestHarness::new_with_admin_session(admin_id).await;
281 182
282 183 let community_id = h.create_community("Test", "test").await;
283 184