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