BlueGit

timfa bluegit

Files

bluegit / internal / server / api_openapi_test.go

api_openapi_test.go

feat: toon repositories en groepen op het profiel
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed 69db8c2f History
api_openapi_test.go 7.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 assertItems(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/jobs", "get", "200", "#/components/schemas/Job")
53 assertResp(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}", "get", "200", "#/components/schemas/Job")
54
55 assertBody(t, paths, "/projects/{owner}/{repo}/repository/branches", "post", "BranchCreate")
56 assertResp(t, paths, "/projects/{owner}/{repo}/repository/branches", "post", "201", "#/components/schemas/Branch")
57 assertBody(t, paths, "/projects/{owner}/{repo}/repository/branches/{branch}", "put", "BranchUpdate")
58 assertBody(t, paths, "/projects/{owner}/{repo}/repository/branches/{branch}", "delete", "BranchDelete")
59
60 assertBody(t, paths, "/projects/{owner}/{repo}/webhooks", "post", "WebhookCreate")
61 assertResp(t, paths, "/projects/{owner}/{repo}/webhooks", "post", "201", "#/components/schemas/Webhook")
62 assertItems(t, paths, "/groups/{path}/webhooks", "get", "200", "#/components/schemas/Webhook")
63 assertBody(t, paths, "/groups/{path}/webhooks", "post", "WebhookCreate")
64
65 user := schemas["User"].(map[string]any)["properties"].(map[string]any)
66 for _, key := range []string{"name", "bio", "web_url", "counts", "repos", "groups"} {
67 if _, ok := user[key]; !ok {
68 t.Errorf("User mist %s", key)
69 }
70 }
71 assertBody(t, paths, "/user", "put", "UserUpdate")
72 assertResp(t, paths, "/user", "get", "200", "#/components/schemas/User")
73 assertResp(t, paths, "/users/{username}", "get", "200", "#/components/schemas/User")
74}
75
76func TestResourceWebURL(t *testing.T) {
77 st, err := store.Open(filepath.Join(t.TempDir(), "bluegit.db"), bytes32())
78 if err != nil {
79 t.Fatal(err)
80 }
81 t.Cleanup(func() { _ = st.Close() })
82 ctx := context.Background()
83 user, err := st.UpsertUser(ctx, 1, "timfa", "", false)
84 if err != nil {
85 t.Fatal(err)
86 }
87 repo, err := st.CreateRepo(ctx, "user", user.ID, "bluegit", "", "master", "public")
88 if err != nil {
89 t.Fatal(err)
90 }
91 mr, err := st.CreateMergeRequest(ctx, repo.ID, user.ID, "Titel", "", "topic", "master")
92 if err != nil {
93 t.Fatal(err)
94 }
95 iss, err := st.CreateIssue(ctx, repo.ID, user.ID, store.NewIssue{Title: "Bug"})
96 if err != nil {
97 t.Fatal(err)
98 }
99 pid, err := st.CreatePipeline(ctx, repo.ID, strings.Repeat("a", 40), "refs/heads/master", "push", "timfa", "titel", nil)
100 if err != nil {
101 t.Fatal(err)
102 }
103 pipe, err := st.PipelineByID(ctx, repo.ID, pid)
104 if err != nil {
105 t.Fatal(err)
106 }
107
108 srv := &Server{Cfg: config.Config{BaseURL: "https://git.timfalken.com/"}, Store: st}
109 base := "https://git.timfalken.com/timfa/bluegit"
110 if got := srv.mrJSON(repo, mr)["web_url"]; got != base+"/-/merge-requests/1" {
111 t.Fatalf("mr web_url %v", got)
112 }
113 if got := srv.issueJSON(repo, iss, nil)["web_url"]; got != base+"/-/issues/1" {
114 t.Fatalf("issue web_url %v", got)
115 }
116 wantPipe := base + "/-/pipelines/" + strconv.FormatInt(pid, 10)
117 if got := srv.pipeJSON(repo, pipe)["web_url"]; got != wantPipe {
118 t.Fatalf("pipeline web_url %v, wil %s", got, wantPipe)
119 }
120 srv.Cfg.BaseURL = ""
121 if got := srv.mrJSON(repo, mr)["web_url"]; got != "/timfa/bluegit/-/merge-requests/1" {
122 t.Fatalf("lege base mr web_url %v", got)
123 }
124}
125
126func assertBody(t *testing.T, paths map[string]any, path, method, schema string) {
127 t.Helper()
128 op := operation(t, paths, path, method)
129 ref := op["requestBody"].(map[string]any)["content"].(map[string]any)["application/json"].(map[string]any)["schema"].(map[string]any)["$ref"]
130 want := "#/components/schemas/" + schema
131 if ref != want {
132 t.Fatalf("%s %s body %v, wil %s", method, path, ref, want)
133 }
134}
135
136func assertResp(t *testing.T, paths map[string]any, path, method, code, ref string) {
137 t.Helper()
138 schema := responseSchema(t, paths, path, method, code)
139 if schema["$ref"] != ref {
140 t.Fatalf("%s %s %s schema %v, wil %s", method, path, code, schema["$ref"], ref)
141 }
142}
143
144func assertItems(t *testing.T, paths map[string]any, path, method, code, ref string) {
145 t.Helper()
146 schema := responseSchema(t, paths, path, method, code)
147 items, _ := schema["items"].(map[string]any)
148 if schema["type"] != "array" || items["$ref"] != ref {
149 t.Fatalf("%s %s %s is geen array van %s: %v", method, path, code, ref, schema)
150 }
151}
152
153func responseSchema(t *testing.T, paths map[string]any, path, method, code string) map[string]any {
154 t.Helper()
155 op := operation(t, paths, path, method)
156 resp := op["responses"].(map[string]any)[code].(map[string]any)
157 return resp["content"].(map[string]any)["application/json"].(map[string]any)["schema"].(map[string]any)
158}
159
160func operation(t *testing.T, paths map[string]any, path, method string) map[string]any {
161 t.Helper()
162 item, ok := paths[path].(map[string]any)
163 if !ok {
164 t.Fatalf("pad ontbreekt: %s", path)
165 }
166 op, ok := item[method].(map[string]any)
167 if !ok {
168 t.Fatalf("operatie ontbreekt: %s %s", method, path)
169 }
170 return op
171}