BlueGit

timfa bluegit

Files

bluegit / internal / server / mr_layout_test.go

mr_layout_test.go

mr_layout_test.go 4.0 KiB Download Raw
1package server
2
3import (
4 "net/http"
5 "net/http/cookiejar"
6 "net/http/httptest"
7 "net/url"
8 "os/exec"
9 "path/filepath"
10 "regexp"
11 "strings"
12 "testing"
13
14 "gitlab.com/Timfa/bluegit/internal/auth"
15 "gitlab.com/Timfa/bluegit/internal/config"
16 "gitlab.com/Timfa/bluegit/internal/store"
17)
18
19func TestMRPageLayout(t *testing.T) {
20 if _, err := exec.LookPath("git"); err != nil {
21 t.Skip("git ontbreekt")
22 }
23 storage := t.TempDir()
24 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
25 if err != nil {
26 t.Fatal(err)
27 }
28 t.Cleanup(func() { _ = st.Close() })
29 cfg := config.Config{
30 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
31 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},
32 Git: config.Git{Binary: "git", DefaultBranch: "master"},
33 }
34 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
35 ts := httptest.NewServer(srv.Handler())
36 t.Cleanup(ts.Close)
37 jar, _ := cookiejar.New(nil)
38 tim := &http.Client{Jar: jar}
39 page := read(t, mustGet(t, tim, ts.URL+"/login"))
40 page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{
41 "csrf": {csrfOf(t, page)}, "name": {"layout"}, "visibility": {"public"},
42 }))
43 page = read(t, mustPost(t, tim, ts.URL+"/settings/tokens", url.Values{
44 "csrf": {csrfOf(t, page)}, "name": {"laptop"},
45 }))
46 token := between(page, `<p class="code">`, "</p>")
47 remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/layout.git"
48 work := t.TempDir()
49 git(t, work, "init", "-b", "master")
50 git(t, work, "config", "user.email", "tim@users.git.timfalken.com")
51 git(t, work, "config", "user.name", "tim")
52 git(t, work, "config", "commit.gpgsign", "false")
53 writeFile(t, filepath.Join(work, "hello.txt"), "alpha\n")
54 git(t, work, "add", "hello.txt")
55 git(t, work, "commit", "-m", "start")
56 git(t, work, "remote", "add", "origin", remote)
57 git(t, work, "push", "-u", "origin", "master")
58 git(t, work, "checkout", "-b", "topic")
59 writeFile(t, filepath.Join(work, "a.txt"), "een\n")
60 git(t, work, "add", "a.txt")
61 git(t, work, "commit", "-m", "eerste")
62 writeFile(t, filepath.Join(work, "b.txt"), "twee\n")
63 git(t, work, "add", "b.txt")
64 git(t, work, "commit", "-m", "tweede")
65 writeFile(t, filepath.Join(work, "a.txt"), "drie\n")
66 git(t, work, "add", "a.txt")
67 git(t, work, "commit", "-m", "derde")
68 git(t, work, "push", "-u", "origin", "topic")
69
70 page = read(t, mustPost(t, tim, ts.URL+"/tim/layout/-/merge-requests", url.Values{
71 "csrf": {csrfOf(t, page)}, "title": {"Layout van de pagina"}, "description": {"tekst"},
72 "source": {"topic"}, "target": {"master"},
73 }))
74 for _, want := range []string{
75 `class="mrstate open"`, ">Open<", "wants to merge", "3 commits",
76 `data-copy="topic"`, `data-copy="master"`,
77 `Commits <span class="count">3</span>`,
78 `Changes <span class="count">2</span>`,
79 `Pipelines <span class="count">0</span>`,
80 "Delete source branch", "> Squash",
81 "created just now", `href="/users/tim"`, `class="userchip"`,
82 "mrwidget",
83 } {
84 if !strings.Contains(page, want) {
85 t.Fatalf("open MR mist %q:\n%s", want, page)
86 }
87 }
88
89 page = read(t, mustPost(t, tim, ts.URL+"/tim/layout/-/merge-requests/1/merge", url.Values{
90 "csrf": {csrfOf(t, page)},
91 }))
92 git(t, work, "fetch", "origin", "master")
93 shaBytes, err := exec.Command("git", "-C", work, "rev-parse", "origin/master").Output()
94 if err != nil {
95 t.Fatal(err)
96 }
97 sha := strings.TrimSpace(string(shaBytes))
98 short := sha
99 if len(short) > 8 {
100 short = short[:8]
101 }
102 for _, want := range []string{
103 `class="mrstate merged"`, ">Merged<", "Merged by", `href="/users/tim"`,
104 "merged just now", `<code>` + short + `</code>`, `data-copy="` + sha + `"`,
105 `Commits <span class="count">3</span>`,
106 `Changes <span class="count">2</span>`,
107 } {
108 if !strings.Contains(page, want) {
109 t.Fatalf("gemergede MR mist %q:\n%s", want, page)
110 }
111 }
112 if regexp.MustCompile(`(?s)pushlist">` + sha).MatchString(page) {
113 t.Fatalf("de sha hoort niet als ruwe tijdlijntekst:\n%s", page)
114 }
115}