forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.09.7 MiB37e985dTahinli3 hours ago363 commits

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