| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "gitlab.com/Timfa/bluegit/internal/auth" |
| 15 | "gitlab.com/Timfa/bluegit/internal/config" |
| 16 | "gitlab.com/Timfa/bluegit/internal/store" |
| 17 | ) |
| 18 | |
| 19 | func TestAPICatalogCoversResources(t *testing.T) { |
| 20 | var b strings.Builder |
| 21 | b.WriteString("GET /api/v1/openapi.json\n") |
| 22 | for _, rt := range apiCatalog { |
| 23 | b.WriteString(rt.Method) |
| 24 | b.WriteByte(' ') |
| 25 | b.WriteString(rt.Path) |
| 26 | b.WriteByte('\n') |
| 27 | } |
| 28 | blob := b.String() |
| 29 | for _, want := range []string{ |
| 30 | "GET /api/v1/user\n", |
| 31 | "PUT /api/v1/user\n", |
| 32 | "GET /api/v1/user/export\n", |
| 33 | "POST /api/v1/user/tokens\n", |
| 34 | "DELETE /api/v1/user/tokens/{id}\n", |
| 35 | "POST /api/v1/user/keys\n", |
| 36 | "POST /api/v1/groups\n", |
| 37 | "POST /api/v1/groups/{path}/transfer\n", |
| 38 | "POST /api/v1/groups/{path}/members\n", |
| 39 | "POST /api/v1/groups/{path}/secrets\n", |
| 40 | "POST /api/v1/groups/{path}/webhooks\n", |
| 41 | "PUT /api/v1/groups/{path}/approvals\n", |
| 42 | "POST /api/v1/groups/{path}/logo\n", |
| 43 | "POST /api/v1/projects\n", |
| 44 | "DELETE /api/v1/projects/{owner}/{repo}\n", |
| 45 | "POST /api/v1/projects/{owner}/{repo}/members\n", |
| 46 | "POST /api/v1/projects/{owner}/{repo}/protected_branches\n", |
| 47 | "POST /api/v1/projects/{owner}/{repo}/protected_tags\n", |
| 48 | "POST /api/v1/projects/{owner}/{repo}/repository/branches\n", |
| 49 | "DELETE /api/v1/projects/{owner}/{repo}/repository/branches/{branch...}\n", |
| 50 | "POST /api/v1/projects/{owner}/{repo}/repository/tags\n", |
| 51 | "GET /api/v1/projects/{owner}/{repo}/repository/files/{path...}\n", |
| 52 | "POST /api/v1/projects/{owner}/{repo}/repository/commits\n", |
| 53 | "GET /api/v1/projects/{owner}/{repo}/repository/commits/{sha}/diff\n", |
| 54 | "GET /api/v1/repos/{owner}/{repo}/issues\n", |
| 55 | "POST /api/v1/repos/{owner}/{repo}/issues\n", |
| 56 | "PUT /api/v1/repos/{owner}/{repo}/issues/{number}\n", |
| 57 | "POST /api/v1/repos/{owner}/{repo}/issues/{number}/notes\n", |
| 58 | "POST /api/v1/projects/{owner}/{repo}/merge_requests\n", |
| 59 | "POST /api/v1/projects/{owner}/{repo}/merge_requests/{number}/notes\n", |
| 60 | "POST /api/v1/projects/{owner}/{repo}/merge_requests/{number}/reviews\n", |
| 61 | "POST /api/v1/projects/{owner}/{repo}/merge_requests/{number}/suggestions\n", |
| 62 | "POST /api/v1/projects/{owner}/{repo}/merge_requests/{number}/merge\n", |
| 63 | "POST /api/v1/projects/{owner}/{repo}/pipelines\n", |
| 64 | "POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/retry\n", |
| 65 | "POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/play\n", |
| 66 | "POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/retry\n", |
| 67 | "POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/cancel\n", |
| 68 | "GET /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/log\n", |
| 69 | "GET /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/artifacts\n", |
| 70 | "PUT /api/v1/projects/{owner}/{repo}/ci/file\n", |
| 71 | "POST /api/v1/projects/{owner}/{repo}/ci/lint\n", |
| 72 | "GET /api/v1/ci/spec\n", |
| 73 | "GET /api/v1/ci/presets\n", |
| 74 | "POST /api/v1/projects/{owner}/{repo}/releases\n", |
| 75 | "POST /api/v1/repos/{owner}/{repo}/releases\n", |
| 76 | "POST /api/v1/repos/{owner}/{repo}/releases/{tag}/assets\n", |
| 77 | "PUT /api/v1/admin/storage\n", |
| 78 | } { |
| 79 | if !strings.Contains(blob, want) { |
| 80 | t.Errorf("catalog mist %s", strings.TrimSpace(want)) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestAPIPermissionsAndResources(t *testing.T) { |
| 86 | if _, err := exec.LookPath("git"); err != nil { |
| 87 | t.Skip("git ontbreekt") |
| 88 | } |
| 89 | storage := t.TempDir() |
| 90 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 91 | if err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | t.Cleanup(func() { _ = st.Close() }) |
| 95 | ctx := context.Background() |
| 96 | ada, err := st.UpsertUser(ctx, 1, "ada", "", false) |
| 97 | if err != nil || !ada.InstanceAdmin { |
| 98 | t.Fatalf("ada admin: %+v %v", ada, err) |
| 99 | } |
| 100 | dev, err := st.UpsertUser(ctx, 2, "dev", "", false) |
| 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | ro, err := st.UpsertUser(ctx, 3, "ro", "", false) |
| 105 | if err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | issue := func(u store.User, name, scopes string, exp time.Time) string { |
| 109 | t.Helper() |
| 110 | plain, _, err := st.IssueToken(ctx, u.ID, store.TokenRequest{Name: name, Scopes: scopes, ExpiresAt: exp}) |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | return plain |
| 115 | } |
| 116 | adaTok := issue(ada, "ada", "api", time.Time{}) |
| 117 | devTok := issue(dev, "dev", "api", time.Time{}) |
| 118 | roTok := issue(ro, "ro", "api", time.Time{}) |
| 119 | readAPI := issue(ada, "read", "read_api", time.Time{}) |
| 120 | writeRepo := issue(dev, "wrepo", "write_repository", time.Time{}) |
| 121 | readRepo := issue(dev, "rrepo", "read_repository", time.Time{}) |
| 122 | expired := issue(ada, "old", "api", time.Now().Add(40*time.Millisecond)) |
| 123 | time.Sleep(80 * time.Millisecond) |
| 124 | |
| 125 | cfg := config.Config{ |
| 126 | BaseURL: "http://127.0.0.1:9", StoragePath: storage, |
| 127 | Auth: config.Auth{Mode: "dev", DevUser: "ada", DevAuthID: 1}, |
| 128 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 129 | CI: config.CI{Enabled: true}, |
| 130 | } |
| 131 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "ada", AuthID: 1}, Git: "git", HookBin: "/bin/true", StateDir: t.TempDir()} |
| 132 | ts := httptest.NewServer(srv.Handler()) |
| 133 | t.Cleanup(ts.Close) |
| 134 | |
| 135 | call := func(method, path, token, body string, hdr string) (int, string) { |
| 136 | t.Helper() |
| 137 | req, err := http.NewRequest(method, ts.URL+path, strings.NewReader(body)) |
| 138 | if err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | if body != "" { |
| 142 | req.Header.Set("Content-Type", "application/json") |
| 143 | } |
| 144 | switch hdr { |
| 145 | case "private": |
| 146 | req.Header.Set("PRIVATE-TOKEN", token) |
| 147 | case "basic": |
| 148 | req.SetBasicAuth("git", token) |
| 149 | default: |
| 150 | if token != "" { |
| 151 | req.Header.Set("Authorization", "Bearer "+token) |
| 152 | } |
| 153 | } |
| 154 | res, err := http.DefaultClient.Do(req) |
| 155 | if err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | return res.StatusCode, read(t, res) |
| 159 | } |
| 160 | want := func(got int, body, label string, status int, contains string) { |
| 161 | t.Helper() |
| 162 | if got != status || (contains != "" && !strings.Contains(body, contains)) { |
| 163 | t.Fatalf("%s: status %d body %s", label, got, body) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | code, body := call(http.MethodGet, "/api/v1/user", "", "", "") |
| 168 | want(code, body, "anoniem", 401, "message") |
| 169 | code, body = call(http.MethodGet, "/api/v1/openapi.json", "", "", "") |
| 170 | want(code, body, "openapi", 200, `"openapi":"3.0.3"`) |
| 171 | if !strings.Contains(body, "privateToken") || !strings.Contains(body, "/projects/{owner}/{repo}/merge_requests") { |
| 172 | t.Fatalf("openapi dekt niet: %s", body[:200]) |
| 173 | } |
| 174 | code, body = call(http.MethodGet, "/api/v1/ci/spec", "", "", "") |
| 175 | want(code, body, "spec", 200, "blue-ci-cd.yml") |
| 176 | code, body = call(http.MethodGet, "/api/v1/ci/presets", "", "", "") |
| 177 | want(code, body, "presets", 200, "bluegit/") |
| 178 | code, body = call(http.MethodGet, "/api/v1/user", expired, "", "") |
| 179 | want(code, body, "verlopen", 401, "message") |
| 180 | code, body = call(http.MethodGet, "/api/v1/user", adaTok, "", "private") |
| 181 | want(code, body, "private-token", 200, `"username":"ada"`) |
| 182 | code, body = call(http.MethodGet, "/api/v1/user", adaTok, "", "basic") |
| 183 | want(code, body, "basic", 200, `"instance_admin":true`) |
| 184 | code, body = call(http.MethodPost, "/api/v1/user/tokens", readAPI, `{"name":"no"}`, "") |
| 185 | want(code, body, "read_api schrijft niet", 403, "message") |
| 186 | code, body = call(http.MethodGet, "/api/v1/user", readRepo, "", "") |
| 187 | want(code, body, "read_repository is geen api-lezen", 403, "message") |
| 188 | |
| 189 | code, body = call(http.MethodPost, "/api/v1/projects", adaTok, `{"name":"Demo","description":"leeg","visibility":"private"}`, "") |
| 190 | want(code, body, "leeg project", 201, `"path":"ada/demo"`) |
| 191 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo/repository/commits", adaTok, "", "") |
| 192 | want(code, body, "geen commits", 200, "[]") |
| 193 | code, body = call(http.MethodPost, "/api/v1/projects", adaTok, `{"name":"Readme","initialize_with_readme":true}`, "") |
| 194 | want(code, body, "readme", 201, `"path":"ada/readme"`) |
| 195 | code, body = call(http.MethodGet, "/api/v1/projects/ada/readme/repository/files/README.md", adaTok, "", "") |
| 196 | want(code, body, "bestand", 200, "# readme") |
| 197 | |
| 198 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo", roTok, "", "") |
| 199 | want(code, body, "prive zonder rol", 404, "message") |
| 200 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/members", adaTok, `{"username":"ro","role":"readonly"}`, "") |
| 201 | want(code, body, "lid ro", 200, `"username":"ro"`) |
| 202 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/members", adaTok, `{"username":"dev","role":"developer"}`, "") |
| 203 | want(code, body, "lid dev", 200, "developer") |
| 204 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo", roTok, "", "") |
| 205 | want(code, body, "readonly ziet", 200, "ada/demo") |
| 206 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/repository/branches", roTok, `{"name":"nope"}`, "") |
| 207 | want(code, body, "readonly branch", 403, "message") |
| 208 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/secrets", devTok, `{"name":"TOKEN","value":"x"}`, "") |
| 209 | want(code, body, "dev secret", 403, "message") |
| 210 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/secrets", adaTok, `{"name":"TOKEN","value":"geheim"}`, "") |
| 211 | want(code, body, "secret", 200, "TOKEN") |
| 212 | code, body = call(http.MethodDelete, "/api/v1/projects/ada/demo", devTok, `{"confirm":"demo"}`, "") |
| 213 | want(code, body, "dev verwijdert niet", 403, "message") |
| 214 | |
| 215 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/repository/commits", adaTok, `{"branch":"master","commit_message":"eerste","actions":[{"action":"create","file_path":"a.txt","content":"hoi\n"}]}`, "") |
| 216 | want(code, body, "eerste commit", 201, `"id":`) |
| 217 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/repository/commits", writeRepo, `{"branch":"side","start_branch":"master","commit_message":"zijtak","actions":[{"action":"create","file_path":"b.txt","content":"meer\n"}]}`, "") |
| 218 | want(code, body, "write_repository commit", 201, `"id":`) |
| 219 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/members", writeRepo, `{"username":"ro","role":"developer"}`, "") |
| 220 | want(code, body, "write_repository geen leden", 403, "message") |
| 221 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/repository/commits", readRepo, `{"branch":"master","commit_message":"nee","actions":[{"action":"update","file_path":"a.txt","content":"x"}]}`, "") |
| 222 | want(code, body, "read_repository schrijft niet", 403, "message") |
| 223 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo/repository/commits", readAPI, "", "") |
| 224 | want(code, body, "read_api leest commits", 200, "eerste") |
| 225 | |
| 226 | code, list := call(http.MethodGet, "/api/v1/projects/ada/demo/repository/commits", adaTok, "", "") |
| 227 | want(code, list, "lijst", 200, "eerste") |
| 228 | var rows []map[string]any |
| 229 | if err := json.Unmarshal([]byte(list), &rows); err != nil || len(rows) == 0 { |
| 230 | t.Fatalf("commits json %v %s", err, list) |
| 231 | } |
| 232 | sha, _ := rows[0]["id"].(string) |
| 233 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo/repository/commits/"+sha+"/diff", adaTok, "", "") |
| 234 | want(code, body, "diff", 200, "a.txt") |
| 235 | |
| 236 | req, err := http.NewRequest(http.MethodGet, ts.URL+"/ada/demo.git/info/refs?service=git-upload-pack", nil) |
| 237 | if err != nil { |
| 238 | t.Fatal(err) |
| 239 | } |
| 240 | req.SetBasicAuth("ada", readAPI) |
| 241 | res, err := http.DefaultClient.Do(req) |
| 242 | if err != nil { |
| 243 | t.Fatal(err) |
| 244 | } |
| 245 | _ = read(t, res) |
| 246 | if res.StatusCode != http.StatusForbidden { |
| 247 | t.Fatalf("read_api clone %d", res.StatusCode) |
| 248 | } |
| 249 | req, _ = http.NewRequest(http.MethodGet, ts.URL+"/ada/demo.git/info/refs?service=git-upload-pack", nil) |
| 250 | req.SetBasicAuth("dev", writeRepo) |
| 251 | res, err = http.DefaultClient.Do(req) |
| 252 | if err != nil { |
| 253 | t.Fatal(err) |
| 254 | } |
| 255 | _ = read(t, res) |
| 256 | if res.StatusCode == http.StatusForbidden || res.StatusCode == http.StatusUnauthorized { |
| 257 | t.Fatalf("write_repository leest git niet: %d", res.StatusCode) |
| 258 | } |
| 259 | |
| 260 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/repository/branches", devTok, `{"name":"feature","from":"master"}`, "") |
| 261 | want(code, body, "branch", 201, "feature") |
| 262 | code, body = call(http.MethodPut, "/api/v1/projects/ada/demo/ci/file", devTok, `{"branch":"feature","base":"master","message":"ci","content":"stages: [test]\n"}`, "") |
| 263 | want(code, body, "ci-editor", 200, `"id"`) |
| 264 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/ci/lint", roTok, `{"body":"stages: [test]\n","ref":"feature"}`, "") |
| 265 | want(code, body, "lint", 200, "status") |
| 266 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/merge_requests", adaTok, `{"title":"Werk","source_branch":"feature","target_branch":"master"}`, "") |
| 267 | want(code, body, "mr", 201, `"iid":`) |
| 268 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/merge_requests", readAPI, `{"title":"Nee","source_branch":"feature","target_branch":"master"}`, "") |
| 269 | want(code, body, "read_api mr", 403, "message") |
| 270 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/merge_requests/1/notes", roTok, `{"body":"gezien"}`, "") |
| 271 | want(code, body, "note", 201, `"id"`) |
| 272 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/merge_requests/1/close", devTok, `{}`, "") |
| 273 | want(code, body, "dev sluit andermans mr niet", 403, "message") |
| 274 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/merge_requests/1/close", adaTok, `{}`, "") |
| 275 | want(code, body, "sluiten", 200, "closed") |
| 276 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/merge_requests/1/reopen", adaTok, `{}`, "") |
| 277 | want(code, body, "heropenen", 200, "open") |
| 278 | code, body = call(http.MethodPost, "/api/v1/projects/ada/demo/pipelines", devTok, `{"ref":"feature"}`, "") |
| 279 | want(code, body, "pipeline", 201, `"id"`) |
| 280 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo/pipelines?ref=feature", roTok, "", "") |
| 281 | want(code, body, "pipelines op ref", 200, `"ref":"refs/heads/feature"`) |
| 282 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo/pipelines?mr=1", roTok, "", "") |
| 283 | want(code, body, "pipelines op mr", 200, `"ref":"refs/heads/feature"`) |
| 284 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo/pipelines?ref=bestaatniet", roTok, "", "") |
| 285 | want(code, body, "pipelines leeg", 200, `[]`) |
| 286 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo/pipelines?sha=xyz", roTok, "", "") |
| 287 | want(code, body, "slechte sha", 400, "message") |
| 288 | code, body = call(http.MethodGet, "/api/v1/projects/ada/demo/pipelines?mr=1&ref=feature", roTok, "", "") |
| 289 | want(code, body, "ref en mr", 400, "message") |
| 290 | |
| 291 | code, body = call(http.MethodPost, "/api/v1/groups", adaTok, `{"name":"Team","description":"samen"}`, "") |
| 292 | want(code, body, "groep", 201, `"path":"team"`) |
| 293 | code, body = call(http.MethodPut, "/api/v1/groups/team/approvals", adaTok, `{"maintainer_required":true,"required_count":1}`, "") |
| 294 | want(code, body, "approvals", 200, "maintainer_required") |
| 295 | code, body = call(http.MethodPost, "/api/v1/groups/team/webhooks", adaTok, `{"name":"ping","preset":"generic","url":"https://example.com/hook","events":["push"]}`, "") |
| 296 | want(code, body, "webhook", 201, `"id"`) |
| 297 | code, body = call(http.MethodPut, "/api/v1/admin/storage", devTok, `{"path":"/tmp"}`, "") |
| 298 | want(code, body, "dev geen admin", 403, "message") |
| 299 | code, body = call(http.MethodGet, "/api/v1/admin/storage", adaTok, "", "") |
| 300 | want(code, body, "storage", 200, storage) |
| 301 | |
| 302 | status, raw := srv.CallAPI(ctx, ada, http.MethodGet, "/user", nil) |
| 303 | if status != 200 || !strings.Contains(string(raw), `"username":"ada"`) { |
| 304 | t.Fatalf("ssh-api %d %s", status, raw) |
| 305 | } |
| 306 | } |