forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

discussions_e2e.rs264 lines, 8.7 KiBHistoryRawDownload
1//! End-to-end: discussions over HTTP against a running server. Covers the settings toggle that
2//! shows and hides the tab, creating a discussion, a reply and a nested reply (refused), marking
3//! an answer in Q&A, and the lock: once locked, a plain viewer's comment is refused while the
4//! owner can still write.
5
6use crate::common::{
7 Http, bin, input_value, launch, login, pick_ports, tool_on_path, write_config,
8};
9use std::path::Path;
10use std::process::Command;
11
12const TAHINLI_PW: &str = "tahinli-password-123";
13const BOB_PW: &str = "bob-password-4567";
14
15fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) {
16 let out = Command::new(bin())
17 .arg("--config")
18 .arg(config)
19 .args(["admin", "create-user", username])
20 .args(extra)
21 .env("FORGE_PASSWORD", password)
22 .output()
23 .expect("run admin create-user");
24 assert!(
25 out.status.success(),
26 "create-user {username} failed:\n{}",
27 String::from_utf8_lossy(&out.stderr)
28 );
29}
30
31/// Creates a public repo through `/new`.
32async fn create_repo(http: &Http, session: &str, name: &str) {
33 let page = http.get("/new", Some(session)).await;
34 assert_eq!(page.status, 200, "GET /new");
35 let csrf = input_value(&page.body, "csrf");
36 let res = http
37 .post_form(
38 "/new",
39 &[
40 ("csrf", &csrf),
41 ("name", name),
42 ("description", ""),
43 ("visibility", "public"),
44 ("default_branch", "main"),
45 ],
46 Some(session),
47 )
48 .await;
49 assert_eq!(res.status, 303, "create repo {name}: {}", res.body);
50 assert_eq!(
51 res.location().as_deref(),
52 Some(format!("/tahinli/{name}").as_str())
53 );
54}
55
56/// The `#comment-<id>` anchor of a redirect location, as the id.
57fn anchored_id(location: Option<&str>) -> String {
58 location
59 .and_then(|l| l.rsplit_once("#comment-"))
60 .map(|(_, id)| id.to_owned())
61 .expect("redirect anchored on a comment")
62}
63
64#[tokio::test]
65async fn discussions_create_reply_answer_lock_and_toggle() {
66 if !tool_on_path("git") {
67 eprintln!("SKIP discussions_e2e: `git` was not found on PATH");
68 return;
69 }
70
71 let root = tempfile::tempdir().expect("temp dir");
72 let dir = root.path().to_path_buf();
73 let ports = pick_ports();
74 let config = write_config(&dir, ports);
75
76 run_admin(&config, "tahinli", &["--admin"], TAHINLI_PW);
77 run_admin(&config, "bob", &[], BOB_PW);
78
79 let (_server, ports) = launch(&dir, ports).await;
80 let http = Http::new(ports.http);
81 let tahinli = login(&http, "tahinli", TAHINLI_PW).await;
82 let bob = login(&http, "bob", BOB_PW).await;
83 create_repo(&http, &tahinli, "pub").await;
84
85 // Off by default: the tab is absent and the pages do not exist.
86 let repo_page = http.get("/tahinli/pub", Some(tahinli.as_str())).await;
87 assert_eq!(repo_page.status, 200);
88 assert!(
89 !repo_page.body.contains("href=\"/tahinli/pub/discussions\""),
90 "the Discussions tab shows while discussions are off"
91 );
92 assert_eq!(
93 http.get("/tahinli/pub/discussions", Some(tahinli.as_str()))
94 .await
95 .status,
96 404,
97 "list while off"
98 );
99
100 // Turn them on from settings, as the owner.
101 let settings = http
102 .get("/tahinli/pub/settings", Some(tahinli.as_str()))
103 .await;
104 assert_eq!(settings.status, 200, "GET settings");
105 let csrf = input_value(&settings.body, "csrf");
106 let res = http
107 .post_form(
108 "/tahinli/pub/settings/discussions",
109 &[("csrf", &csrf), ("enabled", "1")],
110 Some(tahinli.as_str()),
111 )
112 .await;
113 assert_eq!(res.status, 303, "enable: {}", res.body);
114 let repo_page = http.get("/tahinli/pub", Some(tahinli.as_str())).await;
115 assert!(
116 repo_page.body.contains("href=\"/tahinli/pub/discussions\""),
117 "the Discussions tab is missing after turning it on"
118 );
119
120 // Tahinli opens a Q&A discussion.
121 let form = http
122 .get("/tahinli/pub/discussions/new", Some(tahinli.as_str()))
123 .await;
124 assert_eq!(form.status, 200, "GET new discussion");
125 let csrf = input_value(&form.body, "csrf");
126 let res = http
127 .post_form(
128 "/tahinli/pub/discussions/new",
129 &[
130 ("csrf", &csrf),
131 ("category", "qa"),
132 ("title", "How do I rebase?"),
133 ("body", "Any tips?"),
134 ],
135 Some(tahinli.as_str()),
136 )
137 .await;
138 assert_eq!(res.status, 303, "create: {}", res.body);
139 assert_eq!(
140 res.location().as_deref(),
141 Some("/tahinli/pub/discussions/1")
142 );
143 let discussion = "/tahinli/pub/discussions/1";
144
145 // Bob answers at the top level, then replies to that comment.
146 let page = http.get(discussion, Some(bob.as_str())).await;
147 assert_eq!(page.status, 200, "GET discussion as bob");
148 let csrf_bob = input_value(&page.body, "csrf");
149 let res = http
150 .post_form(
151 &format!("{discussion}/comments"),
152 &[("csrf", &csrf_bob), ("body", "Try git rebase -i.")],
153 Some(bob.as_str()),
154 )
155 .await;
156 assert_eq!(res.status, 303, "top comment: {}", res.body);
157 let top = anchored_id(res.location().as_deref());
158
159 let res = http
160 .post_form(
161 &format!("{discussion}/comments"),
162 &[
163 ("csrf", &csrf_bob),
164 ("parent", &top),
165 ("body", "Thanks, that worked."),
166 ],
167 Some(bob.as_str()),
168 )
169 .await;
170 assert_eq!(res.status, 303, "reply: {}", res.body);
171 let reply = anchored_id(res.location().as_deref());
172
173 // A reply to a reply is refused, and the page says why.
174 let res = http
175 .post_form(
176 &format!("{discussion}/comments"),
177 &[
178 ("csrf", &csrf_bob),
179 ("parent", &reply),
180 ("body", "Too deep"),
181 ],
182 Some(bob.as_str()),
183 )
184 .await;
185 assert_eq!(res.status, 200, "nested reply shows the refusal page");
186 assert!(
187 res.body.contains("Could not save") && res.body.contains("one level"),
188 "nested reply was not refused: {}",
189 res.body
190 );
191
192 // Tahinli (the author) marks the top comment as the answer.
193 let page = http.get(discussion, Some(tahinli.as_str())).await;
194 let csrf_tahinli = input_value(&page.body, "csrf");
195 let res = http
196 .post_form(
197 &format!("{discussion}/answer"),
198 &[
199 ("csrf", &csrf_tahinli),
200 ("comment_id", &top),
201 ("answer", "1"),
202 ],
203 Some(tahinli.as_str()),
204 )
205 .await;
206 assert_eq!(res.status, 303, "answer: {}", res.body);
207 let page = http.get(discussion, Some(bob.as_str())).await;
208 assert!(
209 page.body.contains("Answered"),
210 "the answered badge is missing after marking an answer"
211 );
212
213 // Tahinli locks the conversation. Bob can no longer comment, and cannot lock it either.
214 let page = http.get(discussion, Some(tahinli.as_str())).await;
215 let csrf_tahinli = input_value(&page.body, "csrf");
216 let res = http
217 .post_form(
218 &format!("{discussion}/lock"),
219 &[("csrf", &csrf_tahinli), ("locked", "1")],
220 Some(tahinli.as_str()),
221 )
222 .await;
223 assert_eq!(res.status, 303, "lock: {}", res.body);
224 let res = http
225 .post_form(
226 &format!("{discussion}/comments"),
227 &[("csrf", &csrf_bob), ("body", "Still here")],
228 Some(bob.as_str()),
229 )
230 .await;
231 assert_eq!(res.status, 403, "bob comments on a locked discussion");
232 let res = http
233 .post_form(
234 &format!("{discussion}/lock"),
235 &[("csrf", &csrf_bob), ("locked", "0")],
236 Some(bob.as_str()),
237 )
238 .await;
239 assert_eq!(res.status, 403, "bob locks a discussion");
240
241 // Turning discussions off hides the tab and the pages.
242 let settings = http
243 .get("/tahinli/pub/settings", Some(tahinli.as_str()))
244 .await;
245 let csrf = input_value(&settings.body, "csrf");
246 let res = http
247 .post_form(
248 "/tahinli/pub/settings/discussions",
249 &[("csrf", &csrf), ("enabled", "0")],
250 Some(tahinli.as_str()),
251 )
252 .await;
253 assert_eq!(res.status, 303, "disable: {}", res.body);
254 let repo_page = http.get("/tahinli/pub", Some(tahinli.as_str())).await;
255 assert!(
256 !repo_page.body.contains("href=\"/tahinli/pub/discussions\""),
257 "the tab stays after turning discussions off"
258 );
259 assert_eq!(
260 http.get(discussion, Some(tahinli.as_str())).await.status,
261 404,
262 "discussion page while off"
263 );
264}