e2e/cliusage_test.go
119 lines · 4644 bytes
1package e2e
2
3import (
4 "fmt"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10// The CLI sends its own path where it differs from the registered one, so
11// usage and help print a command that exists: gitbay auth keys remove,
12// never gitbay keys remove (#267). Stock ssh sends none and sees the
13// registered path, the only one it can type.
14func TestCLIUsagePrintsTheInvokingPath(t *testing.T) {
15 t.Parallel()
16 inst := startInstance(t)
17 key := inst.newKey(t, "alice")
18 inst.admin(t, "admin", "user", "create", "alice", "--key", key+".pub",
19 "--email", "alice@example.test", "--verified")
20
21 c := &cli{bin: buildGitbayCLI(t), configDir: t.TempDir(), inst: inst, key: key}
22 c.must(t, "", "", "remote", "add", "test", "127.0.0.1",
23 "--port", fmt.Sprint(inst.port),
24 "--ssh-option", "-i", "--ssh-option", key,
25 "--ssh-option", "-oIdentitiesOnly=yes",
26 "--ssh-option", "-oStrictHostKeyChecking=no",
27 "--ssh-option", "-oUserKnownHostsFile="+filepath.Join(inst.sshDir, "kh"),
28 "--ssh-option", "-oBatchMode=yes",
29 "--default")
30
31 _, errOut, code := c.run(t, "", "", "auth", "keys", "remove")
32 if code == 0 || !strings.Contains(errOut, "usage: gitbay auth keys remove <fingerprint>") {
33 t.Errorf("CLI refusal: exit %d, stderr %q", code, errOut)
34 }
35
36 out, errOut, code := c.run(t, "", "", "auth", "keys", "remove", "--help")
37 if code != 0 || !strings.Contains(out, "gitbay auth keys remove <fingerprint>") {
38 t.Errorf("CLI help: exit %d, stdout %q, stderr %q", code, out, errOut)
39 }
40
41 // A command whose CLI path matches sends no --path=, and off a
42 // terminal prints the ssh form as before.
43 _, errOut, code = c.run(t, "", "", "issue", "show", "alice/app")
44 if code == 0 || !strings.Contains(errOut, "usage: ssh git@") || !strings.Contains(errOut, " issue show <owner/name> <n>") {
45 t.Errorf("matching command: exit %d, stderr %q", code, errOut)
46 }
47
48 _, errOut, code = inst.ssh(t, key, "", "keys", "remove")
49 if code == 0 || !strings.Contains(errOut, "usage: ssh git@") || !strings.Contains(errOut, " keys remove <fingerprint>") {
50 t.Errorf("stock ssh: exit %d, stderr %q", code, errOut)
51 }
52 if strings.Contains(errOut, "auth") {
53 t.Errorf("stock ssh saw the CLI's auth grouping: %q", errOut)
54 }
55
56 // A bare `gitbay auth --help` (cliPath == "auth", the registered
57 // prefix it asks for) must still send --path=auth: the registry has
58 // no "auth" command, so withCLIPath's equality shortcut would
59 // otherwise read that as "no override needed" and print the
60 // registered rows the CLI cannot actually type (#267 review finding).
61 out, errOut, code = c.run(t, "", "", "auth", "--help")
62 if code != 0 {
63 t.Errorf("gitbay auth --help: exit %d, stdout %q, stderr %q", code, out, errOut)
64 }
65 for _, want := range []string{"auth keys add", "auth export"} {
66 if !strings.Contains(out, want) {
67 t.Errorf("gitbay auth --help: missing %q in %q", want, out)
68 }
69 }
70
71 out, errOut, code = inst.ssh(t, key, "", "help", "auth")
72 if code != 0 {
73 t.Errorf("ssh help auth: exit %d, stdout %q, stderr %q", code, out, errOut)
74 }
75 for _, want := range []string{"keys add", "account export"} {
76 if !strings.Contains(out, want) {
77 t.Errorf("ssh help auth: missing %q in %q", want, out)
78 }
79 }
80 if strings.Contains(out, "auth keys add") {
81 t.Errorf("stock ssh saw the CLI's auth grouping: %q", out)
82 }
83}
84
85// keys add and pgp add wire stdin directly to the server rather than going
86// through pass(), so they lost the --help check every other passthrough
87// command has: --help was treated as key material instead of showing help
88// (#267).
89func TestKeysAddAndPGPAddCheckHelpBeforeStdin(t *testing.T) {
90 t.Parallel()
91 inst := startInstance(t)
92 key := inst.newKey(t, "alice")
93 inst.admin(t, "admin", "user", "create", "alice", "--key", key+".pub",
94 "--email", "alice@example.test", "--verified")
95
96 c := &cli{bin: buildGitbayCLI(t), configDir: t.TempDir(), inst: inst, key: key}
97 c.must(t, "", "", "remote", "add", "test", "127.0.0.1",
98 "--port", fmt.Sprint(inst.port),
99 "--ssh-option", "-i", "--ssh-option", key,
100 "--ssh-option", "-oIdentitiesOnly=yes",
101 "--ssh-option", "-oStrictHostKeyChecking=no",
102 "--ssh-option", "-oUserKnownHostsFile="+filepath.Join(inst.sshDir, "kh"),
103 "--ssh-option", "-oBatchMode=yes",
104 "--default")
105
106 for _, args := range [][]string{
107 {"auth", "keys", "add", "--help"},
108 {"auth", "pgp", "add", "--help"},
109 } {
110 out, errOut, code := c.run(t, "", "", args...)
111 if code != 0 {
112 t.Errorf("%v: exit %d, stdout %q, stderr %q", args, code, out, errOut)
113 }
114 want := "gitbay " + strings.Join(args[:len(args)-1], " ")
115 if !strings.Contains(out, want) {
116 t.Errorf("%v: stdout %q does not contain %q", args, out, want)
117 }
118 }
119}