BlueGit

timfa bluegit

Files

bluegit / internal / server / api_openapi_test.go

api_openapi_test.go

feat: deel een profiel via een korte url en een openbare api
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed b9293125 History
api_openapi_test.go 7.3 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 op := paths["/users/{username}"].(map[string]any)["get"].(map[string]any)
75 sec, _ := op["security"].([]any)
76 optional := false
77 for _, item := range sec {
78 req, _ := item.(map[string]any)
79 if len(req) == 0 {
80 optional = true
81 }
82 }
83 if !optional {
84 t.Fatal("GET /users/{username} moet zonder token mogen")
85 }
86}
87
88func TestResourceWebURL(t *testing.T) {
89 st, err := store.Open(filepath.Join(t.TempDir(), "bluegit.db"), bytes32())
90 if err != nil {
91 t.Fatal(err)
92 }
93 t.Cleanup(func() { _ = st.Close() })
94 ctx := context.Background()
95 user, err := st.UpsertUser(ctx, 1, "timfa", "", false)
96 if err != nil {
97 t.Fatal(err)
98 }
99 repo, err := st.CreateRepo(ctx, "user", user.ID, "bluegit", "", "master", "public")
100 if err != nil {
101 t.Fatal(err)
102 }
103 mr, err := st.CreateMergeRequest(ctx, repo.ID, user.ID, "Titel", "", "topic", "master")
104 if err != nil {
105 t.Fatal(err)
106 }
107 iss, err := st.CreateIssue(ctx, repo.ID, user.ID, store.NewIssue{Title: "Bug"})
108 if err != nil {
109 t.Fatal(err)
110 }
111 pid, err := st.CreatePipeline(ctx, repo.ID, strings.Repeat("a", 40), "refs/heads/master", "push", "timfa", "titel", nil)
112 if err != nil {
113 t.Fatal(err)
114 }
115 pipe, err := st.PipelineByID(ctx, repo.ID, pid)
116 if err != nil {
117 t.Fatal(err)
118 }
119
120 srv := &Server{Cfg: config.Config{BaseURL: "https://git.timfalken.com/"}, Store: st}
121 base := "https://git.timfalken.com/timfa/bluegit"
122 if got := srv.mrJSON(repo, mr)["web_url"]; got != base+"/-/merge-requests/1" {
123 t.Fatalf("mr web_url %v", got)
124 }
125 if got := srv.issueJSON(repo, iss, nil)["web_url"]; got != base+"/-/issues/1" {
126 t.Fatalf("issue web_url %v", got)
127 }
128 wantPipe := base + "/-/pipelines/" + strconv.FormatInt(pid, 10)
129 if got := srv.pipeJSON(repo, pipe)["web_url"]; got != wantPipe {
130 t.Fatalf("pipeline web_url %v, wil %s", got, wantPipe)
131 }
132 srv.Cfg.BaseURL = ""
133 if got := srv.mrJSON(repo, mr)["web_url"]; got != "/timfa/bluegit/-/merge-requests/1" {
134 t.Fatalf("lege base mr web_url %v", got)
135 }
136}
137
138func assertBody(t *testing.T, paths map[string]any, path, method, schema string) {
139 t.Helper()
140 op := operation(t, paths, path, method)
141 ref := op["requestBody"].(map[string]any)["content"].(map[string]any)["application/json"].(map[string]any)["schema"].(map[string]any)["$ref"]
142 want := "#/components/schemas/" + schema
143 if ref != want {
144 t.Fatalf("%s %s body %v, wil %s", method, path, ref, want)
145 }
146}
147
148func assertResp(t *testing.T, paths map[string]any, path, method, code, ref string) {
149 t.Helper()
150 schema := responseSchema(t, paths, path, method, code)
151 if schema["$ref"] != ref {
152 t.Fatalf("%s %s %s schema %v, wil %s", method, path, code, schema["$ref"], ref)
153 }
154}
155
156func assertItems(t *testing.T, paths map[string]any, path, method, code, ref string) {
157 t.Helper()
158 schema := responseSchema(t, paths, path, method, code)
159 items, _ := schema["items"].(map[string]any)
160 if schema["type"] != "array" || items["$ref"] != ref {
161 t.Fatalf("%s %s %s is geen array van %s: %v", method, path, code, ref, schema)
162 }
163}
164
165func responseSchema(t *testing.T, paths map[string]any, path, method, code string) map[string]any {
166 t.Helper()
167 op := operation(t, paths, path, method)
168 resp := op["responses"].(map[string]any)[code].(map[string]any)
169 return resp["content"].(map[string]any)["application/json"].(map[string]any)["schema"].(map[string]any)
170}
171
172func operation(t *testing.T, paths map[string]any, path, method string) map[string]any {
173 t.Helper()
174 item, ok := paths[path].(map[string]any)
175 if !ok {
176 t.Fatalf("pad ontbreekt: %s", path)
177 }
178 op, ok := item[method].(map[string]any)
179 if !ok {
180 t.Fatalf("operatie ontbreekt: %s %s", method, path)
181 }
182 return op
183}