github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/auth/token/token_test.go (about)

     1  package token
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/ungtb10d/cli/v2/internal/config"
     8  	"github.com/ungtb10d/cli/v2/pkg/cmdutil"
     9  	"github.com/ungtb10d/cli/v2/pkg/iostreams"
    10  	"github.com/google/shlex"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestNewCmdToken(t *testing.T) {
    15  	tests := []struct {
    16  		name       string
    17  		input      string
    18  		output     TokenOptions
    19  		wantErr    bool
    20  		wantErrMsg string
    21  	}{
    22  		{
    23  			name:   "no flags",
    24  			input:  "",
    25  			output: TokenOptions{},
    26  		},
    27  		{
    28  			name:   "with hostname",
    29  			input:  "--hostname github.mycompany.com",
    30  			output: TokenOptions{Hostname: "github.mycompany.com"},
    31  		},
    32  		{
    33  			name:   "with shorthand hostname",
    34  			input:  "-h github.mycompany.com",
    35  			output: TokenOptions{Hostname: "github.mycompany.com"},
    36  		},
    37  	}
    38  
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			ios, _, _, _ := iostreams.Test()
    42  			f := &cmdutil.Factory{
    43  				IOStreams: ios,
    44  				Config: func() (config.Config, error) {
    45  					cfg := config.NewBlankConfig()
    46  					return cfg, nil
    47  				},
    48  			}
    49  			argv, err := shlex.Split(tt.input)
    50  			assert.NoError(t, err)
    51  
    52  			var cmdOpts *TokenOptions
    53  			cmd := NewCmdToken(f, func(opts *TokenOptions) error {
    54  				cmdOpts = opts
    55  				return nil
    56  			})
    57  			// TODO cobra hack-around
    58  			cmd.Flags().BoolP("help", "x", false, "")
    59  
    60  			cmd.SetArgs(argv)
    61  			cmd.SetIn(&bytes.Buffer{})
    62  			cmd.SetOut(&bytes.Buffer{})
    63  			cmd.SetErr(&bytes.Buffer{})
    64  
    65  			_, err = cmd.ExecuteC()
    66  			if tt.wantErr {
    67  				assert.Error(t, err)
    68  				assert.EqualError(t, err, tt.wantErrMsg)
    69  				return
    70  			}
    71  
    72  			assert.NoError(t, err)
    73  			assert.Equal(t, tt.output.Hostname, cmdOpts.Hostname)
    74  		})
    75  	}
    76  }
    77  
    78  func Test_tokenRun(t *testing.T) {
    79  	tests := []struct {
    80  		name       string
    81  		opts       TokenOptions
    82  		wantStdout string
    83  		wantErr    bool
    84  		wantErrMsg string
    85  	}{
    86  		{
    87  			name: "token",
    88  			opts: TokenOptions{
    89  				Config: func() (config.Config, error) {
    90  					cfg := config.NewBlankConfig()
    91  					cfg.Set("github.com", "oauth_token", "gho_ABCDEFG")
    92  					return cfg, nil
    93  				},
    94  			},
    95  			wantStdout: "gho_ABCDEFG\n",
    96  		},
    97  		{
    98  			name: "token by hostname",
    99  			opts: TokenOptions{
   100  				Config: func() (config.Config, error) {
   101  					cfg := config.NewBlankConfig()
   102  					cfg.Set("github.com", "oauth_token", "gho_ABCDEFG")
   103  					cfg.Set("github.mycompany.com", "oauth_token", "gho_1234567")
   104  					return cfg, nil
   105  				},
   106  				Hostname: "github.mycompany.com",
   107  			},
   108  			wantStdout: "gho_1234567\n",
   109  		},
   110  		{
   111  			name: "no token",
   112  			opts: TokenOptions{
   113  				Config: func() (config.Config, error) {
   114  					cfg := config.NewBlankConfig()
   115  					return cfg, nil
   116  				},
   117  			},
   118  			wantErr:    true,
   119  			wantErrMsg: "no oauth token",
   120  		},
   121  	}
   122  
   123  	for _, tt := range tests {
   124  		ios, _, stdout, _ := iostreams.Test()
   125  		tt.opts.IO = ios
   126  
   127  		t.Run(tt.name, func(t *testing.T) {
   128  			err := tokenRun(&tt.opts)
   129  			if tt.wantErr {
   130  				assert.Error(t, err)
   131  				assert.EqualError(t, err, tt.wantErrMsg)
   132  				return
   133  			}
   134  
   135  			assert.NoError(t, err)
   136  			assert.Equal(t, tt.wantStdout, stdout.String())
   137  		})
   138  	}
   139  }