| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "path/filepath" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "gitlab.com/Timfa/bluegit/internal/config" |
| 12 | "gitlab.com/Timfa/bluegit/internal/store" |
| 13 | ) |
| 14 | |
| 15 | func TestOpenAPISchemas(t *testing.T) { |
| 16 | doc := openAPIDoc() |
| 17 | schemas := doc["components"].(map[string]any)["schemas"].(map[string]any) |
| 18 | for _, name := range []string{"Error", "MergeRequest", "Issue", "Pipeline", "Job", "Branch", "Webhook", "User", "UserUpdate"} { |
| 19 | if _, ok := schemas[name]; !ok { |
| 20 | t.Errorf("schema %s ontbreekt", name) |
| 21 | } |
| 22 | } |
| 23 | props := schemas["MergeRequest"].(map[string]any)["properties"].(map[string]any) |
| 24 | if _, ok := props["web_url"]; !ok { |
| 25 | t.Fatal("MergeRequest mist web_url") |
| 26 | } |
| 27 | if _, ok := schemas["Issue"].(map[string]any)["properties"].(map[string]any)["web_url"]; !ok { |
| 28 | t.Fatal("Issue mist web_url") |
| 29 | } |
| 30 | if _, ok := schemas["Pipeline"].(map[string]any)["properties"].(map[string]any)["web_url"]; !ok { |
| 31 | t.Fatal("Pipeline mist web_url") |
| 32 | } |
| 33 | |
| 34 | paths := doc["paths"].(map[string]any) |
| 35 | assertBody(t, paths, "/projects/{owner}/{repo}/merge_requests", "post", "MergeRequestCreate") |
| 36 | assertResp(t, paths, "/projects/{owner}/{repo}/merge_requests", "post", "201", "#/components/schemas/MergeRequest") |
| 37 | assertItems(t, paths, "/projects/{owner}/{repo}/merge_requests", "get", "200", "#/components/schemas/MergeRequest") |
| 38 | assertBody(t, paths, "/projects/{owner}/{repo}/merge_requests/{number}", "put", "MergeRequestUpdate") |
| 39 | assertBody(t, paths, "/projects/{owner}/{repo}/merge_requests/{number}/merge", "post", "MergeRequestMerge") |
| 40 | assertResp(t, paths, "/projects/{owner}/{repo}/merge_requests/{number}/merge", "post", "200", "#/components/schemas/MergeResult") |
| 41 | |
| 42 | if _, ok := paths["/repos/{owner}/{repo}/issues"]; !ok { |
| 43 | t.Fatal("issues ontbreken in openapi") |
| 44 | } |
| 45 | assertBody(t, paths, "/repos/{owner}/{repo}/issues", "post", "IssueWrite") |
| 46 | assertResp(t, paths, "/repos/{owner}/{repo}/issues", "post", "201", "#/components/schemas/Issue") |
| 47 | assertResp(t, paths, "/repos/{owner}/{repo}/issues", "get", "200", "#/components/schemas/IssueList") |
| 48 | assertBody(t, paths, "/repos/{owner}/{repo}/issues/{number}", "put", "IssueWrite") |
| 49 | assertBody(t, paths, "/repos/{owner}/{repo}/issues/{number}/notes", "post", "IssueNoteCreate") |
| 50 | |
| 51 | assertBody(t, paths, "/projects/{owner}/{repo}/pipelines", "post", "PipelineCreate") |
| 52 | assertResp(t, paths, "/projects/{owner}/{repo}/pipelines", "post", "201", "#/components/schemas/Pipeline") |
| 53 | assertBody(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "post", "PipelineRetry") |
| 54 | assertResp(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "post", "200", "#/components/schemas/Pipeline") |
| 55 | assertResp(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "post", "201", "#/components/schemas/Pipeline") |
| 56 | retryBody := operation(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "post")["requestBody"].(map[string]any) |
| 57 | if retryBody["required"] != false { |
| 58 | t.Fatal("PipelineRetry requestBody hoort optioneel te zijn") |
| 59 | } |
| 60 | mode := schemas["PipelineRetry"].(map[string]any)["properties"].(map[string]any)["mode"].(map[string]any) |
| 61 | if mode["default"] != "failed" { |
| 62 | t.Fatalf("PipelineRetry default %v", mode["default"]) |
| 63 | } |
| 64 | got := map[string]bool{} |
| 65 | ev, _ := mode["enum"].([]any) |
| 66 | for _, v := range ev { |
| 67 | got[fmt.Sprint(v)] = true |
| 68 | } |
| 69 | if !got["all"] || !got["failed"] || len(ev) != 2 { |
| 70 | t.Fatalf("PipelineRetry enum %v", mode["enum"]) |
| 71 | } |
| 72 | assertItems(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/jobs", "get", "200", "#/components/schemas/Job") |
| 73 | assertResp(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}", "get", "200", "#/components/schemas/Job") |
| 74 | |
| 75 | assertBody(t, paths, "/projects/{owner}/{repo}/repository/branches", "post", "BranchCreate") |
| 76 | assertResp(t, paths, "/projects/{owner}/{repo}/repository/branches", "post", "201", "#/components/schemas/Branch") |
| 77 | assertBody(t, paths, "/projects/{owner}/{repo}/repository/branches/{branch}", "put", "BranchUpdate") |
| 78 | assertBody(t, paths, "/projects/{owner}/{repo}/repository/branches/{branch}", "delete", "BranchDelete") |
| 79 | |
| 80 | assertBody(t, paths, "/projects/{owner}/{repo}/webhooks", "post", "WebhookCreate") |
| 81 | assertResp(t, paths, "/projects/{owner}/{repo}/webhooks", "post", "201", "#/components/schemas/Webhook") |
| 82 | assertItems(t, paths, "/groups/{path}/webhooks", "get", "200", "#/components/schemas/Webhook") |
| 83 | assertBody(t, paths, "/groups/{path}/webhooks", "post", "WebhookCreate") |
| 84 | |
| 85 | user := schemas["User"].(map[string]any)["properties"].(map[string]any) |
| 86 | for _, key := range []string{"name", "bio", "web_url", "counts", "repos", "groups"} { |
| 87 | if _, ok := user[key]; !ok { |
| 88 | t.Errorf("User mist %s", key) |
| 89 | } |
| 90 | } |
| 91 | assertBody(t, paths, "/user", "put", "UserUpdate") |
| 92 | assertResp(t, paths, "/user", "get", "200", "#/components/schemas/User") |
| 93 | assertResp(t, paths, "/users/{username}", "get", "200", "#/components/schemas/User") |
| 94 | op := paths["/users/{username}"].(map[string]any)["get"].(map[string]any) |
| 95 | sec, _ := op["security"].([]any) |
| 96 | optional := false |
| 97 | for _, item := range sec { |
| 98 | req, _ := item.(map[string]any) |
| 99 | if len(req) == 0 { |
| 100 | optional = true |
| 101 | } |
| 102 | } |
| 103 | if !optional { |
| 104 | t.Fatal("GET /users/{username} moet zonder token mogen") |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestResourceWebURL(t *testing.T) { |
| 109 | st, err := store.Open(filepath.Join(t.TempDir(), "bluegit.db"), bytes32()) |
| 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | t.Cleanup(func() { _ = st.Close() }) |
| 114 | ctx := context.Background() |
| 115 | user, err := st.UpsertUser(ctx, 1, "timfa", "", false) |
| 116 | if err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | repo, err := st.CreateRepo(ctx, "user", user.ID, "bluegit", "", "master", "public") |
| 120 | if err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | mr, err := st.CreateMergeRequest(ctx, repo.ID, user.ID, "Titel", "", "topic", "master") |
| 124 | if err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | iss, err := st.CreateIssue(ctx, repo.ID, user.ID, store.NewIssue{Title: "Bug"}) |
| 128 | if err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | pid, err := st.CreatePipeline(ctx, repo.ID, strings.Repeat("a", 40), "refs/heads/master", "push", "timfa", "titel", nil) |
| 132 | if err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | pipe, err := st.PipelineByID(ctx, repo.ID, pid) |
| 136 | if err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | |
| 140 | srv := &Server{Cfg: config.Config{BaseURL: "https://git.timfalken.com/"}, Store: st} |
| 141 | base := "https://git.timfalken.com/timfa/bluegit" |
| 142 | if got := srv.mrJSON(repo, mr)["web_url"]; got != base+"/-/merge-requests/1" { |
| 143 | t.Fatalf("mr web_url %v", got) |
| 144 | } |
| 145 | if got := srv.issueJSON(repo, iss, nil)["web_url"]; got != base+"/-/issues/1" { |
| 146 | t.Fatalf("issue web_url %v", got) |
| 147 | } |
| 148 | wantPipe := base + "/-/pipelines/" + strconv.FormatInt(pid, 10) |
| 149 | if got := srv.pipeJSON(repo, pipe)["web_url"]; got != wantPipe { |
| 150 | t.Fatalf("pipeline web_url %v, wil %s", got, wantPipe) |
| 151 | } |
| 152 | srv.Cfg.BaseURL = "" |
| 153 | if got := srv.mrJSON(repo, mr)["web_url"]; got != "/timfa/bluegit/-/merge-requests/1" { |
| 154 | t.Fatalf("lege base mr web_url %v", got) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func assertBody(t *testing.T, paths map[string]any, path, method, schema string) { |
| 159 | t.Helper() |
| 160 | op := operation(t, paths, path, method) |
| 161 | ref := op["requestBody"].(map[string]any)["content"].(map[string]any)["application/json"].(map[string]any)["schema"].(map[string]any)["$ref"] |
| 162 | want := "#/components/schemas/" + schema |
| 163 | if ref != want { |
| 164 | t.Fatalf("%s %s body %v, wil %s", method, path, ref, want) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func assertResp(t *testing.T, paths map[string]any, path, method, code, ref string) { |
| 169 | t.Helper() |
| 170 | schema := responseSchema(t, paths, path, method, code) |
| 171 | if schema["$ref"] != ref { |
| 172 | t.Fatalf("%s %s %s schema %v, wil %s", method, path, code, schema["$ref"], ref) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func assertItems(t *testing.T, paths map[string]any, path, method, code, ref string) { |
| 177 | t.Helper() |
| 178 | schema := responseSchema(t, paths, path, method, code) |
| 179 | items, _ := schema["items"].(map[string]any) |
| 180 | if schema["type"] != "array" || items["$ref"] != ref { |
| 181 | t.Fatalf("%s %s %s is geen array van %s: %v", method, path, code, ref, schema) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func responseSchema(t *testing.T, paths map[string]any, path, method, code string) map[string]any { |
| 186 | t.Helper() |
| 187 | op := operation(t, paths, path, method) |
| 188 | resp := op["responses"].(map[string]any)[code].(map[string]any) |
| 189 | return resp["content"].(map[string]any)["application/json"].(map[string]any)["schema"].(map[string]any) |
| 190 | } |
| 191 | |
| 192 | func operation(t *testing.T, paths map[string]any, path, method string) map[string]any { |
| 193 | t.Helper() |
| 194 | item, ok := paths[path].(map[string]any) |
| 195 | if !ok { |
| 196 | t.Fatalf("pad ontbreekt: %s", path) |
| 197 | } |
| 198 | op, ok := item[method].(map[string]any) |
| 199 | if !ok { |
| 200 | t.Fatalf("operatie ontbreekt: %s %s", method, path) |
| 201 | } |
| 202 | return op |
| 203 | } |
| 204 | |
| 205 | func TestOpenAPIProjectRequestBodies(t *testing.T) { |
| 206 | doc := openAPIDoc() |
| 207 | paths := doc["paths"].(map[string]any) |
| 208 | assertBody(t, paths, "/projects", "post", "ProjectCreate") |
| 209 | assertBody(t, paths, "/projects/{owner}/{repo}", "put", "ProjectUpdate") |
| 210 | assertBody(t, paths, "/projects/{owner}/{repo}/protected_branches", "post", "ProtectedPattern") |
| 211 | assertBody(t, paths, "/projects/{owner}/{repo}/ci/lint", "post", "CILint") |
| 212 | assertResp(t, paths, "/projects", "post", "201", "#/components/schemas/Project") |
| 213 | assertResp(t, paths, "/projects/{owner}/{repo}", "put", "200", "#/components/schemas/Project") |
| 214 | |
| 215 | op := paths["/projects/{owner}/{repo}/logo"].(map[string]any)["post"].(map[string]any) |
| 216 | body := op["requestBody"].(map[string]any) |
| 217 | content := body["content"].(map[string]any) |
| 218 | if _, ok := content["multipart/form-data"]; !ok { |
| 219 | t.Fatal("logo requestBody moet multipart/form-data zijn") |
| 220 | } |
| 221 | ref := content["multipart/form-data"].(map[string]any)["schema"].(map[string]any)["$ref"].(string) |
| 222 | if ref != "#/components/schemas/LogoUpload" { |
| 223 | t.Fatalf("logo schema %s", ref) |
| 224 | } |
| 225 | |
| 226 | // Andere POST/PUT/PATCH zonder request-body: alleen acties zonder JSON-veld. |
| 227 | allow := map[string]bool{ |
| 228 | "POST /projects/{owner}/{repo}/star": true, |
| 229 | "POST /projects/{owner}/{repo}/pipelines/{id}/cancel": true, |
| 230 | "POST /projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/play": true, |
| 231 | "POST /projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/retry": true, |
| 232 | "POST /projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/cancel": true, |
| 233 | "POST /admin/runner/pause": true, |
| 234 | "POST /admin/runner/resume": true, |
| 235 | "POST /admin/runners/{id}/pause": true, |
| 236 | "POST /admin/runners/{id}/resume": true, |
| 237 | "POST /user/runners/{id}/pause": true, |
| 238 | "POST /user/runners/{id}/resume": true, |
| 239 | "POST /groups/{path}/runners/{id}/pause": true, |
| 240 | "POST /groups/{path}/runners/{id}/resume": true, |
| 241 | "POST /admin/pipelines/expire-stale": true, |
| 242 | "POST /admin/self-update/force": true, |
| 243 | "POST /admin/cache/clear": true, |
| 244 | "POST /admin/git-maintenance": false, // heeft optionele body; mag ontbreken of staan |
| 245 | } |
| 246 | _ = allow |
| 247 | var missing []string |
| 248 | for path, item := range paths { |
| 249 | ops, _ := item.(map[string]any) |
| 250 | for method, raw := range ops { |
| 251 | m := strings.ToUpper(method) |
| 252 | if m != "POST" && m != "PUT" && m != "PATCH" { |
| 253 | continue |
| 254 | } |
| 255 | op, _ := raw.(map[string]any) |
| 256 | if op == nil { |
| 257 | continue |
| 258 | } |
| 259 | key := m + " " + path |
| 260 | if allow[key] { |
| 261 | continue |
| 262 | } |
| 263 | if _, ok := op["requestBody"]; !ok { |
| 264 | missing = append(missing, key) |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | // Rapporteer alleen de eerder ontbrekende, plus duidelijke schrijf-endpoints |
| 269 | // die we nu vullen. Andere actie-POST's zonder body zijn toegestaan. |
| 270 | need := []string{ |
| 271 | "POST /projects", |
| 272 | "PUT /projects/{owner}/{repo}", |
| 273 | "POST /projects/{owner}/{repo}/logo", |
| 274 | "POST /projects/{owner}/{repo}/protected_branches", |
| 275 | "POST /projects/{owner}/{repo}/ci/lint", |
| 276 | } |
| 277 | has := map[string]bool{} |
| 278 | for _, k := range missing { |
| 279 | has[k] = true |
| 280 | } |
| 281 | for _, k := range need { |
| 282 | if has[k] { |
| 283 | t.Errorf("requestBody ontbreekt: %s", k) |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | func TestOpenAPISlashAvoidingEndpoints(t *testing.T) { |
| 289 | doc := openAPIDoc() |
| 290 | paths := doc["paths"].(map[string]any) |
| 291 | assertBody(t, paths, "/projects/{owner}/{repo}/repository/branches", "delete", "BranchDelete") |
| 292 | assertBody(t, paths, "/projects/{owner}/{repo}/repository/branches", "put", "BranchUpdate") |
| 293 | assertBody(t, paths, "/projects/{owner}/{repo}/protected_branches", "delete", "ProtectedPattern") |
| 294 | assertBody(t, paths, "/projects/{owner}/{repo}/protected_tags", "delete", "ProtectedPattern") |
| 295 | |
| 296 | del := operation(t, paths, "/projects/{owner}/{repo}/repository/branches", "delete") |
| 297 | if del["requestBody"].(map[string]any)["required"] != false { |
| 298 | t.Fatal("collection DELETE branches body hoort optioneel te zijn") |
| 299 | } |
| 300 | params, _ := del["parameters"].([]any) |
| 301 | var hasName bool |
| 302 | for _, raw := range params { |
| 303 | p, _ := raw.(map[string]any) |
| 304 | if p["name"] == "name" && p["in"] == "query" { |
| 305 | hasName = true |
| 306 | } |
| 307 | } |
| 308 | if !hasName { |
| 309 | t.Fatal("DELETE /repository/branches mist query name") |
| 310 | } |
| 311 | |
| 312 | file := operation(t, paths, "/projects/{owner}/{repo}/repository/files", "get") |
| 313 | var hasPath, hasRef bool |
| 314 | for _, raw := range file["parameters"].([]any) { |
| 315 | p, _ := raw.(map[string]any) |
| 316 | if p["in"] != "query" { |
| 317 | continue |
| 318 | } |
| 319 | switch p["name"] { |
| 320 | case "path": |
| 321 | hasPath = p["required"] == true |
| 322 | case "ref": |
| 323 | hasRef = true |
| 324 | } |
| 325 | } |
| 326 | if !hasPath || !hasRef { |
| 327 | t.Fatalf("files query params onvolledig: %v", file["parameters"]) |
| 328 | } |
| 329 | } |