| 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 | // Varianten zonder slash in het pad, voor een ingress die %2F weigert. |
| 115 | recreate() |
| 116 | code, body = call(http.MethodDelete, "/api/v1/projects/ada/demo/repository/branches?name="+url.QueryEscape(name), "") |
| 117 | if code != http.StatusNoContent { |
| 118 | t.Fatalf("DELETE ?name= %d %s", code, body) |
| 119 | } |
| 120 | if exists() { |
| 121 | t.Fatal("query name hoort de branch te verwijderen") |
| 122 | } |
| 123 | recreate() |
| 124 | code, body = call(http.MethodDelete, "/api/v1/projects/ada/demo/repository/branches", `{"name":"`+name+`"}`) |
| 125 | if code != http.StatusNoContent { |
| 126 | t.Fatalf("DELETE body name %d %s", code, body) |
| 127 | } |
| 128 | if exists() { |
| 129 | t.Fatal("body name hoort de branch te verwijderen") |
| 130 | } |
| 131 | recreate() |
| 132 | code, body = call(http.MethodDelete, "/api/v1/projects/ada/demo/repository/branches", `{"name":"`+name+`","confirm":"andere-tak"}`) |
| 133 | if code != http.StatusBadRequest { |
| 134 | t.Fatalf("DELETE body verkeerde confirm %d %s", code, body) |
| 135 | } |
| 136 | if !exists() { |
| 137 | t.Fatal("verkeerde confirm mag de branch niet verwijderen") |
| 138 | } |
| 139 | code, body = call(http.MethodPut, "/api/v1/projects/ada/demo/repository/branches?name="+url.QueryEscape(name), `{"name":"cursor/hernoemd"}`) |
| 140 | if code != http.StatusOK { |
| 141 | t.Fatalf("PUT ?name= %d %s", code, body) |
| 142 | } |
| 143 | if exists() { |
| 144 | t.Fatal("oude naam hoort weg te zijn na hernoemen") |
| 145 | } |
| 146 | out, err := exec.Command("git", "-C", dir, "rev-parse", "--verify", "--quiet", "refs/heads/cursor/hernoemd").Output() |
| 147 | if err != nil || strings.TrimSpace(string(out)) == "" { |
| 148 | t.Fatal("nieuwe naam ontbreekt") |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func TestSlashAvoidingRefEndpoints(t *testing.T) { |
| 153 | if _, err := exec.LookPath("git"); err != nil { |
| 154 | t.Skip("git ontbreekt") |
| 155 | } |
| 156 | storage := t.TempDir() |
| 157 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 158 | if err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | t.Cleanup(func() { _ = st.Close() }) |
| 162 | ctx := context.Background() |
| 163 | ada, err := st.UpsertUser(ctx, 1, "ada", "", false) |
| 164 | if err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | token, _, err := st.IssueToken(ctx, ada.ID, store.TokenRequest{Name: "ada", Scopes: "api"}) |
| 168 | if err != nil { |
| 169 | t.Fatal(err) |
| 170 | } |
| 171 | cfg := config.Config{ |
| 172 | BaseURL: "http://127.0.0.1:9", StoragePath: storage, |
| 173 | Auth: config.Auth{Mode: "dev", DevUser: "ada", DevAuthID: 1}, |
| 174 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 175 | } |
| 176 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "ada", AuthID: 1}, Git: "git", HookBin: "/bin/true", StateDir: t.TempDir()} |
| 177 | ts := httptest.NewServer(srv.Handler()) |
| 178 | t.Cleanup(ts.Close) |
| 179 | |
| 180 | call := func(method, path, body string) (int, string) { |
| 181 | t.Helper() |
| 182 | req, err := http.NewRequest(method, ts.URL+path, strings.NewReader(body)) |
| 183 | if err != nil { |
| 184 | t.Fatal(err) |
| 185 | } |
| 186 | req.Header.Set("Authorization", "Bearer "+token) |
| 187 | if body != "" { |
| 188 | req.Header.Set("Content-Type", "application/json") |
| 189 | } |
| 190 | res, err := http.DefaultClient.Do(req) |
| 191 | if err != nil { |
| 192 | t.Fatal(err) |
| 193 | } |
| 194 | return res.StatusCode, read(t, res) |
| 195 | } |
| 196 | code, body := call(http.MethodPost, "/api/v1/projects", `{"name":"demo","visibility":"public","initialize_with_readme":true}`) |
| 197 | if code != 201 { |
| 198 | t.Fatalf("project %d %s", code, body) |
| 199 | } |
| 200 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/repository/commits", `{"branch":"master","commit_message":"pad","actions":[{"action":"create","file_path":"docs/INGRESS.md","content":"# Ingress\n"}]}`) |
| 201 | if code != 201 { |
| 202 | t.Fatalf("commit %d %s", code, body) |
| 203 | } |
| 204 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo/repository/files?path="+url.QueryEscape("docs/INGRESS.md")+"&ref=master", "") |
| 205 | if code != 200 || !strings.Contains(body, "# Ingress") { |
| 206 | t.Fatalf("GET files?path= %d %s", code, body) |
| 207 | } |
| 208 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo/repository/files/docs%2FINGRESS.md?ref=master", "") |
| 209 | if code != 200 || !strings.Contains(body, "# Ingress") { |
| 210 | t.Fatalf("GET files %%2F %d %s", code, body) |
| 211 | } |
| 212 | |
| 213 | const pattern = "feature/*" |
| 214 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/protected_branches", `{"pattern":"`+pattern+`"}`) |
| 215 | if code != 201 { |
| 216 | t.Fatalf("protect %d %s", code, body) |
| 217 | } |
| 218 | code, body = call(http.MethodDelete, "/api/v1/projects/ada/demo/protected_branches?pattern="+url.QueryEscape(pattern), "") |
| 219 | if code != http.StatusNoContent { |
| 220 | t.Fatalf("unprotect ?pattern= %d %s", code, body) |
| 221 | } |
| 222 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/protected_branches", `{"pattern":"`+pattern+`"}`) |
| 223 | if code != 201 { |
| 224 | t.Fatalf("protect opnieuw %d %s", code, body) |
| 225 | } |
| 226 | code, body = call(http.MethodDelete, "/api/v1/projects/ada/demo/protected_branches", `{"pattern":"`+pattern+`"}`) |
| 227 | if code != http.StatusNoContent { |
| 228 | t.Fatalf("unprotect body pattern %d %s", code, body) |
| 229 | } |
| 230 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo/protected_branches", "") |
| 231 | if code != 200 || strings.Contains(body, "feature/") { |
| 232 | t.Fatalf("pattern zou weg moeten zijn: %d %s", code, body) |
| 233 | } |
| 234 | |
| 235 | const tagPat = "releases/*" |
| 236 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/protected_tags", `{"pattern":"`+tagPat+`"}`) |
| 237 | if code != 201 { |
| 238 | t.Fatalf("protect tag %d %s", code, body) |
| 239 | } |
| 240 | code, body = call(http.MethodDelete, "/api/v1/projects/ada/demo/protected_tags?pattern="+url.QueryEscape(tagPat), "") |
| 241 | if code != http.StatusNoContent { |
| 242 | t.Fatalf("unprotect tag ?pattern= %d %s", code, body) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | func TestPathWildcardEncodedSlash(t *testing.T) { |
| 247 | mux := http.NewServeMux() |
| 248 | var got string |
| 249 | mux.HandleFunc("DELETE /branches/{branch...}", func(w http.ResponseWriter, r *http.Request) { |
| 250 | got = pathWildcard(r, "branch") |
| 251 | w.WriteHeader(http.StatusNoContent) |
| 252 | }) |
| 253 | ts := httptest.NewServer(mux) |
| 254 | t.Cleanup(ts.Close) |
| 255 | for _, path := range []string{"/branches/feature/x", "/branches/feature%2Fx", "/branches/feature%252Fx"} { |
| 256 | got = "" |
| 257 | req, err := http.NewRequest(http.MethodDelete, ts.URL+path, nil) |
| 258 | if err != nil { |
| 259 | t.Fatal(err) |
| 260 | } |
| 261 | res, err := http.DefaultClient.Do(req) |
| 262 | if err != nil { |
| 263 | t.Fatal(err) |
| 264 | } |
| 265 | res.Body.Close() |
| 266 | if res.StatusCode != http.StatusNoContent { |
| 267 | t.Fatalf("%s: %d", path, res.StatusCode) |
| 268 | } |
| 269 | if got != "feature/x" { |
| 270 | t.Fatalf("%s: pathWildcard=%q", path, got) |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | func TestRetryPipelineModeDefault(t *testing.T) { |
| 276 | if retryPipelineMode("") != "failed" || retryPipelineMode(" ") != "failed" { |
| 277 | t.Fatal("lege mode hoort failed te zijn") |
| 278 | } |
| 279 | if retryPipelineMode("all") != "all" || retryPipelineMode("failed") != "failed" { |
| 280 | t.Fatal("expliciete mode moet blijven") |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | func TestPushDeletesSourceBranch(t *testing.T) { |
| 285 | srv, ts, st := mergeFixServer(t) |
| 286 | tim := loginClient(t, ts) |
| 287 | token := mintToken(t, tim, ts) |
| 288 | work := pushPair(t, ts, token, "weg", "een\n", "twee\n") |
| 289 | page := read(t, mustGet(t, tim, ts.URL+"/tim/weg/-/merge-requests/new?source=topic&target=master")) |
| 290 | _ = read(t, mustPost(t, tim, ts.URL+"/tim/weg/-/merge-requests", url.Values{ |
| 291 | "csrf": {csrfOf(t, page)}, "title": {"Weg"}, "source": {"topic"}, "target": {"master"}, |
| 292 | "remove_source_branch": {"1"}, |
| 293 | })) |
| 294 | ctx := context.Background() |
| 295 | repo, err := st.RepoByPath(ctx, "tim", "weg") |
| 296 | if err != nil { |
| 297 | t.Fatal(err) |
| 298 | } |
| 299 | mr, err := st.MergeRequestByNumber(ctx, repo.ID, 1) |
| 300 | if err != nil || !mr.RemoveSourceBranch { |
| 301 | t.Fatalf("remove_source_branch hoort aan te staan: %+v %v", mr, err) |
| 302 | } |
| 303 | git(t, work, "checkout", "master") |
| 304 | git(t, work, "merge", "--no-edit", "topic") |
| 305 | git(t, work, "push", "origin", "master") |
| 306 | mr, err = st.MergeRequestByNumber(ctx, repo.ID, 1) |
| 307 | if err != nil || mr.Status != "merged" { |
| 308 | t.Fatalf("status %s %v", mr.Status, err) |
| 309 | } |
| 310 | if branchRefExists(t, srv, "tim", "weg", "topic") { |
| 311 | t.Fatal("bronbranch hoort weg te zijn") |
| 312 | } |
| 313 | if !branchRefExists(t, srv, "tim", "weg", "master") { |
| 314 | t.Fatal("master hoort te blijven") |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | func TestPushKeepsSourceBranch(t *testing.T) { |
| 319 | srv, ts, st := mergeFixServer(t) |
| 320 | tim := loginClient(t, ts) |
| 321 | token := mintToken(t, tim, ts) |
| 322 | |
| 323 | work := pushPair(t, ts, token, "blijft", "een\n", "twee\n") |
| 324 | page := read(t, mustGet(t, tim, ts.URL+"/tim/blijft/-/merge-requests/new?source=topic&target=master")) |
| 325 | _ = read(t, mustPost(t, tim, ts.URL+"/tim/blijft/-/merge-requests", url.Values{ |
| 326 | "csrf": {csrfOf(t, page)}, "title": {"Blijft"}, "source": {"topic"}, "target": {"master"}, |
| 327 | })) |
| 328 | git(t, work, "checkout", "master") |
| 329 | git(t, work, "merge", "--no-edit", "topic") |
| 330 | git(t, work, "push", "origin", "master") |
| 331 | if !branchRefExists(t, srv, "tim", "blijft", "topic") { |
| 332 | t.Fatal("uitgezette remove_source_branch hoort de tak te laten staan") |
| 333 | } |
| 334 | |
| 335 | work = pushPair(t, ts, token, "slot", "een\n", "twee\n") |
| 336 | page = read(t, mustGet(t, tim, ts.URL+"/tim/slot/-/merge-requests/new?source=topic&target=master")) |
| 337 | _ = read(t, mustPost(t, tim, ts.URL+"/tim/slot/-/merge-requests", url.Values{ |
| 338 | "csrf": {csrfOf(t, page)}, "title": {"Slot"}, "source": {"topic"}, "target": {"master"}, |
| 339 | "remove_source_branch": {"1"}, |
| 340 | })) |
| 341 | ctx := context.Background() |
| 342 | repo, err := st.RepoByPath(ctx, "tim", "slot") |
| 343 | if err != nil { |
| 344 | t.Fatal(err) |
| 345 | } |
| 346 | if err := st.AddProtected(ctx, repo.ID, "topic"); err != nil { |
| 347 | t.Fatal(err) |
| 348 | } |
| 349 | git(t, work, "checkout", "master") |
| 350 | git(t, work, "merge", "--no-edit", "topic") |
| 351 | git(t, work, "push", "origin", "master") |
| 352 | mr, err := st.MergeRequestByNumber(ctx, repo.ID, 1) |
| 353 | if err != nil || mr.Status != "merged" { |
| 354 | t.Fatalf("protected bron hoort de MR wel merged te zetten: %s %v", mr.Status, err) |
| 355 | } |
| 356 | if !branchRefExists(t, srv, "tim", "slot", "topic") { |
| 357 | t.Fatal("beschermde bronbranch hoort te blijven") |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | func branchRefExists(t *testing.T, srv *Server, owner, repo, branch string) bool { |
| 362 | t.Helper() |
| 363 | dir := gitrepo.Dir(srv.StoragePath(), owner, repo) |
| 364 | out, err := exec.Command(srv.Git, "-C", dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch).Output() |
| 365 | return err == nil && strings.TrimSpace(string(out)) != "" |
| 366 | } |