-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathapi_impl_test.go
More file actions
148 lines (121 loc) · 5.12 KB
/
Copy pathapi_impl_test.go
File metadata and controls
148 lines (121 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package wallet
import (
"context"
"fmt"
"math/big"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/brianvoe/gofakeit/v6"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"github.com/status-im/status-go/internal/db/appdatabase"
"github.com/status-im/status-go/internal/db/multiaccounts/accounts"
"github.com/status-im/status-go/internal/db/walletdatabase"
"github.com/status-im/status-go/internal/rpc"
network_mock "github.com/status-im/status-go/internal/rpc/network/mock"
"github.com/status-im/status-go/internal/rpc/network/testutil"
"github.com/status-im/status-go/internal/testutils"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/params/networkhelper"
"github.com/status-im/status-go/pkg/pubsub"
"github.com/status-im/status-go/pkg/security"
"github.com/status-im/status-go/services/wallet/requests"
"github.com/status-im/status-go/services/wallet/token"
mock_tokenbalances "github.com/status-im/status-go/services/wallet/tokenbalances/mock"
)
func TestAPI_GetAddressDetails(t *testing.T) {
appDB, err := testutils.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
require.NoError(t, err)
defer appDB.Close()
accountsDb, err := accounts.NewDB(appDB)
require.NoError(t, err)
defer accountsDb.Close()
db, err := testutils.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
require.NoError(t, err)
defer db.Close()
accountsPublisher := pubsub.NewPublisher()
chainID := uint64(1)
address := "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
// Create a new server that delays the response by 1 second
serverWith1SecDelay := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
fmt.Fprintln(w, `{"result": "0x10"}`)
}))
defer serverWith1SecDelay.Close()
networks := []params.Network{
*testutil.CreateNetwork(chainID, "Ethereum Mainnet", []params.RpcProvider{
*params.NewProxyProvider(chainID, "Test Provider", security.NewSensitiveString(serverWith1SecDelay.URL+"/nodefleet/"), false),
},
),
}
networks = networkhelper.OverrideBasicAuth(networks, params.EmbeddedProxyProviderType, true, security.NewSensitiveString(gofakeit.Username()), security.NewSensitiveString(gofakeit.LetterN(5)))
require.NotEmpty(t, networks)
config := rpc.ClientConfig{
Networks: networks,
DB: appDB,
}
c, err := rpc.NewClient(config)
require.NoError(t, err)
mockCtrl := gomock.NewController(t)
mockNetworkManager := network_mock.NewMockManagerInterface(mockCtrl)
mockNetworkManager.EXPECT().GetActiveNetworks().DoAndReturn(func() ([]*params.Network, error) {
active := make([]*params.Network, 0, len(networks))
for i := range networks {
active = append(active, &networks[i])
}
return active, nil
}).AnyTimes()
mockNetworkManager.EXPECT().GetPublisher().Return(pubsub.NewPublisher()).AnyTimes()
mockNetworkManager.EXPECT().GetTestNetworksEnabled().Return(false, nil).AnyTimes()
tokenManager, err := token.NewTokenManager(db, c, nil, mockNetworkManager, appDB, nil, nil, nil, accountsDb, 0, 0)
require.NoError(t, err)
service, err := NewService(db, accountsDb, appDB, c, accountsPublisher, nil, nil, ¶ms.NodeConfig{}, nil, nil, nil, nil, tokenManager)
require.NoError(t, err)
tokenbalancesFetcher := mock_tokenbalances.NewMockFetcherIface(mockCtrl)
service.tokenBalancesFetcher = tokenbalancesFetcher
api := NewAPI(service)
tokenbalancesFetcher.EXPECT().FetchSingle(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, chainID uint64, tokenAddress common.Address, accountAddress common.Address) (*big.Int, error) {
// Delay the response by 1 second
timer := time.NewTimer(1 * time.Second)
select {
case <-timer.C:
return big.NewInt(1000000000000000000), nil
case <-ctx.Done():
return nil, ctx.Err()
}
}).AnyTimes()
// Test getting address details using `GetAddressDetails` call, that always waits for the request to finish
details, err := api.GetAddressDetails(context.Background(), 1, address)
require.NoError(t, err)
require.Equal(t, true, details.HasActivity)
// empty params
details, err = api.AddressDetails(context.Background(), &requests.AddressDetails{})
require.Error(t, err)
require.ErrorIs(t, err, requests.ErrAddresInvalid)
require.Nil(t, details)
// no response longer than the set timeout
details, err = api.AddressDetails(context.Background(), &requests.AddressDetails{
Address: address,
TimeoutInMilliseconds: 500,
})
require.NoError(t, err)
require.Equal(t, false, details.HasActivity)
// timeout longer than the response time
details, err = api.AddressDetails(context.Background(), &requests.AddressDetails{
Address: address,
TimeoutInMilliseconds: 1200,
})
require.NoError(t, err)
require.Equal(t, true, details.HasActivity)
// specific chain and timeout longer than the response time
details, err = api.AddressDetails(context.Background(), &requests.AddressDetails{
Address: address,
ChainIDs: []uint64{chainID},
TimeoutInMilliseconds: 1200,
})
require.NoError(t, err)
require.Equal(t, true, details.HasActivity)
}