github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/ssh-key/list/list_test.go (about) 1 package list 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 "time" 8 9 "github.com/MakeNowJust/heredoc" 10 "github.com/ungtb10d/cli/v2/internal/config" 11 "github.com/ungtb10d/cli/v2/pkg/httpmock" 12 "github.com/ungtb10d/cli/v2/pkg/iostreams" 13 ) 14 15 func TestListRun(t *testing.T) { 16 tests := []struct { 17 name string 18 opts ListOptions 19 isTTY bool 20 wantStdout string 21 wantStderr string 22 wantErr bool 23 }{ 24 { 25 name: "list tty", 26 opts: ListOptions{ 27 HTTPClient: func() (*http.Client, error) { 28 createdAt := time.Now().Add(time.Duration(-24) * time.Hour) 29 reg := &httpmock.Registry{} 30 reg.Register( 31 httpmock.REST("GET", "user/keys"), 32 httpmock.StringResponse(fmt.Sprintf(`[ 33 { 34 "id": 1234, 35 "key": "ssh-rsa AAAABbBB123", 36 "title": "Mac", 37 "created_at": "%[1]s" 38 }, 39 { 40 "id": 5678, 41 "key": "ssh-rsa EEEEEEEK247", 42 "title": "hubot@Windows", 43 "created_at": "%[1]s" 44 } 45 ]`, createdAt.Format(time.RFC3339))), 46 ) 47 return &http.Client{Transport: reg}, nil 48 }, 49 }, 50 isTTY: true, 51 wantStdout: heredoc.Doc(` 52 TITLE ID KEY ADDED 53 Mac 1234 ssh-rsa AAAABbBB123 1d 54 hubot@Windows 5678 ssh-rsa EEEEEEEK247 1d 55 `), 56 wantStderr: "", 57 }, 58 { 59 name: "list non-tty", 60 opts: ListOptions{ 61 HTTPClient: func() (*http.Client, error) { 62 createdAt, _ := time.Parse(time.RFC3339, "2020-08-31T15:44:24+02:00") 63 reg := &httpmock.Registry{} 64 reg.Register( 65 httpmock.REST("GET", "user/keys"), 66 httpmock.StringResponse(fmt.Sprintf(`[ 67 { 68 "id": 1234, 69 "key": "ssh-rsa AAAABbBB123", 70 "title": "Mac", 71 "created_at": "%[1]s" 72 }, 73 { 74 "id": 5678, 75 "key": "ssh-rsa EEEEEEEK247", 76 "title": "hubot@Windows", 77 "created_at": "%[1]s" 78 } 79 ]`, createdAt.Format(time.RFC3339))), 80 ) 81 return &http.Client{Transport: reg}, nil 82 }, 83 }, 84 isTTY: false, 85 wantStdout: heredoc.Doc(` 86 Mac ssh-rsa AAAABbBB123 2020-08-31T15:44:24+02:00 1234 87 hubot@Windows ssh-rsa EEEEEEEK247 2020-08-31T15:44:24+02:00 5678 88 `), 89 wantStderr: "", 90 }, 91 { 92 name: "no keys tty", 93 opts: ListOptions{ 94 HTTPClient: func() (*http.Client, error) { 95 reg := &httpmock.Registry{} 96 reg.Register( 97 httpmock.REST("GET", "user/keys"), 98 httpmock.StringResponse(`[]`), 99 ) 100 return &http.Client{Transport: reg}, nil 101 }, 102 }, 103 wantStdout: "", 104 wantStderr: "", 105 wantErr: true, 106 isTTY: true, 107 }, 108 { 109 name: "no keys non-tty", 110 opts: ListOptions{ 111 HTTPClient: func() (*http.Client, error) { 112 reg := &httpmock.Registry{} 113 reg.Register( 114 httpmock.REST("GET", "user/keys"), 115 httpmock.StringResponse(`[]`), 116 ) 117 return &http.Client{Transport: reg}, nil 118 }, 119 }, 120 wantStdout: "", 121 wantStderr: "", 122 wantErr: true, 123 isTTY: false, 124 }, 125 } 126 127 for _, tt := range tests { 128 t.Run(tt.name, func(t *testing.T) { 129 ios, _, stdout, stderr := iostreams.Test() 130 ios.SetStdinTTY(tt.isTTY) 131 ios.SetStdoutTTY(tt.isTTY) 132 ios.SetStderrTTY(tt.isTTY) 133 134 opts := tt.opts 135 opts.IO = ios 136 opts.Config = func() (config.Config, error) { return config.NewBlankConfig(), nil } 137 138 err := listRun(&opts) 139 if (err != nil) != tt.wantErr { 140 t.Errorf("listRun() return error: %v", err) 141 return 142 } 143 144 if stdout.String() != tt.wantStdout { 145 t.Errorf("wants stdout %q, got %q", tt.wantStdout, stdout.String()) 146 } 147 if stderr.String() != tt.wantStderr { 148 t.Errorf("wants stderr %q, got %q", tt.wantStderr, stderr.String()) 149 } 150 }) 151 } 152 }