internal/control/stage3account_test.go
85 lines · 3315 bytes
3 symbols in this file
1package control
2
3import (
4 "strings"
5 "testing"
6 "time"
7
8 "gitbay.org/gitbay/internal/store"
9)
10
11// accountFixture is a user with something in every account list task 4
12// migrates, an org they admin, and a profile.
13func accountFixture(t *testing.T) (*store.Store, store.User) {
14 t.Helper()
15 st, _, uid := newQueueTestRepo(t)
16 u := store.User{ID: uid, Username: "alice", IsAdmin: true}
17 if err := st.AddSSHKey(uid, "SHA256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "ssh-ed25519", []byte("blob"), "full", "laptop"); err != nil {
18 t.Fatal(err)
19 }
20 if err := st.AddEmail(uid, "alice@example.test", "smtp", true); err != nil {
21 t.Fatal(err)
22 }
23 if err := st.AddEmail(uid, "old@example.test", "", false); err != nil {
24 t.Fatal(err)
25 }
26 if err := st.AddPGPKey(uid, "ABCDEF0123456789ABCDEF0123456789ABCDEF01", "armored", `["Alice <alice@example.test>"]`, nil, nil); err != nil {
27 t.Fatal(err)
28 }
29 expires := time.Date(2030, 1, 1, 0, 0, 0, 0, time.UTC)
30 if err := st.CreateAPIToken(uid, "ci", strings.Repeat("ab", 32), "read", &expires, 0); err != nil {
31 t.Fatal(err)
32 }
33 if err := st.CreateWebSession(strings.Repeat("cd", 32), uid, time.Hour); err != nil {
34 t.Fatal(err)
35 }
36 dispatchAs(t, st, u, "", "org", "create", "acme")
37 dispatchAs(t, st, u, "", "profile", "set", "--description", "Builds forges", "--website", "https://alice.example.test")
38 return st, u
39}
40
41func TestAccountPlainPinned(t *testing.T) {
42 st, u := accountFixture(t)
43 for name, argv := range map[string][]string{
44 "keys-list": {"keys", "list"},
45 "email-list": {"email", "list"},
46 "pgp-list": {"pgp", "list"},
47 "token-list": {"token", "list"},
48 "web-sessions-list": {"web", "sessions", "list"},
49 "whoami": {"whoami"},
50 "profile-show": {"profile", "show"},
51 "org-profile": {"org", "profile", "acme"},
52 } {
53 pinPlain(t, name, dispatchAs(t, st, u, "", argv...))
54 }
55 pinPlain(t, "profile-set", dispatchAs(t, st, u, "", "profile", "set", "--description", "Builds forges"))
56}
57
58func TestAccountScreens(t *testing.T) {
59 st, u := accountFixture(t)
60 for _, tc := range []struct {
61 argv []string
62 want []string
63 }{
64 {[]string{"keys", "list"}, []string{"SSH keys (1)\n", "full laptop"}},
65 {[]string{"email", "list"}, []string{"Emails (2)\n", "alice@example.test verified primary · smtp", "old@example.test ● unverified"}},
66 {[]string{"pgp", "list"}, []string{"OpenPGP keys (1)\n", "Alice <alice@example.test>"}},
67 {[]string{"token", "list"}, []string{"API tokens (1)\n", "ci read"}},
68 {[]string{"web", "sessions", "list"}, []string{"Browser sessions (1)\n", "cdcdcdcdcdcd"}},
69 {[]string{"whoami"}, []string{"User:", "alice", "Role:", "admin"}},
70 {[]string{"profile", "show"}, []string{"Profile:", "alice user · Builds forges", "Website:", "https://alice.example.test", "Repos (1)\n"}},
71 {[]string{"profile", "set", "--description", "Builds forges"}, []string{"Profile:", "alice"}},
72 {[]string{"org", "profile", "acme"}, []string{"Profile:", "acme org"}},
73 } {
74 out := atTerminal(t, st, u, tc.argv...)
75 for _, w := range tc.want {
76 if !strings.Contains(out, w) {
77 t.Errorf("%v: missing %q in:\n%s", tc.argv, w, out)
78 }
79 }
80 if strings.Contains(out, strings.Repeat("ab", 32)) {
81 t.Errorf("%v: token hash on screen", tc.argv)
82 }
83 checkLegend(t, out)
84 }
85}