Skip to main content

max / makenotwork

6.4 KB · 222 lines History Blame Raw
1 //! Tests for platform admin routes.
2
3 use crate::harness::TestHarness;
4 use uuid::Uuid;
5
6 #[sqlx::test]
7 async fn non_admin_gets_404(_pool: sqlx::PgPool) {
8 let mut h = TestHarness::new().await;
9 let _user = h.login_as("regular").await;
10
11 let resp = h.client.get("/_admin").await;
12 assert_eq!(resp.status, axum::http::StatusCode::NOT_FOUND);
13 }
14
15 #[sqlx::test]
16 async fn admin_can_see_dashboard(_pool: sqlx::PgPool) {
17 let admin_id = Uuid::new_v4();
18 let mut h = TestHarness::new_with_admin_session(admin_id).await;
19
20 let resp = h.client.get("/_admin").await;
21 assert_eq!(resp.status, axum::http::StatusCode::OK);
22 assert!(resp.text.contains("Platform Admin"));
23 }
24
25 #[sqlx::test]
26 async fn admin_can_suspend_community(_pool: sqlx::PgPool) {
27 let admin_id = Uuid::new_v4();
28 let mut h = TestHarness::new_with_admin_session(admin_id).await;
29
30 let community_id = h.create_community("Test Community", "test").await;
31
32 let resp = h
33 .client
34 .post_form(
35 &format!("/_admin/communities/{community_id}/suspend"),
36 "reason=policy+violation",
37 )
38 .await;
39 assert!(resp.status.is_redirection() || resp.status == axum::http::StatusCode::OK);
40
41 let suspended: bool =
42 sqlx::query_scalar("SELECT suspended_at IS NOT NULL FROM communities WHERE id = $1")
43 .bind(community_id)
44 .fetch_one(&h.db)
45 .await
46 .unwrap();
47 assert!(suspended);
48 }
49
50 #[sqlx::test]
51 async fn admin_can_unsuspend_community(_pool: sqlx::PgPool) {
52 let admin_id = Uuid::new_v4();
53 let mut h = TestHarness::new_with_admin_session(admin_id).await;
54
55 let community_id = h.create_community("Test", "test").await;
56
57 sqlx::query(
58 "UPDATE communities SET suspended_at = now(), suspension_reason = 'test' WHERE id = $1",
59 )
60 .bind(community_id)
61 .execute(&h.db)
62 .await
63 .unwrap();
64
65 let resp = h
66 .client
67 .post_form(&format!("/_admin/communities/{community_id}/unsuspend"), "")
68 .await;
69 assert!(resp.status.is_redirection() || resp.status == axum::http::StatusCode::OK);
70
71 let suspended: bool =
72 sqlx::query_scalar("SELECT suspended_at IS NOT NULL FROM communities WHERE id = $1")
73 .bind(community_id)
74 .fetch_one(&h.db)
75 .await
76 .unwrap();
77 assert!(!suspended);
78 }
79
80 #[sqlx::test]
81 async fn admin_can_suspend_user(_pool: sqlx::PgPool) {
82 let admin_id = Uuid::new_v4();
83 let mut h = TestHarness::new_with_admin_session(admin_id).await;
84
85 let target_id = Uuid::new_v4();
86 sqlx::query(
87 "INSERT INTO users (mnw_account_id, username, display_name) VALUES ($1, 'baduser', 'Bad User')",
88 )
89 .bind(target_id)
90 .execute(&h.db)
91 .await
92 .unwrap();
93
94 let resp = h
95 .client
96 .post_form(
97 &format!("/_admin/users/{target_id}/suspend"),
98 "reason=abuse",
99 )
100 .await;
101 assert!(resp.status.is_redirection() || resp.status == axum::http::StatusCode::OK);
102
103 let suspended: bool =
104 sqlx::query_scalar("SELECT suspended_at IS NOT NULL FROM users WHERE mnw_account_id = $1")
105 .bind(target_id)
106 .fetch_one(&h.db)
107 .await
108 .unwrap();
109 assert!(suspended);
110 }
111
112 #[sqlx::test]
113 async fn admin_can_unsuspend_user(_pool: sqlx::PgPool) {
114 let admin_id = Uuid::new_v4();
115 let mut h = TestHarness::new_with_admin_session(admin_id).await;
116
117 let target_id = Uuid::new_v4();
118 sqlx::query(
119 "INSERT INTO users (mnw_account_id, username, display_name, suspended_at, suspension_reason)
120 VALUES ($1, 'baduser', 'Bad User', now(), 'abuse')",
121 )
122 .bind(target_id)
123 .execute(&h.db)
124 .await
125 .unwrap();
126
127 let resp = h
128 .client
129 .post_form(&format!("/_admin/users/{target_id}/unsuspend"), "")
130 .await;
131 assert!(resp.status.is_redirection() || resp.status == axum::http::StatusCode::OK);
132
133 let suspended: bool =
134 sqlx::query_scalar("SELECT suspended_at IS NOT NULL FROM users WHERE mnw_account_id = $1")
135 .bind(target_id)
136 .fetch_one(&h.db)
137 .await
138 .unwrap();
139 assert!(!suspended);
140 }
141
142 #[sqlx::test]
143 async fn admin_search_finds_users(_pool: sqlx::PgPool) {
144 let admin_id = Uuid::new_v4();
145 let mut h = TestHarness::new_with_admin_session(admin_id).await;
146
147 let target_id = Uuid::new_v4();
148 sqlx::query(
149 "INSERT INTO users (mnw_account_id, username, display_name)
150 VALUES ($1, 'findableuser', 'Findable User')",
151 )
152 .bind(target_id)
153 .execute(&h.db)
154 .await
155 .unwrap();
156
157 let resp = h.client.get("/_admin?q=findableuser").await;
158 assert_eq!(resp.status, axum::http::StatusCode::OK);
159 assert!(
160 resp.text.contains("findableuser"),
161 "Search results should include matching user"
162 );
163 }
164
165 #[sqlx::test]
166 async fn admin_invalid_uuid_returns_404(_pool: sqlx::PgPool) {
167 let admin_id = Uuid::new_v4();
168 let mut h = TestHarness::new_with_admin_session(admin_id).await;
169
170 let resp = h
171 .client
172 .post_form("/_admin/communities/not-a-uuid/suspend", "reason=test")
173 .await;
174 // parse_uuid returns 404 (hides admin routes from probing)
175 assert_eq!(resp.status, axum::http::StatusCode::NOT_FOUND);
176 }
177
178 #[sqlx::test]
179 async fn admin_suspend_creates_mod_log_entry(_pool: sqlx::PgPool) {
180 let admin_id = Uuid::new_v4();
181 let mut h = TestHarness::new_with_admin_session(admin_id).await;
182
183 let community_id = h.create_community("Test", "test").await;
184
185 h.client
186 .post_form(
187 &format!("/_admin/communities/{community_id}/suspend"),
188 "reason=policy+violation",
189 )
190 .await;
191
192 let count: i64 = sqlx::query_scalar(
193 "SELECT COUNT(*) FROM mod_log WHERE action = 'suspend_community' AND actor_id = $1",
194 )
195 .bind(admin_id)
196 .fetch_one(&h.db)
197 .await
198 .unwrap();
199 assert_eq!(
200 count, 1,
201 "Should have a mod_log entry for suspend_community"
202 );
203 }
204
205 #[sqlx::test]
206 async fn non_admin_post_to_suspend_returns_404(_pool: sqlx::PgPool) {
207 let admin_id = Uuid::new_v4();
208 let mut h = TestHarness::new_with_admin(admin_id).await;
209 let _user = h.login_as("regular").await;
210
211 let community_id = h.create_community("Test", "test").await;
212
213 let resp = h
214 .client
215 .post_form(
216 &format!("/_admin/communities/{community_id}/suspend"),
217 "reason=test",
218 )
219 .await;
220 assert_eq!(resp.status, axum::http::StatusCode::NOT_FOUND);
221 }
222