github.com/andrewhsu/cli/v2@v2.0.1-0.20210910131313-d4b4061f5b89/pkg/cmd/secret/remove/remove_test.go (about) 1 package remove 2 3 import ( 4 "bytes" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/andrewhsu/cli/v2/internal/config" 10 "github.com/andrewhsu/cli/v2/internal/ghrepo" 11 "github.com/andrewhsu/cli/v2/pkg/cmdutil" 12 "github.com/andrewhsu/cli/v2/pkg/httpmock" 13 "github.com/andrewhsu/cli/v2/pkg/iostreams" 14 "github.com/google/shlex" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestNewCmdRemove(t *testing.T) { 19 tests := []struct { 20 name string 21 cli string 22 wants RemoveOptions 23 wantsErr bool 24 }{ 25 { 26 name: "no args", 27 wantsErr: true, 28 }, 29 { 30 name: "repo", 31 cli: "cool", 32 wants: RemoveOptions{ 33 SecretName: "cool", 34 }, 35 }, 36 { 37 name: "org", 38 cli: "cool --org anOrg", 39 wants: RemoveOptions{ 40 SecretName: "cool", 41 OrgName: "anOrg", 42 }, 43 }, 44 { 45 name: "env", 46 cli: "cool --env anEnv", 47 wants: RemoveOptions{ 48 SecretName: "cool", 49 EnvName: "anEnv", 50 }, 51 }, 52 } 53 54 for _, tt := range tests { 55 t.Run(tt.name, func(t *testing.T) { 56 io, _, _, _ := iostreams.Test() 57 f := &cmdutil.Factory{ 58 IOStreams: io, 59 } 60 61 argv, err := shlex.Split(tt.cli) 62 assert.NoError(t, err) 63 64 var gotOpts *RemoveOptions 65 cmd := NewCmdRemove(f, func(opts *RemoveOptions) error { 66 gotOpts = opts 67 return nil 68 }) 69 cmd.SetArgs(argv) 70 cmd.SetIn(&bytes.Buffer{}) 71 cmd.SetOut(&bytes.Buffer{}) 72 cmd.SetErr(&bytes.Buffer{}) 73 74 _, err = cmd.ExecuteC() 75 if tt.wantsErr { 76 assert.Error(t, err) 77 return 78 } 79 assert.NoError(t, err) 80 81 assert.Equal(t, tt.wants.SecretName, gotOpts.SecretName) 82 assert.Equal(t, tt.wants.OrgName, gotOpts.OrgName) 83 assert.Equal(t, tt.wants.EnvName, gotOpts.EnvName) 84 }) 85 } 86 87 } 88 89 func Test_removeRun_repo(t *testing.T) { 90 reg := &httpmock.Registry{} 91 92 reg.Register( 93 httpmock.REST("DELETE", "repos/owner/repo/actions/secrets/cool_secret"), 94 httpmock.StatusStringResponse(204, "No Content")) 95 96 io, _, _, _ := iostreams.Test() 97 98 opts := &RemoveOptions{ 99 IO: io, 100 HttpClient: func() (*http.Client, error) { 101 return &http.Client{Transport: reg}, nil 102 }, 103 Config: func() (config.Config, error) { 104 return config.NewBlankConfig(), nil 105 }, 106 BaseRepo: func() (ghrepo.Interface, error) { 107 return ghrepo.FromFullName("owner/repo") 108 }, 109 SecretName: "cool_secret", 110 } 111 112 err := removeRun(opts) 113 assert.NoError(t, err) 114 115 reg.Verify(t) 116 } 117 118 func Test_removeRun_env(t *testing.T) { 119 reg := &httpmock.Registry{} 120 121 reg.Register( 122 httpmock.REST("DELETE", "repos/owner/repo/environments/development/secrets/cool_secret"), 123 httpmock.StatusStringResponse(204, "No Content")) 124 125 io, _, _, _ := iostreams.Test() 126 127 opts := &RemoveOptions{ 128 IO: io, 129 HttpClient: func() (*http.Client, error) { 130 return &http.Client{Transport: reg}, nil 131 }, 132 Config: func() (config.Config, error) { 133 return config.NewBlankConfig(), nil 134 }, 135 BaseRepo: func() (ghrepo.Interface, error) { 136 return ghrepo.FromFullName("owner/repo") 137 }, 138 SecretName: "cool_secret", 139 EnvName: "development", 140 } 141 142 err := removeRun(opts) 143 assert.NoError(t, err) 144 145 reg.Verify(t) 146 } 147 148 func Test_removeRun_org(t *testing.T) { 149 tests := []struct { 150 name string 151 opts *RemoveOptions 152 }{ 153 { 154 name: "repo", 155 opts: &RemoveOptions{}, 156 }, 157 { 158 name: "org", 159 opts: &RemoveOptions{ 160 OrgName: "UmbrellaCorporation", 161 }, 162 }, 163 } 164 165 for _, tt := range tests { 166 t.Run(tt.name, func(t *testing.T) { 167 reg := &httpmock.Registry{} 168 169 orgName := tt.opts.OrgName 170 171 if orgName == "" { 172 reg.Register( 173 httpmock.REST("DELETE", "repos/owner/repo/actions/secrets/tVirus"), 174 httpmock.StatusStringResponse(204, "No Content")) 175 } else { 176 reg.Register( 177 httpmock.REST("DELETE", fmt.Sprintf("orgs/%s/actions/secrets/tVirus", orgName)), 178 httpmock.StatusStringResponse(204, "No Content")) 179 } 180 181 io, _, _, _ := iostreams.Test() 182 183 tt.opts.Config = func() (config.Config, error) { 184 return config.NewBlankConfig(), nil 185 } 186 tt.opts.BaseRepo = func() (ghrepo.Interface, error) { 187 return ghrepo.FromFullName("owner/repo") 188 } 189 tt.opts.HttpClient = func() (*http.Client, error) { 190 return &http.Client{Transport: reg}, nil 191 } 192 tt.opts.IO = io 193 tt.opts.SecretName = "tVirus" 194 195 err := removeRun(tt.opts) 196 assert.NoError(t, err) 197 198 reg.Verify(t) 199 200 }) 201 } 202 203 }