github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/secret/delete/delete_test.go (about)

     1  package delete
     2  
     3  import (
     4  	"bytes"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/ungtb10d/cli/v2/internal/config"
     9  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
    10  	"github.com/ungtb10d/cli/v2/pkg/cmdutil"
    11  	"github.com/ungtb10d/cli/v2/pkg/httpmock"
    12  	"github.com/ungtb10d/cli/v2/pkg/iostreams"
    13  	"github.com/google/shlex"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestNewCmdDelete(t *testing.T) {
    18  	tests := []struct {
    19  		name     string
    20  		cli      string
    21  		wants    DeleteOptions
    22  		wantsErr bool
    23  	}{
    24  		{
    25  			name:     "no args",
    26  			wantsErr: true,
    27  		},
    28  		{
    29  			name: "repo",
    30  			cli:  "cool",
    31  			wants: DeleteOptions{
    32  				SecretName: "cool",
    33  			},
    34  		},
    35  		{
    36  			name: "org",
    37  			cli:  "cool --org anOrg",
    38  			wants: DeleteOptions{
    39  				SecretName: "cool",
    40  				OrgName:    "anOrg",
    41  			},
    42  		},
    43  		{
    44  			name: "env",
    45  			cli:  "cool --env anEnv",
    46  			wants: DeleteOptions{
    47  				SecretName: "cool",
    48  				EnvName:    "anEnv",
    49  			},
    50  		},
    51  		{
    52  			name: "user",
    53  			cli:  "cool -u",
    54  			wants: DeleteOptions{
    55  				SecretName:  "cool",
    56  				UserSecrets: true,
    57  			},
    58  		},
    59  		{
    60  			name: "Dependabot repo",
    61  			cli:  "cool --app Dependabot",
    62  			wants: DeleteOptions{
    63  				SecretName:  "cool",
    64  				Application: "Dependabot",
    65  			},
    66  		},
    67  		{
    68  			name: "Dependabot org",
    69  			cli:  "cool --app Dependabot --org UmbrellaCorporation",
    70  			wants: DeleteOptions{
    71  				SecretName:  "cool",
    72  				OrgName:     "UmbrellaCorporation",
    73  				Application: "Dependabot",
    74  			},
    75  		},
    76  		{
    77  			name: "Codespaces org",
    78  			cli:  "cool --app codespaces --org UmbrellaCorporation",
    79  			wants: DeleteOptions{
    80  				SecretName:  "cool",
    81  				OrgName:     "UmbrellaCorporation",
    82  				Application: "Codespaces",
    83  			},
    84  		},
    85  	}
    86  
    87  	for _, tt := range tests {
    88  		t.Run(tt.name, func(t *testing.T) {
    89  			ios, _, _, _ := iostreams.Test()
    90  			f := &cmdutil.Factory{
    91  				IOStreams: ios,
    92  			}
    93  
    94  			argv, err := shlex.Split(tt.cli)
    95  			assert.NoError(t, err)
    96  
    97  			var gotOpts *DeleteOptions
    98  			cmd := NewCmdDelete(f, func(opts *DeleteOptions) error {
    99  				gotOpts = opts
   100  				return nil
   101  			})
   102  			cmd.SetArgs(argv)
   103  			cmd.SetIn(&bytes.Buffer{})
   104  			cmd.SetOut(&bytes.Buffer{})
   105  			cmd.SetErr(&bytes.Buffer{})
   106  
   107  			_, err = cmd.ExecuteC()
   108  			if tt.wantsErr {
   109  				assert.Error(t, err)
   110  				return
   111  			}
   112  			assert.NoError(t, err)
   113  
   114  			assert.Equal(t, tt.wants.SecretName, gotOpts.SecretName)
   115  			assert.Equal(t, tt.wants.OrgName, gotOpts.OrgName)
   116  			assert.Equal(t, tt.wants.EnvName, gotOpts.EnvName)
   117  		})
   118  	}
   119  
   120  }
   121  
   122  func Test_removeRun_repo(t *testing.T) {
   123  	tests := []struct {
   124  		name     string
   125  		opts     *DeleteOptions
   126  		wantPath string
   127  	}{
   128  		{
   129  			name: "Actions",
   130  			opts: &DeleteOptions{
   131  				Application: "actions",
   132  				SecretName:  "cool_secret",
   133  			},
   134  			wantPath: "repos/owner/repo/actions/secrets/cool_secret",
   135  		},
   136  		{
   137  			name: "Dependabot",
   138  			opts: &DeleteOptions{
   139  				Application: "dependabot",
   140  				SecretName:  "cool_dependabot_secret",
   141  			},
   142  			wantPath: "repos/owner/repo/dependabot/secrets/cool_dependabot_secret",
   143  		},
   144  		{
   145  			name: "defaults to Actions",
   146  			opts: &DeleteOptions{
   147  				SecretName: "cool_secret",
   148  			},
   149  			wantPath: "repos/owner/repo/actions/secrets/cool_secret",
   150  		},
   151  	}
   152  
   153  	for _, tt := range tests {
   154  		reg := &httpmock.Registry{}
   155  
   156  		reg.Register(
   157  			httpmock.REST("DELETE", tt.wantPath),
   158  			httpmock.StatusStringResponse(204, "No Content"))
   159  
   160  		ios, _, _, _ := iostreams.Test()
   161  
   162  		tt.opts.IO = ios
   163  		tt.opts.HttpClient = func() (*http.Client, error) {
   164  			return &http.Client{Transport: reg}, nil
   165  		}
   166  		tt.opts.Config = func() (config.Config, error) {
   167  			return config.NewBlankConfig(), nil
   168  		}
   169  		tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
   170  			return ghrepo.FromFullName("owner/repo")
   171  		}
   172  
   173  		err := removeRun(tt.opts)
   174  		assert.NoError(t, err)
   175  
   176  		reg.Verify(t)
   177  	}
   178  }
   179  
   180  func Test_removeRun_env(t *testing.T) {
   181  	reg := &httpmock.Registry{}
   182  
   183  	reg.Register(
   184  		httpmock.REST("DELETE", "repos/owner/repo/environments/development/secrets/cool_secret"),
   185  		httpmock.StatusStringResponse(204, "No Content"))
   186  
   187  	ios, _, _, _ := iostreams.Test()
   188  
   189  	opts := &DeleteOptions{
   190  		IO: ios,
   191  		HttpClient: func() (*http.Client, error) {
   192  			return &http.Client{Transport: reg}, nil
   193  		},
   194  		Config: func() (config.Config, error) {
   195  			return config.NewBlankConfig(), nil
   196  		},
   197  		BaseRepo: func() (ghrepo.Interface, error) {
   198  			return ghrepo.FromFullName("owner/repo")
   199  		},
   200  		SecretName: "cool_secret",
   201  		EnvName:    "development",
   202  	}
   203  
   204  	err := removeRun(opts)
   205  	assert.NoError(t, err)
   206  
   207  	reg.Verify(t)
   208  }
   209  
   210  func Test_removeRun_org(t *testing.T) {
   211  	tests := []struct {
   212  		name     string
   213  		opts     *DeleteOptions
   214  		wantPath string
   215  	}{
   216  		{
   217  			name: "org",
   218  			opts: &DeleteOptions{
   219  				OrgName: "UmbrellaCorporation",
   220  			},
   221  			wantPath: "orgs/UmbrellaCorporation/actions/secrets/tVirus",
   222  		},
   223  		{
   224  			name: "Dependabot org",
   225  			opts: &DeleteOptions{
   226  				Application: "dependabot",
   227  				OrgName:     "UmbrellaCorporation",
   228  			},
   229  			wantPath: "orgs/UmbrellaCorporation/dependabot/secrets/tVirus",
   230  		},
   231  		{
   232  			name: "Codespaces org",
   233  			opts: &DeleteOptions{
   234  				Application: "codespaces",
   235  				OrgName:     "UmbrellaCorporation",
   236  			},
   237  			wantPath: "orgs/UmbrellaCorporation/codespaces/secrets/tVirus",
   238  		},
   239  	}
   240  
   241  	for _, tt := range tests {
   242  		t.Run(tt.name, func(t *testing.T) {
   243  			reg := &httpmock.Registry{}
   244  
   245  			reg.Register(
   246  				httpmock.REST("DELETE", tt.wantPath),
   247  				httpmock.StatusStringResponse(204, "No Content"))
   248  
   249  			ios, _, _, _ := iostreams.Test()
   250  
   251  			tt.opts.Config = func() (config.Config, error) {
   252  				return config.NewBlankConfig(), nil
   253  			}
   254  			tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
   255  				return ghrepo.FromFullName("owner/repo")
   256  			}
   257  			tt.opts.HttpClient = func() (*http.Client, error) {
   258  				return &http.Client{Transport: reg}, nil
   259  			}
   260  			tt.opts.IO = ios
   261  			tt.opts.SecretName = "tVirus"
   262  
   263  			err := removeRun(tt.opts)
   264  			assert.NoError(t, err)
   265  
   266  			reg.Verify(t)
   267  
   268  		})
   269  	}
   270  
   271  }
   272  
   273  func Test_removeRun_user(t *testing.T) {
   274  	reg := &httpmock.Registry{}
   275  
   276  	reg.Register(
   277  		httpmock.REST("DELETE", "user/codespaces/secrets/cool_secret"),
   278  		httpmock.StatusStringResponse(204, "No Content"))
   279  
   280  	ios, _, _, _ := iostreams.Test()
   281  
   282  	opts := &DeleteOptions{
   283  		IO: ios,
   284  		HttpClient: func() (*http.Client, error) {
   285  			return &http.Client{Transport: reg}, nil
   286  		},
   287  		Config: func() (config.Config, error) {
   288  			return config.NewBlankConfig(), nil
   289  		},
   290  		SecretName:  "cool_secret",
   291  		UserSecrets: true,
   292  	}
   293  
   294  	err := removeRun(opts)
   295  	assert.NoError(t, err)
   296  
   297  	reg.Verify(t)
   298  }