internal/hookd/pushhints_test.go
74 lines · 2172 bytes
1 symbol in this file
1package hookd
2
3import (
4 "path/filepath"
5 "strings"
6 "testing"
7
8 "gitbay.org/gitbay/internal/config"
9 "gitbay.org/gitbay/internal/control"
10 "gitbay.org/gitbay/internal/policy"
11 "gitbay.org/gitbay/internal/store"
12)
13
14func TestPushHints(t *testing.T) {
15 root := t.TempDir()
16 st, err := store.Open(filepath.Join(root, "gitbay.db"))
17 if err != nil {
18 t.Fatal(err)
19 }
20 defer st.Close()
21
22 if err := st.MigrateUp(); err != nil {
23 t.Fatal(err)
24 }
25 uid, err := st.CreateUser("alice", true)
26 if err != nil {
27 t.Fatal(err)
28 }
29 repoID, err := st.CreateRepo("user", uid, "app", "public")
30 if err != nil {
31 t.Fatal(err)
32 }
33 var cfg config.Config
34 cfg.Server.Root = root
35 cfg.Server.SiteURL = "https://gitbay.org"
36
37 // Default branch push produces no hint.
38 got := control.PushHints(st, cfg, repoID, []policy.RefUpdate{
39 {Ref: "refs/heads/main", Old: strings.Repeat("0", 40), New: strings.Repeat("a", 40)},
40 })
41 if got != "" {
42 t.Fatalf("default branch hint = %q, want empty", got)
43 }
44
45 // Feature branch without an MR suggests creating one.
46 got = control.PushHints(st, cfg, repoID, []policy.RefUpdate{
47 {Ref: "refs/heads/feat-x", Old: strings.Repeat("0", 40), New: strings.Repeat("a", 40)},
48 })
49 if !strings.Contains(got, "gitbay mr create --source feat-x") ||
50 !strings.Contains(got, "https://gitbay.org/alice/app/mrs/new?source=feat-x") {
51 t.Fatalf("new branch hint = %q", got)
52 }
53
54 // Branch deletion produces no hint.
55 got = control.PushHints(st, cfg, repoID, []policy.RefUpdate{
56 {Ref: "refs/heads/feat-x", Old: strings.Repeat("a", 40), New: strings.Repeat("0", 40), IsDelete: true},
57 })
58 if got != "" {
59 t.Fatalf("delete hint = %q, want empty", got)
60 }
61
62 // Branch with an open MR prints the existing MR URL.
63 n, err := st.CreateMR(repoID, uid, repoID, "feat-x", "main", "my feat", "", strings.Repeat("a", 40), "md", false)
64 if err != nil {
65 t.Fatal(err)
66 }
67 got = control.PushHints(st, cfg, repoID, []policy.RefUpdate{
68 {Ref: "refs/heads/feat-x", Old: strings.Repeat("a", 40), New: strings.Repeat("b", 40)},
69 })
70 want := "open merge request !1: https://gitbay.org/alice/app/mrs/1"
71 if n != 1 || got != want {
72 t.Fatalf("open MR hint = %q, want %q", got, want)
73 }
74}