| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/cookiejar" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "gitlab.com/Timfa/bluegit/internal/auth" |
| 16 | "gitlab.com/Timfa/bluegit/internal/config" |
| 17 | "gitlab.com/Timfa/bluegit/internal/store" |
| 18 | ) |
| 19 | |
| 20 | func TestStaleManualPlayBlockedAndAdmin(t *testing.T) { |
| 21 | if _, err := exec.LookPath("git"); err != nil { |
| 22 | t.Skip("git ontbreekt") |
| 23 | } |
| 24 | storage := t.TempDir() |
| 25 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | t.Cleanup(func() { _ = st.Close() }) |
| 30 | cfg := config.Config{ |
| 31 | BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage, |
| 32 | Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"}, |
| 33 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 34 | } |
| 35 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} |
| 36 | ts := httptest.NewServer(srv.Handler()) |
| 37 | t.Cleanup(ts.Close) |
| 38 | jar, _ := cookiejar.New(nil) |
| 39 | tim := &http.Client{Jar: jar} |
| 40 | page := read(t, mustGet(t, tim, ts.URL+"/login")) |
| 41 | csrf := csrfOf(t, page) |
| 42 | if _, err := tim.PostForm(ts.URL+"/new/repo", url.Values{"csrf": {csrf}, "name": {"rel"}, "visibility": {"public"}}); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | ctx := context.Background() |
| 46 | repo, err := st.RepoByPath(ctx, "tim", "rel") |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | mk := func(sha string) (int64, int64) { |
| 51 | id, err := st.CreatePipeline(ctx, repo.ID, sha, "refs/heads/master", "push", "tim", "x", []store.NewJob{ |
| 52 | {Name: "release", Stage: "release", Script: "true", When: "manual"}, |
| 53 | }) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | js, _ := st.JobsByPipeline(ctx, id) |
| 58 | return id, js[0].ID |
| 59 | } |
| 60 | oldP, oldJ := mk("a1") |
| 61 | newP, newJ := mk("a2") |
| 62 | srv.expireSuperseded(ctx, repo.ID, "refs/heads/master", newP) |
| 63 | |
| 64 | u, _ := st.UserByUsername(ctx, "tim") |
| 65 | if _, err := srv.opJobAction(ctx, u, repo, oldP, oldJ, "play"); err == nil || statusOf(err) != 409 { |
| 66 | t.Fatalf("verlopen play: %v", err) |
| 67 | } |
| 68 | if _, err := srv.opJobAction(ctx, u, repo, oldP, oldJ, "retry"); err == nil || statusOf(err) != 409 { |
| 69 | t.Fatalf("verlopen retry: %v", err) |
| 70 | } |
| 71 | // Jobpagina: melding via eigen modal (data-alert), geen startformulier. |
| 72 | body := read(t, mustGet(t, tim, ts.URL+"/tim/rel/-/pipelines/"+strconv.FormatInt(oldP, 10)+"/jobs/"+strconv.FormatInt(oldJ, 10))) |
| 73 | if !strings.Contains(body, "data-alert=") || strings.Contains(body, "/jobs/"+strconv.FormatInt(oldJ, 10)+"/play") { |
| 74 | t.Fatal("jobpagina mist modal of heeft nog startformulier") |
| 75 | } |
| 76 | // Nieuwste blijft startbaar. |
| 77 | if j, err := srv.opJobAction(ctx, u, repo, newP, newJ, "play"); err != nil || j.Status != "queued" { |
| 78 | t.Fatalf("nieuwste: %v %s", err, j.Status) |
| 79 | } |
| 80 | // Beheer > Pipelines. |
| 81 | ap := read(t, mustGet(t, tim, ts.URL+"/admin/pipelines")) |
| 82 | if !strings.Contains(ap, `name="stale_manual_days"`) { |
| 83 | t.Fatal("beheerpagina") |
| 84 | } |
| 85 | if _, err := tim.PostForm(ts.URL+"/admin/pipelines", url.Values{"csrf": {csrfOf(t, ap)}, "stale_manual_days": {"0"}}); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | if d, _ := st.StaleManualDays(ctx); d != 0 { |
| 89 | t.Fatalf("instelling %d", d) |
| 90 | } |
| 91 | sp := read(t, mustGet(t, tim, ts.URL+"/tim/rel/-/settings")) |
| 92 | if !strings.Contains(sp, `name="stale_manual_days"`) { |
| 93 | t.Fatal("projectinstelling ontbreekt") |
| 94 | } |
| 95 | if _, err := tim.PostForm(ts.URL+"/tim/rel/-/settings", url.Values{"csrf": {csrfOf(t, sp)}, "visibility": {"public"}, "stale_manual_days": {"3"}}); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | if d, _ := st.RepoStaleManualDays(ctx, repo.ID); d != 3 { |
| 99 | t.Fatalf("project %d", d) |
| 100 | } |
| 101 | } |