diff --git a/accounts/mocks/account_manager_interface.go b/accounts/mocks/account_manager_interface.go new file mode 100644 index 00000000..0bc33fab --- /dev/null +++ b/accounts/mocks/account_manager_interface.go @@ -0,0 +1,120 @@ +// Code generated by mockery v2.30.1. DO NOT EDIT. + +package mocks + +import ( + ecdsa "crypto/ecdsa" + + accounts "github.com/ethereum/go-ethereum/accounts" + + mock "github.com/stretchr/testify/mock" +) + +// AccountManagerInterface is an autogenerated mock type for the AccountManagerInterface type +type AccountManagerInterface struct { + mock.Mock +} + +// CreateAccount provides a mock function with given fields: keystorePath, password +func (_m *AccountManagerInterface) CreateAccount(keystorePath string, password string) accounts.Account { + ret := _m.Called(keystorePath, password) + + var r0 accounts.Account + if rf, ok := ret.Get(0).(func(string, string) accounts.Account); ok { + r0 = rf(keystorePath, password) + } else { + r0 = ret.Get(0).(accounts.Account) + } + + return r0 +} + +// GetPrivateKey provides a mock function with given fields: address, password +func (_m *AccountManagerInterface) GetPrivateKey(address string, password string) (*ecdsa.PrivateKey, error) { + ret := _m.Called(address, password) + + var r0 *ecdsa.PrivateKey + var r1 error + if rf, ok := ret.Get(0).(func(string, string) (*ecdsa.PrivateKey, error)); ok { + return rf(address, password) + } + if rf, ok := ret.Get(0).(func(string, string) *ecdsa.PrivateKey); ok { + r0 = rf(address, password) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*ecdsa.PrivateKey) + } + } + + if rf, ok := ret.Get(1).(func(string, string) error); ok { + r1 = rf(address, password) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewAccount provides a mock function with given fields: passphrase +func (_m *AccountManagerInterface) NewAccount(passphrase string) (accounts.Account, error) { + ret := _m.Called(passphrase) + + var r0 accounts.Account + var r1 error + if rf, ok := ret.Get(0).(func(string) (accounts.Account, error)); ok { + return rf(passphrase) + } + if rf, ok := ret.Get(0).(func(string) accounts.Account); ok { + r0 = rf(passphrase) + } else { + r0 = ret.Get(0).(accounts.Account) + } + + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(passphrase) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// SignData provides a mock function with given fields: hash, address, password +func (_m *AccountManagerInterface) SignData(hash []byte, address string, password string) ([]byte, error) { + ret := _m.Called(hash, address, password) + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func([]byte, string, string) ([]byte, error)); ok { + return rf(hash, address, password) + } + if rf, ok := ret.Get(0).(func([]byte, string, string) []byte); ok { + r0 = rf(hash, address, password) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func([]byte, string, string) error); ok { + r1 = rf(hash, address, password) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewAccountManagerInterface creates a new instance of AccountManagerInterface. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewAccountManagerInterface(t interface { + mock.TestingT + Cleanup(func()) +}) *AccountManagerInterface { + mock := &AccountManagerInterface{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/cmd/create_test.go b/cmd/create_test.go index b6ba0e16..cb7bf36e 100644 --- a/cmd/create_test.go +++ b/cmd/create_test.go @@ -2,7 +2,10 @@ package cmd import ( "errors" + accountsPkgMocks "razor/accounts/mocks" "razor/core/types" + pathPkgMocks "razor/path/mocks" + utilsPkgMocks "razor/utils/mocks" "testing" "github.com/ethereum/go-ethereum/accounts" @@ -11,6 +14,103 @@ import ( "github.com/stretchr/testify/mock" ) +func TestCreate(t *testing.T) { + var password string + + nilAccount := accounts.Account{Address: common.Address{0x00}, + URL: accounts.URL{Scheme: "TestKeyScheme", Path: "test/key/path"}, + } + + type args struct { + path string + pathErr error + accountManagerErr error + account accounts.Account + } + tests := []struct { + name string + args args + want accounts.Account + wantErr error + }{ + { + name: "Test 1: When create function executes successfully", + args: args{ + path: "/home/local", + pathErr: nil, + account: accounts.Account{Address: common.HexToAddress("0x000000000000000000000000000000000000dea1"), + URL: accounts.URL{Scheme: "TestKeyScheme", Path: "test/key/path"}, + }, + }, + want: accounts.Account{Address: common.HexToAddress("0x000000000000000000000000000000000000dea1"), + URL: accounts.URL{Scheme: "TestKeyScheme", Path: "test/key/path"}, + }, + wantErr: nil, + }, + { + name: "Test 2: When create fails due to path error", + args: args{ + path: "/home/local", + pathErr: errors.New("path error"), + account: accounts.Account{Address: common.HexToAddress("0x000000000000000000000000000000000000dea1"), + URL: accounts.URL{Scheme: "TestKeyScheme", Path: "test/key/path"}, + }, + }, + want: nilAccount, + wantErr: errors.New("path error"), + }, + { + name: "Test 3: When there is an error in getting account manager", + args: args{ + path: "/home/local", + pathErr: nil, + accountManagerErr: errors.New("account manager error"), + account: accounts.Account{Address: common.HexToAddress("0x000000000000000000000000000000000000dea1"), + URL: accounts.URL{Scheme: "TestKeyScheme", Path: "test/key/path"}, + }, + }, + want: nilAccount, + wantErr: errors.New("account manager error"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + accountsMock := new(accountsPkgMocks.AccountManagerInterface) + + var pathMock *pathPkgMocks.PathInterface + var utilsMock *utilsPkgMocks.Utils + + pathMock = new(pathPkgMocks.PathInterface) + pathUtils = pathMock + + utilsMock = new(utilsPkgMocks.Utils) + razorUtils = utilsMock + + pathMock.On("GetDefaultPath").Return(tt.args.path, tt.args.pathErr) + utilsMock.On("AccountManagerForKeystore").Return(accountsMock, tt.args.accountManagerErr) + + accountsMock.On("CreateAccount", mock.Anything, mock.Anything).Return(tt.args.account) + + utils := &UtilsStruct{} + got, err := utils.Create(password) + + if got.Address != tt.want.Address { + t.Errorf("New address created, got = %v, want %v", got, tt.want.Address) + } + + if err == nil || tt.wantErr == nil { + if err != tt.wantErr { + t.Errorf("Error for Create function, got = %v, want %v", got, tt.wantErr) + } + } else { + if err.Error() != tt.wantErr.Error() { + t.Errorf("Error for Create function, got = %v, want %v", got, tt.wantErr) + } + } + }) + } +} + func TestExecuteCreate(t *testing.T) { var flagSet *pflag.FlagSet diff --git a/cmd/test_utils_test.go b/cmd/test_utils_test.go index 24f9a78d..f410e24a 100644 --- a/cmd/test_utils_test.go +++ b/cmd/test_utils_test.go @@ -6,7 +6,6 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/crypto" "math/big" - //accountsPkgMocks "razor/accounts/mocks" "razor/cmd/mocks" "razor/path" pathPkgMocks "razor/path/mocks" @@ -51,7 +50,6 @@ var ( osMock *mocks.OSInterface pathMock *pathPkgMocks.PathInterface osPathMock *pathPkgMocks.OSInterface - //accountsMock *accountsPkgMocks.AccountManagerInterface ) func SetUpMockInterfaces() { @@ -165,8 +163,6 @@ func SetUpMockInterfaces() { osPathMock = new(pathPkgMocks.OSInterface) path.OSUtilsInterface = osPathMock - - //accountsMock = new(accountsPkgMocks.AccountManagerInterface) } var privateKey, _ = ecdsa.GenerateKey(crypto.S256(), rand.Reader) diff --git a/core/types/account.go b/core/types/account.go index 2d2c6351..da16d30d 100644 --- a/core/types/account.go +++ b/core/types/account.go @@ -5,6 +5,8 @@ import ( "github.com/ethereum/go-ethereum/accounts" ) +//go:generate mockery --name=AccountManagerInterface --output=../../accounts/mocks --case=underscore + type Account struct { Address string Password string diff --git a/utils/mocks/utils.go b/utils/mocks/utils.go index e71e49a0..285ee426 100644 --- a/utils/mocks/utils.go +++ b/utils/mocks/utils.go @@ -4,12 +4,10 @@ package mocks import ( big "math/big" - accounts "razor/accounts" + bindings "razor/pkg/bindings" bind "github.com/ethereum/go-ethereum/accounts/abi/bind" - bindings "razor/pkg/bindings" - cache "razor/cache" common "github.com/ethereum/go-ethereum/common" @@ -29,19 +27,19 @@ type Utils struct { } // AccountManagerForKeystore provides a mock function with given fields: -func (_m *Utils) AccountManagerForKeystore() (*accounts.AccountManager, error) { +func (_m *Utils) AccountManagerForKeystore() (types.AccountManagerInterface, error) { ret := _m.Called() - var r0 *accounts.AccountManager + var r0 types.AccountManagerInterface var r1 error - if rf, ok := ret.Get(0).(func() (*accounts.AccountManager, error)); ok { + if rf, ok := ret.Get(0).(func() (types.AccountManagerInterface, error)); ok { return rf() } - if rf, ok := ret.Get(0).(func() *accounts.AccountManager); ok { + if rf, ok := ret.Get(0).(func() types.AccountManagerInterface); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(*accounts.AccountManager) + r0 = ret.Get(0).(types.AccountManagerInterface) } }