BlueGit

timfa bluegit

Files

bluegit / internal / server / branch_page_test.go

branch_page_test.go

feat: geef de branches- en tagspagina de GitLab-opbouw
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed 6e85d750 History
branch_page_test.go 6.3 KiB Download Raw
1package server
2
3import (
4 "io"
5 "net/http"
6 "net/http/cookiejar"
7 "net/http/httptest"
8 "net/url"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "strings"
13 "testing"
14 "time"
15
16 "gitlab.com/Timfa/bluegit/internal/auth"
17 "gitlab.com/Timfa/bluegit/internal/config"
18 "gitlab.com/Timfa/bluegit/internal/store"
19)
20
21func TestBranchIsStale(t *testing.T) {
22 now := time.Date(2026, 10, 23, 12, 0, 0, 0, time.UTC)
23 old := now.Add(-91 * 24 * time.Hour).Unix()
24 fresh := now.Add(-10 * 24 * time.Hour).Unix()
25 if !branchIsStale(old, false, now) {
26 t.Fatal("een branch zonder commit in drie maanden is inactief")
27 }
28 if branchIsStale(old, true, now) {
29 t.Fatal("de standaardbranch blijft actief")
30 }
31 if branchIsStale(fresh, false, now) {
32 t.Fatal("een recente branch is actief")
33 }
34 if branchIsStale(0, false, now) {
35 t.Fatal("een branch zonder datum is actief")
36 }
37}
38
39func TestBranchGroupsAndArchive(t *testing.T) {
40 if _, err := exec.LookPath("git"); err != nil {
41 t.Skip("git ontbreekt")
42 }
43 storage := t.TempDir()
44 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
45 if err != nil {
46 t.Fatal(err)
47 }
48 t.Cleanup(func() { _ = st.Close() })
49 cfg := config.Config{
50 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
51 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},
52 Git: config.Git{Binary: "git", DefaultBranch: "master"},
53 }
54 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
55 ts := httptest.NewServer(srv.Handler())
56 t.Cleanup(ts.Close)
57 jar, _ := cookiejar.New(nil)
58 tim := &http.Client{Jar: jar}
59 page := read(t, mustGet(t, tim, ts.URL+"/login"))
60 page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{
61 "csrf": {csrfOf(t, page)}, "name": {"groepen"}, "visibility": {"public"},
62 }))
63 page = read(t, mustPost(t, tim, ts.URL+"/settings/tokens", url.Values{
64 "csrf": {csrfOf(t, page)}, "name": {"laptop"},
65 }))
66 token := between(page, `<p class="code">`, "</p>")
67 remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/groepen.git"
68 work := t.TempDir()
69 git(t, work, "init", "-b", "master")
70 git(t, work, "config", "user.email", "tim@users.git.timfalken.com")
71 git(t, work, "config", "user.name", "tim")
72 writeFile(t, filepath.Join(work, "hello.txt"), "alpha\n")
73 git(t, work, "add", "hello.txt")
74 git(t, work, "commit", "-m", "start")
75 git(t, work, "remote", "add", "origin", remote)
76 git(t, work, "push", "-u", "origin", "master")
77
78 git(t, work, "checkout", "-b", "vers")
79 writeFile(t, filepath.Join(work, "hello.txt"), "beta\n")
80 git(t, work, "add", "hello.txt")
81 git(t, work, "commit", "-m", "vers-onderwerp")
82 git(t, work, "push", "-u", "origin", "vers")
83
84 git(t, work, "checkout", "-b", "oud", "master")
85 writeFile(t, filepath.Join(work, "hello.txt"), "oud\n")
86 git(t, work, "add", "hello.txt")
87 commitAt(t, work, "oud-onderwerp", "2020-01-15T12:00:00")
88 git(t, work, "push", "-u", "origin", "oud")
89
90 overview := read(t, mustGet(t, tim, ts.URL+"/tim/groepen/-/branches"))
91 for _, want := range []string{
92 `class="on"`, "Overview", "Active branches", "Stale branches",
93 `data-name="master"`, `data-name="vers"`, `data-name="oud"`,
94 "Filter by branch name", "New branch", "default",
95 `data-copy="oud"`, "Create MR", "vers-onderwerp", "oud-onderwerp",
96 "/-/branches/archive?name=oud&amp;format=zip",
97 "/-/branches/archive?name=oud&amp;format=tar.gz",
98 "/-/merge-requests/new?source=oud&amp;target=master",
99 } {
100 if !strings.Contains(overview, want) {
101 t.Fatalf("overzicht mist %q", want)
102 }
103 }
104 if strings.Contains(overview, `data-delete="master"`) {
105 t.Fatal("de standaardbranch heeft een verwijderknop")
106 }
107 if !strings.Contains(overview, `data-delete="oud"`) || !strings.Contains(overview, `data-delete="vers"`) {
108 t.Fatal("een gewone branch mist de verwijderknop")
109 }
110 oudAt := strings.Index(overview, `data-name="oud"`)
111 staleAt := strings.Index(overview, "Stale branches")
112 activeAt := strings.Index(overview, "Active branches")
113 if activeAt < 0 || staleAt < activeAt || oudAt < staleAt {
114 t.Fatalf("oud hoort in de inactieve groep:\n%s", overview)
115 }
116
117 active := read(t, mustGet(t, tim, ts.URL+"/tim/groepen/-/branches?state=active"))
118 if strings.Contains(active, `data-name="oud"`) || strings.Contains(active, "Stale branches") || !strings.Contains(active, `data-name="master"`) {
119 t.Fatalf("actief:\n%s", active)
120 }
121 stale := read(t, mustGet(t, tim, ts.URL+"/tim/groepen/-/branches?state=stale"))
122 if !strings.Contains(stale, `data-name="oud"`) || strings.Contains(stale, `data-name="master"`) || strings.Contains(stale, "Active branches") {
123 t.Fatalf("inactief:\n%s", stale)
124 }
125 if !strings.Contains(stale, `state=stale"`) || !strings.Contains(stale, `class="on"`) {
126 t.Fatalf("tab inactief:\n%s", stale)
127 }
128
129 zipRes := mustGet(t, tim, ts.URL+"/tim/groepen/-/branches/archive?name=oud&format=zip")
130 zipBody, err := io.ReadAll(zipRes.Body)
131 zipRes.Body.Close()
132 if err != nil {
133 t.Fatal(err)
134 }
135 if zipRes.StatusCode != http.StatusOK || len(zipBody) < 2 || zipBody[0] != 'P' || zipBody[1] != 'K' {
136 t.Fatalf("zip status %d lengte %d", zipRes.StatusCode, len(zipBody))
137 }
138 if !strings.Contains(zipRes.Header.Get("Content-Disposition"), "groepen-oud.zip") {
139 t.Fatalf("bestandsnaam: %s", zipRes.Header.Get("Content-Disposition"))
140 }
141 tarRes := mustGet(t, tim, ts.URL+"/tim/groepen/-/branches/archive?name=vers&format=tar.gz")
142 tarBody, err := io.ReadAll(tarRes.Body)
143 tarRes.Body.Close()
144 if err != nil {
145 t.Fatal(err)
146 }
147 if tarRes.StatusCode != http.StatusOK || len(tarBody) < 2 || tarBody[0] != 0x1f || tarBody[1] != 0x8b {
148 t.Fatalf("tar.gz status %d", tarRes.StatusCode)
149 }
150 missing := mustGet(t, tim, ts.URL+"/tim/groepen/-/branches/archive?name=geen&format=zip")
151 missing.Body.Close()
152 if missing.StatusCode != http.StatusNotFound {
153 t.Fatalf("ontbrekende branch: %d", missing.StatusCode)
154 }
155 bad := mustGet(t, tim, ts.URL+"/tim/groepen/-/branches/archive?name=master&format=exe")
156 bad.Body.Close()
157 if bad.StatusCode != http.StatusNotFound {
158 t.Fatalf("formaat: %d", bad.StatusCode)
159 }
160}
161
162func commitAt(t *testing.T, dir, msg, when string) {
163 t.Helper()
164 cmd := exec.Command("git", "-c", "commit.gpgsign=false", "commit", "-m", msg)
165 cmd.Dir = dir
166 cmd.Env = append(os.Environ(), "GIT_AUTHOR_DATE="+when, "GIT_COMMITTER_DATE="+when)
167 out, err := cmd.CombinedOutput()
168 if err != nil {
169 t.Fatalf("commit: %v\n%s", err, out)
170 }
171}