forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

user_delete_e2e.rs261 lines, 8.4 KiBHistoryRawDownload
1//! Black-box tests for account deletion: the admin delete page, the self-service section on
2//! /settings/security, and what stays visible afterwards. Like the other e2e tests, this starts
3//! the real `forge` binary and drives it over HTTP. Cookies are set by hand because the
4//! session cookie is `__Host-` and `Secure`.
5
6use crate::common::{
7 Http, Resp, Server, bin, cookie_named, describe, input_value, launch, login, pick_ports,
8 write_config,
9};
10use std::fs;
11use std::future::Future;
12use std::path::{Path, PathBuf};
13use std::process::Command;
14
15use forge_core::{Store, Visibility};
16use reqwest::header::LOCATION;
17
18/// Each test starts a server on ports picked just before, so the tests run one at a time.
19static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> =
20 std::sync::LazyLock::new(|| tokio::sync::Mutex::new(()));
21
22const PW: &str = "tahinli-password-123";
23
24/// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded.
25fn 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/// Submits the login form. Returns the response, so callers decide what it means.
42async 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/// Creates the users (`dana` is the first user, so an administrator), then runs `seed` against
61/// the store before the server starts. `seed` gets the data dir and the open store.
62struct Env {
63 _root: tempfile::TempDir,
64 dir: PathBuf,
65 _server: Server,
66 http: Http,
67}
68
69async fn start<F, Fut>(seed: F) -> Env
70where
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/// Bob owns public `proj`. Tahinli wrote issue #1 and a comment on it.
96async 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]
124async 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]
175async 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 // Tahinli owns nothing: a wrong password is refused and the account stays.
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 // Bob owns `proj`: deletion is refused and the repo is named.
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]
238async 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}