2018-05-03 10:36:56 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"github.com/status-im/status-go/params"
|
2018-05-03 10:36:56 +00:00
|
|
|
"github.com/status-im/status-go/services/status"
|
2019-10-04 15:21:24 +00:00
|
|
|
"github.com/status-im/status-go/t/utils"
|
2018-05-03 10:36:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type statusTestParams struct {
|
|
|
|
Address string
|
|
|
|
Password string
|
|
|
|
HandlerFactory func(string, string) func(string)
|
|
|
|
ExpectedError error
|
|
|
|
ChannelName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusAPISuite(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
2018-05-03 10:36:56 +00:00
|
|
|
s := new(StatusAPISuite)
|
|
|
|
s.upstream = false
|
|
|
|
suite.Run(t, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusAPISuiteUpstream(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
2018-05-03 10:36:56 +00:00
|
|
|
s := new(StatusAPISuite)
|
|
|
|
s.upstream = true
|
|
|
|
suite.Run(t, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
type StatusAPISuite struct {
|
|
|
|
BaseJSONRPCSuite
|
|
|
|
upstream bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusAPISuite) TestAccessibleStatusAPIs() {
|
2019-10-04 15:21:24 +00:00
|
|
|
if s.upstream && utils.GetNetworkID() == params.StatusChainNetworkID {
|
2018-05-03 10:36:56 +00:00
|
|
|
s.T().Skip()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-25 13:27:17 +00:00
|
|
|
err := s.SetupTest(s.upstream, true, false)
|
2018-05-03 10:36:56 +00:00
|
|
|
s.NoError(err)
|
|
|
|
defer func() {
|
|
|
|
err := s.Backend.StopNode()
|
|
|
|
s.NoError(err)
|
|
|
|
}()
|
|
|
|
// These status APIs should be unavailable
|
|
|
|
s.AssertAPIMethodUnexported("status_login")
|
|
|
|
s.AssertAPIMethodUnexported("status_signup")
|
|
|
|
|
|
|
|
// These status APIs should be available only for IPC
|
|
|
|
s.AssertAPIMethodExportedPrivately("status_login")
|
|
|
|
s.AssertAPIMethodExportedPrivately("status_signup")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusAPISuite) TestStatusLoginSuccess() {
|
|
|
|
addressKeyID := s.testStatusLogin(statusTestParams{
|
2019-10-04 15:21:24 +00:00
|
|
|
Address: utils.TestConfig.Account1.WalletAddress,
|
|
|
|
Password: utils.TestConfig.Account1.Password,
|
2018-05-03 10:36:56 +00:00
|
|
|
})
|
|
|
|
s.NotEmpty(addressKeyID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusAPISuite) TestStatusLoginInvalidAddress() {
|
|
|
|
s.testStatusLogin(statusTestParams{
|
|
|
|
Address: "invalidaccount",
|
2019-10-04 15:21:24 +00:00
|
|
|
Password: utils.TestConfig.Account1.Password,
|
2018-05-03 10:36:56 +00:00
|
|
|
ExpectedError: account.ErrAddressToAccountMappingFailure,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusAPISuite) TestStatusLoginInvalidPassword() {
|
|
|
|
s.testStatusLogin(statusTestParams{
|
|
|
|
Address: "invalidaccount",
|
2019-10-04 15:21:24 +00:00
|
|
|
Password: utils.TestConfig.Account1.Password,
|
2018-05-03 10:36:56 +00:00
|
|
|
ExpectedError: account.ErrAddressToAccountMappingFailure,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusAPISuite) TestStatusSignupSuccess() {
|
|
|
|
var pwd = "randompassword"
|
|
|
|
|
|
|
|
res := s.testStatusSignup(statusTestParams{
|
|
|
|
Password: pwd,
|
|
|
|
})
|
2019-01-18 09:01:14 +00:00
|
|
|
s.NotEmpty(res.WalletAddress)
|
|
|
|
s.NotEmpty(res.WalletPubkey)
|
2018-05-03 10:36:56 +00:00
|
|
|
s.Equal(12, len(strings.Split(res.Mnemonic, " ")))
|
|
|
|
|
|
|
|
// I should be able to login with the newly created account
|
|
|
|
_ = s.testStatusLogin(statusTestParams{
|
2019-01-18 09:01:14 +00:00
|
|
|
Address: res.WalletAddress,
|
2018-05-03 10:36:56 +00:00
|
|
|
Password: pwd,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusAPISuite) testStatusLogin(testParams statusTestParams) *status.LoginResponse {
|
|
|
|
// Test upstream if that's not StatusChain
|
2019-10-04 15:21:24 +00:00
|
|
|
if s.upstream && utils.GetNetworkID() == params.StatusChainNetworkID {
|
2018-05-03 10:36:56 +00:00
|
|
|
s.T().Skip()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-25 13:27:17 +00:00
|
|
|
err := s.SetupTest(s.upstream, true, false)
|
2018-05-03 10:36:56 +00:00
|
|
|
s.NoError(err)
|
|
|
|
defer func() {
|
|
|
|
err := s.Backend.StopNode()
|
|
|
|
s.NoError(err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
req := status.LoginRequest{
|
|
|
|
Addr: testParams.Address,
|
|
|
|
Password: testParams.Password,
|
|
|
|
}
|
|
|
|
body, _ := json.Marshal(req)
|
|
|
|
|
|
|
|
basicCall := fmt.Sprintf(
|
|
|
|
`{"jsonrpc":"2.0","method":"status_login","params":[%s],"id":67}`,
|
|
|
|
body)
|
|
|
|
|
2018-12-20 08:31:17 +00:00
|
|
|
result, err := s.Backend.CallPrivateRPC(basicCall)
|
|
|
|
s.NoError(err)
|
2018-05-03 10:36:56 +00:00
|
|
|
if testParams.ExpectedError == nil {
|
|
|
|
var r struct {
|
2018-05-10 06:41:29 +00:00
|
|
|
Error string `json:"error"`
|
2018-05-03 10:36:56 +00:00
|
|
|
Result *status.LoginResponse `json:"result"`
|
|
|
|
}
|
|
|
|
s.NoError(json.Unmarshal([]byte(result), &r))
|
2018-05-10 06:41:29 +00:00
|
|
|
s.Empty(r.Error)
|
2018-05-03 10:36:56 +00:00
|
|
|
|
|
|
|
return r.Result
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Contains(result, testParams.ExpectedError.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusAPISuite) testStatusSignup(testParams statusTestParams) *status.SignupResponse {
|
|
|
|
// Test upstream if that's not StatusChain
|
2019-10-04 15:21:24 +00:00
|
|
|
if s.upstream && utils.GetNetworkID() == params.StatusChainNetworkID {
|
2018-05-03 10:36:56 +00:00
|
|
|
s.T().Skip()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-25 13:27:17 +00:00
|
|
|
err := s.SetupTest(s.upstream, true, false)
|
2018-05-03 10:36:56 +00:00
|
|
|
s.NoError(err)
|
|
|
|
defer func() {
|
|
|
|
err := s.Backend.StopNode()
|
|
|
|
s.NoError(err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
req := status.SignupRequest{
|
|
|
|
Password: testParams.Password,
|
|
|
|
}
|
|
|
|
body, _ := json.Marshal(req)
|
|
|
|
|
|
|
|
basicCall := fmt.Sprintf(
|
|
|
|
`{"jsonrpc":"2.0","method":"status_signup","params":[%s],"id":67}`,
|
|
|
|
body)
|
|
|
|
|
2018-12-20 08:31:17 +00:00
|
|
|
result, err := s.Backend.CallPrivateRPC(basicCall)
|
|
|
|
s.NoError(err)
|
2018-05-03 10:36:56 +00:00
|
|
|
|
|
|
|
if testParams.ExpectedError == nil {
|
|
|
|
var r struct {
|
2018-05-10 06:41:29 +00:00
|
|
|
Error string `json:"error"`
|
2018-05-03 10:36:56 +00:00
|
|
|
Result *status.SignupResponse `json:"result"`
|
|
|
|
}
|
|
|
|
s.NoError(json.Unmarshal([]byte(result), &r))
|
2018-05-10 06:41:29 +00:00
|
|
|
s.Empty(r.Error)
|
2018-05-03 10:36:56 +00:00
|
|
|
|
|
|
|
return r.Result
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Contains(result, testParams.ExpectedError.Error())
|
|
|
|
return nil
|
|
|
|
}
|