internal/control/mirrorcmd_test.go
66 lines · 2474 bytes
2 symbols in this file
1package control
2
3import (
4 "strconv"
5 "strings"
6 "testing"
7
8 "gitbay.org/gitbay/internal/protocol"
9)
10
11// mirror add refuses a host sync would refuse as numeric, on an
12// instance that allows local targets or not, and stores nothing (#298).
13func TestMirrorAddRefusesNumericHost(t *testing.T) {
14 for _, allowLocal := range []bool{true, false} {
15 for _, host := range []string{"127.1", "2130706433", "0x7f.1"} {
16 c, errOut, st, _ := importCtx(t, allowLocal)
17 code := Dispatch(c, []string{"repo", "mirror", "add", "alice/app", "https://" + host + "/x.git", "--direction", "pull"})
18 if code != protocol.ExitUsage || !strings.Contains(errOut.String(), "numeric address") {
19 t.Fatalf("%s (allow_local %v): exit %d, %q", host, allowLocal, code, errOut.String())
20 }
21 repo, err := st.RepoByPath("alice/app")
22 if err != nil {
23 t.Fatal(err)
24 }
25 if ms, err := st.ListMirrors(repo.ID); err != nil || len(ms) != 0 {
26 t.Fatalf("%s: mirrors %v, %v", host, ms, err)
27 }
28 }
29 }
30}
31
32// set-url changes the URL in place, keeping the id and the sealed token,
33// and refuses a different host or an unknown id.
34func TestMirrorSetURL(t *testing.T) {
35 c, errOut, st, _ := importCtx(t, false)
36 repo, err := st.RepoByPath("alice/app")
37 if err != nil {
38 t.Fatal(err)
39 }
40 id, err := st.AddMirror(repo.ID, "push", "https://github.com/old/app.git", "u", "secret")
41 if err != nil {
42 t.Fatal(err)
43 }
44 if err := st.SetMirrorResult(id, ""); err != nil {
45 t.Fatal(err)
46 }
47 ids := strconv.FormatInt(id, 10)
48
49 if code := Dispatch(c, []string{"repo", "mirror", "set-url", "alice/app", ids, "https://example.com/new/app.git"}); code != protocol.ExitUsage || !strings.Contains(errOut.String(), "host cannot change") {
50 t.Fatalf("other host: exit %d, %q", code, errOut.String())
51 }
52 if code := Dispatch(c, []string{"repo", "mirror", "set-url", "alice/app", "999", "https://github.com/new/app.git"}); code != protocol.ExitNotFound {
53 t.Fatalf("unknown id: exit %d, %q", code, errOut.String())
54 }
55 if code := Dispatch(c, []string{"repo", "mirror", "set-url", "alice/app", ids, "https://github.com/new/app.git"}); code != protocol.ExitOK {
56 t.Fatalf("set-url: exit %d, %q", code, errOut.String())
57 }
58 ms, err := st.ListMirrors(repo.ID)
59 if err != nil || len(ms) != 1 {
60 t.Fatalf("mirrors %v, %v", ms, err)
61 }
62 m := ms[0]
63 if m.ID != id || m.URL != "https://github.com/new/app.git" || m.Token != "secret" || m.Username != "u" || !m.Dirty {
64 t.Fatalf("mirror after set-url: %+v", m)
65 }
66}