2018-05-03 10:36:56 +00:00
|
|
|
package status
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2020-12-15 18:00:31 +00:00
|
|
|
"github.com/status-im/status-go/account/generator"
|
|
|
|
|
2018-05-03 10:36:56 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2019-12-11 13:59:37 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2018-05-03 10:36:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStatusSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(StatusSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type StatusSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
am *MockAccountManager
|
2018-05-07 08:22:38 +00:00
|
|
|
w *MockWhisperService
|
2018-05-03 10:36:56 +00:00
|
|
|
api *PublicAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusSuite) SetupTest() {
|
|
|
|
ctrl := gomock.NewController(s.T())
|
|
|
|
s.am = NewMockAccountManager(ctrl)
|
2018-05-07 08:22:38 +00:00
|
|
|
s.w = NewMockWhisperService(ctrl)
|
2018-05-03 10:36:56 +00:00
|
|
|
service := New(s.w)
|
|
|
|
service.SetAccountManager(s.am)
|
|
|
|
|
|
|
|
s.api = NewAPI(service)
|
|
|
|
}
|
|
|
|
|
|
|
|
var logintests = []struct {
|
|
|
|
name string
|
|
|
|
expectedAddressKey string
|
|
|
|
expectedError error
|
|
|
|
prepareExpectations func(*StatusSuite)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "success login",
|
|
|
|
expectedAddressKey: "addressKey",
|
|
|
|
expectedError: nil,
|
|
|
|
prepareExpectations: func(s *StatusSuite) {
|
2019-12-19 18:27:27 +00:00
|
|
|
key := types.Key{
|
2018-05-03 10:36:56 +00:00
|
|
|
PrivateKey: &ecdsa.PrivateKey{},
|
|
|
|
}
|
2019-12-19 18:27:27 +00:00
|
|
|
s.am.EXPECT().AddressToDecryptedAccount("0x01", "password").Return(types.Account{}, &key, nil)
|
2018-05-03 10:36:56 +00:00
|
|
|
s.w.EXPECT().AddKeyPair(key.PrivateKey).Return("addressKey", nil)
|
2019-07-26 14:45:10 +00:00
|
|
|
|
|
|
|
loginParams := account.LoginParams{
|
2019-12-11 13:59:37 +00:00
|
|
|
MainAccount: types.HexToAddress("0x01"),
|
|
|
|
ChatAddress: types.HexToAddress("0x01"),
|
2019-07-26 14:45:10 +00:00
|
|
|
Password: "password",
|
|
|
|
}
|
|
|
|
s.am.EXPECT().SelectAccount(loginParams).Return(nil)
|
2018-05-03 10:36:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error when decrypting account from address",
|
|
|
|
expectedAddressKey: "",
|
|
|
|
expectedError: errors.New("foo"),
|
|
|
|
prepareExpectations: func(s *StatusSuite) {
|
2019-12-19 18:27:27 +00:00
|
|
|
key := types.Key{
|
2018-05-03 10:36:56 +00:00
|
|
|
PrivateKey: &ecdsa.PrivateKey{},
|
|
|
|
}
|
2019-12-19 18:27:27 +00:00
|
|
|
s.am.EXPECT().AddressToDecryptedAccount("0x01", "password").Return(types.Account{}, &key, errors.New("foo"))
|
2018-05-03 10:36:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error when adding key pair to whisper",
|
|
|
|
expectedAddressKey: "",
|
|
|
|
expectedError: errors.New("foo"),
|
|
|
|
prepareExpectations: func(s *StatusSuite) {
|
2019-12-19 18:27:27 +00:00
|
|
|
key := types.Key{
|
2018-05-03 10:36:56 +00:00
|
|
|
PrivateKey: &ecdsa.PrivateKey{},
|
|
|
|
}
|
2019-12-19 18:27:27 +00:00
|
|
|
s.am.EXPECT().AddressToDecryptedAccount("0x01", "password").Return(types.Account{}, &key, nil)
|
2018-05-03 10:36:56 +00:00
|
|
|
s.w.EXPECT().AddKeyPair(key.PrivateKey).Return("", errors.New("foo"))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error when selecting account",
|
|
|
|
expectedAddressKey: "",
|
|
|
|
expectedError: errors.New("foo"),
|
|
|
|
prepareExpectations: func(s *StatusSuite) {
|
2019-12-19 18:27:27 +00:00
|
|
|
key := types.Key{
|
2018-05-03 10:36:56 +00:00
|
|
|
PrivateKey: &ecdsa.PrivateKey{},
|
|
|
|
}
|
2019-12-19 18:27:27 +00:00
|
|
|
s.am.EXPECT().AddressToDecryptedAccount("0x01", "password").Return(types.Account{}, &key, nil)
|
2018-05-03 10:36:56 +00:00
|
|
|
s.w.EXPECT().AddKeyPair(key.PrivateKey).Return("", nil)
|
2019-07-26 14:45:10 +00:00
|
|
|
|
|
|
|
loginParams := account.LoginParams{
|
2019-12-11 13:59:37 +00:00
|
|
|
MainAccount: types.HexToAddress("0x01"),
|
|
|
|
ChatAddress: types.HexToAddress("0x01"),
|
2019-07-26 14:45:10 +00:00
|
|
|
Password: "password",
|
|
|
|
}
|
|
|
|
s.am.EXPECT().SelectAccount(loginParams).Return(errors.New("foo"))
|
2018-05-03 10:36:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusSuite) TestLogin() {
|
|
|
|
for _, t := range logintests {
|
2019-07-26 14:45:10 +00:00
|
|
|
req := LoginRequest{Addr: "0x01", Password: "password"}
|
2018-05-03 10:36:56 +00:00
|
|
|
|
|
|
|
t.prepareExpectations(s)
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
res, err := s.api.Login(ctx, req)
|
|
|
|
s.Equal(t.expectedAddressKey, res.AddressKeyID, "failed scenario : "+t.name)
|
|
|
|
s.Equal(t.expectedError, err, "failed scenario : "+t.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var signuptests = []struct {
|
|
|
|
name string
|
|
|
|
expectedResponse SignupResponse
|
|
|
|
expectedError error
|
|
|
|
prepareExpectations func(*StatusSuite)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "success signup",
|
|
|
|
expectedResponse: SignupResponse{
|
2019-01-18 09:01:14 +00:00
|
|
|
WalletAddress: "addr",
|
|
|
|
WalletPubkey: "pubkey",
|
|
|
|
Mnemonic: "mnemonic",
|
2018-05-03 10:36:56 +00:00
|
|
|
},
|
|
|
|
expectedError: nil,
|
|
|
|
prepareExpectations: func(s *StatusSuite) {
|
2020-12-10 16:24:19 +00:00
|
|
|
mKInfo := generator.GeneratedAccountInfo{
|
|
|
|
IdentifiedAccountInfo: generator.IdentifiedAccountInfo{},
|
|
|
|
Mnemonic: "",
|
|
|
|
}
|
2019-01-18 09:01:14 +00:00
|
|
|
accountInfo := account.Info{
|
|
|
|
WalletAddress: "addr",
|
|
|
|
WalletPubKey: "pubkey",
|
|
|
|
ChatAddress: "addr",
|
|
|
|
ChatPubKey: "pubkey",
|
|
|
|
}
|
2020-12-10 16:24:19 +00:00
|
|
|
s.am.EXPECT().CreateAccount("password").Return(mKInfo, accountInfo, "mnemonic", nil)
|
2018-05-03 10:36:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success signup",
|
|
|
|
expectedResponse: SignupResponse{
|
2019-01-18 09:01:14 +00:00
|
|
|
WalletAddress: "",
|
|
|
|
WalletPubkey: "",
|
|
|
|
Mnemonic: "",
|
2018-05-03 10:36:56 +00:00
|
|
|
},
|
|
|
|
expectedError: errors.New("could not create the specified account : foo"),
|
|
|
|
prepareExpectations: func(s *StatusSuite) {
|
2020-12-10 16:24:19 +00:00
|
|
|
mKInfo := generator.GeneratedAccountInfo{
|
|
|
|
IdentifiedAccountInfo: generator.IdentifiedAccountInfo{},
|
|
|
|
Mnemonic: "",
|
|
|
|
}
|
|
|
|
s.am.EXPECT().CreateAccount("password").Return(mKInfo, account.Info{}, "", errors.New("foo"))
|
2018-05-03 10:36:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusSuite) TestSignup() {
|
|
|
|
for _, t := range signuptests {
|
|
|
|
t.prepareExpectations(s)
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
res, err := s.api.Signup(ctx, SignupRequest{Password: "password"})
|
2019-01-18 09:01:14 +00:00
|
|
|
s.Equal(t.expectedResponse.WalletAddress, res.WalletAddress, "failed scenario : "+t.name)
|
|
|
|
s.Equal(t.expectedResponse.WalletPubkey, res.WalletPubkey, "failed scenario : "+t.name)
|
2018-05-03 10:36:56 +00:00
|
|
|
s.Equal(t.expectedResponse.Mnemonic, res.Mnemonic, "failed scenario : "+t.name)
|
|
|
|
s.Equal(t.expectedError, err, "failed scenario : "+t.name)
|
|
|
|
}
|
|
|
|
}
|