Skip to content

Commit

Permalink
add unit tests for LookupWallet ref #139
Browse files Browse the repository at this point in the history
  • Loading branch information
stdevAlDen committed Dec 7, 2019
1 parent 33d50ee commit b704262
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 1 deletion.
127 changes: 127 additions & 0 deletions src/coin/mocks/PersistibleSet.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/coin/skycoin/models/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,11 @@ func NewWalletDirectory(dirPath string) *WalletDirectory {
type WalletDirectory struct {
//Implements WallentEnv interface
WalletDir string
wltService *SkycoinLocalWallet
wltService core.PersistibleSet
}

func (wd *WalletDirectory) SetWltService(wltSrv core.PersistibleSet) {
wd.wltService = wltSrv
}

func lookupWallet(env core.WalletEnv, firstAddr string) (core.Wallet, error) {
Expand Down
48 changes: 48 additions & 0 deletions src/coin/skycoin/models/wallet_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package skycoin

import (
mocks2 "github.com/fibercrypto/fibercryptowallet/src/coin/mocks"
"github.com/fibercrypto/fibercryptowallet/src/errors"
"math"
"os"
"strconv"
"testing"

Expand Down Expand Up @@ -1043,3 +1046,48 @@ func TestSkycoinWalletTypes(t *testing.T) {
require.Equal(t, wallet.WalletTypeBip44, wltSet.DefaultWalletType())
require.Equal(t, []string{wallet.WalletTypeDeterministic, wallet.WalletTypeBip44}, wltSet.SupportedWalletTypes())
}

func TestLookupWalletOk(t *testing.T) {
// Giving
ws := &mocks2.PersistibleSet{}
_, kd, err := makeUxOutWithSecret(t)
require.NoError(t, err)
wallets := makeLocalWalletsFromKeyData(t, []KeyData{*kd})
ws.On("ListWallets").Return(NewSkycoinWalletIterator(wallets))
we := NewWalletDirectory(os.TempDir())
we.SetWltService(ws)
addrs := wallets[0].GenAddresses(core.AccountAddress, 0, 1, nil)
require.True(t, addrs.Next())
addr1 := addrs.Value()

// When
wltFound, err := we.LookupWallet(addr1.String())

// Then
require.NoError(t, err)
require.Equal(t, wallets[0], wltFound)
}

func TestLookupWalletFailAsExpected(t *testing.T) {
// Giving
ws := &mocks2.PersistibleSet{}
_, kd, err := makeUxOutWithSecret(t)
require.NoError(t, err)
wallets := makeLocalWalletsFromKeyData(t, []KeyData{*kd})
ws.On("ListWallets").Return(NewSkycoinWalletIterator(wallets))
we := NewWalletDirectory(os.TempDir())
we.SetWltService(ws)
addrs := wallets[0].GenAddresses(core.AccountAddress, 0, 1, nil)
require.True(t, addrs.Next())
addr1 := addrs.Value()
strAddr := addr1.String()
strAddr = strAddr[len(strAddr)/2:] + strAddr[:len(strAddr)/2]

// When
wltFound, err := we.LookupWallet(strAddr)

// Then
require.Error(t, err)
require.Equal(t, errors.ErrWltFromAddrNotFound, err)
require.Equal(t, nil, wltFound)
}
5 changes: 5 additions & 0 deletions src/core/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ type WalletSet interface {
SupportedWalletTypes() []string
}

type PersistibleSet interface {
WalletSet
WalletStorage
}

// WalletStorage provides access to the underlying wallets data store
type WalletStorage interface {
// Encrypt protects wallet data using cryptography
Expand Down

0 comments on commit b704262

Please sign in to comment.