use forge_core::AuditAction; use forge_core::store::backup_runs::RunKind; use forge_core::store::replication::DB_SUBJECT; use serde_json::Value; use topcoat::router::request::Request; use topcoat::router::{Body, Router}; use crate::audit::{self, Actor, Target}; use crate::auth::SESSION_COOKIE; use crate::live::{Topic, admin}; use crate::state::AppState; use crate::test_support::fixture::{next_event, next_frame}; use crate::test_support::{app_router, create_session, test_state}; const ADMIN_TOKEN: &str = "admin-live-admin-session"; const USER_TOKEN: &str = "admin-live-user-session"; const CSRF: &str = "admin-live-csrf"; fn streams(user_id: &str) -> Vec { vec![ "/admin/users/events".to_owned(), format!("/admin/users/{user_id}/events"), "/admin/audit/events".to_owned(), "/admin/backups/events".to_owned(), "/admin/replication/events".to_owned(), "/admin/health/events".to_owned(), "/admin/server/events".to_owned(), ] } struct Fixture { _dir: tempfile::TempDir, app: Router, state: AppState, admin_id: String, user_id: String, } async fn fixture() -> Fixture { let dir = tempfile::tempdir().expect("tempdir"); let data_dir = dir.path().join("data"); std::fs::create_dir_all(&data_dir).expect("data dir"); let state = test_state(&data_dir).await; let admin = state .store .create_user("root", None, "h", true) .await .expect("admin"); let user = state .store .create_user("plain", None, "h", false) .await .expect("user"); create_session(&state.store, &admin.id, ADMIN_TOKEN, CSRF).await; create_session(&state.store, &user.id, USER_TOKEN, CSRF).await; let app = app_router(state.clone()); Fixture { _dir: dir, app, state, admin_id: admin.id, user_id: user.id, } } async fn open(app: &Router, uri: &str, token: Option<&str>) -> topcoat::router::response::Response { let mut builder = Request::builder().method("GET").uri(uri); if let Some(token) = token { builder = builder.header("cookie", format!("__Host-{SESSION_COOKIE}={token}")); } app.handle(builder.body(Body::from(String::new())).expect("request")) .await } fn changed(data: &str) -> Vec { format!("event: changed\ndata: {data}\n\n").into_bytes() } #[tokio::test] async fn admin_streams_answer_only_admins_and_a_missing_account_is_404() { let fx = fixture().await; for uri in streams(&fx.user_id) { let anonymous = open(&fx.app, &uri, None).await; assert_eq!(anonymous.status().as_u16(), 404, "anonymous: {uri}"); let plain = open(&fx.app, &uri, Some(USER_TOKEN)).await; assert_eq!(plain.status().as_u16(), 404, "a plain user: {uri}"); let admin_reply = open(&fx.app, &uri, Some(ADMIN_TOKEN)).await; assert_eq!(admin_reply.status().as_u16(), 200, "an admin: {uri}"); } let reply = open( &fx.app, "/admin/users/no-such-account/events", Some(ADMIN_TOKEN), ) .await; assert_eq!(reply.status().as_u16(), 404, "a missing account"); } #[tokio::test] async fn account_list_account_page_and_audit_send_for_their_changes() { let fx = fixture().await; let mut list = open(&fx.app, "/admin/users/events", Some(ADMIN_TOKEN)) .await .into_body() .into_data_stream(); let retry = next_frame(&mut list).await.expect("the retry field"); assert!(retry.starts_with(b"retry: "), "{retry:?}"); let page_uri = format!("/admin/users/{}/events", fx.user_id); let mut page = open(&fx.app, &page_uri, Some(ADMIN_TOKEN)) .await .into_body() .into_data_stream(); let mut audit = open( &fx.app, "/admin/audit/events?action=login_failed", Some(ADMIN_TOKEN), ) .await .into_body() .into_data_stream(); let newcomer = fx .state .store .create_user("newcomer", None, "h", false) .await .expect("user"); audit::record( &fx.state.store, None, Actor::Cli, AuditAction::UserCreated, Target { kind: "user", id: &newcomer.id, name: "newcomer", }, Value::Null, ) .await; assert_eq!( next_event(&mut list).await, Some(changed("1")), "account list" ); fx.state .store .suspend_user(&fx.user_id, "spam", Some(&fx.admin_id)) .await .expect("suspend"); audit::record( &fx.state.store, None, Actor::Cli, AuditAction::UserSuspended, Target { kind: "user", id: &fx.user_id, name: "plain", }, Value::Null, ) .await; assert_eq!( next_event(&mut page).await, Some(changed("1")), "account page" ); audit::record( &fx.state.store, None, Actor::Anonymous, AuditAction::LoginFailed, Target::default(), Value::Null, ) .await; assert_eq!( next_event(&mut audit).await, Some(changed("1")), "audit log" ); } #[tokio::test] async fn backups_replication_health_and_server_send_for_their_changes() { let fx = fixture().await; let mut backups = open(&fx.app, "/admin/backups/events", Some(ADMIN_TOKEN)) .await .into_body() .into_data_stream(); let mut replication = open(&fx.app, "/admin/replication/events", Some(ADMIN_TOKEN)) .await .into_body() .into_data_stream(); let mut health = open(&fx.app, "/admin/health/events", Some(ADMIN_TOKEN)) .await .into_body() .into_data_stream(); let mut restarting = open( &fx.app, "/admin/server/events?restarting=an-older-boot", Some(ADMIN_TOKEN), ) .await .into_body() .into_data_stream(); assert_eq!( next_event(&mut restarting).await, Some(changed("restarted")), "a restart" ); let mut settings = open(&fx.app, "/admin/server/events", Some(ADMIN_TOKEN)) .await .into_body() .into_data_stream(); let guard = fx .state .jobs .claim(RunKind::Backup) .expect("the slot is free"); assert_eq!( next_event(&mut backups).await, Some(changed("1")), "run start" ); drop(guard); assert_eq!( next_event(&mut backups).await, Some(changed("1")), "run end" ); let outcome = crate::replication::worker::record( &fx.state.store, DB_SUBJECT, Err("standby unreachable".to_owned()), ) .await; assert!(outcome.is_err()); assert_eq!( next_event(&mut replication).await, Some(changed("1")), "copy" ); crate::alerts::raise( &fx.state, forge_core::alerts::AlertTopic::RunnerOffline, "runner-offline:tahinli-ci", "tahinli-ci", "silent for an hour", ) .await; assert_eq!(next_event(&mut health).await, Some(changed("1")), "alert"); admin().bump(&Topic::AdminServer); assert_eq!( next_event(&mut settings).await, Some(changed("settings")), "setting" ); }