| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/cookiejar" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 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/gitrepo" |
| 17 | "gitlab.com/Timfa/bluegit/internal/store" |
| 18 | ) |
| 19 | |
| 20 | func TestPipelineEditorAndProtectedBranch(t *testing.T) { |
| 21 | if _, err := exec.LookPath("git"); err != nil { |
| 22 | t.Skip("git ontbreekt") |
| 23 | } |
| 24 | storage := t.TempDir() |
| 25 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | t.Cleanup(func() { _ = st.Close() }) |
| 30 | cfg := config.Config{ |
| 31 | BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage, |
| 32 | Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"}, |
| 33 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 34 | } |
| 35 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} |
| 36 | ts := httptest.NewServer(srv.Handler()) |
| 37 | t.Cleanup(ts.Close) |
| 38 | jar, _ := cookiejar.New(nil) |
| 39 | tim := &http.Client{Jar: jar} |
| 40 | res, err := tim.Get(ts.URL + "/login") |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | page := read(t, res) |
| 45 | res, err = tim.PostForm(ts.URL+"/new/repo", url.Values{ |
| 46 | "csrf": {csrfOf(t, page)}, "name": {"cied"}, "visibility": {"public"}, |
| 47 | }) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | page = read(t, res) |
| 52 | res, err = tim.PostForm(ts.URL+"/settings/tokens", url.Values{"csrf": {csrfOf(t, page)}, "name": {"laptop"}}) |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | token := between(read(t, res), `<p class="code">`, "</p>") |
| 57 | remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/cied.git" |
| 58 | work := t.TempDir() |
| 59 | git(t, work, "init", "-b", "master") |
| 60 | git(t, work, "config", "user.email", "tim@users.git.timfalken.com") |
| 61 | git(t, work, "config", "user.name", "tim") |
| 62 | yml := "stages:\n - test\nunit:\n stage: test\n image: alpine:3.20\n script:\n - echo ok\n" |
| 63 | writeFile(t, filepath.Join(work, "blue-ci-cd.yml"), yml) |
| 64 | git(t, work, "add", "blue-ci-cd.yml") |
| 65 | git(t, work, "commit", "-m", "ci") |
| 66 | git(t, work, "remote", "add", "origin", remote) |
| 67 | git(t, work, "push", "-u", "origin", "master") |
| 68 | git(t, work, "checkout", "-b", "feature") |
| 69 | git(t, work, "push", "-u", "origin", "feature") |
| 70 | |
| 71 | res, err = tim.Get(ts.URL + "/tim/cied/-/ci/editor?ref=feature") |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | editor := read(t, res) |
| 76 | for _, want := range []string{"Pipeline Editor", "No issues found", "echo ok", "ci-gutter", "Commit message"} { |
| 77 | if !strings.Contains(editor, want) { |
| 78 | t.Errorf("editor mist %q", want) |
| 79 | } |
| 80 | } |
| 81 | csrf := csrfOf(t, editor) |
| 82 | res, err = tim.PostForm(ts.URL+"/tim/cied/-/ci/lint", url.Values{ |
| 83 | "csrf": {csrf}, "ref": {"feature"}, "body": {"stages: [\n"}, |
| 84 | }) |
| 85 | if err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | raw := read(t, res) |
| 89 | var lint struct { |
| 90 | Status string `json:"status"` |
| 91 | StatusClass string `json:"statusClass"` |
| 92 | Issues []struct { |
| 93 | Line int `json:"line"` |
| 94 | Text string `json:"text"` |
| 95 | } `json:"issues"` |
| 96 | } |
| 97 | if err := json.Unmarshal([]byte(raw), &lint); err != nil { |
| 98 | t.Fatalf("lint json: %v\n%s", err, raw) |
| 99 | } |
| 100 | if lint.StatusClass != "failed" || len(lint.Issues) == 0 || strings.Count(lint.Issues[0].Text, "invalid YAML") != 1 { |
| 101 | t.Fatalf("lint: %+v", lint) |
| 102 | } |
| 103 | |
| 104 | changed := "stages:\n - test\nunit:\n stage: test\n image: alpine:3.20\n script:\n - echo changed\n" |
| 105 | res, err = tim.PostForm(ts.URL+"/tim/cied/-/ci/editor", url.Values{ |
| 106 | "csrf": {csrf}, "base": {"feature"}, "branch": {"feature"}, "message": {"pas de editor aan"}, "body": {changed}, |
| 107 | }) |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | saved := read(t, res) |
| 112 | if !strings.Contains(saved, "Committed.") || !strings.Contains(saved, "echo changed") { |
| 113 | t.Fatalf("na commit:\n%s", saved) |
| 114 | } |
| 115 | dir := gitrepo.Dir(storage, "tim", "cied") |
| 116 | if got := mustGit(t, dir, "show", "feature:blue-ci-cd.yml"); !strings.Contains(got, "echo changed") { |
| 117 | t.Fatalf("bestand: %s", got) |
| 118 | } |
| 119 | if msg := mustGit(t, dir, "log", "-1", "--format=%s", "feature"); strings.TrimSpace(msg) != "pas de editor aan" { |
| 120 | t.Fatalf("bericht: %s", msg) |
| 121 | } |
| 122 | |
| 123 | res, err = tim.PostForm(ts.URL+"/tim/cied/-/ci/editor", url.Values{ |
| 124 | "csrf": {csrfOf(t, saved)}, "base": {"feature"}, "branch": {"ci-try"}, "message": {"nieuwe branch"}, "body": {changed}, |
| 125 | }) |
| 126 | if err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | _ = read(t, res) |
| 130 | if got := mustGit(t, dir, "show", "ci-try:blue-ci-cd.yml"); !strings.Contains(got, "echo changed") { |
| 131 | t.Fatalf("nieuwe branch: %s", got) |
| 132 | } |
| 133 | |
| 134 | ctx := res.Request.Context() |
| 135 | repo, err := st.RepoByPath(ctx, "tim", "cied") |
| 136 | if err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | ada, err := st.UpsertUser(ctx, 3, "ada", "", false) |
| 140 | if err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | if err := st.SetMember(ctx, "repo", repo.ID, "ada", "developer"); err != nil { |
| 144 | t.Fatal(err) |
| 145 | } |
| 146 | adaC := clientAs(t, ts.URL, st, ada.ID) |
| 147 | res, err = adaC.Get(ts.URL + "/tim/cied/-/ci/editor?ref=master") |
| 148 | if err != nil { |
| 149 | t.Fatal(err) |
| 150 | } |
| 151 | adaPage := read(t, res) |
| 152 | res, err = adaC.PostForm(ts.URL+"/tim/cied/-/ci/editor", url.Values{ |
| 153 | "csrf": {csrfOf(t, adaPage)}, "base": {"master"}, "branch": {"master"}, "message": {"ada op master"}, "body": {changed + "\n"}, |
| 154 | }) |
| 155 | if err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | denied := read(t, res) |
| 159 | if !strings.Contains(denied, "This branch is protected.") { |
| 160 | t.Fatalf("beschermd:\n%s", denied) |
| 161 | } |
| 162 | if got := mustGit(t, dir, "show", "master:blue-ci-cd.yml"); strings.Contains(got, "echo changed") { |
| 163 | t.Fatalf("master veranderde:\n%s", got) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func TestPipelineEditorMissingFile(t *testing.T) { |
| 168 | if _, err := exec.LookPath("git"); err != nil { |
| 169 | t.Skip("git ontbreekt") |
| 170 | } |
| 171 | storage := t.TempDir() |
| 172 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 173 | if err != nil { |
| 174 | t.Fatal(err) |
| 175 | } |
| 176 | t.Cleanup(func() { _ = st.Close() }) |
| 177 | cfg := config.Config{ |
| 178 | BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage, |
| 179 | Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"}, |
| 180 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 181 | } |
| 182 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} |
| 183 | ts := httptest.NewServer(srv.Handler()) |
| 184 | t.Cleanup(ts.Close) |
| 185 | jar, _ := cookiejar.New(nil) |
| 186 | tim := &http.Client{Jar: jar} |
| 187 | res, err := tim.Get(ts.URL + "/login") |
| 188 | if err != nil { |
| 189 | t.Fatal(err) |
| 190 | } |
| 191 | page := read(t, res) |
| 192 | res, err = tim.PostForm(ts.URL+"/new/repo", url.Values{ |
| 193 | "csrf": {csrfOf(t, page)}, "name": {"leeg"}, "visibility": {"public"}, |
| 194 | }) |
| 195 | if err != nil { |
| 196 | t.Fatal(err) |
| 197 | } |
| 198 | page = read(t, res) |
| 199 | res, err = tim.PostForm(ts.URL+"/settings/tokens", url.Values{"csrf": {csrfOf(t, page)}, "name": {"laptop"}}) |
| 200 | if err != nil { |
| 201 | t.Fatal(err) |
| 202 | } |
| 203 | token := between(read(t, res), `<p class="code">`, "</p>") |
| 204 | remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/leeg.git" |
| 205 | work := t.TempDir() |
| 206 | git(t, work, "init", "-b", "master") |
| 207 | git(t, work, "config", "user.email", "tim@users.git.timfalken.com") |
| 208 | git(t, work, "config", "user.name", "tim") |
| 209 | writeFile(t, filepath.Join(work, "README.md"), "leeg\n") |
| 210 | git(t, work, "add", "README.md") |
| 211 | git(t, work, "commit", "-m", "leesmij") |
| 212 | git(t, work, "remote", "add", "origin", remote) |
| 213 | git(t, work, "push", "-u", "origin", "master") |
| 214 | |
| 215 | res, err = tim.Get(ts.URL + "/tim/leeg/-/ci/editor") |
| 216 | if err != nil { |
| 217 | t.Fatal(err) |
| 218 | } |
| 219 | editor := read(t, res) |
| 220 | for _, want := range []string{"echo hello", "No issues found", "There is no blue-ci-cd.yml on this branch yet.", `value="Add blue-ci-cd.yml"`, `name="branch" value="master"`} { |
| 221 | if !strings.Contains(editor, want) { |
| 222 | t.Errorf("editor mist %q", want) |
| 223 | } |
| 224 | } |
| 225 | if strings.Contains(editor, "must be a mapping") || strings.Contains(editor, `name="open_mr"`) { |
| 226 | t.Fatalf("beheerder ziet een fout of een merge-request:\n%s", editor) |
| 227 | } |
| 228 | |
| 229 | ctx := res.Request.Context() |
| 230 | repo, err := st.RepoByPath(ctx, "tim", "leeg") |
| 231 | if err != nil { |
| 232 | t.Fatal(err) |
| 233 | } |
| 234 | ada, err := st.UpsertUser(ctx, 3, "ada", "", false) |
| 235 | if err != nil { |
| 236 | t.Fatal(err) |
| 237 | } |
| 238 | if err := st.SetMember(ctx, "repo", repo.ID, "ada", "developer"); err != nil { |
| 239 | t.Fatal(err) |
| 240 | } |
| 241 | adaC := clientAs(t, ts.URL, st, ada.ID) |
| 242 | res, err = adaC.Get(ts.URL + "/tim/leeg/-/ci/editor?ref=master") |
| 243 | if err != nil { |
| 244 | t.Fatal(err) |
| 245 | } |
| 246 | adaPage := read(t, res) |
| 247 | for _, want := range []string{`name="open_mr" value="1" checked`, `name="branch" value="add-blue-ci-cd"`, "You cannot commit to this protected branch."} { |
| 248 | if !strings.Contains(adaPage, want) { |
| 249 | t.Errorf("ada mist %q\n%s", want, adaPage) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | dir := gitrepo.Dir(storage, "tim", "leeg") |
| 254 | res, err = adaC.PostForm(ts.URL+"/tim/leeg/-/ci/editor", url.Values{ |
| 255 | "csrf": {csrfOf(t, adaPage)}, "base": {"master"}, "branch": {"master"}, "message": {"ada op master"}, "body": {ciTemplate}, |
| 256 | }) |
| 257 | if err != nil { |
| 258 | t.Fatal(err) |
| 259 | } |
| 260 | denied := read(t, res) |
| 261 | if !strings.Contains(denied, "This branch is protected.") { |
| 262 | t.Fatalf("beschermd:\n%s", denied) |
| 263 | } |
| 264 | if _, err := gitOut(dir, "cat-file", "-e", "master:blue-ci-cd.yml"); err == nil { |
| 265 | t.Fatal("master kreeg toch een ci-bestand") |
| 266 | } |
| 267 | |
| 268 | res, err = adaC.PostForm(ts.URL+"/tim/leeg/-/ci/editor", url.Values{ |
| 269 | "csrf": {csrfOf(t, adaPage)}, "base": {"master"}, "branch": {"master"}, "open_mr": {"1"}, "message": {"ada op master"}, "body": {ciTemplate}, |
| 270 | }) |
| 271 | if err != nil { |
| 272 | t.Fatal(err) |
| 273 | } |
| 274 | bad := read(t, res) |
| 275 | if !strings.Contains(bad, "That branch name cannot be used.") { |
| 276 | t.Fatalf("zelfde branch:\n%s", bad) |
| 277 | } |
| 278 | |
| 279 | job := "stages:\n - test\nunit:\n stage: test\n image: alpine:3.20\n script:\n - echo from-ada\n" |
| 280 | res, err = adaC.PostForm(ts.URL+"/tim/leeg/-/ci/editor", url.Values{ |
| 281 | "csrf": {csrfOf(t, adaPage)}, "base": {"master"}, "branch": {"add-blue-ci-cd"}, "open_mr": {"1"}, "message": {"ada voegt ci toe"}, "body": {job}, |
| 282 | }) |
| 283 | if err != nil { |
| 284 | t.Fatal(err) |
| 285 | } |
| 286 | opened := read(t, res) |
| 287 | if !strings.Contains(opened, "Merge request opened") || !strings.Contains(opened, "ada voegt ci toe") { |
| 288 | t.Fatalf("merge request:\n%s", opened) |
| 289 | } |
| 290 | if got := mustGit(t, dir, "show", "add-blue-ci-cd:blue-ci-cd.yml"); !strings.Contains(got, "echo from-ada") { |
| 291 | t.Fatalf("nieuwe branch: %s", got) |
| 292 | } |
| 293 | if _, err := gitOut(dir, "cat-file", "-e", "master:blue-ci-cd.yml"); err == nil { |
| 294 | t.Fatal("master kreeg het bestand via de merge request") |
| 295 | } |
| 296 | |
| 297 | res, err = tim.Get(ts.URL + "/tim/leeg/-/ci/editor?ref=master") |
| 298 | if err != nil { |
| 299 | t.Fatal(err) |
| 300 | } |
| 301 | again := read(t, res) |
| 302 | res, err = tim.PostForm(ts.URL+"/tim/leeg/-/ci/editor", url.Values{ |
| 303 | "csrf": {csrfOf(t, again)}, "base": {"master"}, "branch": {"master"}, "message": {"Add blue-ci-cd.yml"}, "body": {ciTemplate}, |
| 304 | }) |
| 305 | if err != nil { |
| 306 | t.Fatal(err) |
| 307 | } |
| 308 | saved := read(t, res) |
| 309 | if !strings.Contains(saved, "Committed.") { |
| 310 | t.Fatalf("opslaan:\n%s", saved) |
| 311 | } |
| 312 | if got := mustGit(t, dir, "show", "master:blue-ci-cd.yml"); !strings.Contains(got, "echo hello") { |
| 313 | t.Fatalf("master: %s", got) |
| 314 | } |
| 315 | } |