internal/httpd/mrcommitview_test.go

ac8d910ff31c2fadc452928e1d56c64b4af25495
gitbay/internal/httpd/mrcommitview_test.go

55 lines · 1808 bytes

 1package httpd
 2
 3import (
 4	"net/http/httptest"
 5	"strconv"
 6	"strings"
 7	"testing"
 8
 9	"gitbay.org/gitbay/internal/control"
10)
11
12// The per-commit diff is read-only (no comment links: review comments
13// are anchored to the head diff's lines), and only a commit the merge
14// request carries is shown.
15func TestMRCommitViewReadOnlyAndInRange(t *testing.T) {
16	st, cfg, alice, _, repo, n, v1, v2, _ := rangeDiffFixture(t)
17	git := rangeDiffGitRunner(t)
18	dir := control.RepoDir(cfg.Server.Root, repo.OwnerName, repo.Name)
19	git(dir, "update-ref", "refs/merge-requests/"+strconv.FormatInt(n, 10)+"/head", v2)
20	s := New(cfg, st, nil)
21	cookie := loginCookie(t, st, alice.ID)
22
23	get := func(query string) string {
24		t.Helper()
25		req := httptest.NewRequest("GET", "/alice/secret/mrs/"+strconv.FormatInt(n, 10)+query, nil)
26		req.SetPathValue("owner", repo.OwnerName)
27		req.SetPathValue("repo", repo.Name)
28		req.SetPathValue("n", strconv.FormatInt(n, 10))
29		req.AddCookie(cookie)
30		rr := httptest.NewRecorder()
31		s.mr(rr, req)
32		if rr.Code != 200 {
33			t.Fatalf("%s: status %d", query, rr.Code)
34		}
35		return rr.Body.String()
36	}
37
38	full := get("?view=diff")
39	if !strings.Contains(full, `class="cmt"`) {
40		t.Fatal("full diff has no comment links")
41	}
42	one := get("?view=diff&commit=" + v2)
43	if strings.Contains(one, `class="cmt"`) {
44		t.Error("per-commit diff offers comment links")
45	}
46	if !strings.Contains(one, "read-only") {
47		t.Error("per-commit diff does not say it is read-only")
48	}
49	// v1 was amended away: it is in the repository's objects but not in
50	// the merge request's range, so the page falls back to the full diff.
51	stale := get("?view=diff&commit=" + v1)
52	if strings.Contains(stale, "read-only") || !strings.Contains(stale, `class="cmt"`) {
53		t.Error("a commit outside the range was shown as the selected commit")
54	}
55}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form