| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "gitlab.com/Timfa/bluegit/internal/auth" |
| 14 | "gitlab.com/Timfa/bluegit/internal/config" |
| 15 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 16 | "gitlab.com/Timfa/bluegit/internal/store" |
| 17 | ) |
| 18 | |
| 19 | func TestDeleteBranchWithSlash(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 | ctx := context.Background() |
| 30 | ada, err := st.UpsertUser(ctx, 1, "ada", "", false) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | token, _, err := st.IssueToken(ctx, ada.ID, store.TokenRequest{Name: "ada", Scopes: "api"}) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | cfg := config.Config{ |
| 39 | BaseURL: "http://127.0.0.1:9", StoragePath: storage, |
| 40 | Auth: config.Auth{Mode: "dev", DevUser: "ada", DevAuthID: 1}, |
| 41 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 42 | } |
| 43 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "ada", AuthID: 1}, Git: "git", HookBin: "/bin/true", StateDir: t.TempDir()} |
| 44 | ts := httptest.NewServer(srv.Handler()) |
| 45 | t.Cleanup(ts.Close) |
| 46 | |
| 47 | call := func(method, path, body string) (int, string) { |
| 48 | t.Helper() |
| 49 | req, err := http.NewRequest(method, ts.URL+path, strings.NewReader(body)) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | req.Header.Set("Authorization", "Bearer "+token) |
| 54 | if body != "" { |
| 55 | req.Header.Set("Content-Type", "application/json") |
| 56 | } |
| 57 | res, err := http.DefaultClient.Do(req) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | return res.StatusCode, read(t, res) |
| 62 | } |
| 63 | code, body := call(http.MethodPost, "/api/v1/projects", `{"name":"demo","visibility":"public","initialize_with_readme":true}`) |
| 64 | if code != 201 { |
| 65 | t.Fatalf("project %d %s", code, body) |
| 66 | } |
| 67 | const name = "cursor/api-schemas-65da" |
| 68 | dir := gitrepo.Dir(storage, "ada", "demo") |
| 69 | exists := func() bool { |
| 70 | t.Helper() |
| 71 | out, err := exec.Command("git", "-C", dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+name).Output() |
| 72 | return err == nil && strings.TrimSpace(string(out)) != "" |
| 73 | } |
| 74 | recreate := func() { |
| 75 | t.Helper() |
| 76 | if exists() { |
| 77 | return |
| 78 | } |
| 79 | code, body := call(http.MethodPost, "/api/v1/projects/ada/demo/repository/branches", `{"name":"`+name+`","from":"master"}`) |
| 80 | if code != 201 { |
| 81 | t.Fatalf("aanmaken %d %s", code, body) |
| 82 | } |
| 83 | } |
| 84 | deleteAt := func(path, confirm string, status int) { |
| 85 | t.Helper() |
| 86 | recreate() |
| 87 | code, body := call(http.MethodDelete, path, `{"confirm":"`+confirm+`"}`) |
| 88 | if code != status { |
| 89 | t.Fatalf("DELETE %s confirm %q: %d %s", path, confirm, code, body) |
| 90 | } |
| 91 | } |
| 92 | base := "/api/v1/projects/ada/demo/repository/branches/" |
| 93 | deleteAt(base+"cursor/api-schemas-65da", name, http.StatusNoContent) |
| 94 | if exists() { |
| 95 | t.Fatal("letterlijke slash hoort de branch te verwijderen") |
| 96 | } |
| 97 | deleteAt(base+"cursor%2Fapi-schemas-65da", name, http.StatusNoContent) |
| 98 | if exists() { |
| 99 | t.Fatal("procent-2F hoort de branch te verwijderen") |
| 100 | } |
| 101 | deleteAt(base+"cursor%2Fapi-schemas-65da", "cursor%2Fapi-schemas-65da", http.StatusNoContent) |
| 102 | if exists() { |
| 103 | t.Fatal("gecodeerde bevestiging hoort ook te werken") |
| 104 | } |
| 105 | deleteAt(base+"cursor%252Fapi-schemas-65da", name, http.StatusNoContent) |
| 106 | if exists() { |
| 107 | t.Fatal("dubbel gecodeerde slash hoort de branch te verwijderen") |
| 108 | } |
| 109 | deleteAt(base+"cursor%2Fapi-schemas-65da", "andere-tak", http.StatusBadRequest) |
| 110 | if !exists() { |
| 111 | t.Fatal("foute bevestiging mag de branch niet verwijderen") |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestPushDeletesSourceBranch(t *testing.T) { |
| 116 | srv, ts, st := mergeFixServer(t) |
| 117 | tim := loginClient(t, ts) |
| 118 | token := mintToken(t, tim, ts) |
| 119 | work := pushPair(t, ts, token, "weg", "een\n", "twee\n") |
| 120 | page := read(t, mustGet(t, tim, ts.URL+"/tim/weg/-/merge-requests/new?source=topic&target=master")) |
| 121 | _ = read(t, mustPost(t, tim, ts.URL+"/tim/weg/-/merge-requests", url.Values{ |
| 122 | "csrf": {csrfOf(t, page)}, "title": {"Weg"}, "source": {"topic"}, "target": {"master"}, |
| 123 | "remove_source_branch": {"1"}, |
| 124 | })) |
| 125 | ctx := context.Background() |
| 126 | repo, err := st.RepoByPath(ctx, "tim", "weg") |
| 127 | if err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | mr, err := st.MergeRequestByNumber(ctx, repo.ID, 1) |
| 131 | if err != nil || !mr.RemoveSourceBranch { |
| 132 | t.Fatalf("remove_source_branch hoort aan te staan: %+v %v", mr, err) |
| 133 | } |
| 134 | git(t, work, "checkout", "master") |
| 135 | git(t, work, "merge", "--no-edit", "topic") |
| 136 | git(t, work, "push", "origin", "master") |
| 137 | mr, err = st.MergeRequestByNumber(ctx, repo.ID, 1) |
| 138 | if err != nil || mr.Status != "merged" { |
| 139 | t.Fatalf("status %s %v", mr.Status, err) |
| 140 | } |
| 141 | if branchRefExists(t, srv, "tim", "weg", "topic") { |
| 142 | t.Fatal("bronbranch hoort weg te zijn") |
| 143 | } |
| 144 | if !branchRefExists(t, srv, "tim", "weg", "master") { |
| 145 | t.Fatal("master hoort te blijven") |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestPushKeepsSourceBranch(t *testing.T) { |
| 150 | srv, ts, st := mergeFixServer(t) |
| 151 | tim := loginClient(t, ts) |
| 152 | token := mintToken(t, tim, ts) |
| 153 | |
| 154 | work := pushPair(t, ts, token, "blijft", "een\n", "twee\n") |
| 155 | page := read(t, mustGet(t, tim, ts.URL+"/tim/blijft/-/merge-requests/new?source=topic&target=master")) |
| 156 | _ = read(t, mustPost(t, tim, ts.URL+"/tim/blijft/-/merge-requests", url.Values{ |
| 157 | "csrf": {csrfOf(t, page)}, "title": {"Blijft"}, "source": {"topic"}, "target": {"master"}, |
| 158 | })) |
| 159 | git(t, work, "checkout", "master") |
| 160 | git(t, work, "merge", "--no-edit", "topic") |
| 161 | git(t, work, "push", "origin", "master") |
| 162 | if !branchRefExists(t, srv, "tim", "blijft", "topic") { |
| 163 | t.Fatal("uitgezette remove_source_branch hoort de tak te laten staan") |
| 164 | } |
| 165 | |
| 166 | work = pushPair(t, ts, token, "slot", "een\n", "twee\n") |
| 167 | page = read(t, mustGet(t, tim, ts.URL+"/tim/slot/-/merge-requests/new?source=topic&target=master")) |
| 168 | _ = read(t, mustPost(t, tim, ts.URL+"/tim/slot/-/merge-requests", url.Values{ |
| 169 | "csrf": {csrfOf(t, page)}, "title": {"Slot"}, "source": {"topic"}, "target": {"master"}, |
| 170 | "remove_source_branch": {"1"}, |
| 171 | })) |
| 172 | ctx := context.Background() |
| 173 | repo, err := st.RepoByPath(ctx, "tim", "slot") |
| 174 | if err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | if err := st.AddProtected(ctx, repo.ID, "topic"); err != nil { |
| 178 | t.Fatal(err) |
| 179 | } |
| 180 | git(t, work, "checkout", "master") |
| 181 | git(t, work, "merge", "--no-edit", "topic") |
| 182 | git(t, work, "push", "origin", "master") |
| 183 | mr, err := st.MergeRequestByNumber(ctx, repo.ID, 1) |
| 184 | if err != nil || mr.Status != "merged" { |
| 185 | t.Fatalf("protected bron hoort de MR wel merged te zetten: %s %v", mr.Status, err) |
| 186 | } |
| 187 | if !branchRefExists(t, srv, "tim", "slot", "topic") { |
| 188 | t.Fatal("beschermde bronbranch hoort te blijven") |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | func branchRefExists(t *testing.T, srv *Server, owner, repo, branch string) bool { |
| 193 | t.Helper() |
| 194 | dir := gitrepo.Dir(srv.StoragePath(), owner, repo) |
| 195 | out, err := exec.Command(srv.Git, "-C", dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch).Output() |
| 196 | return err == nil && strings.TrimSpace(string(out)) != "" |
| 197 | } |