//go:build !integration package login import ( "bytes" "encoding/json" "fmt" "io" "net/http" "strings" "testing" "time" "github.com/MakeNowJust/heredoc/v2" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/zalando/go-keyring" gitlab "gitlab.com/gitlab-org/api/client-go/v3" "gitlab.com/gitlab-org/cli/internal/api" "gitlab.com/gitlab-org/cli/internal/cmdutils" "gitlab.com/gitlab-org/cli/internal/config" "gitlab.com/gitlab-org/cli/internal/glinstance" "gitlab.com/gitlab-org/cli/internal/oauth2" "gitlab.com/gitlab-org/cli/internal/testing/cmdtest" ) type roundTripFunc func(r *http.Request) (*http.Response, error) func (s roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return s(r) } func Test_helperRun(t *testing.T) { // NOTE: we have to remove the values for the possible token env variables, because it'll take precedence over the config. // See config.EnvKeyEquivalence function. t.Setenv("GITLAB_TOKEN", "") t.Setenv("GITLAB_ACCESS_TOKEN", "") t.Setenv("OAUTH_TOKEN", "") t.Setenv("USER", "") oauth2SuccessExpiryTime := time.Now().Add(10 * time.Minute) // Expiry time resolution is down to minutes only. Truncate to a minute // boundary without platform-sized integer arithmetic. oauth2SuccessExpiryTime = oauth2SuccessExpiryTime.Truncate(time.Minute) oauth2ApiClient := func(config config.Config, responseFunc roundTripFunc) func(repoHost string) (*api.Client, error) { return func(repoHost string) (*api.Client, error) { tokenSource, _ := oauth2.NewConfigTokenSource(config, &http.Client{}, glinstance.DefaultProtocol, repoHost, true) if responseFunc != nil { return cmdtest.NewTestAuthSourceApiClient(t, &http.Client{Transport: responseFunc}, gitlab.OAuthTokenSource{TokenSource: tokenSource}, repoHost), nil } return cmdtest.NewTestAuthSourceApiClient(t, nil, gitlab.OAuthTokenSource{TokenSource: tokenSource}, repoHost), nil } } tests := []struct { name string opts options input string wantStdout []string wantStderr string wantErr bool wantErrIs error wantErrContains string wantValidateErr bool testOAuth2 bool apiResponse roundTripFunc env map[string]string }{ { name: "host only, credentials found", opts: options{ operation: "get", config: func() config.Config { return config.NewFromString(heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: user: "monalisa" token: "some-password" `)) }, }, input: heredoc.Doc(` protocol=https host=example.com `), wantErr: false, wantValidateErr: false, wantStdout: []string{ "username=monalisa", "password=some-password", }, wantStderr: "", }, { name: "host plus user", opts: options{ operation: "get", config: func() config.Config { return config.NewFromString(heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: user: "monalisa" token: "some-password" `)) }, }, input: heredoc.Doc(` protocol=https host=example.com username=monalisa `), wantErr: false, wantValidateErr: false, wantStdout: []string{ "username=monalisa", "password=some-password", }, wantStderr: "", }, { name: "url input", opts: options{ operation: "get", config: func() config.Config { return config.NewFromString(heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: user: "monalisa" token: "some-password" `)) }, }, input: heredoc.Doc(` url=https://monalisa@example.com `), wantErr: false, wantValidateErr: false, wantStdout: []string{ "username=monalisa", "password=some-password", }, wantStderr: "", }, { name: "host only, no credentials found", opts: options{ operation: "get", config: func() config.Config { return config.NewFromString(heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: user: "monalisa" `)) }, }, input: heredoc.Doc(` protocol=https host=example.com `), wantErr: true, wantErrIs: cmdutils.SilentError, wantValidateErr: false, wantStderr: "", }, { name: "token from env", opts: options{ operation: "get", config: func() config.Config { return config.NewFromString(heredoc.Doc(` _source: "GITLAB_TOKEN" hosts: example.com: user: "clemsbot" token: "some-password" `)) }, }, input: heredoc.Doc(` protocol=https host=example.com username=clemsbot `), wantErr: false, wantValidateErr: false, wantStdout: []string{ "username=clemsbot", "password=some-password", }, wantStderr: "", }, { // https://gitlab.com/gitlab-org/cli/-/issues/8044 name: "token only, no user on record falls back to a placeholder username", opts: options{ operation: "get", config: func() config.Config { return config.NewFromString(heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: token: "some-password" `)) }, }, input: heredoc.Doc(` protocol=https host=example.com `), wantErr: false, wantValidateErr: false, wantStdout: []string{ "username=glab", "password=some-password", }, wantStderr: "", }, { name: "support OAuth2", opts: options{ operation: "get", config: func() config.Config { configTemplate := heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: user: "monalisa" is_oauth2: "true" oauth2_refresh_token: "some-refresh-token" oauth2_expiry_date: %s token: "some-access-token" client_id: "1234567890abcdef1234567890abcdef" `) return config.NewFromString(fmt.Sprintf( configTemplate, oauth2SuccessExpiryTime.Format(time.RFC822), )) }, }, input: heredoc.Doc(` protocol=https host=example.com `), wantErr: false, wantValidateErr: false, wantStdout: []string{ "username=oauth2", "password=some-access-token", fmt.Sprintf("password_expiry_utc=%d", oauth2SuccessExpiryTime.UTC().Unix()), "oauth_refresh_token=some-refresh-token", }, wantStderr: "", testOAuth2: true, }, { name: "failed to refresh", opts: options{ operation: "get", config: func() config.Config { configTemplate := heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: gitlab.example.com: user: "monalisa" is_oauth2: "true" oauth2_refresh_token: "some-refresh-token" oauth2_expiry_date: %s token: "some-access-token" client_id: "1234567890abcdef1234567890abcdef" `) return config.NewFromString(fmt.Sprintf( configTemplate, time.Now().Add(-10*time.Minute).Format(time.RFC822), )) }, }, input: heredoc.Doc(` protocol=https host=gitlab.example.com `), wantErr: true, wantErrContains: "failed to resolve the credentials", wantValidateErr: false, wantStdout: nil, wantStderr: "", testOAuth2: true, apiResponse: func(req *http.Request) (*http.Response, error) { return nil, fmt.Errorf("connection refused") }, }, { // Additional test case for gitlab.com, as it does not require // client_id. name: "failed to refresh official gitlab", opts: options{ operation: "get", config: func() config.Config { configTemplate := heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: gitlab.com: user: "monalisa" is_oauth2: "true" oauth2_refresh_token: "some-refresh-token" oauth2_expiry_date: %s token: "some-access-token" `) return config.NewFromString(fmt.Sprintf( configTemplate, time.Now().Add(-10*time.Minute).Format(time.RFC822), )) }, }, input: heredoc.Doc(` protocol=https host=gitlab.com `), wantErr: true, wantErrContains: "failed to resolve the credentials", wantValidateErr: false, wantStdout: nil, wantStderr: "", testOAuth2: true, apiResponse: func(req *http.Request) (*http.Response, error) { type RefreshErrorResponse struct { Error string `json:"error"` ErrorDescription string `json:"error_description,omitempty"` ErrorUri string `json:"error_uri,omitempty"` } responseBody, _ := json.Marshal(&RefreshErrorResponse{ Error: "invalid_client", ErrorDescription: "Not a valid client_id", }) response := &http.Response{ Proto: "HTTP/1.1", Status: "400 Bad Request", StatusCode: http.StatusBadRequest, Body: io.NopCloser(bytes.NewBuffer(responseBody)), } return response, nil }, }, { name: "support Job Token", opts: options{ operation: "get", config: func() config.Config { return config.NewFromString(heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: user: "monalisa" job_token: "some-job-token" `)) }, }, input: heredoc.Doc(` protocol=https host=example.com `), wantErr: false, wantValidateErr: false, wantStdout: []string{ "username=gitlab-ci-token", "password=some-job-token", }, wantStderr: "", }, { name: "token and job token stored, token wins", opts: options{ operation: "get", config: func() config.Config { return config.NewFromString(heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: user: "monalisa" token: "some-password" job_token: "some-job-token" `)) }, }, input: heredoc.Doc(` protocol=https host=example.com `), wantStdout: []string{ "username=monalisa", "password=some-password", }, }, { name: "stored OAuth2 overridden by GITLAB_TOKEN", opts: options{ operation: "get", config: func() config.Config { return config.NewFromString(heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: user: "monalisa" is_oauth2: "true" oauth2_refresh_token: "some-refresh-token" token: "some-access-token" `)) }, }, env: map[string]string{"GITLAB_TOKEN": "env-pat"}, input: heredoc.Doc(` protocol=https host=example.com `), wantStdout: []string{ "username=monalisa", "password=env-pat", }, }, { name: "store command", opts: options{ operation: "store", config: func() config.Config { return config.NewFromString(heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: user: "monalisa" token: "some-password" `)) }, }, input: heredoc.Doc(` protocol=https host=example.com `), wantErr: false, wantValidateErr: true, wantStdout: nil, wantStderr: "", }, { name: "erase command", opts: options{ operation: "erase", config: func() config.Config { return config.NewFromString(heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: user: "monalisa" token: "some-password" `)) }, }, input: heredoc.Doc(` protocol=https host=example.com `), wantErr: false, wantValidateErr: true, wantStdout: nil, wantStderr: "", }, { name: "invalid command", opts: options{ operation: "not-a-valid-command", config: func() config.Config { return config.NewFromString(heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: user: "monalisa" token: "some-password" `)) }, }, input: "", wantErr: false, wantValidateErr: true, wantStdout: nil, wantStderr: "", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { for k, v := range tt.env { t.Setenv(k, v) } if tt.testOAuth2 { tt.opts.apiClient = oauth2ApiClient(tt.opts.config(), tt.apiResponse) } if tt.opts.apiClient == nil { cfg := tt.opts.config() tt.opts.apiClient = func(repoHost string) (*api.Client, error) { return api.NewClientFromConfig(repoHost, cfg, false, "test") } } io, stdin, stdout, stderr := cmdtest.TestIOStreams() fmt.Fprint(stdin, tt.input) opts := &tt.opts opts.io = io validateErr := opts.validate() if tt.wantValidateErr { require.Error(t, validateErr) } else { require.NoError(t, validateErr) } // No need to run if validate error is expected if tt.wantValidateErr { return } runErr := opts.run(t.Context()) if tt.wantErr { require.Error(t, runErr) if tt.wantErrIs != nil { require.ErrorIs(t, runErr, tt.wantErrIs) } if tt.wantErrContains != "" { require.ErrorContains(t, runErr, tt.wantErrContains) } } else { require.NoError(t, runErr) } if tt.wantStdout != nil { stdout := stdout.String() assert.Truef(t, strings.HasPrefix(stdout, "capability[]=authtype\n"), "first line of stdout must always be the capability preamble") t.Log(stdout) for _, expectedKV := range tt.wantStdout { assert.Contains(t, stdout, expectedKV) } } if tt.wantStderr != "" { assert.Equal(t, tt.wantStderr, stderr.String()) } }) } } func Test_helperRun_ClientBuildErrorIsNotSilent(t *testing.T) { t.Setenv("GITLAB_TOKEN", "") t.Setenv("GITLAB_ACCESS_TOKEN", "") t.Setenv("OAUTH_TOKEN", "") keyring.MockInit() t.Cleanup(keyring.MockInit) // The host keeps its token in the keyring, but the keyring has no entry for it. cfg := config.NewFromString(heredoc.Doc(` hosts: example.com: user: "monalisa" use_keyring: "true" `)) io, stdin, stdout, _ := cmdtest.TestIOStreams() fmt.Fprint(stdin, "protocol=https\nhost=example.com\n") opts := &options{ io: io, config: func() config.Config { return cfg }, operation: "get", apiClient: func(repoHost string) (*api.Client, error) { return api.NewClientFromConfig(repoHost, cfg, false, "test") }, } err := opts.run(t.Context()) require.Error(t, err) require.NotErrorIs(t, err, cmdutils.SilentError) require.ErrorContains(t, err, `failed to build the API client for "example.com"`) require.ErrorContains(t, err, "keyring") assert.Empty(t, stdout.String()) } func Test_helperRun_OAuth2AccessTokenOnly(t *testing.T) { t.Setenv("GITLAB_TOKEN", "") t.Setenv("GITLAB_ACCESS_TOKEN", "") t.Setenv("OAUTH_TOKEN", "") cfg := config.NewFromString(heredoc.Doc(` _source: "/Users/monalisa/.config/glab/config.yml" hosts: example.com: user: "monalisa" is_oauth2: "true" token: "some-access-token" oauth2_expiry_date: "2099-01-02T03:04:05Z" `)) client, err := api.NewClientFromConfig("example.com", cfg, false, "test", api.WithoutTokenFromEnvironment()) require.NoError(t, err) exec := cmdtest.SetupCmdForTest( t, NewCmdCredential, false, cmdtest.WithStdin("protocol=https\nhost=example.com\n\n"), cmdtest.WithConfig(cfg), cmdtest.WithApiClient(client), ) out, err := exec("get") require.NoError(t, err) stdout := out.String() assert.True(t, strings.HasPrefix(stdout, "capability[]=authtype\n"), "first line of stdout must always be the capability preamble") assert.Contains(t, stdout, "username=oauth2") assert.Contains(t, stdout, "password=some-access-token") assert.Contains(t, stdout, fmt.Sprintf("password_expiry_utc=%d", time.Date(2099, time.January, 2, 3, 4, 5, 0, time.UTC).Unix())) assert.NotContains(t, stdout, "oauth_refresh_token", "there is no refresh token to report") } func Test_remoteNameScore(t *testing.T) { tests := []struct { name string remoteName string expectedScore int }{ {"origin gets highest score", "origin", 3}, {"upstream gets second highest", "upstream", 2}, {"gitlab gets third", "gitlab", 1}, {"other names get zero", "fork", 0}, {"case insensitive - ORIGIN", "ORIGIN", 3}, {"case insensitive - Upstream", "Upstream", 2}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { score := remoteNameScore(tt.remoteName) assert.Equal(t, tt.expectedScore, score) }) } } func Test_calculateHostScore(t *testing.T) { tests := []struct { name string host detectedHost expectedScore int }{ { name: "gitlab.com with origin remote", host: detectedHost{ hostname: "gitlab.com", remotes: []string{"origin"}, authenticated: false, }, expectedScore: 8, // gitlab.com bonus (5) + origin (3) }, { name: "authenticated self-hosted with origin", host: detectedHost{ hostname: "gitlab.company.com", remotes: []string{"origin"}, authenticated: true, }, expectedScore: 13, // authenticated (10) + origin (3) }, { name: "multiple remotes to same host", host: detectedHost{ hostname: "gitlab.company.com", remotes: []string{"origin", "upstream"}, authenticated: false, }, expectedScore: 5, // origin (3) + upstream (2) }, { name: "unknown remote name", host: detectedHost{ hostname: "gitlab.example.com", remotes: []string{"fork"}, authenticated: false, }, expectedScore: 0, // fork (0) }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { score := calculateHostScore(&tt.host) assert.Equal(t, tt.expectedScore, score) }) } } func Test_prioritizeHosts(t *testing.T) { tests := []struct { name string hosts []detectedHost expectedOrder []string // Expected order of hostnames }{ { name: "authenticated host prioritized", hosts: []detectedHost{ {hostname: "gitlab.com", remotes: []string{"fork"}, authenticated: false}, {hostname: "gitlab.company.com", remotes: []string{"origin"}, authenticated: true}, }, expectedOrder: []string{"gitlab.company.com", "gitlab.com"}, }, { name: "origin beats upstream", hosts: []detectedHost{ {hostname: "gitlab.upstream.com", remotes: []string{"upstream"}, authenticated: false}, {hostname: "gitlab.origin.com", remotes: []string{"origin"}, authenticated: false}, }, expectedOrder: []string{"gitlab.origin.com", "gitlab.upstream.com"}, }, { name: "gitlab.com gets boost", hosts: []detectedHost{ {hostname: "gitlab.example.com", remotes: []string{"origin"}, authenticated: false}, {hostname: "gitlab.com", remotes: []string{"fork"}, authenticated: false}, }, expectedOrder: []string{"gitlab.com", "gitlab.example.com"}, // gitlab.com bonus (5) + fork (0) = 5 > origin (3) }, { name: "multiple remotes increase score", hosts: []detectedHost{ {hostname: "gitlab.single.com", remotes: []string{"origin"}, authenticated: false}, {hostname: "gitlab.multi.com", remotes: []string{"origin", "upstream"}, authenticated: false}, }, expectedOrder: []string{"gitlab.multi.com", "gitlab.single.com"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { sorted := prioritizeHosts(tt.hosts) var actualOrder []string for _, host := range sorted { actualOrder = append(actualOrder, host.hostname) } assert.Equal(t, tt.expectedOrder, actualOrder) }) } } func Test_detectedHost_String(t *testing.T) { tests := []struct { name string host detectedHost expected string }{ { name: "single remote, not authenticated", host: detectedHost{ hostname: "gitlab.com", remotes: []string{"origin"}, authenticated: false, }, expected: "gitlab.com (origin)", }, { name: "multiple remotes, authenticated", host: detectedHost{ hostname: "gitlab.company.com", remotes: []string{"origin", "upstream"}, authenticated: true, }, expected: "gitlab.company.com (origin, upstream) [authenticated]", }, { name: "remotes sorted alphabetically", host: detectedHost{ hostname: "gitlab.example.com", remotes: []string{"upstream", "fork", "origin"}, authenticated: false, }, expected: "gitlab.example.com (fork, origin, upstream)", }, { name: "nil remotes, not authenticated", host: detectedHost{ hostname: "gitlab.example.com", remotes: nil, authenticated: false, }, expected: "gitlab.example.com", }, { name: "empty remotes, authenticated", host: detectedHost{ hostname: "gitlab.company.com", remotes: []string{}, authenticated: true, }, expected: "gitlab.company.com [authenticated]", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := tt.host.String() assert.Equal(t, tt.expected, result) }) } }