forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.010.7 MiB41ee476Tahinli29 days ago283 commits

pulls_e2e.rs738 lines, 24.7 KiBHistoryRawDownload
1//! End-to-end: pull requests against the real `forge` binary, with real `git` pushes.
2//!
3//! Each test boots `forge serve` on random ports with a temp data dir, signs in through the
4//! `/login` form, creates tokens, and pushes over HTTPS. Pages are scraped for CSRF tokens. The
5//! scenarios cover a same-repo merge that closes an issue, a push that updates an open pull,
6//! a fork pull merged by squash, and a conflict that blocks the merge.
7
8use crate::common::{
9 Http, Resp, Server, bin, input_value, launch, login, pick_ports, tool_on_path, write_config,
10};
11use std::fs;
12use std::path::{Path, PathBuf};
13use std::process::{Command, Output};
14use std::time::{Duration, Instant};
15
16use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, COOKIE};
17
18const TAHINLI_PW: &str = "tahinli-password-123";
19const BOB_PW: &str = "bob-password-4567";
20const BOB_EMAIL: &str = "bob@users.noreply.127.0.0.1";
21const REPO_PATH: &str = "/tahinli/r";
22
23/// Text of the first state badge on a page, e.g. `Open`, `Closed` or `Merged`.
24fn state_badge(body: &str) -> String {
25 let doc = scraper::Html::parse_document(body);
26 let selector = scraper::Selector::parse(".head-sub > .badge, .head-sub > .issue-badge").expect("selector");
27 doc.select(&selector)
28 .next()
29 .map(|el| el.text().collect::<String>().trim().to_owned())
30 .unwrap_or_default()
31}
32
33fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) {
34 let out = Command::new(bin())
35 .arg("--config")
36 .arg(config)
37 .args(["admin", "create-user", username])
38 .args(extra)
39 .env("FORGE_PASSWORD", password)
40 .output()
41 .expect("run admin create-user");
42 assert!(
43 out.status.success(),
44 "create-user {username} failed:\n{}",
45 String::from_utf8_lossy(&out.stderr)
46 );
47}
48
49/// Runs git with credential helpers disabled and extra env, returning stdout trimmed. Panics on
50/// failure.
51fn git_env(cwd: &Path, args: &[&str], env: &[(&str, &str)]) -> String {
52 let mut cmd = Command::new("git");
53 cmd.current_dir(cwd)
54 .args(["-c", "credential.helper="])
55 .args(args)
56 .env("GIT_TERMINAL_PROMPT", "0");
57 for (key, value) in env {
58 cmd.env(key, value);
59 }
60 let out: Output = cmd.output().expect("run git");
61 assert!(
62 out.status.success(),
63 "git {args:?} failed:\n{}",
64 String::from_utf8_lossy(&out.stderr)
65 );
66 String::from_utf8_lossy(&out.stdout).trim().to_owned()
67}
68
69fn git(cwd: &Path, args: &[&str]) -> String {
70 git_env(cwd, args, &[])
71}
72
73/// Writes `file`, commits it with `who` as author and committer, and returns the new HEAD.
74fn commit(
75 repo: &Path,
76 file: &str,
77 contents: &str,
78 message: &str,
79 who: &str,
80 email: &str,
81) -> String {
82 fs::write(repo.join(file), contents).expect("write file");
83 git(repo, &["add", file]);
84 git_env(
85 repo,
86 &["-c", "commit.gpgsign=false", "commit", "-q", "-m", message],
87 &[
88 ("GIT_AUTHOR_NAME", who),
89 ("GIT_AUTHOR_EMAIL", email),
90 ("GIT_COMMITTER_NAME", who),
91 ("GIT_COMMITTER_EMAIL", email),
92 ],
93 );
94 git(repo, &["rev-parse", "HEAD"])
95}
96
97/// Clones `url` into `into` (a path that does not exist yet) and returns it.
98fn clone(url: &str, into: &Path) -> PathBuf {
99 let parent = into.parent().expect("clone target has a parent");
100 git(
101 parent,
102 &["clone", "-q", url, into.to_str().expect("utf-8 path")],
103 );
104 into.to_path_buf()
105}
106
107/// The HTTPS remote for `owner/repo`, authenticated with a token.
108fn remote(port: u16, user: &str, token: &str, owner: &str, repo: &str) -> String {
109 format!("http://{user}:{token}@127.0.0.1:{port}/{owner}/{repo}.git")
110}
111
112/// The commit a remote branch points at, from `git ls-remote`.
113fn remote_sha(dir: &Path, url: &str, branch: &str) -> String {
114 let out = git(dir, &["ls-remote", url, &format!("refs/heads/{branch}")]);
115 out.split_whitespace()
116 .next()
117 .unwrap_or_else(|| panic!("branch {branch} missing on remote"))
118 .to_owned()
119}
120
121/// Number of parents of `commit` in the repository at `dir`.
122fn parent_count(dir: &Path, commit: &str) -> usize {
123 let line = git(dir, &["rev-list", "--parents", "-n1", commit]);
124 line.split_whitespace().count() - 1
125}
126
127/// The session's CSRF token, scraped from the token settings page.
128async fn csrf_token(http: &Http, session: &str) -> String {
129 let page = http.get_cookie("/settings/keys", session).await;
130 assert_eq!(page.status, 200, "GET /settings/keys");
131 input_value(&page.body, "csrf")
132}
133
134/// Creates an access token through `/settings/tokens` and returns the token shown once.
135async fn create_token(http: &Http, session: &str, name: &str) -> String {
136 let csrf = csrf_token(http, session).await;
137 let res = http
138 .post_form(
139 "/settings/tokens",
140 &[("csrf", &csrf), ("name", name), ("expires", "30")],
141 Some(session),
142 )
143 .await;
144 assert_eq!(res.status, 200, "create token: {}", res.body);
145 let doc = scraper::Html::parse_document(&res.body);
146 let selector = scraper::Selector::parse("pre.code code").expect("selector");
147 let token: String = doc
148 .select(&selector)
149 .next()
150 .expect("token shown after creation")
151 .text()
152 .collect();
153 let token = token.trim().to_owned();
154 assert!(token.starts_with("forge_"), "unexpected token shape");
155 token
156}
157
158/// Creates a public repo `name` owned by tahinli through `/new`.
159async fn create_repo(http: &Http, session: &str, name: &str) {
160 let page = http.get_cookie("/new", session).await;
161 assert_eq!(page.status, 200, "GET /new");
162 let csrf = input_value(&page.body, "csrf");
163 let res = http
164 .post_form(
165 "/new",
166 &[
167 ("csrf", &csrf),
168 ("name", name),
169 ("description", ""),
170 ("visibility", "public"),
171 ("default_branch", "main"),
172 ],
173 Some(session),
174 )
175 .await;
176 assert_eq!(res.status, 303, "create repo {name}: {}", res.body);
177 assert_eq!(
178 res.location().as_deref(),
179 Some(format!("/tahinli/{name}").as_str())
180 );
181}
182
183/// A running server with its own temp data dir. Field order matters: the server is dropped (and
184/// killed) before the temp dir is removed.
185struct Booted {
186 _server: Server,
187 _root: tempfile::TempDir,
188 dir: PathBuf,
189 http: Http,
190 http_port: u16,
191}
192
193/// The server's stdout and stderr, for assertion messages.
194fn server_log(b: &Booted) -> String {
195 fs::read_to_string(b.dir.join("serve.log")).unwrap_or_default()
196}
197
198/// Creates tahinli (admin) and bob, then boots the server on fresh ports.
199async fn boot() -> Booted {
200 if !tool_on_path("git") {
201 panic!("`git` was not found on PATH");
202 }
203 // Keep the path short: the internal unix socket path is limited to 108 bytes.
204 let root = tempfile::Builder::new()
205 .prefix("pr2e")
206 .tempdir_in("/tmp")
207 .expect("temp dir");
208 let dir = root.path().to_path_buf();
209 let ports = pick_ports();
210 let config = write_config(&dir, ports);
211
212 run_admin(&config, "tahinli", &["--admin"], TAHINLI_PW);
213 run_admin(&config, "bob", &[], BOB_PW);
214 let (server, ports) = launch(&dir, ports).await;
215 Booted {
216 _server: server,
217 _root: root,
218 dir,
219 http: Http::new(ports.http),
220 http_port: ports.http,
221 }
222}
223
224/// Creates `tahinli/r` with `main` pushed as tahinli, and returns tahinli's session and token.
225async fn tahinli_repo_with_main(b: &Booted) -> (String, String, PathBuf) {
226 let tahinli = login(&b.http, "tahinli", TAHINLI_PW).await;
227 let token = create_token(&b.http, &tahinli, "pulls-token").await;
228 create_repo(&b.http, &tahinli, "r").await;
229 let work = clone(
230 &remote(b.http_port, "tahinli", &token, "tahinli", "r"),
231 &b.dir.join("tahinli-work"),
232 );
233 commit(
234 &work,
235 "base.txt",
236 "base\n",
237 "base commit",
238 "tahinli",
239 "tahinli@example.test",
240 );
241 git(&work, &["push", "-q", "origin", "HEAD:refs/heads/main"]);
242 (tahinli, token, work)
243}
244
245/// Opens a pull request `base...head` on tahinli/r through the compare form. Returns its number.
246async fn open_pull(http: &Http, session: &str, base: &str, head: &str, title: &str) -> u32 {
247 let compare = http
248 .get_cookie(&format!("{REPO_PATH}/compare/{base}...{head}"), session)
249 .await;
250 assert_eq!(compare.status, 200, "GET compare {base}...{head}");
251 let csrf = csrf_token(http, session).await;
252 let res = http
253 .post_form(
254 &format!("{REPO_PATH}/pulls"),
255 &[
256 ("csrf", &csrf),
257 ("title", title),
258 ("body", ""),
259 ("base", base),
260 ("head", head),
261 ],
262 Some(session),
263 )
264 .await;
265 assert_eq!(res.status, 303, "open pull {head}: {}", res.body);
266 let location = res.location().expect("redirect to the pull");
267 location
268 .rsplit('/')
269 .next()
270 .and_then(|n| n.parse().ok())
271 .unwrap_or_else(|| panic!("no pull number in {location}"))
272}
273
274/// Creates an issue on tahinli/r through the web form. Returns its number.
275async fn open_issue(http: &Http, session: &str, title: &str) -> u32 {
276 let page = http
277 .get_cookie(&format!("{REPO_PATH}/issues/new"), session)
278 .await;
279 assert_eq!(page.status, 200, "GET issues/new");
280 let csrf = input_value(&page.body, "csrf");
281 let res = http
282 .post_form(
283 &format!("{REPO_PATH}/issues/new"),
284 &[("csrf", &csrf), ("title", title), ("body", "details")],
285 Some(session),
286 )
287 .await;
288 assert_eq!(res.status, 303, "create issue: {}", res.body);
289 let location = res.location().expect("redirect to the issue");
290 location
291 .rsplit('/')
292 .next()
293 .and_then(|n| n.parse().ok())
294 .unwrap_or_else(|| panic!("no issue number in {location}"))
295}
296
297/// Polls `path` until `done` accepts the body, failing after 10 seconds.
298async fn wait_page(
299 http: &Http,
300 session: &str,
301 path: &str,
302 what: &str,
303 done: impl Fn(&str) -> bool,
304) -> String {
305 let deadline = Instant::now() + Duration::from_secs(10);
306 loop {
307 let res = http.get_cookie(path, session).await;
308 assert_eq!(res.status, 200, "GET {path}");
309 if done(&res.body) {
310 return res.body;
311 }
312 assert!(
313 Instant::now() < deadline,
314 "timed out after 10s waiting for {what} on {path}"
315 );
316 tokio::time::sleep(Duration::from_millis(200)).await;
317 }
318}
319
320/// Posts a merge for pull `number` with `style` as `session`.
321async fn merge(http: &Http, session: &str, number: u32, style: &str) -> Resp {
322 let csrf = csrf_token(http, session).await;
323 http.post_form(
324 &format!("{REPO_PATH}/pulls/{number}/merge"),
325 &[("csrf", &csrf), ("style", style), ("message", "")],
326 Some(session),
327 )
328 .await
329}
330
331#[tokio::test]
332async fn same_repo_pull_merges_and_closes_issue() {
333 let b = boot().await;
334 let (tahinli, token, work) = tahinli_repo_with_main(&b).await;
335
336 let issue = open_issue(&b.http, &tahinli, "add x").await;
337 assert_eq!(issue, 1);
338
339 git(&work, &["checkout", "-q", "-b", "feature"]);
340 let feature_head = commit(
341 &work,
342 "x.txt",
343 "x\n",
344 "Add x\n\nFixes #1",
345 "tahinli",
346 "tahinli@example.test",
347 );
348 git(&work, &["push", "-q", "origin", "HEAD:refs/heads/feature"]);
349
350 let compare = b
351 .http
352 .get_cookie(&format!("{REPO_PATH}/compare/main...feature"), &tahinli)
353 .await;
354 assert_eq!(compare.status, 200, "GET compare");
355 assert!(compare.body.contains("Add x"), "compare lists the commit");
356
357 let number = open_pull(&b.http, &tahinli, "main", "feature", "Add x").await;
358 assert_eq!(number, 2, "the pull shares the issue number sequence");
359
360 let files = b
361 .http
362 .get_cookie(&format!("{REPO_PATH}/pulls/2/files"), &tahinli)
363 .await;
364 assert_eq!(files.status, 200, "GET pull files");
365 assert!(files.body.contains("x.txt"), "files tab shows x.txt");
366
367 let res = merge(&b.http, &tahinli, 2, "merge").await;
368 assert_eq!(res.status, 303, "merge: {}", res.body);
369 assert_eq!(res.location().as_deref(), Some("/tahinli/r/pulls/2"));
370
371 let url = remote(b.http_port, "tahinli", &token, "tahinli", "r");
372 let main_sha = remote_sha(&b.dir, &url, "main");
373 assert_ne!(main_sha, feature_head, "main is a new merge commit");
374 let check = clone(&url, &b.dir.join("check"));
375 assert_eq!(
376 parent_count(&check, "HEAD"),
377 2,
378 "main tip is a merge commit with two parents"
379 );
380 assert!(
381 git(&check, &["log", "--format=%s", "-n", "5"]).contains("Add x"),
382 "feature commit reachable from main"
383 );
384
385 let pr = b
386 .http
387 .get_cookie(&format!("{REPO_PATH}/pulls/2"), &tahinli)
388 .await;
389 assert_eq!(state_badge(&pr.body), "Merged");
390
391 wait_page(
392 &b.http,
393 &tahinli,
394 &format!("{REPO_PATH}/issues/1"),
395 "issue closed",
396 |body| state_badge(body) == "Closed",
397 )
398 .await;
399}
400
401#[tokio::test]
402async fn push_to_head_branch_updates_open_pull() {
403 let b = boot().await;
404 let (tahinli, _token, work) = tahinli_repo_with_main(&b).await;
405
406 git(&work, &["checkout", "-q", "-b", "feature2"]);
407 commit(
408 &work,
409 "one.txt",
410 "1\n",
411 "first on feature2",
412 "tahinli",
413 "tahinli@example.test",
414 );
415 git(&work, &["push", "-q", "origin", "HEAD:refs/heads/feature2"]);
416 let number = open_pull(&b.http, &tahinli, "main", "feature2", "Feature two").await;
417
418 let second = commit(
419 &work,
420 "two.txt",
421 "2\n",
422 "second on feature2",
423 "tahinli",
424 "tahinli@example.test",
425 );
426 git(&work, &["push", "-q", "origin", "HEAD:refs/heads/feature2"]);
427
428 let path = format!("{REPO_PATH}/pulls/{number}");
429 let page = wait_page(&b.http, &tahinli, &path, "head_updated event", |body| {
430 body.contains("pushed new commits")
431 })
432 .await;
433 assert!(page.contains("pushed new commits"));
434
435 let commits = b
436 .http
437 .get_cookie(&format!("{path}/commits"), &tahinli)
438 .await;
439 assert_eq!(commits.status, 200, "GET pull commits");
440 assert!(
441 commits.body.contains("second on feature2"),
442 "new commit listed"
443 );
444 assert!(
445 commits.body.contains("first on feature2"),
446 "old commit listed"
447 );
448 assert!(commits.body.contains(&second[..7]), "new commit sha shown");
449}
450
451/// Opens a live stream, as `session` or anonymously.
452async fn open_stream(http: &Http, path: &str, session: Option<&str>) -> reqwest::Response {
453 let mut req = http.client.get(format!("{}{path}", http.base));
454 if let Some(session) = session {
455 req = req.header(COOKIE, session);
456 }
457 let mut res = req.send().await.expect("stream request");
458 // A stream that opens sends its `retry` field first. It is not an event, so it is read here.
459 if res.status().is_success() {
460 let retry = tokio::time::timeout(Duration::from_secs(5), res.chunk())
461 .await
462 .expect("the retry field")
463 .expect("stream chunk")
464 .expect("stream still open");
465 assert!(String::from_utf8_lossy(&retry).contains("retry: 1000"));
466 }
467 res
468}
469
470/// Waits for the next event of a stream, which must be `changed`.
471async fn next_changed(res: &mut reqwest::Response, what: &str) {
472 let chunk = tokio::time::timeout(Duration::from_secs(5), res.chunk())
473 .await
474 .unwrap_or_else(|_| panic!("no event after {what}"))
475 .expect("stream chunk")
476 .expect("stream still open");
477 let text = String::from_utf8_lossy(&chunk);
478 assert!(text.contains("event: changed"), "{what}: {text}");
479}
480
481/// Asserts that nothing arrives on a stream for two seconds.
482async fn assert_quiet(res: &mut reqwest::Response, what: &str) {
483 let got = tokio::time::timeout(Duration::from_secs(2), res.chunk()).await;
484 assert!(got.is_err(), "{what}: the stream sent an event");
485}
486
487#[tokio::test]
488async fn pull_pages_follow_writes_live() {
489 let b = boot().await;
490 let (tahinli, token, work) = tahinli_repo_with_main(&b).await;
491
492 git(&work, &["checkout", "-q", "-b", "live"]);
493 let first = commit(
494 &work,
495 "live.txt",
496 "live\n",
497 "first on live",
498 "tahinli",
499 "tahinli@example.test",
500 );
501 git(&work, &["push", "-q", "origin", "HEAD:refs/heads/live"]);
502 let number = open_pull(&b.http, &tahinli, "main", "live", "Live pull").await;
503 let pull = format!("{REPO_PATH}/pulls/{number}");
504 let list = format!("{REPO_PATH}/pulls");
505
506 // The pull page and the list render the streams they follow.
507 let page = b.http.get_cookie(&pull, &tahinli).await;
508 assert_eq!(page.status, 200, "GET pull");
509 assert!(
510 page.body.contains(&format!("data-live=\"{pull}/events\"")),
511 "pull page follows its stream"
512 );
513 let listing = b.http.get_cookie(&list, &tahinli).await;
514 assert!(
515 listing
516 .body
517 .contains(&format!("data-live=\"{list}/events\"")),
518 "pull list follows its stream"
519 );
520
521 let mut pull_events = open_stream(&b.http, &format!("{pull}/events"), Some(&tahinli)).await;
522 assert_eq!(pull_events.status().as_u16(), 200, "pull stream");
523 let mut list_events = open_stream(&b.http, &format!("{list}/events"), Some(&tahinli)).await;
524 assert_eq!(list_events.status().as_u16(), 200, "list stream");
525 assert_quiet(&mut pull_events, "connecting to the pull stream").await;
526
527 // A comment on the conversation wakes both the pull and the list.
528 let csrf = csrf_token(&b.http, &tahinli).await;
529 let res = b
530 .http
531 .post_form(
532 &format!("{pull}/comments"),
533 &[
534 ("csrf", &csrf),
535 ("body", "a comment"),
536 ("action", "comment"),
537 ],
538 Some(&tahinli),
539 )
540 .await;
541 assert_eq!(res.status, 303, "comment: {}", res.body);
542 next_changed(&mut pull_events, "a comment").await;
543 next_changed(&mut list_events, "a comment").await;
544
545 // A review submitted on the head.
546 let csrf = csrf_token(&b.http, &tahinli).await;
547 let res = b
548 .http
549 .post_form(
550 &format!("{pull}/reviews"),
551 &[
552 ("csrf", &csrf),
553 ("state", "comment"),
554 ("body", "a review"),
555 ("head_oid", &first),
556 ],
557 Some(&tahinli),
558 )
559 .await;
560 assert_eq!(res.status, 303, "review: {}", res.body);
561 next_changed(&mut pull_events, "a review").await;
562
563 // A push to the head branch updates the pull and the list.
564 let second = commit(
565 &work,
566 "second.txt",
567 "2\n",
568 "second on live",
569 "tahinli",
570 "tahinli@example.test",
571 );
572 git(&work, &["push", "-q", "origin", "HEAD:refs/heads/live"]);
573 next_changed(&mut pull_events, "a push to the head").await;
574 next_changed(&mut list_events, "a push to the head").await;
575
576 // A commit status for the head commit (what CI reports) changes the pull.
577 let status = b
578 .http
579 .client
580 .post(format!(
581 "{}/api/v1/repos/tahinli/r/statuses/{second}",
582 b.http.base
583 ))
584 .header(AUTHORIZATION, format!("Bearer {token}"))
585 .header(CONTENT_TYPE, "application/json")
586 .body(r#"{"state":"success","context":"ci/test"}"#)
587 .send()
588 .await
589 .expect("status request");
590 assert_eq!(status.status().as_u16(), 201, "status posted");
591 next_changed(&mut pull_events, "a commit status on the head").await;
592
593 // Merging the pull changes it, and the list.
594 let res = merge(&b.http, &tahinli, number, "merge").await;
595 assert_eq!(res.status, 303, "merge: {}", res.body);
596 next_changed(&mut pull_events, "the merge").await;
597 next_changed(&mut list_events, "the merge").await;
598
599 // A private repo streams only to a viewer who can read it.
600 let csrf = csrf_token(&b.http, &tahinli).await;
601 let res = b
602 .http
603 .post_form(
604 "/new",
605 &[
606 ("csrf", &csrf),
607 ("name", "priv"),
608 ("description", ""),
609 ("visibility", "private"),
610 ("default_branch", "main"),
611 ],
612 Some(&tahinli),
613 )
614 .await;
615 assert_eq!(res.status, 303, "create private repo: {}", res.body);
616 let anonymous = open_stream(&b.http, "/tahinli/priv/pulls/events", None).await;
617 assert_eq!(anonymous.status().as_u16(), 404, "private list, anonymous");
618 let anonymous = open_stream(&b.http, "/tahinli/priv/pulls/1/events", None).await;
619 assert_eq!(anonymous.status().as_u16(), 404, "private pull, anonymous");
620 let member = open_stream(&b.http, "/tahinli/priv/pulls/events", Some(&tahinli)).await;
621 assert_eq!(member.status().as_u16(), 200, "private list, owner");
622}
623
624#[tokio::test]
625async fn fork_pull_squash_merge_credits_contributor() {
626 let b = boot().await;
627 let (tahinli, tahinli_token, _work) = tahinli_repo_with_main(&b).await;
628
629 let bob = login(&b.http, "bob", BOB_PW).await;
630 let bob_token = create_token(&b.http, &bob, "bob-token").await;
631
632 // Bob forks tahinli/r. The fork keeps the source name under bob's owner.
633 let csrf = csrf_token(&b.http, &bob).await;
634 let fork = b
635 .http
636 .post_form(&format!("{REPO_PATH}/fork"), &[("csrf", &csrf)], Some(&bob))
637 .await;
638 assert_eq!(fork.status, 303, "fork: {}\n{}", fork.body, server_log(&b));
639 assert_eq!(fork.location().as_deref(), Some("/bob/r"));
640
641 let bob_url = remote(b.http_port, "bob", &bob_token, "bob", "r");
642 let bob_work = clone(&bob_url, &b.dir.join("bob-work"));
643 git(&bob_work, &["checkout", "-q", "-b", "bob-feature"]);
644 let bob_head = commit(
645 &bob_work,
646 "bob.txt",
647 "bob\n",
648 "Bob change",
649 "bob",
650 BOB_EMAIL,
651 );
652 git(
653 &bob_work,
654 &["push", "-q", "origin", "HEAD:refs/heads/bob-feature"],
655 );
656
657 let number = open_pull(&b.http, &bob, "main", "bob:bob-feature", "Bob change").await;
658
659 // Bob can open the pull but cannot merge it.
660 let denied = merge(&b.http, &bob, number, "merge").await;
661 assert_eq!(denied.status, 403, "bob must not merge");
662
663 let res = merge(&b.http, &tahinli, number, "squash").await;
664 assert_eq!(res.status, 303, "tahinli squash merge: {}", res.body);
665
666 let tahinli_url = remote(b.http_port, "tahinli", &tahinli_token, "tahinli", "r");
667 let check = clone(&tahinli_url, &b.dir.join("check"));
668 assert_eq!(parent_count(&check, "HEAD"), 1, "squash makes one parent");
669 assert_ne!(
670 git(&check, &["rev-parse", "HEAD"]),
671 bob_head,
672 "main has a new squash commit, not bob's commit"
673 );
674 assert_eq!(git(&check, &["log", "-1", "--format=%an"]), "bob");
675 assert_eq!(git(&check, &["log", "-1", "--format=%ae"]), BOB_EMAIL);
676}
677
678#[tokio::test]
679async fn conflicting_pull_cannot_merge() {
680 let b = boot().await;
681 let (tahinli, token, work) = tahinli_repo_with_main(&b).await;
682
683 // Both sides edit the same line. The pull is opened while main still matches its base.
684 git(&work, &["checkout", "-q", "-b", "conflict"]);
685 commit(
686 &work,
687 "shared.txt",
688 "branch side\n",
689 "branch edit",
690 "tahinli",
691 "tahinli@example.test",
692 );
693 git(&work, &["push", "-q", "origin", "HEAD:refs/heads/conflict"]);
694 let number = open_pull(&b.http, &tahinli, "main", "conflict", "Conflicting").await;
695
696 git(&work, &["checkout", "-q", "main"]);
697 let main_head = commit(
698 &work,
699 "shared.txt",
700 "main side\n",
701 "main edit",
702 "tahinli",
703 "tahinli@example.test",
704 );
705 git(&work, &["push", "-q", "origin", "HEAD:refs/heads/main"]);
706
707 let path = format!("{REPO_PATH}/pulls/{number}");
708 let page = wait_page(&b.http, &tahinli, &path, "conflict detection", |body| {
709 body.contains("This branch has conflicts that must be resolved")
710 })
711 .await;
712 assert!(page.contains("shared.txt"), "conflict lists the file");
713 assert!(
714 !page.contains("merge-form"),
715 "no merge form on a conflicting pull"
716 );
717
718 let res = merge(&b.http, &tahinli, number, "merge").await;
719 assert_eq!(
720 res.status,
721 303,
722 "conflicting merge: {}\n{}",
723 res.body,
724 server_log(&b)
725 );
726 assert_eq!(
727 res.location().as_deref(),
728 Some(format!("{REPO_PATH}/pulls/{number}?merge_error=conflict").as_str()),
729 "refusal redirects back to the pull with the conflict code"
730 );
731
732 let url = remote(b.http_port, "tahinli", &token, "tahinli", "r");
733 assert_eq!(
734 remote_sha(&b.dir, &url, "main"),
735 main_head,
736 "main is unchanged by the refused merge"
737 );
738}