BlueGit

timfa bluegit

Files

bluegit / internal / server / templates_test.go

templates_test.go

feat: lees .bluegit-sjablonen en klikbare takenlijsten
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
79722e46 History
templates_test.go 9.7 KiB Download Raw
1package server
2
3import (
4 "context"
5 "encoding/json"
6 "net/http"
7 "net/http/cookiejar"
8 "net/http/httptest"
9 "net/url"
10 "path/filepath"
11 "strings"
12 "testing"
13
14 "gitlab.com/Timfa/bluegit/internal/access"
15 "gitlab.com/Timfa/bluegit/internal/auth"
16 "gitlab.com/Timfa/bluegit/internal/config"
17 "gitlab.com/Timfa/bluegit/internal/store"
18)
19
20func TestTemplatesAndTasks(t *testing.T) {
21 storage := t.TempDir()
22 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
23 if err != nil {
24 t.Fatal(err)
25 }
26 t.Cleanup(func() { _ = st.Close() })
27 cfg := config.Config{
28 BaseURL: "http://git.example", Listen: "127.0.0.1:0", StoragePath: storage,
29 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},
30 Git: config.Git{Binary: "git", DefaultBranch: "master"},
31 }
32 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
33 ts := httptest.NewServer(srv.Handler())
34 t.Cleanup(ts.Close)
35
36 jar, _ := cookiejar.New(nil)
37 tim := &http.Client{Jar: jar}
38 page := read(t, mustGet(t, tim, ts.URL+"/login"))
39 page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{
40 "csrf": {csrfOf(t, page)}, "name": {"sjablonen"}, "visibility": {"public"},
41 }))
42 page = read(t, mustPost(t, tim, ts.URL+"/settings/tokens", url.Values{"csrf": {csrfOf(t, page)}, "name": {"tim"}}))
43 token := between(page, `<p class="code">`, "</p>")
44 ctx := context.Background()
45 repo, err := st.RepoByPath(ctx, "tim", "sjablonen")
46 if err != nil {
47 t.Fatal(err)
48 }
49 rep, err := st.UpsertUser(ctx, 4, "rep", "", false)
50 if err != nil {
51 t.Fatal(err)
52 }
53 ro, err := st.UpsertUser(ctx, 3, "ro", "", false)
54 if err != nil {
55 t.Fatal(err)
56 }
57 if err := st.SetMember(ctx, "repo", repo.ID, "rep", access.RoleReporter); err != nil {
58 t.Fatal(err)
59 }
60 if err := st.SetMember(ctx, "repo", repo.ID, "ro", access.RoleReadonly); err != nil {
61 t.Fatal(err)
62 }
63 repToken, err := st.CreateToken(ctx, rep.ID, "rep")
64 if err != nil {
65 t.Fatal(err)
66 }
67 roToken, err := st.CreateToken(ctx, ro.ID, "ro")
68 if err != nil {
69 t.Fatal(err)
70 }
71
72 work := t.TempDir()
73 git(t, work, "init", "-b", "master")
74 git(t, work, "config", "user.email", "tim@example")
75 git(t, work, "config", "user.name", "tim")
76 writeFile(t, filepath.Join(work, ".bluegit", "issue_templates", "Default.md"), "Standaard issue-tekst\n")
77 writeFile(t, filepath.Join(work, ".bluegit", "issue_templates", "Bug.md"), "Bluegit bugtekst\n")
78 writeFile(t, filepath.Join(work, ".gitlab", "issue_templates", "Bug.md"), "Gitlab bugtekst\n")
79 writeFile(t, filepath.Join(work, ".gitlab", "issue_templates", "Legacy.md"), "Oude gitlab-tekst\n")
80 writeFile(t, filepath.Join(work, ".bluegit", "merge_request_templates", "Default.md"), "- [ ] controle\nBron %{source_branch}\nDoel %{target_branch}\n%{closes_issue}\n\n%{first_commit}\n")
81 writeFile(t, filepath.Join(work, ".gitlab", "merge_request_templates", "Default.md"), "GITLAB MR\n")
82 writeFile(t, filepath.Join(work, "README.md"), "start\n")
83 git(t, work, "add", ".")
84 git(t, work, "commit", "-m", "sjablonen")
85 remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/sjablonen.git"
86 git(t, work, "remote", "add", "origin", remote)
87 git(t, work, "push", "-u", "origin", "master")
88 git(t, work, "checkout", "-b", "12-fix")
89 writeFile(t, filepath.Join(work, "fix.txt"), "ok\n")
90 git(t, work, "add", "fix.txt")
91 git(t, work, "commit", "-m", "Fix login", "-m", "Details here")
92 git(t, work, "push", "-u", "origin", "12-fix")
93
94 page = read(t, mustGet(t, tim, ts.URL+"/tim/sjablonen/-/issues/new"))
95 for _, want := range []string{"Standaard issue-tekst", `value="Bug"`, `value="Legacy"`, `value="Default" selected`} {
96 if !strings.Contains(page, want) {
97 t.Fatalf("nieuw issue mist %q\n%s", want, page)
98 }
99 }
100 if strings.Contains(page, "Gitlab bugtekst") {
101 t.Fatal("gitlab-bug hoort niet de standaard te zijn")
102 }
103 bug := read(t, mustGet(t, tim, ts.URL+"/tim/sjablonen/-/issues/new?template=Bug"))
104 if !strings.Contains(bug, "Bluegit bugtekst") || strings.Contains(bug, "Gitlab bugtekst") {
105 t.Fatalf("bug-sjabloon:\n%s", bug)
106 }
107 legacy := read(t, mustGet(t, tim, ts.URL+"/tim/sjablonen/-/issues/new?template=Legacy"))
108 if !strings.Contains(legacy, "Oude gitlab-tekst") {
109 t.Fatalf("terugval:\n%s", legacy)
110 }
111
112 res := postJSON(t, http.MethodPost, ts.URL+"/api/v1/repos/tim/sjablonen/labels", token, map[string]any{"name": "bug", "color": "#428fdc"})
113 if res.StatusCode != http.StatusCreated {
114 t.Fatalf("label %d %s", res.StatusCode, read(t, res))
115 }
116 res.Body.Close()
117
118 res = postJSON(t, http.MethodPost, ts.URL+"/api/v1/repos/tim/sjablonen/issues", repToken, map[string]any{
119 "title": "Gelabeld", "description": "/label ~bug\n\n- [ ] een\n- [x] twee\n",
120 })
121 body := read(t, res)
122 if res.StatusCode != http.StatusCreated || strings.Contains(body, "/label") || !strings.Contains(body, "- [ ] een") {
123 t.Fatalf("reporter %d %s", res.StatusCode, body)
124 }
125 var created map[string]any
126 if err := json.Unmarshal([]byte(body), &created); err != nil {
127 t.Fatal(err)
128 }
129 labels, _ := created["labels"].([]any)
130 if len(labels) != 1 || labels[0] != "bug" {
131 t.Fatalf("labels %#v", created["labels"])
132 }
133
134 res = postJSON(t, http.MethodPost, ts.URL+"/api/v1/repos/tim/sjablonen/issues", roToken, map[string]any{
135 "title": "Blijft", "description": "/label ~bug\n/confidential\n\nTekst\n",
136 })
137 body = read(t, res)
138 if res.StatusCode != http.StatusCreated || !strings.Contains(body, "/label ~bug") || strings.Contains(body, "/confidential") {
139 t.Fatalf("readonly %d %s", res.StatusCode, body)
140 }
141 if err := json.Unmarshal([]byte(body), &created); err != nil {
142 t.Fatal(err)
143 }
144 if created["confidential"] != true {
145 t.Fatalf("confidential %#v", created["confidential"])
146 }
147 if labs, _ := created["labels"].([]any); len(labs) != 0 {
148 t.Fatalf("readonly labels %#v", created["labels"])
149 }
150
151 res = postJSON(t, http.MethodPost, ts.URL+"/api/v1/repos/tim/sjablonen/issues", repToken, map[string]any{
152 "title": "Hek", "description": "```\n/label ~bug\n```\nBlijft\n",
153 })
154 body = read(t, res)
155 if res.StatusCode != http.StatusCreated || !strings.Contains(body, "/label ~bug") {
156 t.Fatalf("hek %d %s", res.StatusCode, body)
157 }
158 if err := json.Unmarshal([]byte(body), &created); err != nil {
159 t.Fatal(err)
160 }
161 if labs, _ := created["labels"].([]any); len(labs) != 0 {
162 t.Fatalf("hek labels %#v", created["labels"])
163 }
164
165 page = read(t, mustGet(t, tim, ts.URL+"/tim/sjablonen/-/issues/1"))
166 if !strings.Contains(page, "1 of 2 tasks completed") || !strings.Contains(page, `name="kind" value="issue"`) {
167 t.Fatalf("taken:\n%s", page)
168 }
169 page = read(t, mustPost(t, tim, ts.URL+"/tim/sjablonen/-/tasks", url.Values{
170 "csrf": {csrfOf(t, page)}, "kind": {"issue"}, "id": {"1"}, "index": {"0"}, "checked": {"1"},
171 }))
172 if !strings.Contains(page, "2 of 2 tasks completed") || !strings.Contains(page, "- [x] een") {
173 t.Fatalf("na klik:\n%s", page)
174 }
175 page = read(t, mustPost(t, tim, ts.URL+"/tim/sjablonen/-/issues/1", url.Values{
176 "csrf": {csrfOf(t, page)}, "action": {"note"}, "body": {"- [ ] reactie"},
177 }))
178 if !strings.Contains(page, `name="kind" value="issue-note"`) {
179 t.Fatalf("reactie:\n%s", page)
180 }
181 noteID := between(page[strings.Index(page, `name="kind" value="issue-note"`):], `name="id" value="`, `"`)
182 page = read(t, mustPost(t, tim, ts.URL+"/tim/sjablonen/-/tasks", url.Values{
183 "csrf": {csrfOf(t, page)}, "kind": {"issue-note"}, "id": {noteID}, "index": {"0"}, "checked": {"1"},
184 }))
185 if !strings.Contains(page, "reactie") {
186 t.Fatalf("reactie na klik:\n%s", page)
187 }
188 res = postJSON(t, http.MethodGet, ts.URL+"/api/v1/repos/tim/sjablonen/issues/1", token, nil)
189 body = read(t, res)
190 if !strings.Contains(body, "- [x] reactie") {
191 t.Fatalf("bron van de reactie:\n%s", body)
192 }
193
194 res = postJSON(t, http.MethodPost, ts.URL+"/api/v1/repos/tim/sjablonen/issues/1/notes", repToken, map[string]any{"body": "/assign @tim\n\nZie dit"})
195 body = read(t, res)
196 if res.StatusCode != http.StatusCreated || strings.Contains(body, "/assign") || !strings.Contains(body, "Zie dit") {
197 t.Fatalf("toewijzen %d %s", res.StatusCode, body)
198 }
199 res = postJSON(t, http.MethodGet, ts.URL+"/api/v1/repos/tim/sjablonen/issues/1", repToken, nil)
200 body = read(t, res)
201 if !strings.Contains(body, `"assignees":["tim"]`) {
202 t.Fatalf("assignee:\n%s", body)
203 }
204
205 mrPage := read(t, mustGet(t, tim, ts.URL+"/tim/sjablonen/-/merge-requests/new?source=12-fix&target=master"))
206 for _, want := range []string{"%{source_branch}", "- [ ] controle", `value="Default" selected`} {
207 if !strings.Contains(mrPage, want) {
208 t.Fatalf("mr-formulier mist %q\n%s", want, mrPage)
209 }
210 }
211 if strings.Contains(mrPage, "GITLAB MR") {
212 t.Fatal("gitlab-default wint niet")
213 }
214 made := read(t, mustPost(t, tim, ts.URL+"/tim/sjablonen/-/merge-requests", url.Values{
215 "csrf": {csrfOf(t, mrPage)}, "source": {"12-fix"}, "target": {"master"}, "title": {"Fix"},
216 "description": {"- [ ] controle\nBron %{source_branch}\nDoel %{target_branch}\n%{closes_issue}\n\n%{first_commit}\n"},
217 }))
218 for _, want := range []string{"Bron 12-fix", "Doel master", "Closes #12", "Fix login", "Details here", "0 of 1 tasks completed"} {
219 if !strings.Contains(made, want) {
220 t.Fatalf("mr mist %q\n%s", want, made)
221 }
222 }
223 if strings.Contains(made, "%{source_branch}") || strings.Contains(made, "GITLAB MR") {
224 t.Fatalf("variabelen bleven staan:\n%s", made)
225 }
226 made = read(t, mustPost(t, tim, ts.URL+"/tim/sjablonen/-/tasks", url.Values{
227 "csrf": {csrfOf(t, made)}, "kind": {"mr"}, "id": {"1"}, "index": {"0"}, "checked": {"1"},
228 }))
229 if !strings.Contains(made, "1 of 1 tasks completed") || !strings.Contains(made, "- [x] controle") {
230 t.Fatalf("mr-taak:\n%s", made)
231 }
232
233 info := read(t, mustGet(t, tim, ts.URL+"/-/info"))
234 for _, want := range []string{`id="templates"`, "Default.md", ".gitlab/", "tasks completed", "/label"} {
235 if !strings.Contains(info, want) {
236 t.Fatalf("info mist %q", want)
237 }
238 }
239}