BlueGit

timfa bluegit

Files

bluegit / internal / server / api_openapi_test.go

api_openapi_test.go

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