forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

templates_route_tests.rs268 lines, 7.8 KiBHistoryRawDownload
1use forge_core::Visibility;
2
3use crate::test_support::fixture::{Fixture, git};
4use forge_web_pages::pages::issues_list::test_util::{CSRF, OWNER, REPO, TOKEN, send};
5
6const BUG: &str = "---\nname: Bug report\nabout: Something broke\ntitle: \"[Bug]: \"\nlabels: bug, needs triage\n---\n## Steps to reproduce\n\n1. Do the thing\n";
7const FEATURE: &str = "---\nname: Feature request\nabout: Suggest an idea\n---\n## Idea\n";
8
9/// A repo whose default branch gets `files` committed on top of the fixture's history.
10/// Tahinli is signed in.
11async fn setup(files: &[(&str, &str)]) -> Fixture {
12 let fx = Fixture::new(Visibility::Public).await;
13 for (path, text) in files {
14 let full = fx.work.join(path);
15 std::fs::create_dir_all(full.parent().unwrap()).unwrap();
16 std::fs::write(full, text).unwrap();
17 }
18 git(&fx.work, &["add", "-A"]);
19 git(&fx.work, &["commit", "-q", "-m", "Add templates"]);
20 git(
21 &fx.work,
22 &[
23 "push",
24 "-q",
25 &fx.bare.to_string_lossy(),
26 "HEAD:refs/heads/main",
27 ],
28 );
29 let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
30 crate::test_support::create_session(&fx.store, &owner.id, TOKEN, CSRF).await;
31 fx
32}
33
34fn new_issue_uri(query: &str) -> String {
35 format!("/{OWNER}/{REPO}/issues/new{query}")
36}
37
38/// The text of the tag that starts at `needle`, up to its closing `>`.
39fn tag_at(html: &str, needle: &str) -> String {
40 let at = html.find(needle).expect("needle in page");
41 let end = html[at..].find('>').expect("tag end") + at;
42 html[at..end].to_owned()
43}
44
45#[tokio::test]
46async fn several_templates_show_a_chooser() {
47 let fx = setup(&[
48 (".forge/ISSUE_TEMPLATE/bug.md", BUG),
49 (".forge/ISSUE_TEMPLATE/feature.md", FEATURE),
50 ])
51 .await;
52 let reply = send(&fx.router, "GET", &new_issue_uri(""), true, None, &[]).await;
53 assert_eq!(reply.status, 200);
54 let html = reply.text();
55 assert!(html.contains("Bug report"));
56 assert!(html.contains("Something broke"));
57 assert!(html.contains("issues/new?template=bug\""));
58 assert!(html.contains("issues/new?template=feature\""));
59 assert!(html.contains("Open a blank issue"));
60 assert!(html.contains("issues/new?blank=1\""));
61 assert!(!html.contains("name=\"title\""));
62}
63
64#[tokio::test]
65async fn chosen_template_prefills_and_preselects_existing_labels() {
66 let fx = setup(&[
67 (".forge/ISSUE_TEMPLATE/bug.md", BUG),
68 (".forge/ISSUE_TEMPLATE/feature.md", FEATURE),
69 ])
70 .await;
71 let repo = fx
72 .store
73 .repo_by_owner_and_name(OWNER, REPO)
74 .await
75 .unwrap()
76 .unwrap();
77 fx.store
78 .create_label(&repo.id, "bug", "#d73a4a", "")
79 .await
80 .unwrap();
81 fx.store
82 .create_label(&repo.id, "docs", "#0075ca", "")
83 .await
84 .unwrap();
85 let bug_id = fx
86 .store
87 .list_labels(&repo.id)
88 .await
89 .unwrap()
90 .into_iter()
91 .find(|l| l.name == "bug")
92 .unwrap()
93 .id;
94
95 let reply = send(
96 &fx.router,
97 "GET",
98 &new_issue_uri("?template=bug"),
99 true,
100 None,
101 &[],
102 )
103 .await;
104 assert_eq!(reply.status, 200);
105 let html = reply.text();
106 assert!(html.contains("name=\"title\""));
107 assert!(html.contains("value=\"[Bug]:\""));
108 assert!(html.contains("## Steps to reproduce"));
109 assert!(!html.contains("Choose a template"));
110 assert!(tag_at(&html, &format!("value=\"{bug_id}\"")).contains("checked"));
111 assert!(!html.contains("value=\"needs triage\""));
112 assert_eq!(html.matches("name=\"label\"").count(), 2);
113 let docs_id = fx
114 .store
115 .list_labels(&repo.id)
116 .await
117 .unwrap()
118 .into_iter()
119 .find(|l| l.name == "docs")
120 .unwrap()
121 .id;
122 assert!(!tag_at(&html, &format!("value=\"{docs_id}\"")).contains("checked"));
123}
124
125#[tokio::test]
126async fn blank_issue_from_chooser_is_empty() {
127 let fx = setup(&[
128 (".forge/ISSUE_TEMPLATE/bug.md", BUG),
129 (".forge/ISSUE_TEMPLATE/feature.md", FEATURE),
130 ])
131 .await;
132 let reply = send(
133 &fx.router,
134 "GET",
135 &new_issue_uri("?blank=1"),
136 true,
137 None,
138 &[],
139 )
140 .await;
141 let html = reply.text();
142 assert!(html.contains("name=\"title\""));
143 assert!(!html.contains("Steps to reproduce"));
144 assert!(!html.contains("Choose a template"));
145}
146
147#[tokio::test]
148async fn single_template_prefills_without_chooser() {
149 let fx = setup(&[(".forge/ISSUE_TEMPLATE/bug.md", BUG)]).await;
150 let html = send(&fx.router, "GET", &new_issue_uri(""), true, None, &[])
151 .await
152 .text();
153 assert!(html.contains("value=\"[Bug]:\""));
154 assert!(html.contains("## Steps to reproduce"));
155 assert!(!html.contains("Open a blank issue"));
156}
157
158#[tokio::test]
159async fn single_file_under_github_is_used_as_is() {
160 let fx = setup(&[(".github/ISSUE_TEMPLATE.md", "Plain body text\n")]).await;
161 let html = send(&fx.router, "GET", &new_issue_uri(""), true, None, &[])
162 .await
163 .text();
164 assert!(html.contains("Plain body text"));
165}
166
167#[tokio::test]
168async fn chooser_lists_at_most_twenty_templates() {
169 let names: Vec<String> = (0..21).map(|n| format!("t{n:02}")).collect();
170 let files: Vec<(String, String)> = names
171 .iter()
172 .map(|n| {
173 (
174 format!(".forge/ISSUE_TEMPLATE/{n}.md"),
175 format!("body {n}\n"),
176 )
177 })
178 .collect();
179 let refs: Vec<(&str, &str)> = files
180 .iter()
181 .map(|(p, t)| (p.as_str(), t.as_str()))
182 .collect();
183 let fx = setup(&refs).await;
184 let html = send(&fx.router, "GET", &new_issue_uri(""), true, None, &[])
185 .await
186 .text();
187 assert!(html.contains("issues/new?template=t19\""));
188 assert!(!html.contains("issues/new?template=t20\""));
189}
190
191#[tokio::test]
192async fn submitting_with_a_label_applies_it_to_the_issue() {
193 let fx = setup(&[(".forge/ISSUE_TEMPLATE/bug.md", BUG)]).await;
194 let repo = fx
195 .store
196 .repo_by_owner_and_name(OWNER, REPO)
197 .await
198 .unwrap()
199 .unwrap();
200 fx.store
201 .create_label(&repo.id, "bug", "#d73a4a", "")
202 .await
203 .unwrap();
204 let bug_id = fx
205 .store
206 .list_labels(&repo.id)
207 .await
208 .unwrap()
209 .into_iter()
210 .next()
211 .unwrap()
212 .id;
213 let form = format!("csrf={CSRF}&title=Crash+on+start&body=details&label={bug_id}");
214 let reply = send(
215 &fx.router,
216 "POST",
217 &new_issue_uri(""),
218 true,
219 Some(&form),
220 &[],
221 )
222 .await;
223 assert_eq!(reply.status, 303);
224 let issue = fx
225 .store
226 .issue_by_number(&repo.id, 1)
227 .await
228 .unwrap()
229 .unwrap();
230 let labels = fx.store.issue_labels(&issue.id).await.unwrap();
231 assert_eq!(labels.len(), 1);
232 assert_eq!(labels[0].name, "bug");
233}
234
235#[tokio::test]
236async fn compare_form_starts_with_the_pull_request_template() {
237 let fx = setup(&[(
238 ".forge/PULL_REQUEST_TEMPLATE.md",
239 "## Summary\n\nWhat changed?\n",
240 )])
241 .await;
242 git(&fx.work, &["checkout", "-q", "-b", "feature"]);
243 std::fs::write(fx.work.join("feature.txt"), "feature\n").unwrap();
244 git(&fx.work, &["add", "-A"]);
245 git(&fx.work, &["commit", "-q", "-m", "Add feature file"]);
246 git(
247 &fx.work,
248 &[
249 "push",
250 "-q",
251 &fx.bare.to_string_lossy(),
252 "feature:refs/heads/feature",
253 ],
254 );
255 git(&fx.work, &["checkout", "-q", "main"]);
256
257 let reply = send(
258 &fx.router,
259 "GET",
260 &format!("/{OWNER}/{REPO}/compare/main...feature"),
261 true,
262 None,
263 &[],
264 )
265 .await;
266 assert_eq!(reply.status, 200);
267 assert!(reply.text().contains("What changed?"));
268}