github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/repo/deploy-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/ghrepo" 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 httpStubs func(t *testing.T, reg *httpmock.Registry) 21 wantStdout string 22 wantStderr string 23 wantErr bool 24 }{ 25 { 26 name: "list tty", 27 isTTY: true, 28 httpStubs: func(t *testing.T, reg *httpmock.Registry) { 29 createdAt := time.Now().Add(time.Duration(-24) * time.Hour) 30 reg.Register( 31 httpmock.REST("GET", "repos/OWNER/REPO/keys"), 32 httpmock.StringResponse(fmt.Sprintf(`[ 33 { 34 "id": 1234, 35 "key": "ssh-rsa AAAABbBB123", 36 "title": "Mac", 37 "created_at": "%[1]s", 38 "read_only": true 39 }, 40 { 41 "id": 5678, 42 "key": "ssh-rsa EEEEEEEK247", 43 "title": "hubot@Windows", 44 "created_at": "%[1]s", 45 "read_only": false 46 } 47 ]`, createdAt.Format(time.RFC3339))), 48 ) 49 }, 50 wantStdout: heredoc.Doc(` 51 1234 Mac read-only ssh-rsa AAAABbBB123 1d 52 5678 hubot@Windows read-write ssh-rsa EEEEEEEK247 1d 53 `), 54 wantStderr: "", 55 }, 56 { 57 name: "list non-tty", 58 isTTY: false, 59 httpStubs: func(t *testing.T, reg *httpmock.Registry) { 60 createdAt, _ := time.Parse(time.RFC3339, "2020-08-31T15:44:24+02:00") 61 reg.Register( 62 httpmock.REST("GET", "repos/OWNER/REPO/keys"), 63 httpmock.StringResponse(fmt.Sprintf(`[ 64 { 65 "id": 1234, 66 "key": "ssh-rsa AAAABbBB123", 67 "title": "Mac", 68 "created_at": "%[1]s", 69 "read_only": false 70 }, 71 { 72 "id": 5678, 73 "key": "ssh-rsa EEEEEEEK247", 74 "title": "hubot@Windows", 75 "created_at": "%[1]s", 76 "read_only": true 77 } 78 ]`, createdAt.Format(time.RFC3339))), 79 ) 80 }, 81 wantStdout: heredoc.Doc(` 82 1234 Mac read-write ssh-rsa AAAABbBB123 2020-08-31T15:44:24+02:00 83 5678 hubot@Windows read-only ssh-rsa EEEEEEEK247 2020-08-31T15:44:24+02:00 84 `), 85 wantStderr: "", 86 }, 87 { 88 name: "no keys", 89 isTTY: true, 90 httpStubs: func(t *testing.T, reg *httpmock.Registry) { 91 reg.Register( 92 httpmock.REST("GET", "repos/OWNER/REPO/keys"), 93 httpmock.StringResponse(`[]`)) 94 }, 95 wantStdout: "", 96 wantStderr: "", 97 wantErr: true, 98 }, 99 } 100 101 for _, tt := range tests { 102 t.Run(tt.name, func(t *testing.T) { 103 ios, _, stdout, stderr := iostreams.Test() 104 ios.SetStdoutTTY(tt.isTTY) 105 ios.SetStdinTTY(tt.isTTY) 106 ios.SetStderrTTY(tt.isTTY) 107 108 reg := &httpmock.Registry{} 109 if tt.httpStubs != nil { 110 tt.httpStubs(t, reg) 111 } 112 113 opts := tt.opts 114 opts.IO = ios 115 opts.BaseRepo = func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil } 116 opts.HTTPClient = func() (*http.Client, error) { return &http.Client{Transport: reg}, nil } 117 118 err := listRun(&opts) 119 if (err != nil) != tt.wantErr { 120 t.Errorf("listRun() return error: %v", err) 121 return 122 } 123 124 if stdout.String() != tt.wantStdout { 125 t.Errorf("wants stdout %q, got %q", tt.wantStdout, stdout.String()) 126 } 127 if stderr.String() != tt.wantStderr { 128 t.Errorf("wants stderr %q, got %q", tt.wantStderr, stderr.String()) 129 } 130 }) 131 } 132 }