use crate::common::{ Http, PW, Resp, Server, cookie_named, csrf_for, input_value, launch, login, provision, }; use std::fs; use std::future::Future; use std::path::PathBuf; use forge_core::{Store, Visibility}; use reqwest::header::LOCATION; async fn try_login(http: &Http, username: &str) -> Resp { let page = http.get("/login", None).await; assert_eq!(page.status, 200, "GET /login"); let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); let precsrf = input_value(&page.body, "precsrf"); http.post_form( "/login", &[ ("username", username), ("password", PW), ("next", "/"), ("precsrf", &precsrf), ], Some(&precsrf_cookie), ) .await } struct Env { _root: tempfile::TempDir, dir: PathBuf, _server: Server, http: Http, } async fn start(seed: F) -> Env where F: FnOnce(PathBuf, Store) -> Fut, Fut: Future, { let root = tempfile::tempdir().expect("temp dir"); let dir = root.path().to_path_buf(); let (_, ports) = provision( &dir, "", &[ &["create-user", "dana"], &["create-user", "tahinli"], &["create-user", "bob"], ], ); let store = Store::open(&dir.join("forge.db")) .await .expect("open store"); seed(dir.clone(), store).await; let (server, ports) = launch(&dir, ports).await; let http = Http::new(ports.http); Env { _root: root, dir, _server: server, http, } } async fn seed_issue_by_tahinli(dir: PathBuf, store: Store) { let tahinli = store .user_by_username("tahinli") .await .unwrap() .expect("tahinli"); let bob = store.user_by_username("bob").await.unwrap().expect("bob"); store .create_repo(&bob.id, "proj", "", Visibility::Public, "main") .await .expect("create repo"); let repo = store .repo_by_owner_and_name("bob", "proj") .await .unwrap() .expect("repo"); let issue = store .create_issue(&repo.id, &tahinli.id, "tahinli bug report", "body text") .await .expect("create issue"); store .add_comment(&issue.id, &tahinli.id, "tahinli follow-up comment") .await .expect("add comment"); fs::write(dir.join("tahinli.id"), &tahinli.id).expect("write tahinli id"); } #[tokio::test] async fn admin_delete_moves_content_to_ghost_and_pages_still_render() { let env = start(seed_issue_by_tahinli).await; let http = &env.http; let dana = login(http, "dana", PW).await; let tahinli_id = fs::read_to_string(env.dir.join("tahinli.id")).expect("tahinli id"); let path = format!("/admin/users/{tahinli_id}/delete"); let page = http.get(&path, Some(&dana)).await; assert_eq!(page.status, 200, "{}", page.body); assert!(page.body.contains("Delete user"), "{}", page.body); let csrf = csrf_for(http, &path, &dana).await; let wrong = http .post_form( &path, &[("csrf", &csrf), ("confirm_username", "Alicia")], Some(&dana), ) .await; assert_eq!(wrong.status, 200); assert!(wrong.body.contains("exactly to confirm"), "{}", wrong.body); let done = http .post_form( &path, &[("csrf", &csrf), ("confirm_username", "tahinli")], Some(&dana), ) .await; assert_eq!(done.status, 303, "{}", done.body); assert_eq!(done.header(LOCATION).as_deref(), Some("/admin/users")); let issue = http.get("/bob/proj/issues/1", None).await; assert_eq!(issue.status, 200, "{}", issue.body); assert!( issue.body.contains("tahinli follow-up comment"), "{}", issue.body ); assert!(issue.body.contains("Deleted user"), "{}", issue.body); let tahinli_login = try_login(http, "tahinli").await; assert_ne!( tahinli_login.status, 303, "deleted account must not sign in" ); assert!(cookie_named(&tahinli_login, "forge_session").is_none()); } #[tokio::test] async fn self_delete_refuses_wrong_password_and_owned_repos() { let env = start(seed_issue_by_tahinli).await; let http = &env.http; let tahinli = login(http, "tahinli", PW).await; let csrf = csrf_for(http, "/settings/security", &tahinli).await; let wrong = http .post_form( "/settings/security/delete", &[ ("csrf", &csrf), ("current_password", "not-the-password"), ("confirm_username", "tahinli"), ], Some(&tahinli), ) .await; assert_eq!(wrong.status, 200, "{}", wrong.body); assert!( wrong.body.contains("Current password is incorrect."), "{}", wrong.body ); assert!( try_login(http, "tahinli").await.status == 303, "account must still exist" ); let bob = login(http, "bob", PW).await; let csrf = csrf_for(http, "/settings/security", &bob).await; let refused = http .post_form( "/settings/security/delete", &[ ("csrf", &csrf), ("current_password", PW), ("confirm_username", "bob"), ], Some(&bob), ) .await; assert_eq!(refused.status, 200, "{}", refused.body); assert!( refused .body .contains("transfer or delete their repositories first") || refused.body.contains("bob/proj"), "{}", refused.body ); assert!( try_login(http, "bob").await.status == 303, "bob must still exist" ); } #[tokio::test] async fn self_delete_with_correct_password_signs_out_and_removes_login() { let env = start(seed_issue_by_tahinli).await; let http = &env.http; let tahinli = login(http, "tahinli", PW).await; let csrf = csrf_for(http, "/settings/security", &tahinli).await; let done = http .post_form( "/settings/security/delete", &[ ("csrf", &csrf), ("current_password", PW), ("confirm_username", "tahinli"), ], Some(&tahinli), ) .await; assert_eq!(done.status, 303, "{}", done.body); assert_eq!(done.header(LOCATION).as_deref(), Some("/")); let after = http.get("/settings/security", Some(&tahinli)).await; assert_ne!(after.status, 200, "the old session must be gone"); assert_ne!(try_login(http, "tahinli").await.status, 303); }