Skip to content

Commit

Permalink
chore(cmd): fix login tests
Browse files Browse the repository at this point in the history
Signed-off-by: Michele Meloni <[email protected]>
  • Loading branch information
mmeloni committed Jan 18, 2022
1 parent 61f475f commit ef735ad
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 101 deletions.
31 changes: 16 additions & 15 deletions cmd/immuadmin/command/commandline.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ package immuadmin
import (
"context"
"fmt"
"github.com/codenotary/immudb/pkg/client/tokenservice"
"github.com/spf13/viper"
"path/filepath"
"strings"

c "github.com/codenotary/immudb/cmd/helper"
"github.com/codenotary/immudb/pkg/client"
"github.com/codenotary/immudb/pkg/client/tokenservice"
"github.com/codenotary/immudb/pkg/immuos"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"path/filepath"
"strings"
)

// Commandline ...
Expand Down Expand Up @@ -67,6 +66,18 @@ func NewCommandLine() *commandline {
cl.passwordReader = c.DefaultPasswordReader
cl.context = context.Background()
//
opt := Options()
tfAbsPath := opt.TokenFileName
if !viper.IsSet("tokenfile") {
tfAbsPath = filepath.Join(c.STATE_FOLDER, client.DefaultTokenFileName)
}
if !strings.HasSuffix(tfAbsPath, client.AdminTokenFileSuffix) {
tfAbsPath += client.AdminTokenFileSuffix
}
cl.options = opt.WithTokenFileName(tfAbsPath)
// token service is needed here because the one in cl.immuClient is not exposed
cl.ts = tokenservice.NewFileTokenService().WithTokenFileAbsPath(tfAbsPath)

return cl
}

Expand All @@ -75,16 +86,6 @@ func (cl *commandline) ConfigChain(post func(cmd *cobra.Command, args []string)
if err = cl.config.LoadConfig(cmd); err != nil {
return err
}
opt := Options()
tfAbsPath := opt.TokenFileName
if !viper.IsSet("tokenfile") {
tfAbsPath = filepath.Join(c.STATE_FOLDER, client.DefaultTokenFileName)
}
if !strings.HasSuffix(tfAbsPath, client.AdminTokenFileSuffix) {
tfAbsPath += client.AdminTokenFileSuffix
}
cl.options = opt.WithTokenFileName(tfAbsPath)
cl.ts = tokenservice.NewFileTokenService().WithTokenFileAbsPath(tfAbsPath)
if post != nil {
return post(cmd, args)
}
Expand Down
49 changes: 5 additions & 44 deletions cmd/immuadmin/command/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,50 +208,11 @@ func TestCommandLine_LoginLogout(t *testing.T) {
}

func TestCommandLine_CheckLoggedIn(t *testing.T) {
options := server.DefaultOptions().WithAuth(true)
bs := servertest.NewBufconnServer(options)

bs.Start()
defer bs.Stop()

defer os.RemoveAll(options.Dir)
defer os.Remove(".state-")

cl := commandline{}
cmd, _ := cl.NewCmd()
cl.context = context.Background()
cl.passwordReader = pwReaderMock
dialOptions := []grpc.DialOption{
grpc.WithContextDialer(bs.Dialer), grpc.WithInsecure(),
ts := clienttest.DefaultTokenServiceMock()
cl := &commandline{
ts: ts,
}

cmd.SetArgs([]string{"login", "immudb"})
cmd.Execute()

cl.options = Options()
cl.options.DialOptions = dialOptions
cl.login(cmd)

cmd1 := cobra.Command{}
cl1 := new(commandline)
cl1.context = context.Background()
cl1.passwordReader = pwReaderMock
tkf := cmdtest.RandString()
cl1.ts = tokenservice.NewFileTokenService().WithTokenFileAbsPath(tkf)
dialOptions1 := []grpc.DialOption{
grpc.WithContextDialer(bs.Dialer), grpc.WithInsecure(),
}

cl1.options = Options()
cl1.options.DialOptions = dialOptions1
err := cl1.checkLoggedIn(&cmd1, nil)
cmd, _ := cl.NewCmd()
err := cl.checkLoggedIn(cmd, nil)
assert.Nil(t, err)
}

func newHomedirServiceMock() *clienttest.HomedirServiceMock {
h := clienttest.DefaultHomedirServiceMock()
h.FileExistsInUserHomeDirF = func(pathToFile string) (bool, error) {
return true, nil
}
return h
}
19 changes: 9 additions & 10 deletions cmd/immuclient/command/commandline.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ type commandline struct {
func NewCommandLine() commandline {
cl := commandline{}
cl.config.Name = "immuclient"
cl.options = client.DefaultOptions()
opt := immuc.Options()
tfAbsPath := opt.TokenFileName
if !viper.IsSet("tokenfile") {
tfAbsPath = filepath.Join(c.STATE_FOLDER, client.DefaultTokenFileName)
}
opt.WithTokenFileName(tfAbsPath)

cl.options = opt
cl.immucl = immuc.Init(cl.options)
return cl
}

Expand All @@ -29,15 +37,6 @@ func (cl *commandline) ConfigChain(post func(cmd *cobra.Command, args []string)
return err
}

opt := immuc.Options()
tfAbsPath := opt.TokenFileName
if !viper.IsSet("tokenfile") {
tfAbsPath = filepath.Join(c.STATE_FOLDER, client.DefaultTokenFileName)
}
opt.WithTokenFileName(tfAbsPath)

cl.options = opt
cl.immucl, err = immuc.Init(cl.options)
if post != nil {
return post(cmd, args)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/immuclient/immuc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ type Client interface {
}

// Init ...
func Init(opts *client.Options) (*immuc, error) {
func Init(opts *client.Options) *immuc {
ic := new(immuc)
ic.passwordReader = opts.PasswordReader
ic.options = opts
return ic, nil
return ic
}

func (i *immuc) Connect(args []string) (err error) {
Expand Down
8 changes: 3 additions & 5 deletions cmd/immuclient/immuc/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ func TestConnect(t *testing.T) {
dialOptions := []grpc.DialOption{
grpc.WithContextDialer(bs.Dialer), grpc.WithInsecure(),
}
imc, err := Init(Options().WithDialOptions(dialOptions))
if err != nil {
t.Fatal(err)
}
err = imc.Connect([]string{""})
imc := Init(Options().WithDialOptions(dialOptions))

err := imc.Connect([]string{""})
if err != nil {
t.Fatal(err)
}
Expand Down
15 changes: 5 additions & 10 deletions cmd/immuclient/immuc/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ func TestLogin(t *testing.T) {
c.DefaultPasswordReader = &immuclienttest.PasswordReader{
Pass: []string{"immudb"},
}
imc, err := Init(Options().WithDialOptions(dialOptions))
if err != nil {
t.Fatal(err)
}
err = imc.Connect([]string{""})
imc := Init(Options().WithDialOptions(dialOptions))

err := imc.Connect([]string{""})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -86,11 +84,8 @@ func TestLogout(t *testing.T) {
pr.Pass = []string{"immudb"}
c.DefaultPasswordReader = pr

imc, err := Init(Options().WithDialOptions(dialOptions))
if err != nil {
t.Fatal(err)
}
err = imc.Connect([]string{""})
imc := Init(Options().WithDialOptions(dialOptions))
err := imc.Connect([]string{""})
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 3 additions & 5 deletions cmd/immuclient/immuclienttest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,9 @@ func (c *clientTest) Connect(dialer servertest.BuffDialer) {
grpc.WithContextDialer(dialer), grpc.WithInsecure(),
}

ic, err := immuc.Init(c.Options.WithDialOptions(dialOptions).WithPasswordReader(c.Pr))
if err != nil {
log.Fatal(err)
}
err = ic.Connect([]string{""})
ic := immuc.Init(c.Options.WithDialOptions(dialOptions).WithPasswordReader(c.Pr))

err := ic.Connect([]string{""})
if err != nil {
log.Fatal(err)
}
Expand Down
10 changes: 0 additions & 10 deletions pkg/client/clienttest/token_service_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package clienttest

import (
"github.com/codenotary/immudb/pkg/client/homedir"
"github.com/codenotary/immudb/pkg/client/tokenservice"
)

Expand Down Expand Up @@ -49,15 +48,6 @@ func (ts TokenServiceMock) GetDatabase() (string, error) {
return "", nil
}

func (ts TokenServiceMock) WithHds(hds homedir.HomedirService) tokenservice.TokenService {
return ts
}

func (ts TokenServiceMock) WithTokenFileName(tfn string) tokenservice.TokenService {
return ts
}

// DefaultHomedirServiceMock ...
func DefaultTokenServiceMock() *TokenServiceMock {
return &TokenServiceMock{
GetTokenF: func() (string, error) {
Expand Down

0 comments on commit ef735ad

Please sign in to comment.