github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/gist/delete/delete_test.go (about) 1 package delete 2 3 import ( 4 "bytes" 5 "net/http" 6 "testing" 7 8 "github.com/cli/cli/internal/config" 9 "github.com/cli/cli/pkg/cmd/gist/shared" 10 "github.com/cli/cli/pkg/cmdutil" 11 "github.com/cli/cli/pkg/httpmock" 12 "github.com/cli/cli/pkg/iostreams" 13 "github.com/cli/cli/pkg/prompt" 14 "github.com/google/shlex" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestNewCmdDelete(t *testing.T) { 19 tests := []struct { 20 name string 21 cli string 22 wants DeleteOptions 23 }{ 24 { 25 name: "valid selector", 26 cli: "123", 27 wants: DeleteOptions{ 28 Selector: "123", 29 }, 30 }, 31 } 32 33 for _, tt := range tests { 34 t.Run(tt.name, func(t *testing.T) { 35 f := &cmdutil.Factory{} 36 37 argv, err := shlex.Split(tt.cli) 38 assert.NoError(t, err) 39 var gotOpts *DeleteOptions 40 cmd := NewCmdDelete(f, func(opts *DeleteOptions) error { 41 gotOpts = opts 42 return nil 43 }) 44 45 cmd.SetArgs(argv) 46 cmd.SetIn(&bytes.Buffer{}) 47 cmd.SetOut(&bytes.Buffer{}) 48 cmd.SetErr(&bytes.Buffer{}) 49 50 _, err = cmd.ExecuteC() 51 assert.NoError(t, err) 52 53 assert.Equal(t, tt.wants.Selector, gotOpts.Selector) 54 }) 55 } 56 } 57 58 func Test_deleteRun(t *testing.T) { 59 tests := []struct { 60 name string 61 opts *DeleteOptions 62 gist *shared.Gist 63 httpStubs func(*httpmock.Registry) 64 askStubs func(*prompt.AskStubber) 65 nontty bool 66 wantErr bool 67 wantStderr string 68 wantParams map[string]interface{} 69 }{ 70 { 71 name: "no such gist", 72 wantErr: true, 73 }, { 74 name: "another user's gist", 75 gist: &shared.Gist{ 76 ID: "1234", 77 Files: map[string]*shared.GistFile{ 78 "cicada.txt": { 79 Filename: "cicada.txt", 80 Content: "bwhiizzzbwhuiiizzzz", 81 Type: "text/plain", 82 }, 83 }, 84 Owner: &shared.GistOwner{Login: "octocat2"}, 85 }, 86 wantErr: true, 87 wantStderr: "You do not own this gist.", 88 }, { 89 name: "successfully delete", 90 gist: &shared.Gist{ 91 ID: "1234", 92 Files: map[string]*shared.GistFile{ 93 "cicada.txt": { 94 Filename: "cicada.txt", 95 Content: "bwhiizzzbwhuiiizzzz", 96 Type: "text/plain", 97 }, 98 }, 99 Owner: &shared.GistOwner{Login: "octocat"}, 100 }, 101 httpStubs: func(reg *httpmock.Registry) { 102 reg.Register(httpmock.REST("DELETE", "gists/1234"), 103 httpmock.StringResponse("{}")) 104 }, 105 wantErr: false, 106 }, 107 } 108 109 for _, tt := range tests { 110 reg := &httpmock.Registry{} 111 if tt.gist == nil { 112 reg.Register(httpmock.REST("GET", "gists/1234"), 113 httpmock.StatusStringResponse(404, "Not Found")) 114 } else { 115 reg.Register(httpmock.REST("GET", "gists/1234"), 116 httpmock.JSONResponse(tt.gist)) 117 reg.Register(httpmock.GraphQL(`query UserCurrent\b`), 118 httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`)) 119 } 120 121 if tt.httpStubs != nil { 122 tt.httpStubs(reg) 123 } 124 125 as, teardown := prompt.InitAskStubber() 126 defer teardown() 127 if tt.askStubs != nil { 128 tt.askStubs(as) 129 } 130 131 if tt.opts == nil { 132 tt.opts = &DeleteOptions{} 133 } 134 135 tt.opts.HttpClient = func() (*http.Client, error) { 136 return &http.Client{Transport: reg}, nil 137 } 138 tt.opts.Config = func() (config.Config, error) { 139 return config.NewBlankConfig(), nil 140 } 141 io, _, _, _ := iostreams.Test() 142 io.SetStdoutTTY(!tt.nontty) 143 io.SetStdinTTY(!tt.nontty) 144 tt.opts.IO = io 145 tt.opts.Selector = "1234" 146 147 t.Run(tt.name, func(t *testing.T) { 148 err := deleteRun(tt.opts) 149 reg.Verify(t) 150 if tt.wantErr { 151 assert.Error(t, err) 152 if tt.wantStderr != "" { 153 assert.EqualError(t, err, tt.wantStderr) 154 } 155 return 156 } 157 assert.NoError(t, err) 158 159 }) 160 } 161 }