forge
Explore
Log in

Don't you dare go Hollow

Admin page tests share their fixtures and merge into tables

Browse files at this commit

6efb4762724dfabbee5bf93bdb67aa59fdd3d81eAhmet Kaan GümüşParent: 2f69218
  • test
    • admin page tests share their fixtures and merge into tables
Show full message
✅ test: admin page tests share their fixtures and merge into tables

Per file, test count before and after:
- admin_runners.rs: 10 -> 4
- admin_server.rs: 10 -> 4
- admin_mail.rs: 8 -> 3
- admin_live_tests.rs: 9 -> 3

Gate, CSRF and refusal checks now run inside the scenario that owns the
fixture state. Every assertion is kept, with its scenario named in the
message.
crates/forge-web/src/pages/admin_live_tests.rsmodified+67 -87
@@ -113,7 +113,7 @@ fn changed(data: &str) -> Vec<u8> {
113113 }
114114
115115 #[tokio::test]
116-async fn admin_streams_answer_only_admins() {
116+async fn admin_streams_answer_only_admins_and_a_missing_account_is_404() {
117117 let fx = fixture().await;
118118 for uri in streams(&fx.user_id) {
119119 let anonymous = open(&fx.app, &uri, None).await;
@@ -123,18 +123,41 @@ async fn admin_streams_answer_only_admins() {
123123 let admin_reply = open(&fx.app, &uri, Some(ADMIN_TOKEN)).await;
124124 assert_eq!(admin_reply.status().as_u16(), 200, "an admin: {uri}");
125125 }
126+ let reply = open(
127+ &fx.app,
128+ "/admin/users/no-such-account/events",
129+ Some(ADMIN_TOKEN),
130+ )
131+ .await;
132+ assert_eq!(reply.status().as_u16(), 404, "a missing account");
126133 }
127134
135+// Scenario: the account list, one account's page and the audit log each send one `changed`
136+// event for their own change. All three streams are open before the writes they should see.
128137 #[tokio::test]
129-async fn the_account_list_sends_when_an_account_is_created() {
138+async fn account_list_account_page_and_audit_send_for_their_changes() {
130139 let fx = fixture().await;
131- let mut frames = open(&fx.app, "/admin/users/events", Some(ADMIN_TOKEN))
140+ let mut list = open(&fx.app, "/admin/users/events", Some(ADMIN_TOKEN))
132141 .await
133142 .into_body()
134143 .into_data_stream();
135- let retry = next_frame(&mut frames).await.expect("the retry field");
144+ let retry = next_frame(&mut list).await.expect("the retry field");
136145 assert!(retry.starts_with(b"retry: "), "{retry:?}");
146+ let page_uri = format!("/admin/users/{}/events", fx.user_id);
147+ let mut page = open(&fx.app, &page_uri, Some(ADMIN_TOKEN))
148+ .await
149+ .into_body()
150+ .into_data_stream();
151+ let mut audit = open(
152+ &fx.app,
153+ "/admin/audit/events?action=login_failed",
154+ Some(ADMIN_TOKEN),
155+ )
156+ .await
157+ .into_body()
158+ .into_data_stream();
137159
160+ // A new account is listed.
138161 let newcomer = fx
139162 .state
140163 .store
@@ -154,18 +177,9 @@ async fn the_account_list_sends_when_an_account_is_created() {
154177 Value::Null,
155178 )
156179 .await;
157- assert_eq!(next_event(&mut frames).await, changed("1"));
158-}
159-
160-#[tokio::test]
161-async fn an_account_page_sends_when_its_account_is_suspended() {
162- let fx = fixture().await;
163- let uri = format!("/admin/users/{}/events", fx.user_id);
164- let mut frames = open(&fx.app, &uri, Some(ADMIN_TOKEN))
165- .await
166- .into_body()
167- .into_data_stream();
180+ assert_eq!(next_event(&mut list).await, changed("1"), "account list");
168181
182+ // A suspended account's page sends.
169183 fx.state
170184 .store
171185 .suspend_user(&fx.user_id, "spam", Some(&fx.admin_id))
@@ -184,33 +198,9 @@ async fn an_account_page_sends_when_its_account_is_suspended() {
184198 Value::Null,
185199 )
186200 .await;
187- assert_eq!(next_event(&mut frames).await, changed("1"));
188-}
189-
190-#[tokio::test]
191-async fn an_account_page_of_a_missing_account_is_404() {
192- let fx = fixture().await;
193- let reply = open(
194- &fx.app,
195- "/admin/users/no-such-account/events",
196- Some(ADMIN_TOKEN),
197- )
198- .await;
199- assert_eq!(reply.status().as_u16(), 404);
200-}
201-
202-#[tokio::test]
203-async fn the_audit_log_sends_a_new_entry_of_its_filter() {
204- let fx = fixture().await;
205- let mut frames = open(
206- &fx.app,
207- "/admin/audit/events?action=login_failed",
208- Some(ADMIN_TOKEN),
209- )
210- .await
211- .into_body()
212- .into_data_stream();
201+ assert_eq!(next_event(&mut page).await, changed("1"), "account page");
213202
203+ // A new entry of the filtered audit log sends.
214204 audit::record(
215205 &fx.state.store,
216206 None,
@@ -220,13 +210,40 @@ async fn the_audit_log_sends_a_new_entry_of_its_filter() {
220210 Value::Null,
221211 )
222212 .await;
223- assert_eq!(next_event(&mut frames).await, changed("1"));
213+ assert_eq!(next_event(&mut audit).await, changed("1"), "audit log");
224214 }
225215
216+// Scenario: the backups, replication, health and server pages each send for their own change.
226217 #[tokio::test]
227-async fn the_backups_page_sends_when_a_run_starts_and_ends() {
218+async fn backups_replication_health_and_server_send_for_their_changes() {
228219 let fx = fixture().await;
229- let mut frames = open(&fx.app, "/admin/backups/events", Some(ADMIN_TOKEN))
220+ let mut backups = open(&fx.app, "/admin/backups/events", Some(ADMIN_TOKEN))
221+ .await
222+ .into_body()
223+ .into_data_stream();
224+ let mut replication = open(&fx.app, "/admin/replication/events", Some(ADMIN_TOKEN))
225+ .await
226+ .into_body()
227+ .into_data_stream();
228+ let mut health = open(&fx.app, "/admin/health/events", Some(ADMIN_TOKEN))
229+ .await
230+ .into_body()
231+ .into_data_stream();
232+ // A page that waits for a restart: a process other than the one it waits for sends at once.
233+ let mut restarting = open(
234+ &fx.app,
235+ "/admin/server/events?restarting=an-older-boot",
236+ Some(ADMIN_TOKEN),
237+ )
238+ .await
239+ .into_body()
240+ .into_data_stream();
241+ assert_eq!(
242+ next_event(&mut restarting).await,
243+ changed("restarted"),
244+ "a restart"
245+ );
246+ let mut settings = open(&fx.app, "/admin/server/events", Some(ADMIN_TOKEN))
230247 .await
231248 .into_body()
232249 .into_data_stream();
@@ -237,18 +254,9 @@ async fn the_backups_page_sends_when_a_run_starts_and_ends() {
237254 .jobs
238255 .claim(RunKind::Backup)
239256 .expect("the slot is free");
240- assert_eq!(next_event(&mut frames).await, changed("1"));
257+ assert_eq!(next_event(&mut backups).await, changed("1"), "run start");
241258 drop(guard);
242- assert_eq!(next_event(&mut frames).await, changed("1"));
243-}
244-
245-#[tokio::test]
246-async fn the_replication_page_sends_when_a_copy_fails() {
247- let fx = fixture().await;
248- let mut frames = open(&fx.app, "/admin/replication/events", Some(ADMIN_TOKEN))
249- .await
250- .into_body()
251- .into_data_stream();
259+ assert_eq!(next_event(&mut backups).await, changed("1"), "run end");
252260
253261 // The worker's own recording path: a failed step is written and the page is woken.
254262 let outcome = crate::replication::worker::record(
@@ -258,16 +266,7 @@ async fn the_replication_page_sends_when_a_copy_fails() {
258266 )
259267 .await;
260268 assert!(outcome.is_err());
261- assert_eq!(next_event(&mut frames).await, changed("1"));
262-}
263-
264-#[tokio::test]
265-async fn the_health_page_sends_when_an_alert_is_raised() {
266- let fx = fixture().await;
267- let mut frames = open(&fx.app, "/admin/health/events", Some(ADMIN_TOKEN))
268- .await
269- .into_body()
270- .into_data_stream();
269+ assert_eq!(next_event(&mut replication).await, changed("1"), "copy");
271270
272271 crate::alerts::raise(
273272 &fx.state,
@@ -277,28 +276,9 @@ async fn the_health_page_sends_when_an_alert_is_raised() {
277276 "silent for an hour",
278277 )
279278 .await;
280- assert_eq!(next_event(&mut frames).await, changed("1"));
281-}
279+ assert_eq!(next_event(&mut health).await, changed("1"), "alert");
282280
283-#[tokio::test]
284-async fn the_server_page_sends_when_a_setting_changes_and_on_a_restart() {
285- let fx = fixture().await;
286- // A page that waits for a restart: a process other than the one it waits for sends at once.
287- let mut frames = open(
288- &fx.app,
289- "/admin/server/events?restarting=an-older-boot",
290- Some(ADMIN_TOKEN),
291- )
292- .await
293- .into_body()
294- .into_data_stream();
295- assert_eq!(next_event(&mut frames).await, changed("restarted"));
296-
297- // Any saved or reset setting wakes the stream.
298- let mut settings = open(&fx.app, "/admin/server/events", Some(ADMIN_TOKEN))
299- .await
300- .into_body()
301- .into_data_stream();
281+ // Any saved or reset setting wakes the server stream.
302282 admin().bump(&Topic::AdminServer);
303- assert_eq!(next_event(&mut settings).await, changed("settings"));
283+ assert_eq!(next_event(&mut settings).await, changed("settings"), "setting");
304284 }
crates/forge-web/src/pages/admin_mail.rsmodified+65 -78
@@ -697,9 +697,14 @@ mod tests {
697697 .any(|e| e.action == action)
698698 }
699699
700+ // Scenario: the settings page. Gates and refusals come first while the store is still
701+ // pristine, then a save, the page with no password shown, the test mail while mail is on,
702+ // and the Turkish page. Each step is a precondition of the next one.
700703 #[tokio::test]
701- async fn the_page_and_its_posts_are_404_for_anonymous_and_non_admins() {
704+ async fn settings_gates_saves_test_mail_and_turkish_page() {
702705 let fx = fixture().await;
706+
707+ // Anonymous and non-admin users get 404 on the page and its posts.
703708 let (anon, _) = send(&fx.router, "GET", "/admin/mail", None, None).await;
704709 assert_eq!(anon, 404);
705710 let (user, _) = send(&fx.router, "GET", "/admin/mail", Some(USER_TOKEN), None).await;
@@ -730,11 +735,8 @@ mod tests {
730735 .expect("settings")
731736 .has_password
732737 );
733- }
734738
735- #[tokio::test]
736- async fn an_admin_saves_settings_and_the_password_is_never_shown() {
737- let fx = fixture().await;
739+ // A missing CSRF token is refused.
738740 let (status, _) = send(
739741 &fx.router,
740742 "POST",
@@ -745,35 +747,27 @@ mod tests {
745747 .await;
746748 assert_eq!(status, 403, "a missing CSRF token is refused");
747749
750+ // Test mail while mail is off queues nothing.
748751 let (status, _) = send(
749752 &fx.router,
750753 "POST",
751- "/admin/mail",
754+ "/admin/mail/test",
752755 Some(ADMIN_TOKEN),
753- Some(settings_form(CSRF, "465", "pw-secret-9")),
756+ Some(format!("csrf={CSRF}")),
754757 )
755758 .await;
756759 assert_eq!(status, 303);
757- let saved = fx.state.store.mail_settings().await.expect("settings");
758- assert_eq!(saved.host, "smtp.example");
759- assert_eq!(saved.port, 465);
760- assert_eq!(saved.security, Security::Tls);
761- assert!(saved.enabled);
762- assert!(saved.has_password);
763- assert!(audit_has(&fx.state.store, AuditAction::MailSettingsUpdated).await);
764-
765- let (page, body) = send(&fx.router, "GET", "/admin/mail", Some(ADMIN_TOKEN), None).await;
766- assert_eq!(page, 200);
767- assert!(body.contains("SMTP server"));
768760 assert!(
769- !body.contains("pw-secret-9"),
770- "the stored password is never rendered"
761+ fx.state
762+ .store
763+ .recent_mail(10)
764+ .await
765+ .expect("rows")
766+ .is_empty(),
767+ "nothing is queued while mail is off"
771768 );
772- }
773769
774- #[tokio::test]
775- async fn a_bad_port_changes_nothing() {
776- let fx = fixture().await;
770+ // A bad port is answered with a notice and changes nothing.
777771 let (status, _) = send(
778772 &fx.router,
779773 "POST",
@@ -787,38 +781,33 @@ mod tests {
787781 assert_eq!(saved.host, "");
788782 assert!(!saved.has_password);
789783 assert!(!audit_has(&fx.state.store, AuditAction::MailSettingsUpdated).await);
790- }
791784
792- #[tokio::test]
793- async fn a_test_mail_is_queued_only_while_mail_is_on() {
794- let fx = fixture().await;
785+ // A good save stores the settings, audits them, and the page never shows the password.
795786 let (status, _) = send(
796787 &fx.router,
797788 "POST",
798- "/admin/mail/test",
789+ "/admin/mail",
799790 Some(ADMIN_TOKEN),
800- Some(format!("csrf={CSRF}")),
791+ Some(settings_form(CSRF, "465", "pw-secret-9")),
801792 )
802793 .await;
803794 assert_eq!(status, 303);
795+ let saved = fx.state.store.mail_settings().await.expect("settings");
796+ assert_eq!(saved.host, "smtp.example");
797+ assert_eq!(saved.port, 465);
798+ assert_eq!(saved.security, Security::Tls);
799+ assert!(saved.enabled);
800+ assert!(saved.has_password);
801+ assert!(audit_has(&fx.state.store, AuditAction::MailSettingsUpdated).await);
802+ let (page, body) = send(&fx.router, "GET", "/admin/mail", Some(ADMIN_TOKEN), None).await;
803+ assert_eq!(page, 200);
804+ assert!(body.contains("SMTP server"));
804805 assert!(
805- fx.state
806- .store
807- .recent_mail(10)
808- .await
809- .expect("rows")
810- .is_empty(),
811- "nothing is queued while mail is off"
806+ !body.contains("pw-secret-9"),
807+ "the stored password is never rendered"
812808 );
813809
814- send(
815- &fx.router,
816- "POST",
817- "/admin/mail",
818- Some(ADMIN_TOKEN),
819- Some(settings_form(CSRF, "587", "")),
820- )
821- .await;
810+ // With mail on, a test mail is queued to the admin's address and audited.
822811 let (status, _) = send(
823812 &fx.router,
824813 "POST",
@@ -833,11 +822,8 @@ mod tests {
833822 assert_eq!(rows[0].to_addr, "admin@example.com");
834823 assert!(rows[0].idem_key.starts_with("test:"));
835824 assert!(audit_has(&fx.state.store, AuditAction::MailTestQueued).await);
836- }
837825
838- #[tokio::test]
839- async fn turkish_viewer_gets_the_turkish_mail_page() {
840- let fx = fixture().await;
826+ // The page follows the viewer's language.
841827 let req = Request::builder()
842828 .method("GET")
843829 .uri("/admin/mail")
@@ -870,9 +856,38 @@ mod tests {
870856 }
871857 }
872858
859+ // Scenario: the queue tab is admin only and live, filters by state, and never renders a
860+ // body. The queue actions and event stream are gated the same way.
873861 #[tokio::test]
874- async fn the_queue_tab_is_live_filters_by_state_and_never_renders_a_body() {
862+ async fn queue_tab_is_admin_only_live_filters_and_never_renders_a_body() {
875863 let fx = fixture().await;
864+
865+ // Anonymous and plain users get 404 on the stream and on both queue actions.
866+ for (method, uri) in [
867+ ("GET", "/admin/mail/events"),
868+ ("POST", "/admin/mail/queue/retry"),
869+ ("POST", "/admin/mail/queue/discard"),
870+ ] {
871+ let (anon, _) = send(
872+ &fx.router,
873+ method,
874+ uri,
875+ None,
876+ Some(format!("csrf={CSRF}&id=x")),
877+ )
878+ .await;
879+ assert_eq!(anon, 404, "{method} {uri} for anonymous");
880+ let (user, _) = send(
881+ &fx.router,
882+ method,
883+ uri,
884+ Some(USER_TOKEN),
885+ Some(format!("csrf={CSRF}&id=x")),
886+ )
887+ .await;
888+ assert_eq!(user, 404, "{method} {uri} for a plain user");
889+ }
890+
876891 let reset = outgoing(
877892 "a@example.com",
878893 "Reset your password",
@@ -952,35 +967,7 @@ mod tests {
952967 !settings.contains("Reset your password"),
953968 "the queue is on its own tab"
954969 );
955- }
956970
957- #[tokio::test]
958- async fn the_queue_events_and_its_actions_are_admin_only_and_admins_get_the_stream() {
959- let fx = fixture().await;
960- for (method, uri) in [
961- ("GET", "/admin/mail/events"),
962- ("POST", "/admin/mail/queue/retry"),
963- ("POST", "/admin/mail/queue/discard"),
964- ] {
965- let (anon, _) = send(
966- &fx.router,
967- method,
968- uri,
969- None,
970- Some(format!("csrf={CSRF}&id=x")),
971- )
972- .await;
973- assert_eq!(anon, 404, "{method} {uri} for anonymous");
974- let (user, _) = send(
975- &fx.router,
976- method,
977- uri,
978- Some(USER_TOKEN),
979- Some(format!("csrf={CSRF}&id=x")),
980- )
981- .await;
982- assert_eq!(user, 404, "{method} {uri} for a plain user");
983- }
984971 // An admin gets the queue event stream.
985972 let request = Request::builder()
986973 .method("GET")
crates/forge-web/src/pages/admin_runners.rsmodified+114 -202
@@ -1524,10 +1524,38 @@ mod tests {
15241524 );
15251525 }
15261526
1527+ fn token_from(page: &str) -> String {
1528+ page.split("--token ")
1529+ .nth(1)
1530+ .and_then(|rest| rest.split(' ').next())
1531+ .expect("token")
1532+ .to_owned()
1533+ }
1534+
1535+ async fn one_runner(store: &Store, router: &Router) -> forge_core::ci::CiRunner {
1536+ let (_, created) = send(
1537+ router,
1538+ "POST",
1539+ "/admin/runners/tokens",
1540+ Some(ADMIN_TOKEN),
1541+ csrf_form(""),
1542+ )
1543+ .await;
1544+ let token = token_from(&created);
1545+ store
1546+ .register_runner(&token, "tahinli-box", &["linux".to_owned()])
1547+ .await
1548+ .expect("register");
1549+ store.list_runners().await.expect("list").remove(0)
1550+ }
1551+
1552+ // Scenario: the page gates, the one-time token, and labels as chips with a state badge.
15271553 #[tokio::test]
1528- async fn page_is_404_for_anonymous_and_non_admins() {
1554+ async fn gates_token_once_and_labels_render_as_chips() {
15291555 let dir = tempfile::tempdir().expect("tempdir");
1530- let (_, router) = fixture(dir.path()).await;
1556+ let (store, router) = fixture(dir.path()).await;
1557+
1558+ // Anonymous and non-admin users get 404 on every runner route.
15311559 let (anon, _) = send(&router, "GET", "/admin/runners", None, None).await;
15321560 assert_eq!(anon, 404, "anonymous gets 404 here, not a login redirect");
15331561 let (user, _) = send(&router, "GET", "/admin/runners", Some(USER_TOKEN), None).await;
@@ -1550,19 +1578,14 @@ mod tests {
15501578 )
15511579 .await;
15521580 assert_eq!(del, 404);
1553- }
15541581
1555- #[tokio::test]
1556- async fn posts_without_a_valid_csrf_token_are_forbidden() {
1557- let dir = tempfile::tempdir().expect("tempdir");
1558- let (store, router) = fixture(dir.path()).await;
1559- let bad = Some("csrf=wrong".to_owned());
1582+ // An admin post without a valid CSRF token is forbidden and changes nothing.
15601583 let (tok, _) = send(
15611584 &router,
15621585 "POST",
15631586 "/admin/runners/tokens",
15641587 Some(ADMIN_TOKEN),
1565- bad.clone(),
1588+ Some("csrf=wrong".to_owned()),
15661589 )
15671590 .await;
15681591 assert_eq!(tok, 403);
@@ -1571,7 +1594,7 @@ mod tests {
15711594 "POST",
15721595 "/admin/runners/x/delete",
15731596 Some(ADMIN_TOKEN),
1574- bad.clone(),
1597+ Some("csrf=wrong".to_owned()),
15751598 )
15761599 .await;
15771600 assert_eq!(del, 403);
@@ -1585,12 +1608,8 @@ mod tests {
15851608 .await;
15861609 assert_eq!(dis, 403);
15871610 assert!(store.list_runners().await.expect("list").is_empty());
1588- }
15891611
1590- #[tokio::test]
1591- async fn token_page_shows_the_token_once_and_the_list_never_does() {
1592- let dir = tempfile::tempdir().expect("tempdir");
1593- let (store, router) = fixture(dir.path()).await;
1612+ // The token is shown once, in the box and in the command, and never in the list.
15941613 let (status, created) = send(
15951614 &router,
15961615 "POST",
@@ -1600,12 +1619,7 @@ mod tests {
16001619 )
16011620 .await;
16021621 assert_eq!(status, 200);
1603- let token = created
1604- .split("--token ")
1605- .nth(1)
1606- .and_then(|rest| rest.split(' ').next())
1607- .expect("command with token")
1608- .to_owned();
1622+ let token = token_from(&created);
16091623 assert!(created.contains("forge runner register --url"));
16101624 assert!(
16111625 created.contains("--name &lt;name&gt; --labels linux,x86_64 --config ~/.config/forge-runner/runner.toml"),
@@ -1616,32 +1630,13 @@ mod tests {
16161630 2,
16171631 "token in the box and the command"
16181632 );
1619-
16201633 let (_, list) = send(&router, "GET", "/admin/runners", Some(ADMIN_TOKEN), None).await;
16211634 assert!(!list.contains(&token), "the list never shows the token");
16221635 assert!(!list.contains("forge runner register"));
16231636 // Only the hash is stored, so the list and the database hold no plain token.
16241637 assert!(store.list_runners().await.expect("list").is_empty());
1625- }
16261638
1627- #[tokio::test]
1628- async fn runner_labels_are_separate_chips_and_state_is_a_badge() {
1629- let dir = tempfile::tempdir().expect("tempdir");
1630- let (store, router) = fixture(dir.path()).await;
1631- let (_, created) = send(
1632- &router,
1633- "POST",
1634- "/admin/runners/tokens",
1635- Some(ADMIN_TOKEN),
1636- csrf_form(""),
1637- )
1638- .await;
1639- let token = created
1640- .split("--token ")
1641- .nth(1)
1642- .and_then(|rest| rest.split(' ').next())
1643- .expect("token")
1644- .to_owned();
1639+ // Labels are separate chips and the state is a badge that follows disabling.
16451640 store
16461641 .register_runner(
16471642 &token,
@@ -1666,29 +1661,12 @@ mod tests {
16661661 assert!(page.contains(r#"<span class="ci-state runner-disabled">disabled</span>"#));
16671662 }
16681663
1664+ // Scenario: restart is capability-gated and audited, then the runner is disabled, enabled and deleted.
16691665 #[tokio::test]
1670- async fn restart_needs_the_capability_and_shows_pending_until_the_runner_returns() {
1666+ async fn restart_then_disable_and_delete_follow_one_runner() {
16711667 let dir = tempfile::tempdir().expect("tempdir");
16721668 let (store, router) = fixture(dir.path()).await;
1673- let (_, created) = send(
1674- &router,
1675- "POST",
1676- "/admin/runners/tokens",
1677- Some(ADMIN_TOKEN),
1678- csrf_form(""),
1679- )
1680- .await;
1681- let token = created
1682- .split("--token ")
1683- .nth(1)
1684- .and_then(|rest| rest.split(' ').next())
1685- .expect("token")
1686- .to_owned();
1687- store
1688- .register_runner(&token, "box-1", &["linux".to_owned()])
1689- .await
1690- .expect("register");
1691- let runner = store.list_runners().await.expect("list").remove(0);
1669+ let runner = one_runner(&store, &router).await;
16921670 let restart = format!("/admin/runners/{}/restart", runner.id);
16931671
16941672 // Not logged in as an admin, or a bad token, changes nothing.
@@ -1772,31 +1750,8 @@ mod tests {
17721750 .unwrap();
17731751 let (_, list) = send(&router, "GET", "/admin/runners", Some(ADMIN_TOKEN), None).await;
17741752 assert!(!list.contains("restart pending"));
1775- }
17761753
1777- #[tokio::test]
1778- async fn disable_enable_and_delete_a_runner() {
1779- let dir = tempfile::tempdir().expect("tempdir");
1780- let (store, router) = fixture(dir.path()).await;
1781- let (_, created) = send(
1782- &router,
1783- "POST",
1784- "/admin/runners/tokens",
1785- Some(ADMIN_TOKEN),
1786- csrf_form(""),
1787- )
1788- .await;
1789- let token = created
1790- .split("--token ")
1791- .nth(1)
1792- .and_then(|rest| rest.split(' ').next())
1793- .expect("token")
1794- .to_owned();
1795- store
1796- .register_runner(&token, "box-1", &["linux".to_owned()])
1797- .await
1798- .expect("register");
1799- let runner = store.list_runners().await.expect("list").remove(0);
1754+ // Disable, then enable again.
18001755 let disable = format!("/admin/runners/{}/disabled", runner.id);
18011756 let (status, _) = send(
18021757 &router,
@@ -1808,7 +1763,6 @@ mod tests {
18081763 .await;
18091764 assert_eq!(status, 303);
18101765 assert!(store.list_runners().await.expect("list")[0].disabled);
1811-
18121766 let (_, _) = send(
18131767 &router,
18141768 "POST",
@@ -1819,11 +1773,11 @@ mod tests {
18191773 .await;
18201774 assert!(!store.list_runners().await.expect("list")[0].disabled);
18211775
1776+ // Delete it, then a second delete reports that the id is gone.
18221777 let delete = format!("/admin/runners/{}/delete", runner.id);
18231778 let (status, _) = send(&router, "POST", &delete, Some(ADMIN_TOKEN), csrf_form("")).await;
18241779 assert_eq!(status, 303);
18251780 assert!(store.list_runners().await.expect("list").is_empty());
1826-
18271781 let (_, _) = send(&router, "POST", &delete, Some(ADMIN_TOKEN), csrf_form("")).await;
18281782 let (_, page) = send(
18291783 &router,
@@ -1836,35 +1790,83 @@ mod tests {
18361790 assert!(page.contains("No runner has that id"));
18371791 }
18381792
1839- async fn one_runner(store: &Store, router: &Router) -> forge_core::ci::CiRunner {
1840- let (_, created) = send(
1841- router,
1842- "POST",
1843- "/admin/runners/tokens",
1844- Some(ADMIN_TOKEN),
1845- csrf_form(""),
1846- )
1847- .await;
1848- let token = created
1849- .split("--token ")
1850- .nth(1)
1851- .and_then(|rest| rest.split(' ').next())
1852- .expect("token")
1853- .to_owned();
1854- store
1855- .register_runner(&token, "tahinli-box", &["linux".to_owned()])
1856- .await
1857- .expect("register");
1858- store.list_runners().await.expect("list").remove(0)
1859- }
1860-
1793+ // Scenario: settings save per tab and per field, concurrency and config reset, and every
1794+ // config, settings, events and detail route is admin only.
18611795 #[tokio::test]
1862- async fn admin_sets_and_resets_concurrency_and_config_fields() {
1796+ async fn settings_save_per_field_reset_and_stay_admin_only() {
18631797 let dir = tempfile::tempdir().expect("tempdir");
18641798 let (store, router) = fixture(dir.path()).await;
18651799 let runner = one_runner(&store, &router).await;
18661800 let action = format!("/admin/runners/{}/config", runner.id);
1801+ let settings = format!("/admin/runners/{}/settings", runner.id);
1802+ let detail_uri = format!("/admin/runners/{}", runner.id);
1803+
1804+ // Non-admins get 404 on every route, and a bad CSRF token is forbidden.
1805+ let (status, _) = send(
1806+ &router,
1807+ "POST",
1808+ &action,
1809+ Some(USER_TOKEN),
1810+ csrf_form("&key=concurrency&value=2"),
1811+ )
1812+ .await;
1813+ assert_eq!(status, 404);
1814+ let (status, _) = send(
1815+ &router,
1816+ "POST",
1817+ &action,
1818+ Some(ADMIN_TOKEN),
1819+ Some("csrf=wrong&key=concurrency&value=2".into()),
1820+ )
1821+ .await;
1822+ assert_eq!(status, 403);
1823+ assert_eq!(
1824+ store.list_runners().await.unwrap()[0].concurrency_override,
1825+ None
1826+ );
1827+ let (status, _) = send(&router, "GET", "/admin/runners/events", Some(USER_TOKEN), None).await;
1828+ assert_eq!(status, 404);
1829+ let (status, _) = send(&router, "GET", &detail_uri, Some(USER_TOKEN), None).await;
1830+ assert_eq!(status, 404);
1831+ let (status, _) = send(
1832+ &router,
1833+ "POST",
1834+ &settings,
1835+ Some(USER_TOKEN),
1836+ csrf_form("&tab=limits&limits.memory=1g"),
1837+ )
1838+ .await;
1839+ assert_eq!(status, 404);
18671840
1841+ // One post saves the changed fields of a tab; blank fields stay unset.
1842+ let (status, _) = send(
1843+ &router,
1844+ "POST",
1845+ &settings,
1846+ Some(ADMIN_TOKEN),
1847+ csrf_form("&tab=limits&limits.memory=8g&limits.cpus=&limits.pids="),
1848+ )
1849+ .await;
1850+ assert_eq!(status, 303);
1851+ let after = store.list_runners().await.unwrap().remove(0);
1852+ assert_eq!(
1853+ after.config_overrides.get("limits.memory"),
1854+ Some(&serde_json::json!("8g"))
1855+ );
1856+ assert_eq!(after.config_overrides.len(), 1, "blank fields stay unset");
1857+ let (status, page) = send(
1858+ &router,
1859+ "GET",
1860+ &format!("/admin/runners/{}?tab=limits", runner.id),
1861+ Some(ADMIN_TOKEN),
1862+ None,
1863+ )
1864+ .await;
1865+ assert_eq!(status, 200);
1866+ assert!(page.contains("Memory limit"));
1867+ assert!(page.contains("Reset to runner.toml"));
1868+
1869+ // Concurrency: set it, show it in the list and the detail page, and refuse bad values.
18681870 let (status, _) = send(
18691871 &router,
18701872 "POST",
@@ -1900,6 +1902,7 @@ mod tests {
19001902 Some(3)
19011903 );
19021904
1905+ // A config field saved from the detail page shows there, live.
19031906 let (status, _) = send(
19041907 &router,
19051908 "POST",
@@ -1909,12 +1912,12 @@ mod tests {
19091912 )
19101913 .await;
19111914 assert_eq!(status, 303);
1912- let detail_uri = format!("/admin/runners/{}", runner.id);
19131915 let (status, detail) = send(&router, "GET", &detail_uri, Some(ADMIN_TOKEN), None).await;
19141916 assert_eq!(status, 200);
19151917 assert!(detail.contains("Concurrency"));
19161918 assert!(detail.contains(r#"data-live="/admin/runners/events""#));
19171919
1920+ // Reset both overrides; the runner goes back to its runner.toml values.
19181921 let (_, _) = send(
19191922 &router,
19201923 "POST",
@@ -1935,95 +1938,4 @@ mod tests {
19351938 assert_eq!(after.concurrency_override, None);
19361939 assert!(after.config_overrides.is_empty());
19371940 }
1938-
1939- #[tokio::test]
1940- async fn one_post_saves_the_changed_fields_of_a_tab() {
1941- let dir = tempfile::tempdir().expect("tempdir");
1942- let (store, router) = fixture(dir.path()).await;
1943- let runner = one_runner(&store, &router).await;
1944- let action = format!("/admin/runners/{}/settings", runner.id);
1945- let (status, _) = send(
1946- &router,
1947- "POST",
1948- &action,
1949- Some(ADMIN_TOKEN),
1950- csrf_form("&tab=limits&limits.memory=8g&limits.cpus=&limits.pids="),
1951- )
1952- .await;
1953- assert_eq!(status, 303);
1954- let after = store.list_runners().await.unwrap().remove(0);
1955- assert_eq!(
1956- after.config_overrides.get("limits.memory"),
1957- Some(&serde_json::json!("8g"))
1958- );
1959- assert_eq!(after.config_overrides.len(), 1, "blank fields stay unset");
1960- let (status, page) = send(
1961- &router,
1962- "GET",
1963- &format!("/admin/runners/{}?tab=limits", runner.id),
1964- Some(ADMIN_TOKEN),
1965- None,
1966- )
1967- .await;
1968- assert_eq!(status, 200);
1969- assert!(page.contains("Memory limit"));
1970- assert!(page.contains("Reset to runner.toml"));
1971- let (status, _) = send(
1972- &router,
1973- "POST",
1974- &action,
1975- Some(USER_TOKEN),
1976- csrf_form("&tab=limits&limits.memory=1g"),
1977- )
1978- .await;
1979- assert_eq!(status, 404);
1980- }
1981-
1982- #[tokio::test]
1983- async fn config_posts_need_admin_and_csrf_and_the_events_stream_is_admin_only() {
1984- let dir = tempfile::tempdir().expect("tempdir");
1985- let (store, router) = fixture(dir.path()).await;
1986- let runner = one_runner(&store, &router).await;
1987- let action = format!("/admin/runners/{}/config", runner.id);
1988- let (status, _) = send(
1989- &router,
1990- "POST",
1991- &action,
1992- Some(USER_TOKEN),
1993- csrf_form("&key=concurrency&value=2"),
1994- )
1995- .await;
1996- assert_eq!(status, 404);
1997- let (status, _) = send(
1998- &router,
1999- "POST",
2000- &action,
2001- Some(ADMIN_TOKEN),
2002- Some("csrf=wrong&key=concurrency&value=2".into()),
2003- )
2004- .await;
2005- assert_eq!(status, 403);
2006- assert_eq!(
2007- store.list_runners().await.unwrap()[0].concurrency_override,
2008- None
2009- );
2010- let (status, _) = send(
2011- &router,
2012- "GET",
2013- "/admin/runners/events",
2014- Some(USER_TOKEN),
2015- None,
2016- )
2017- .await;
2018- assert_eq!(status, 404);
2019- let (status, _) = send(
2020- &router,
2021- "GET",
2022- &format!("/admin/runners/{}", runner.id),
2023- Some(USER_TOKEN),
2024- None,
2025- )
2026- .await;
2027- assert_eq!(status, 404);
2028- }
20291941 }
crates/forge-web/src/pages/admin_server.rsmodified+89 -97
@@ -653,8 +653,10 @@ mod tests {
653653 admin_get(fx, &location).await
654654 }
655655
656+ // Scenario: non-admins get 404 on every route, bad CSRF is refused and refused values are
657+ // never stored. Nothing here saves, so the store stays pristine for the next scenarios.
656658 #[tokio::test]
657- async fn the_page_and_its_posts_are_404_for_anonymous_and_non_admins() {
659+ async fn gates_csrf_read_only_and_bad_values_change_nothing() {
658660 let fx = fixture().await;
659661 for token in [None, Some(USER_TOKEN)] {
660662 let (get, _, _) = send(&fx.router, "GET", "/admin/server", token, None).await;
@@ -666,13 +668,16 @@ mod tests {
666668 let (post, _, _) =
667669 send(&fx.router, "POST", "/admin/server/reset", token, Some(form)).await;
668670 assert_eq!(post, 404);
671+ let form = format!("csrf={CSRF}&tab=general");
672+ let (post, _, _) =
673+ send(&fx.router, "POST", "/admin/server/restart", token, Some(form)).await;
674+ assert_eq!(post, 404, "restart");
675+ let (events, _, _) = send(&fx.router, "GET", "/admin/server/events", token, None).await;
676+ assert_eq!(events, 404, "events");
669677 }
670678 assert!(fx.state.store.server_overrides().await.unwrap().is_empty());
671- }
672679
673- #[tokio::test]
674- async fn a_bad_csrf_token_is_refused() {
675- let fx = fixture().await;
680+ // A bad CSRF token is refused for a settings save and for a restart.
676681 let form = "csrf=wrong&tab=limits&ci.max_log_mb=99".to_owned();
677682 let (status, _, _) = send(
678683 &fx.router,
@@ -682,14 +687,53 @@ mod tests {
682687 Some(form),
683688 )
684689 .await;
685- assert_eq!(status, 403);
690+ assert_eq!(status, 403, "bad csrf save");
691+ let (status, _, _) = send(
692+ &fx.router,
693+ "POST",
694+ "/admin/server/restart",
695+ Some(ADMIN_TOKEN),
696+ Some("csrf=wrong&tab=general".to_owned()),
697+ )
698+ .await;
699+ assert_eq!(status, 403, "bad csrf restart");
700+ assert_eq!(fx.state.live().ci.max_log_mb, 16);
701+
702+ // A read-only field is shown but cannot be posted.
703+ let page = admin_get(&fx, "/admin/server?tab=general").await;
704+ assert!(page.contains("127.0.0.1:7660"));
705+ assert!(!page.contains("name=\"listen\""));
706+ save(&fx, "general", "listen=0.0.0.0%3A1").await;
707+ assert!(fx.state.store.server_overrides().await.unwrap().is_empty());
708+
709+ // Out-of-range, wrong-kind and unknown-storage values are rejected, and nothing is stored.
710+ for (tab, fields) in [
711+ ("general", "registration=everyone"),
712+ ("general", "trusted_proxies=not-an-ip"),
713+ ("limits", "ci.max_log_mb=0"),
714+ ("limits", "ci.max_log_mb=lots"),
715+ ("storage", "storage.backend=r2"),
716+ ("backups", "backup.keep=0&backup.dir=%2Fvar%2Fbackups"),
717+ ] {
718+ let body = save(&fx, tab, fields).await;
719+ assert!(!body.contains("Server settings saved."), "{fields}: {body}");
720+ assert!(
721+ fx.state.store.server_overrides().await.unwrap().is_empty(),
722+ "{fields}"
723+ );
724+ }
686725 assert_eq!(fx.state.live().ci.max_log_mb, 16);
687726 }
688727
728+ // Scenario: a live override applies at once and reset goes back; registration follows the
729+ // saved general setting.
689730 #[tokio::test]
690- async fn a_live_override_applies_at_once_and_reset_goes_back() {
731+ async fn live_overrides_apply_reset_and_registration_follows() {
691732 let fx = fixture().await;
692733 assert_eq!(fx.state.live().ci.max_log_mb, 16);
734+ let (closed, _, _) = send(&fx.router, "GET", "/register", None, None).await;
735+ assert_eq!(closed, 404, "registration is closed by default");
736+
693737 let body = save(&fx, "limits", "ci.max_log_mb=99&ci.enabled=true").await;
694738 assert!(body.contains("Server settings saved."), "{body}");
695739 assert_eq!(fx.state.live().ci.max_log_mb, 99);
@@ -714,42 +758,19 @@ mod tests {
714758 assert_eq!(status, 303);
715759 assert_eq!(fx.state.live().ci.max_log_mb, 16);
716760 assert!(fx.state.store.server_overrides().await.unwrap().is_empty());
717- }
718761
719- #[tokio::test]
720- async fn live_registration_is_read_by_the_register_page() {
721- let fx = fixture().await;
722- let (closed, _, _) = send(&fx.router, "GET", "/register", None, None).await;
723- assert_eq!(closed, 404);
724762 save(&fx, "general", "registration=open&orgs.creation=all").await;
725763 let (open, _, _) = send(&fx.router, "GET", "/register", None, None).await;
726- assert_eq!(open, 200);
764+ assert_eq!(open, 200, "registration opens at once");
727765 }
728766
767+ // Scenario: a restart field is stored but waits for a restart, secrets are never rendered
768+ // and are stored sealed, and saves are audited without their values.
729769 #[tokio::test]
730- async fn bad_values_are_rejected_and_nothing_is_stored() {
770+ async fn restart_fields_secrets_and_audit_hold_their_rules() {
731771 let fx = fixture().await;
732- for (tab, fields) in [
733- ("general", "registration=everyone"),
734- ("general", "trusted_proxies=not-an-ip"),
735- ("limits", "ci.max_log_mb=0"),
736- ("limits", "ci.max_log_mb=lots"),
737- ("storage", "storage.backend=r2"),
738- ("backups", "backup.keep=0&backup.dir=%2Fvar%2Fbackups"),
739- ] {
740- let body = save(&fx, tab, fields).await;
741- assert!(!body.contains("Server settings saved."), "{fields}: {body}");
742- assert!(
743- fx.state.store.server_overrides().await.unwrap().is_empty(),
744- "{fields}"
745- );
746- }
747- assert_eq!(fx.state.live().ci.max_log_mb, 16);
748- }
749772
750- #[tokio::test]
751- async fn a_restart_field_is_saved_but_waits_for_a_restart() {
752- let fx = fixture().await;
773+ // A restart field is stored, the live value stays, and the page says so.
753774 save(&fx, "general", "base_url=https%3A%2F%2Fnew.example").await;
754775 assert_eq!(
755776 fx.state
@@ -765,21 +786,8 @@ mod tests {
765786 let page = admin_get(&fx, "/admin/server?tab=general").await;
766787 assert!(page.contains("restart needed"));
767788 assert!(page.contains("https://new.example"));
768- }
769789
770- #[tokio::test]
771- async fn read_only_fields_are_shown_and_cannot_be_posted() {
772- let fx = fixture().await;
773- let page = admin_get(&fx, "/admin/server?tab=general").await;
774- assert!(page.contains("127.0.0.1:7660"));
775- assert!(!page.contains("name=\"listen\""));
776- save(&fx, "general", "listen=0.0.0.0%3A1").await;
777- assert!(fx.state.store.server_overrides().await.unwrap().is_empty());
778- }
779-
780- #[tokio::test]
781- async fn secrets_are_never_rendered_and_are_stored_sealed() {
782- let fx = fixture().await;
790+ // A secret is saved, never shown on any tab, and a blank post keeps it.
783791 let secret = "https://hc.example/ping/TOPSECRET-TOKEN";
784792 let body = save(
785793 &fx,
@@ -794,13 +802,16 @@ mod tests {
794802 }
795803 let page = admin_get(&fx, "/admin/server?tab=backups").await;
796804 assert!(page.contains("set"));
797- // Stored sealed, readable only through the store with the instance key.
798- let conn_value = fx.state.store.server_overrides().await.unwrap();
799805 assert_eq!(
800- conn_value.get("backup.healthcheck_url").map(String::as_str),
806+ fx.state
807+ .store
808+ .server_overrides()
809+ .await
810+ .unwrap()
811+ .get("backup.healthcheck_url")
812+ .map(String::as_str),
801813 Some(secret)
802814 );
803- // A blank post keeps it.
804815 save(&fx, "backups", "backup.healthcheck_url=").await;
805816 assert!(
806817 fx.state
@@ -810,7 +821,7 @@ mod tests {
810821 .unwrap()
811822 .contains_key("backup.healthcheck_url")
812823 );
813- // Reset removes it.
824+ // Reset removes the secret and leaves the restart field alone.
814825 let form = format!("csrf={CSRF}&tab=backups&key=backup.healthcheck_url");
815826 send(
816827 &fx.router,
@@ -820,35 +831,34 @@ mod tests {
820831 Some(form),
821832 )
822833 .await;
823- assert!(fx.state.store.server_overrides().await.unwrap().is_empty());
834+ let stored = fx.state.store.server_overrides().await.unwrap();
835+ assert!(!stored.contains_key("backup.healthcheck_url"));
836+ assert!(stored.contains_key("base_url"));
837+
838+ // Saves are audited by key only: no value, secret or otherwise, reaches the log.
839+ save(&fx, "limits", "ci.max_log_mb=77").await;
840+ let entries = fx
841+ .state
842+ .store
843+ .list_audit(&Default::default(), None, 50)
844+ .await
845+ .unwrap();
846+ let updates: Vec<_> = entries
847+ .iter()
848+ .filter(|e| e.action == AuditAction::ServerSettingsUpdated)
849+ .collect();
850+ assert!(updates.iter().any(|e| e.data.contains("ci.max_log_mb")));
851+ assert!(updates.iter().any(|e| e.data.contains("backup.healthcheck_url")));
852+ for entry in &updates {
853+ assert!(!entry.data.contains("77"), "{}", entry.data);
854+ assert!(!entry.data.contains("TOPSECRET"), "{}", entry.data);
855+ }
824856 }
825857
858+ // Scenario: restart is audited, and the page shows the restarting state until a new process runs.
826859 #[tokio::test]
827- async fn restart_is_admin_only_audited_and_the_page_shows_the_restarting_state() {
860+ async fn restart_is_audited_and_the_page_shows_the_restarting_state() {
828861 let fx = fixture().await;
829- for token in [None, Some(USER_TOKEN)] {
830- let form = format!("csrf={CSRF}&tab=general");
831- let (status, _, _) = send(
832- &fx.router,
833- "POST",
834- "/admin/server/restart",
835- token,
836- Some(form),
837- )
838- .await;
839- assert_eq!(status, 404);
840- let (status, _, _) = send(&fx.router, "GET", "/admin/server/events", token, None).await;
841- assert_eq!(status, 404);
842- }
843- let (status, _, _) = send(
844- &fx.router,
845- "POST",
846- "/admin/server/restart",
847- Some(ADMIN_TOKEN),
848- Some("csrf=wrong&tab=general".to_owned()),
849- )
850- .await;
851- assert_eq!(status, 403);
852862
853863 // The page offers the button with a confirm step.
854864 let page = admin_get(&fx, "/admin/server").await;
@@ -888,22 +898,4 @@ mod tests {
888898 assert!(page.contains(r#"data-live="/admin/server/events""#));
889899 assert!(page.contains("The server restarted"));
890900 }
891-
892- #[tokio::test]
893- async fn saving_is_audited_without_values() {
894- let fx = fixture().await;
895- save(&fx, "limits", "ci.max_log_mb=77").await;
896- let entries = fx
897- .state
898- .store
899- .list_audit(&Default::default(), None, 50)
900- .await
901- .unwrap();
902- let entry = entries
903- .iter()
904- .find(|e| e.action == AuditAction::ServerSettingsUpdated)
905- .expect("audit entry");
906- assert!(entry.data.contains("ci.max_log_mb"));
907- assert!(!entry.data.contains("77"));
908- }
909901 }