2018-05-03 10:36:56 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
. "github.com/status-im/status-go/t/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type statusTestParams struct {
|
|
|
|
Address string
|
|
|
|
Password string
|
|
|
|
HandlerFactory func(string, string) func(string)
|
|
|
|
ExpectedError error
|
|
|
|
ChannelName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusAPISuite(t *testing.T) {
|
|
|
|
s := new(StatusAPISuite)
|
|
|
|
s.upstream = false
|
|
|
|
suite.Run(t, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusAPISuiteUpstream(t *testing.T) {
|
|
|
|
s := new(StatusAPISuite)
|
|
|
|
s.upstream = true
|
|
|
|
suite.Run(t, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
type StatusAPISuite struct {
|
|
|
|
BaseJSONRPCSuite
|
|
|
|
upstream bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusAPISuite) TestAccessibleStatusAPIs() {
|
|
|
|
if s.upstream && GetNetworkID() == params.StatusChainNetworkID {
|
|
|
|
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{
|
|
|
|
Address: TestConfig.Account1.Address,
|
|
|
|
Password: TestConfig.Account1.Password,
|
|
|
|
})
|
|
|
|
s.NotEmpty(addressKeyID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusAPISuite) TestStatusLoginInvalidAddress() {
|
|
|
|
s.testStatusLogin(statusTestParams{
|
|
|
|
Address: "invalidaccount",
|
|
|
|
Password: TestConfig.Account1.Password,
|
|
|
|
ExpectedError: account.ErrAddressToAccountMappingFailure,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusAPISuite) TestStatusLoginInvalidPassword() {
|
|
|
|
s.testStatusLogin(statusTestParams{
|
|
|
|
Address: "invalidaccount",
|
|
|
|
Password: TestConfig.Account1.Password,
|
|
|
|
ExpectedError: account.ErrAddressToAccountMappingFailure,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusAPISuite) TestStatusSignupSuccess() {
|
|
|
|
var pwd = "randompassword"
|
|
|
|
|
|
|
|
res := s.testStatusSignup(statusTestParams{
|
|
|
|
Password: pwd,
|
|
|
|
})
|
|
|
|
s.NotEmpty(res.Address)
|
|
|
|
s.NotEmpty(res.Pubkey)
|
|
|
|
s.Equal(12, len(strings.Split(res.Mnemonic, " ")))
|
|
|
|
|
|
|
|
// I should be able to login with the newly created account
|
|
|
|
_ = s.testStatusLogin(statusTestParams{
|
|
|
|
Address: res.Address,
|
|
|
|
Password: pwd,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusAPISuite) testStatusLogin(testParams statusTestParams) *status.LoginResponse {
|
|
|
|
// Test upstream if that's not StatusChain
|
|
|
|
if s.upstream && GetNetworkID() == params.StatusChainNetworkID {
|
|
|
|
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
|
|
|
|
if s.upstream && GetNetworkID() == params.StatusChainNetworkID {
|
|
|
|
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
|
|
|
|
}
|