Skip to content

Commit

Permalink
Replace SessionRequestBuilders w File,Prompter builders
Browse files Browse the repository at this point in the history
  • Loading branch information
dimroc committed Jul 20, 2018
1 parent 3147b59 commit b48bd34
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 59 deletions.
17 changes: 9 additions & 8 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 3 additions & 9 deletions cmd/remote_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
37 changes: 18 additions & 19 deletions cmd/remote_client_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
}
})
}
}
Expand Down
38 changes: 28 additions & 10 deletions internal/cltest/cltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 10 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

0 comments on commit b48bd34

Please sign in to comment.