| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/cookiejar" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "gitlab.com/Timfa/bluegit/internal/auth" |
| 18 | "gitlab.com/Timfa/bluegit/internal/config" |
| 19 | "gitlab.com/Timfa/bluegit/internal/store" |
| 20 | "gitlab.com/Timfa/bluegit/internal/webhook" |
| 21 | ) |
| 22 | |
| 23 | func TestWebhookPayloads(t *testing.T) { |
| 24 | if _, err := exec.LookPath("git"); err != nil { |
| 25 | t.Skip("git ontbreekt") |
| 26 | } |
| 27 | storage := t.TempDir() |
| 28 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | t.Cleanup(func() { _ = st.Close() }) |
| 33 | cfg := config.Config{ |
| 34 | BaseURL: "http://git.example", Listen: "127.0.0.1:0", StoragePath: storage, |
| 35 | Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"}, |
| 36 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 37 | } |
| 38 | var bodies [][]byte |
| 39 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} |
| 40 | srv.hookSink = func(b []byte) { |
| 41 | bodies = append(bodies, append([]byte(nil), b...)) |
| 42 | } |
| 43 | ts := httptest.NewServer(srv.Handler()) |
| 44 | t.Cleanup(ts.Close) |
| 45 | jar, _ := cookiejar.New(nil) |
| 46 | tim := &http.Client{Jar: jar} |
| 47 | page := read(t, mustGet(t, tim, ts.URL+"/login")) |
| 48 | page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{ |
| 49 | "csrf": {csrfOf(t, page)}, "name": {"hooks"}, "visibility": {"public"}, |
| 50 | })) |
| 51 | ctx := context.Background() |
| 52 | repo, err := st.RepoByPath(ctx, "tim", "hooks") |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if _, err := st.CreateWebhook(ctx, store.Webhook{ |
| 57 | ScopeKind: "repo", ScopeID: repo.ID, Name: "alles", Preset: "generic", |
| 58 | URLTemplate: "https://example.test/hook", |
| 59 | Events: []string{"push", "merge_request", "pipeline_failure", "release_published"}, |
| 60 | }); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | if err := st.PutSecret(ctx, "repo", repo.ID, "DEPLOY_TOKEN", "super-secret-token", true); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | |
| 67 | page = read(t, mustPost(t, tim, ts.URL+"/settings/tokens", url.Values{ |
| 68 | "csrf": {csrfOf(t, page)}, "name": {"laptop"}, |
| 69 | })) |
| 70 | token := between(page, `<p class="code">`, "</p>") |
| 71 | remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/hooks.git" |
| 72 | work := t.TempDir() |
| 73 | git(t, work, "init", "-b", "master") |
| 74 | git(t, work, "config", "user.email", "tim@users.git.timfalken.com") |
| 75 | git(t, work, "config", "user.name", "tim") |
| 76 | writeFile(t, filepath.Join(work, "README.md"), "a\n") |
| 77 | git(t, work, "add", "README.md") |
| 78 | git(t, work, "commit", "-m", "eerste regel", "-m", "meer tekst") |
| 79 | writeFile(t, filepath.Join(work, "README.md"), "b\n") |
| 80 | git(t, work, "add", "README.md") |
| 81 | git(t, work, "commit", "-m", "tweede") |
| 82 | git(t, work, "remote", "add", "origin", remote) |
| 83 | git(t, work, "push", "-u", "origin", "master") |
| 84 | |
| 85 | push := decodeKind(t, bodies, "push") |
| 86 | if push["ref"] != "refs/heads/master" || push["before"] != strings.Repeat("0", 40) { |
| 87 | t.Fatalf("push ref/before %v", push) |
| 88 | } |
| 89 | commits := push["commits"].([]any) |
| 90 | if len(commits) != 2 { |
| 91 | t.Fatalf("commits %d %s", len(commits), mustJSON(t, push)) |
| 92 | } |
| 93 | first := commits[0].(map[string]any) |
| 94 | second := commits[1].(map[string]any) |
| 95 | if first["message"] != "eerste regel\n\nmeer tekst" || second["message"] != "tweede" { |
| 96 | t.Fatalf("berichten %v %v", first["message"], second["message"]) |
| 97 | } |
| 98 | if first["author"] != "tim <tim@users.git.timfalken.com>" || !strings.Contains(first["url"].(string), "/tim/hooks/-/commit/") { |
| 99 | t.Fatalf("auteur of url %v", first) |
| 100 | } |
| 101 | if push["after"] != second["sha"] { |
| 102 | t.Fatalf("after %v tip %v", push["after"], second["sha"]) |
| 103 | } |
| 104 | |
| 105 | git(t, work, "checkout", "-b", "topic") |
| 106 | writeFile(t, filepath.Join(work, "extra.txt"), "c\n") |
| 107 | git(t, work, "add", "extra.txt") |
| 108 | git(t, work, "commit", "-m", "derde") |
| 109 | git(t, work, "push", "-u", "origin", "topic") |
| 110 | |
| 111 | page = read(t, mustGet(t, tim, ts.URL+"/tim/hooks/-/merge-requests/new?source=topic&target=master")) |
| 112 | page = read(t, mustPost(t, tim, ts.URL+"/tim/hooks/-/merge-requests", url.Values{ |
| 113 | "csrf": {csrfOf(t, page)}, "source": {"topic"}, "target": {"master"}, |
| 114 | "title": {"Toon de kloon-URL"}, "description": {"SSH en HTTPS."}, |
| 115 | })) |
| 116 | opened := decodeAction(t, bodies, "opened") |
| 117 | mr := opened["merge_request"].(map[string]any) |
| 118 | if mr["title"] != "Toon de kloon-URL" || mr["description"] != "SSH en HTTPS." || mr["source_branch"] != "topic" || mr["target_branch"] != "master" || mr["status"] != "open" || mr["author"] != "tim" || mr["id"] != float64(1) { |
| 119 | t.Fatalf("opened %s", mustJSON(t, mr)) |
| 120 | } |
| 121 | if _, ok := opened["comment"]; ok { |
| 122 | t.Fatalf("opened heeft een comment: %s", mustJSON(t, opened)) |
| 123 | } |
| 124 | |
| 125 | page = read(t, mustPost(t, tim, ts.URL+"/tim/hooks/-/merge-requests/1/notes", url.Values{ |
| 126 | "csrf": {csrfOf(t, page)}, "body": {"dit is het commentaar"}, |
| 127 | })) |
| 128 | commented := decodeAction(t, bodies, "commented") |
| 129 | if commented["comment"] != "dit is het commentaar" { |
| 130 | t.Fatalf("comment %s", mustJSON(t, commented)) |
| 131 | } |
| 132 | |
| 133 | page = read(t, mustPost(t, tim, ts.URL+"/tim/hooks/-/merge-requests/1/merge", url.Values{ |
| 134 | "csrf": {csrfOf(t, page)}, |
| 135 | })) |
| 136 | if strings.Contains(page, "flash") && strings.Contains(strings.ToLower(page), "error") { |
| 137 | t.Fatalf("merge pagina:\n%s", page) |
| 138 | } |
| 139 | merged := decodeAction(t, bodies, "merged") |
| 140 | if merged["merge_request"].(map[string]any)["status"] != "merged" { |
| 141 | t.Fatalf("merged %s", mustJSON(t, merged)) |
| 142 | } |
| 143 | |
| 144 | sha := second["sha"].(string) |
| 145 | id, err := st.CreatePipeline(ctx, repo.ID, sha, "refs/heads/master", "push", "tim", "tweede", []store.NewJob{ |
| 146 | {Name: "test", Stage: "test", Script: "exit 1"}, |
| 147 | {Name: "build", Stage: "build", Script: "echo ok"}, |
| 148 | {Name: "soft", Stage: "test", Script: "exit 1", AllowFailure: true}, |
| 149 | }) |
| 150 | if err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | jobs, err := st.JobsByPipeline(ctx, id) |
| 154 | if err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | var lines []string |
| 158 | for i := 0; i < webhook.LogMaxLines+40; i++ { |
| 159 | lines = append(lines, "regel") |
| 160 | } |
| 161 | lines = append(lines, "token super-secret-token") |
| 162 | log := strings.Join(lines, "\n") |
| 163 | for _, j := range jobs { |
| 164 | if _, err := st.MarkJobRunning(ctx, j.ID); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | switch j.Name { |
| 168 | case "test": |
| 169 | if err := st.AppendJobLog(ctx, j.ID, log); err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | if err := st.FinishJob(ctx, j.ID, "failed", ""); err != nil { |
| 173 | t.Fatal(err) |
| 174 | } |
| 175 | case "soft": |
| 176 | if err := st.AppendJobLog(ctx, j.ID, "zacht super-secret-token\n"); err != nil { |
| 177 | t.Fatal(err) |
| 178 | } |
| 179 | if err := st.FinishJob(ctx, j.ID, "failed", ""); err != nil { |
| 180 | t.Fatal(err) |
| 181 | } |
| 182 | default: |
| 183 | if err := st.AppendJobLog(ctx, j.ID, "gebouwd\n"); err != nil { |
| 184 | t.Fatal(err) |
| 185 | } |
| 186 | if err := st.FinishJob(ctx, j.ID, "success", ""); err != nil { |
| 187 | t.Fatal(err) |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | if _, err := st.RefreshPipelineStatus(ctx, id); err != nil { |
| 192 | t.Fatal(err) |
| 193 | } |
| 194 | before := len(bodies) |
| 195 | srv.Dispatch(repo.ID, webhook.Event{ |
| 196 | Type: webhook.EventPipelineFailure, PipelineID: strconv.FormatInt(id, 10), |
| 197 | }) |
| 198 | if len(bodies) == before { |
| 199 | t.Fatal("geen pipeline-payload") |
| 200 | } |
| 201 | var pipe map[string]any |
| 202 | if err := json.Unmarshal(bodies[len(bodies)-1], &pipe); err != nil { |
| 203 | t.Fatal(err) |
| 204 | } |
| 205 | if pipe["object_kind"] != "pipeline" || pipe["type"] != "pipeline_failure" { |
| 206 | t.Fatalf("pipeline %s", bodies[len(bodies)-1]) |
| 207 | } |
| 208 | detail := pipe["pipeline"].(map[string]any) |
| 209 | if detail["id"] != float64(id) || detail["status"] != "failed" || detail["ref"] != "refs/heads/master" || detail["sha"] != sha || detail["author"] != "tim" { |
| 210 | t.Fatalf("detail %s", mustJSON(t, detail)) |
| 211 | } |
| 212 | if !strings.Contains(detail["commit_message"].(string), "tweede") { |
| 213 | t.Fatalf("commit_message %v", detail["commit_message"]) |
| 214 | } |
| 215 | if _, ok := detail["duration"]; !ok { |
| 216 | t.Fatal("duur ontbreekt") |
| 217 | } |
| 218 | var fullLogURL string |
| 219 | for _, item := range pipe["jobs"].([]any) { |
| 220 | job := item.(map[string]any) |
| 221 | raw, _ := json.Marshal(job) |
| 222 | if strings.Contains(string(raw), "super-secret-token") { |
| 223 | t.Fatalf("geheim in job %s", raw) |
| 224 | } |
| 225 | if _, ok := job["log"]; !ok { |
| 226 | t.Fatalf("elke job heeft een log: %s", raw) |
| 227 | } |
| 228 | if _, ok := job["log_truncated"]; !ok { |
| 229 | t.Fatalf("elke job heeft log_truncated: %s", raw) |
| 230 | } |
| 231 | switch job["name"] { |
| 232 | case "test": |
| 233 | if job["log_truncated"] != true || job["allow_failure"] != false { |
| 234 | t.Fatalf("test %s", raw) |
| 235 | } |
| 236 | text := job["log"].(string) |
| 237 | if !strings.Contains(text, "[MASKED]") || len(strings.Split(text, "\n")) != webhook.LogMaxLines { |
| 238 | t.Fatalf("log %d regels, staart %q", len(strings.Split(text, "\n")), text[max(0, len(text)-40):]) |
| 239 | } |
| 240 | fullLogURL, _ = job["log_url"].(string) |
| 241 | if !strings.Contains(fullLogURL, "/api/v1/repos/tim/hooks/jobs/") || !strings.HasSuffix(fullLogURL, "/log") || !strings.Contains(job["web_url"].(string), "/jobs/") { |
| 242 | t.Fatalf("urls %s", raw) |
| 243 | } |
| 244 | case "build": |
| 245 | if job["status"] != "success" || job["log"] != "gebouwd\n" || job["log_truncated"] != false { |
| 246 | t.Fatalf("build %s", raw) |
| 247 | } |
| 248 | if _, ok := job["log_url"]; ok { |
| 249 | t.Fatalf("korte geslaagde log heeft een url: %s", raw) |
| 250 | } |
| 251 | case "soft": |
| 252 | if job["allow_failure"] != true || job["log_truncated"] != false || !strings.Contains(job["log"].(string), "[MASKED]") { |
| 253 | t.Fatalf("soft %s", raw) |
| 254 | } |
| 255 | if _, ok := job["log_url"]; ok { |
| 256 | t.Fatalf("korte log heeft een url: %s", raw) |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | if fullLogURL == "" { |
| 261 | t.Fatal("geen log_url") |
| 262 | } |
| 263 | logPath := mustParse(t, fullLogURL).Path |
| 264 | noAuth := mustGet(t, http.DefaultClient, ts.URL+logPath) |
| 265 | if noAuth.StatusCode != http.StatusUnauthorized { |
| 266 | t.Fatalf("zonder token %d", noAuth.StatusCode) |
| 267 | } |
| 268 | noAuth.Body.Close() |
| 269 | full := getBearer(t, ts.URL+logPath, token) |
| 270 | if full.StatusCode != http.StatusOK || !strings.HasPrefix(full.Header.Get("Content-Type"), "text/plain") { |
| 271 | t.Fatalf("volle log %d %s", full.StatusCode, full.Header.Get("Content-Type")) |
| 272 | } |
| 273 | fullText := read(t, full) |
| 274 | if strings.Contains(fullText, "super-secret-token") || !strings.Contains(fullText, "[MASKED]") { |
| 275 | t.Fatal("volle log maskeert het geheim niet") |
| 276 | } |
| 277 | if n := len(strings.Split(fullText, "\n")); n <= webhook.LogMaxLines { |
| 278 | t.Fatalf("volle log heeft %d regels", n) |
| 279 | } |
| 280 | bad := getBearer(t, ts.URL+logPath, "bgj_geen-job") |
| 281 | if bad.StatusCode != http.StatusUnauthorized { |
| 282 | t.Fatalf("job-token %d", bad.StatusCode) |
| 283 | } |
| 284 | bad.Body.Close() |
| 285 | |
| 286 | rel := srv.releaseEvent(repo, "v1.2.0", "BlueGit 1.2.0", sha, []store.ReleaseAsset{{Name: "blue git", Size: 9}}) |
| 287 | raw, err := webhook.RenderBody("", rel, nil) |
| 288 | if err != nil { |
| 289 | t.Fatal(err) |
| 290 | } |
| 291 | var release map[string]any |
| 292 | if err := json.Unmarshal(raw, &release); err != nil { |
| 293 | t.Fatal(err) |
| 294 | } |
| 295 | info := release["release"].(map[string]any) |
| 296 | asset := info["assets"].([]any)[0].(map[string]any) |
| 297 | if info["tag"] != "v1.2.0" || info["name"] != "BlueGit 1.2.0" || info["commit"] != sha || !strings.Contains(asset["url"].(string), "/-/releases/v1.2.0/assets/blue%20git") { |
| 298 | t.Fatalf("release %s", raw) |
| 299 | } |
| 300 | |
| 301 | if elapsedSeconds(time.Unix(10, 0), time.Unix(12, 0)) != 2 { |
| 302 | t.Fatal("duur") |
| 303 | } |
| 304 | |
| 305 | infoPage := read(t, mustGet(t, tim, ts.URL+"/-/info")) |
| 306 | for _, want := range []string{"Webhook payloads", "log_truncated", "log_url", "/api/v1/webhooks/openapi.json", "/api/v1/repos/{owner}/{repo}/jobs/{id}/log", "commit_message", "source_branch"} { |
| 307 | if !strings.Contains(infoPage, want) { |
| 308 | t.Errorf("infopagina mist %q", want) |
| 309 | } |
| 310 | } |
| 311 | specRes, err := http.Get(ts.URL + "/api/v1/webhooks/openapi.json") |
| 312 | if err != nil { |
| 313 | t.Fatal(err) |
| 314 | } |
| 315 | spec := read(t, specRes) |
| 316 | if specRes.StatusCode != 200 || !strings.Contains(spec, `"openapi": "3.1.0"`) || !strings.Contains(spec, "log_truncated") || !strings.Contains(spec, "/repos/{owner}/{repo}/jobs/{id}/log") { |
| 317 | t.Fatalf("spec %d %s", specRes.StatusCode, spec[:min(200, len(spec))]) |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | func decodeKind(t *testing.T, bodies [][]byte, kind string) map[string]any { |
| 322 | t.Helper() |
| 323 | for i := len(bodies) - 1; i >= 0; i-- { |
| 324 | var doc map[string]any |
| 325 | if err := json.Unmarshal(bodies[i], &doc); err != nil { |
| 326 | continue |
| 327 | } |
| 328 | if doc["object_kind"] == kind { |
| 329 | return doc |
| 330 | } |
| 331 | } |
| 332 | t.Fatalf("geen %s in %d payloads", kind, len(bodies)) |
| 333 | return nil |
| 334 | } |
| 335 | |
| 336 | func decodeAction(t *testing.T, bodies [][]byte, action string) map[string]any { |
| 337 | t.Helper() |
| 338 | for i := len(bodies) - 1; i >= 0; i-- { |
| 339 | var doc map[string]any |
| 340 | if err := json.Unmarshal(bodies[i], &doc); err != nil { |
| 341 | continue |
| 342 | } |
| 343 | mr, _ := doc["merge_request"].(map[string]any) |
| 344 | if mr != nil && mr["action"] == action { |
| 345 | return doc |
| 346 | } |
| 347 | } |
| 348 | t.Fatalf("geen actie %s", action) |
| 349 | return nil |
| 350 | } |
| 351 | |
| 352 | func TestJobLogAPI(t *testing.T) { |
| 353 | storage := t.TempDir() |
| 354 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 355 | if err != nil { |
| 356 | t.Fatal(err) |
| 357 | } |
| 358 | t.Cleanup(func() { _ = st.Close() }) |
| 359 | ctx := context.Background() |
| 360 | tim, err := st.UpsertUser(ctx, 1, "tim", "", false) |
| 361 | if err != nil { |
| 362 | t.Fatal(err) |
| 363 | } |
| 364 | ada, err := st.UpsertUser(ctx, 2, "ada", "", false) |
| 365 | if err != nil { |
| 366 | t.Fatal(err) |
| 367 | } |
| 368 | priv, err := st.CreateRepo(ctx, "user", tim.ID, "priv", "", "master", "private") |
| 369 | if err != nil { |
| 370 | t.Fatal(err) |
| 371 | } |
| 372 | open, err := st.CreateRepo(ctx, "user", tim.ID, "open", "", "master", "public") |
| 373 | if err != nil { |
| 374 | t.Fatal(err) |
| 375 | } |
| 376 | if err := st.PutSecret(ctx, "repo", priv.ID, "DEPLOY_TOKEN", "super-secret-token", true); err != nil { |
| 377 | t.Fatal(err) |
| 378 | } |
| 379 | if err := st.PutSecret(ctx, "repo", open.ID, "DEPLOY_TOKEN", "super-secret-token", true); err != nil { |
| 380 | t.Fatal(err) |
| 381 | } |
| 382 | privJob := seedJobLog(t, st, priv.ID, "regel\nsuper-secret-token\n") |
| 383 | openJob := seedJobLog(t, st, open.ID, "publiek super-secret-token\n") |
| 384 | ownerTok, err := st.CreateToken(ctx, tim.ID, "laptop") |
| 385 | if err != nil { |
| 386 | t.Fatal(err) |
| 387 | } |
| 388 | adaTok, err := st.CreateToken(ctx, ada.ID, "ada") |
| 389 | if err != nil { |
| 390 | t.Fatal(err) |
| 391 | } |
| 392 | cfg := config.Config{ |
| 393 | BaseURL: "http://git.example", Listen: "127.0.0.1:0", StoragePath: storage, |
| 394 | Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"}, |
| 395 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 396 | } |
| 397 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git"} |
| 398 | ts := httptest.NewServer(srv.Handler()) |
| 399 | t.Cleanup(ts.Close) |
| 400 | |
| 401 | privPath := ts.URL + "/api/v1/repos/tim/priv/jobs/" + strconv.FormatInt(privJob, 10) + "/log" |
| 402 | openPath := ts.URL + "/api/v1/repos/tim/open/jobs/" + strconv.FormatInt(openJob, 10) + "/log" |
| 403 | |
| 404 | missing := mustGet(t, http.DefaultClient, privPath) |
| 405 | if missing.StatusCode != http.StatusUnauthorized { |
| 406 | t.Fatalf("zonder token %d", missing.StatusCode) |
| 407 | } |
| 408 | missing.Body.Close() |
| 409 | for _, tok := range []string{"nope", "bg_onbekend", "bgj_job"} { |
| 410 | res := getBearer(t, privPath, tok) |
| 411 | if res.StatusCode != http.StatusUnauthorized { |
| 412 | t.Fatalf("token %s geeft %d", tok, res.StatusCode) |
| 413 | } |
| 414 | res.Body.Close() |
| 415 | } |
| 416 | stranger := getBearer(t, privPath, adaTok) |
| 417 | if stranger.StatusCode != http.StatusForbidden { |
| 418 | t.Fatalf("vreemde op prive %d", stranger.StatusCode) |
| 419 | } |
| 420 | stranger.Body.Close() |
| 421 | other := getBearer(t, ts.URL+"/api/v1/repos/tim/priv/jobs/"+strconv.FormatInt(openJob, 10)+"/log", ownerTok) |
| 422 | if other.StatusCode != http.StatusNotFound { |
| 423 | t.Fatalf("job van andere repo %d", other.StatusCode) |
| 424 | } |
| 425 | other.Body.Close() |
| 426 | |
| 427 | own := getBearer(t, privPath, ownerTok) |
| 428 | if own.StatusCode != http.StatusOK || !strings.HasPrefix(own.Header.Get("Content-Type"), "text/plain") { |
| 429 | t.Fatalf("eigenaar %d %s", own.StatusCode, own.Header.Get("Content-Type")) |
| 430 | } |
| 431 | body := read(t, own) |
| 432 | if body != "regel\n[MASKED]\n" { |
| 433 | t.Fatalf("prive log %q", body) |
| 434 | } |
| 435 | pub := getBearer(t, openPath, adaTok) |
| 436 | if pub.StatusCode != http.StatusOK { |
| 437 | t.Fatalf("publiek met vreemd token %d", pub.StatusCode) |
| 438 | } |
| 439 | if got := read(t, pub); got != "publiek [MASKED]\n" { |
| 440 | t.Fatalf("publieke log %q", got) |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | func seedJobLog(t *testing.T, st *store.Store, repoID int64, log string) int64 { |
| 445 | t.Helper() |
| 446 | ctx := context.Background() |
| 447 | id, err := st.CreatePipeline(ctx, repoID, strings.Repeat("a", 40), "refs/heads/master", "push", "tim", "titel", []store.NewJob{ |
| 448 | {Name: "build", Stage: "build", Script: "echo ok"}, |
| 449 | }) |
| 450 | if err != nil { |
| 451 | t.Fatal(err) |
| 452 | } |
| 453 | jobs, err := st.JobsByPipeline(ctx, id) |
| 454 | if err != nil || len(jobs) != 1 { |
| 455 | t.Fatal(err) |
| 456 | } |
| 457 | if err := st.AppendJobLog(ctx, jobs[0].ID, log); err != nil { |
| 458 | t.Fatal(err) |
| 459 | } |
| 460 | return jobs[0].ID |
| 461 | } |
| 462 | |
| 463 | func getBearer(t *testing.T, raw, token string) *http.Response { |
| 464 | t.Helper() |
| 465 | req, err := http.NewRequest(http.MethodGet, raw, nil) |
| 466 | if err != nil { |
| 467 | t.Fatal(err) |
| 468 | } |
| 469 | req.Header.Set("Authorization", "Bearer "+token) |
| 470 | res, err := http.DefaultClient.Do(req) |
| 471 | if err != nil { |
| 472 | t.Fatal(err) |
| 473 | } |
| 474 | return res |
| 475 | } |
| 476 | |
| 477 | func mustParse(t *testing.T, raw string) *url.URL { |
| 478 | t.Helper() |
| 479 | u, err := url.Parse(raw) |
| 480 | if err != nil { |
| 481 | t.Fatal(err) |
| 482 | } |
| 483 | return u |
| 484 | } |
| 485 | |
| 486 | func mustJSON(t *testing.T, v any) string { |
| 487 | t.Helper() |
| 488 | b, err := json.Marshal(v) |
| 489 | if err != nil { |
| 490 | t.Fatal(err) |
| 491 | } |
| 492 | return string(b) |
| 493 | } |