status-go/t/e2e/services/status_api_test.go

187 lines
4.4 KiB
Go
Raw Normal View History

package services
import (
"encoding/json"
"fmt"
"strings"
"testing"
2020-01-02 09:10:19 +00:00
"github.com/stretchr/testify/suite"
"github.com/status-im/status-go/account"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/services/status"
"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) {
utils.Init()
s := new(StatusAPISuite)
s.upstream = false
suite.Run(t, s)
}
func TestStatusAPISuiteUpstream(t *testing.T) {
utils.Init()
s := new(StatusAPISuite)
s.upstream = true
suite.Run(t, s)
}
type StatusAPISuite struct {
BaseJSONRPCSuite
upstream bool
}
func (s *StatusAPISuite) TestAccessibleStatusAPIs() {
if s.upstream && utils.GetNetworkID() == params.StatusChainNetworkID {
s.T().Skip()
return
}
err := s.SetupTest(s.upstream, true, false)
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: utils.TestConfig.Account1.WalletAddress,
Password: utils.TestConfig.Account1.Password,
})
s.NotEmpty(addressKeyID)
}
func (s *StatusAPISuite) TestStatusLoginInvalidAddress() {
s.testStatusLogin(statusTestParams{
Address: "invalidaccount",
Password: utils.TestConfig.Account1.Password,
ExpectedError: account.ErrAddressToAccountMappingFailure,
})
}
func (s *StatusAPISuite) TestStatusLoginInvalidPassword() {
s.testStatusLogin(statusTestParams{
Address: "invalidaccount",
Password: utils.TestConfig.Account1.Password,
ExpectedError: account.ErrAddressToAccountMappingFailure,
})
}
func (s *StatusAPISuite) TestStatusSignupSuccess() {
var pwd = "randompassword"
res := s.testStatusSignup(statusTestParams{
Password: pwd,
})
s.NotEmpty(res.WalletAddress)
s.NotEmpty(res.WalletPubkey)
s.Equal(12, len(strings.Split(res.Mnemonic, " ")))
// I should be able to login with the newly created account
_ = s.testStatusLogin(statusTestParams{
Address: res.WalletAddress,
Password: pwd,
})
}
func (s *StatusAPISuite) testStatusLogin(testParams statusTestParams) *status.LoginResponse {
// Test upstream if that's not StatusChain
if s.upstream && utils.GetNetworkID() == params.StatusChainNetworkID {
s.T().Skip()
return nil
}
err := s.SetupTest(s.upstream, true, false)
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)
result, err := s.Backend.CallPrivateRPC(basicCall)
s.NoError(err)
if testParams.ExpectedError == nil {
var r struct {
2018-05-10 06:41:29 +00:00
Error string `json:"error"`
Result *status.LoginResponse `json:"result"`
}
s.NoError(json.Unmarshal([]byte(result), &r))
2018-05-10 06:41:29 +00:00
s.Empty(r.Error)
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 && utils.GetNetworkID() == params.StatusChainNetworkID {
s.T().Skip()
return nil
}
err := s.SetupTest(s.upstream, true, false)
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)
result, err := s.Backend.CallPrivateRPC(basicCall)
s.NoError(err)
if testParams.ExpectedError == nil {
var r struct {
2018-05-10 06:41:29 +00:00
Error string `json:"error"`
Result *status.SignupResponse `json:"result"`
}
s.NoError(json.Unmarshal([]byte(result), &r))
2018-05-10 06:41:29 +00:00
s.Empty(r.Error)
return r.Result
}
s.Contains(result, testParams.ExpectedError.Error())
return nil
}