| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use crate::common::{ |
| 7 | Http, Resp, Server, bin, cookie_named, describe, input_value, launch, login, pick_ports, |
| 8 | write_config, |
| 9 | }; |
| 10 | use std::fs; |
| 11 | use std::future::Future; |
| 12 | use std::path::{Path, PathBuf}; |
| 13 | use std::process::Command; |
| 14 | |
| 15 | use forge_core::{Store, Visibility}; |
| 16 | use reqwest::header::LOCATION; |
| 17 | |
| 18 | |
| 19 | static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = |
| 20 | std::sync::LazyLock::new(|| tokio::sync::Mutex::new(())); |
| 21 | |
| 22 | const PW: &str = "tahinli-password-123"; |
| 23 | |
| 24 | |
| 25 | fn admin(config: &Path, args: &[&str]) { |
| 26 | let out = Command::new(bin()) |
| 27 | .arg("--config") |
| 28 | .arg(config) |
| 29 | .arg("admin") |
| 30 | .args(args) |
| 31 | .env("FORGE_PASSWORD", PW) |
| 32 | .output() |
| 33 | .expect("run admin command"); |
| 34 | assert!( |
| 35 | out.status.success(), |
| 36 | "admin {args:?} failed:\n{}", |
| 37 | describe(&out) |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | async fn try_login(http: &Http, username: &str) -> Resp { |
| 43 | let page = http.get("/login", None).await; |
| 44 | assert_eq!(page.status, 200, "GET /login"); |
| 45 | let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); |
| 46 | let precsrf = input_value(&page.body, "precsrf"); |
| 47 | http.post_form( |
| 48 | "/login", |
| 49 | &[ |
| 50 | ("username", username), |
| 51 | ("password", PW), |
| 52 | ("next", "/"), |
| 53 | ("precsrf", &precsrf), |
| 54 | ], |
| 55 | Some(&precsrf_cookie), |
| 56 | ) |
| 57 | .await |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | struct Env { |
| 63 | _root: tempfile::TempDir, |
| 64 | dir: PathBuf, |
| 65 | _server: Server, |
| 66 | http: Http, |
| 67 | } |
| 68 | |
| 69 | async fn start<F, Fut>(seed: F) -> Env |
| 70 | where |
| 71 | F: FnOnce(PathBuf, Store) -> Fut, |
| 72 | Fut: Future<Output = ()>, |
| 73 | { |
| 74 | let root = tempfile::tempdir().expect("temp dir"); |
| 75 | let dir = root.path().to_path_buf(); |
| 76 | let ports = pick_ports(); |
| 77 | let config = write_config(&dir, ports); |
| 78 | for user in ["dana", "tahinli", "bob"] { |
| 79 | admin(&config, &["create-user", user]); |
| 80 | } |
| 81 | let store = Store::open(&dir.join("forge.db")) |
| 82 | .await |
| 83 | .expect("open store"); |
| 84 | seed(dir.clone(), store).await; |
| 85 | let (server, ports) = launch(&dir, ports).await; |
| 86 | let http = Http::new(ports.http); |
| 87 | Env { |
| 88 | _root: root, |
| 89 | dir, |
| 90 | _server: server, |
| 91 | http, |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | async fn seed_issue_by_tahinli(dir: PathBuf, store: Store) { |
| 97 | let tahinli = store |
| 98 | .user_by_username("tahinli") |
| 99 | .await |
| 100 | .unwrap() |
| 101 | .expect("tahinli"); |
| 102 | let bob = store.user_by_username("bob").await.unwrap().expect("bob"); |
| 103 | store |
| 104 | .create_repo(&bob.id, "proj", "", Visibility::Public, "main") |
| 105 | .await |
| 106 | .expect("create repo"); |
| 107 | let repo = store |
| 108 | .repo_by_owner_and_name("bob", "proj") |
| 109 | .await |
| 110 | .unwrap() |
| 111 | .expect("repo"); |
| 112 | let issue = store |
| 113 | .create_issue(&repo.id, &tahinli.id, "tahinli bug report", "body text") |
| 114 | .await |
| 115 | .expect("create issue"); |
| 116 | store |
| 117 | .add_comment(&issue.id, &tahinli.id, "tahinli follow-up comment") |
| 118 | .await |
| 119 | .expect("add comment"); |
| 120 | fs::write(dir.join("tahinli.id"), &tahinli.id).expect("write tahinli id"); |
| 121 | } |
| 122 | |
| 123 | #[tokio::test] |
| 124 | async fn admin_delete_moves_content_to_ghost_and_pages_still_render() { |
| 125 | let _serial = SERIAL.lock().await; |
| 126 | let env = start(seed_issue_by_tahinli).await; |
| 127 | let http = &env.http; |
| 128 | let dana = login(http, "dana", PW).await; |
| 129 | let tahinli_id = fs::read_to_string(env.dir.join("tahinli.id")).expect("tahinli id"); |
| 130 | let path = format!("/admin/users/{tahinli_id}/delete"); |
| 131 | |
| 132 | let page = http.get(&path, Some(&dana)).await; |
| 133 | assert_eq!(page.status, 200, "{}", page.body); |
| 134 | assert!(page.body.contains("Delete user"), "{}", page.body); |
| 135 | let csrf = input_value(&page.body, "csrf"); |
| 136 | |
| 137 | let wrong = http |
| 138 | .post_form( |
| 139 | &path, |
| 140 | &[("csrf", &csrf), ("confirm_username", "Alicia")], |
| 141 | Some(&dana), |
| 142 | ) |
| 143 | .await; |
| 144 | assert_eq!(wrong.status, 200); |
| 145 | assert!(wrong.body.contains("exactly to confirm"), "{}", wrong.body); |
| 146 | |
| 147 | let done = http |
| 148 | .post_form( |
| 149 | &path, |
| 150 | &[("csrf", &csrf), ("confirm_username", "tahinli")], |
| 151 | Some(&dana), |
| 152 | ) |
| 153 | .await; |
| 154 | assert_eq!(done.status, 303, "{}", done.body); |
| 155 | assert_eq!(done.header(LOCATION).as_deref(), Some("/admin/users")); |
| 156 | |
| 157 | let issue = http.get("/bob/proj/issues/1", None).await; |
| 158 | assert_eq!(issue.status, 200, "{}", issue.body); |
| 159 | assert!( |
| 160 | issue.body.contains("tahinli follow-up comment"), |
| 161 | "{}", |
| 162 | issue.body |
| 163 | ); |
| 164 | assert!(issue.body.contains("Deleted user"), "{}", issue.body); |
| 165 | |
| 166 | let tahinli_login = try_login(http, "tahinli").await; |
| 167 | assert_ne!( |
| 168 | tahinli_login.status, 303, |
| 169 | "deleted account must not sign in" |
| 170 | ); |
| 171 | assert!(cookie_named(&tahinli_login, "forge_session").is_none()); |
| 172 | } |
| 173 | |
| 174 | #[tokio::test] |
| 175 | async fn self_delete_refuses_wrong_password_and_owned_repos() { |
| 176 | let _serial = SERIAL.lock().await; |
| 177 | let env = start(seed_issue_by_tahinli).await; |
| 178 | let http = &env.http; |
| 179 | |
| 180 | |
| 181 | let tahinli = login(http, "tahinli", PW).await; |
| 182 | let security = http.get("/settings/security", Some(&tahinli)).await; |
| 183 | assert_eq!(security.status, 200); |
| 184 | let csrf = input_value(&security.body, "csrf"); |
| 185 | let wrong = http |
| 186 | .post_form( |
| 187 | "/settings/security/delete", |
| 188 | &[ |
| 189 | ("csrf", &csrf), |
| 190 | ("current_password", "not-the-password"), |
| 191 | ("confirm_username", "tahinli"), |
| 192 | ], |
| 193 | Some(&tahinli), |
| 194 | ) |
| 195 | .await; |
| 196 | assert_eq!(wrong.status, 200, "{}", wrong.body); |
| 197 | assert!( |
| 198 | wrong.body.contains("Current password is incorrect."), |
| 199 | "{}", |
| 200 | wrong.body |
| 201 | ); |
| 202 | assert!( |
| 203 | try_login(http, "tahinli").await.status == 303, |
| 204 | "account must still exist" |
| 205 | ); |
| 206 | |
| 207 | |
| 208 | let bob = login(http, "bob", PW).await; |
| 209 | let security = http.get("/settings/security", Some(&bob)).await; |
| 210 | let csrf = input_value(&security.body, "csrf"); |
| 211 | let refused = http |
| 212 | .post_form( |
| 213 | "/settings/security/delete", |
| 214 | &[ |
| 215 | ("csrf", &csrf), |
| 216 | ("current_password", PW), |
| 217 | ("confirm_username", "bob"), |
| 218 | ], |
| 219 | Some(&bob), |
| 220 | ) |
| 221 | .await; |
| 222 | assert_eq!(refused.status, 200, "{}", refused.body); |
| 223 | assert!( |
| 224 | refused |
| 225 | .body |
| 226 | .contains("transfer or delete their repositories first") |
| 227 | || refused.body.contains("bob/proj"), |
| 228 | "{}", |
| 229 | refused.body |
| 230 | ); |
| 231 | assert!( |
| 232 | try_login(http, "bob").await.status == 303, |
| 233 | "bob must still exist" |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | #[tokio::test] |
| 238 | async fn self_delete_with_correct_password_signs_out_and_removes_login() { |
| 239 | let _serial = SERIAL.lock().await; |
| 240 | let env = start(seed_issue_by_tahinli).await; |
| 241 | let http = &env.http; |
| 242 | let tahinli = login(http, "tahinli", PW).await; |
| 243 | let security = http.get("/settings/security", Some(&tahinli)).await; |
| 244 | let csrf = input_value(&security.body, "csrf"); |
| 245 | let done = http |
| 246 | .post_form( |
| 247 | "/settings/security/delete", |
| 248 | &[ |
| 249 | ("csrf", &csrf), |
| 250 | ("current_password", PW), |
| 251 | ("confirm_username", "tahinli"), |
| 252 | ], |
| 253 | Some(&tahinli), |
| 254 | ) |
| 255 | .await; |
| 256 | assert_eq!(done.status, 303, "{}", done.body); |
| 257 | assert_eq!(done.header(LOCATION).as_deref(), Some("/")); |
| 258 | let after = http.get("/settings/security", Some(&tahinli)).await; |
| 259 | assert_ne!(after.status, 200, "the old session must be gone"); |
| 260 | assert_ne!(try_login(http, "tahinli").await.status, 303); |
| 261 | } |