diff --git a/cmd/client.go b/cmd/client.go index 25f316fa558..f26fed01eee 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -25,14 +25,15 @@ import ( // Client is the shell for the node, local commands and remote commands. type Client struct { Renderer - Config store.Config - AppFactory AppFactory - KeyStoreAuthenticator KeyStoreAuthenticator - FallbackAPIInitializer APIInitializer - Runner Runner - CookieAuthenticator CookieAuthenticator - SessionRequestBuilders []SessionRequestBuilder - HTTP HTTPClient + Config store.Config + AppFactory AppFactory + KeyStoreAuthenticator KeyStoreAuthenticator + FallbackAPIInitializer APIInitializer + Runner Runner + HTTP HTTPClient + CookieAuthenticator CookieAuthenticator + FileSessionRequestBuilder SessionRequestBuilder + PromptingSessionRequestBuilder SessionRequestBuilder } func (cli *Client) errorOut(err error) error { diff --git a/cmd/remote_client.go b/cmd/remote_client.go index 64e837c7001..363661910fe 100644 --- a/cmd/remote_client.go +++ b/cmd/remote_client.go @@ -235,16 +235,10 @@ func (cli *Client) RemoteLogin(c *clipkg.Context) error { } func (cli *Client) buildSessionRequest(flag string) (models.SessionRequest, error) { - var allerrs error - for _, builder := range cli.SessionRequestBuilders { - sessionRequest, err := builder.Build(flag) - if err == nil { - return sessionRequest, nil - } - allerrs = multierr.Append(allerrs, err) + if len(flag) > 0 { + return cli.FileSessionRequestBuilder.Build(flag) } - - return models.SessionRequest{}, allerrs + return cli.PromptingSessionRequestBuilder.Build("") } func getBufferFromJSON(s string) (buf *bytes.Buffer, err error) { diff --git a/cmd/remote_client_test.go b/cmd/remote_client_test.go index 5db2836de77..806e7fead1e 100644 --- a/cmd/remote_client_test.go +++ b/cmd/remote_client_test.go @@ -1,12 +1,10 @@ package cmd_test import ( - "errors" "flag" "path" "testing" - "github.com/smartcontractkit/chainlink/cmd" "github.com/smartcontractkit/chainlink/internal/cltest" "github.com/smartcontractkit/chainlink/store/models" "github.com/smartcontractkit/chainlink/store/presenters" @@ -291,34 +289,35 @@ func TestClient_RemoteLogin(t *testing.T) { app, cleanup := cltest.NewApplication() defer cleanup() - client, _ := app.NewClientAndRenderer() + app.Start() tests := []struct { - name string - wantError error + name, file string + email, pwd string + wantError bool }{ - {"success", nil}, - {"failure", cli.NewExitError("login error", 1)}, + {"success prompt", "", cltest.APIEmail, cltest.Password, false}, + {"success file", "../internal/fixtures/apicredentials", "", "", false}, + {"failure prompt", "", "wrong@email.com", "wrongpwd", true}, + {"failure file", "/tmp/doesntexist", "", "", true}, + {"failure file w correct prompt", "/tmp/doesntexist", cltest.APIEmail, cltest.Password, true}, } - for _, test := range tests { t.Run(test.name, func(t *testing.T) { - failingBuilder := &cltest.MockSessionRequestBuilder{Error: errors.New("no credential file")} - succeedingBuilder := &cltest.MockSessionRequestBuilder{} - client.SessionRequestBuilders = []cmd.SessionRequestBuilder{ - failingBuilder, - succeedingBuilder, - failingBuilder, - } - client.CookieAuthenticator = cltest.MockCookieAuthenticator{test.wantError} + enteredStrings := []string{test.email, test.pwd} + prompter := &cltest.MockCountingPrompter{EnteredStrings: enteredStrings} + client := app.NewAuthenticatingClient(prompter) set := flag.NewFlagSet("test", 0) + set.String("file", test.file, "") c := cli.NewContext(nil, set, nil) err := client.RemoteLogin(c) - assert.Equal(t, 1, failingBuilder.Count) - assert.Equal(t, 1, succeedingBuilder.Count) - assert.Equal(t, test.wantError, err) + if test.wantError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } }) } } diff --git a/internal/cltest/cltest.go b/internal/cltest/cltest.go index ceb4b7ba9a2..3a0ae2d7b94 100644 --- a/internal/cltest/cltest.go +++ b/internal/cltest/cltest.go @@ -230,21 +230,39 @@ func (ta *TestApplication) NewHTTPClient() HTTPClientCleaner { func (ta *TestApplication) NewClientAndRenderer() (*cmd.Client, *RendererMock) { ta.MustSeedUserSession() r := &RendererMock{} - builders := []cmd.SessionRequestBuilder{&MockSessionRequestBuilder{}} client := &cmd.Client{ - Renderer: r, - Config: ta.Config, - AppFactory: EmptyAppFactory{}, - KeyStoreAuthenticator: CallbackAuthenticator{func(*store.Store, string) error { return nil }}, - FallbackAPIInitializer: &MockAPIInitializer{}, - Runner: EmptyRunner{}, - CookieAuthenticator: MockCookieAuthenticator{}, - SessionRequestBuilders: builders, - HTTP: NewMockAuthenticatedHTTPClient(ta.Config), + Renderer: r, + Config: ta.Config, + AppFactory: EmptyAppFactory{}, + KeyStoreAuthenticator: CallbackAuthenticator{func(*store.Store, string) error { return nil }}, + FallbackAPIInitializer: &MockAPIInitializer{}, + Runner: EmptyRunner{}, + HTTP: NewMockAuthenticatedHTTPClient(ta.Config), + CookieAuthenticator: MockCookieAuthenticator{}, + FileSessionRequestBuilder: &MockSessionRequestBuilder{}, + PromptingSessionRequestBuilder: &MockSessionRequestBuilder{}, } return client, r } +func (ta *TestApplication) NewAuthenticatingClient(prompter cmd.Prompter) *cmd.Client { + ta.MustSeedUserSession() + cookieAuth := cmd.NewSessionCookieAuthenticator(ta.Config) + client := &cmd.Client{ + Renderer: &RendererMock{}, + Config: ta.Config, + AppFactory: EmptyAppFactory{}, + KeyStoreAuthenticator: CallbackAuthenticator{func(*store.Store, string) error { return nil }}, + FallbackAPIInitializer: &MockAPIInitializer{}, + Runner: EmptyRunner{}, + HTTP: cmd.NewAuthenticatedHTTPClient(ta.Config, cookieAuth), + CookieAuthenticator: cookieAuth, + FileSessionRequestBuilder: cmd.NewFileSessionRequestBuilder(), + PromptingSessionRequestBuilder: cmd.NewPromptingSessionRequestBuilder(prompter), + } + return client +} + // NewStoreWithConfig creates a new store with given config func NewStoreWithConfig(config *TestConfig) (*store.Store, func()) { s := store.NewStore(config.Config) diff --git a/main.go b/main.go index f1c6ab75f40..6263d336b7c 100644 --- a/main.go +++ b/main.go @@ -158,19 +158,16 @@ func NewProductionClient() *cmd.Client { cfg := store.NewConfig() prompter := cmd.NewTerminalPrompter() cookieAuth := cmd.NewSessionCookieAuthenticator(cfg) - builders := []cmd.SessionRequestBuilder{ - cmd.NewFileSessionRequestBuilder(), - cmd.NewPromptingSessionRequestBuilder(prompter), - } return &cmd.Client{ - Renderer: cmd.RendererTable{Writer: os.Stdout}, - Config: cfg, - AppFactory: cmd.ChainlinkAppFactory{}, - KeyStoreAuthenticator: cmd.TerminalAuthenticator{Prompter: prompter}, - FallbackAPIInitializer: cmd.NewPromptingAPIInitializer(prompter), - Runner: cmd.ChainlinkRunner{}, - CookieAuthenticator: cookieAuth, - SessionRequestBuilders: builders, - HTTP: cmd.NewAuthenticatedHTTPClient(cfg, cookieAuth), + Renderer: cmd.RendererTable{Writer: os.Stdout}, + Config: cfg, + AppFactory: cmd.ChainlinkAppFactory{}, + KeyStoreAuthenticator: cmd.TerminalAuthenticator{Prompter: prompter}, + FallbackAPIInitializer: cmd.NewPromptingAPIInitializer(prompter), + Runner: cmd.ChainlinkRunner{}, + HTTP: cmd.NewAuthenticatedHTTPClient(cfg, cookieAuth), + CookieAuthenticator: cookieAuth, + FileSessionRequestBuilder: cmd.NewFileSessionRequestBuilder(), + PromptingSessionRequestBuilder: cmd.NewPromptingSessionRequestBuilder(prompter), } }