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