-
Notifications
You must be signed in to change notification settings - Fork 246
/
communities_messenger_test_suite_base_test.go
140 lines (118 loc) · 5.37 KB
/
communities_messenger_test_suite_base_test.go
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
package protocol
import (
"crypto/ecdsa"
"math/big"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
gethcommon "github.com/ethereum/go-ethereum/common"
hexutil "github.com/ethereum/go-ethereum/common/hexutil"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
// TODO: in future adapt this struct to use waku v2 and switch all tests to waku v2
type CommunitiesMessengerTestSuiteBase struct {
suite.Suite
// If one wants to send messages between different instances of Messenger,
// a single Waku service should be shared.
shh types.Waku
logger *zap.Logger
mockedBalances communities.BalancesByChain
mockedCollectibles communities.CollectiblesByChain
collectiblesServiceMock *CollectiblesServiceMock
collectiblesManagerMock *CollectiblesManagerMock
accountsTestData map[string][]string
accountsPasswords map[string]string
}
func (s *CommunitiesMessengerTestSuiteBase) SetupTest() {
s.logger = tt.MustCreateTestLogger()
s.collectiblesServiceMock = &CollectiblesServiceMock{}
s.accountsTestData = make(map[string][]string)
s.accountsPasswords = make(map[string]string)
s.mockedCollectibles = make(communities.CollectiblesByChain)
s.collectiblesManagerMock = &CollectiblesManagerMock{
Collectibles: &s.mockedCollectibles,
}
s.mockedBalances = make(communities.BalancesByChain)
config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
shh := waku.New(&config, s.logger)
s.shh = gethbridge.NewGethWakuWrapper(shh)
s.Require().NoError(shh.Start())
}
func (s *CommunitiesMessengerTestSuiteBase) TearDownTest() {
_ = s.logger.Sync()
}
func (s *CommunitiesMessengerTestSuiteBase) newMessenger(password string, walletAddresses []string) *Messenger {
privateKey, err := crypto.GenerateKey()
s.Require().NoError(err)
return s.newMessengerWithConfig(testMessengerConfig{
logger: s.logger,
privateKey: privateKey,
}, password, walletAddresses)
}
func (s *CommunitiesMessengerTestSuiteBase) newMessengerWithKey(privateKey *ecdsa.PrivateKey, password string, walletAddresses []string) *Messenger {
return s.newMessengerWithConfig(testMessengerConfig{
privateKey: privateKey,
logger: s.logger,
}, password, walletAddresses)
}
func (s *CommunitiesMessengerTestSuiteBase) newMessengerWithConfig(config testMessengerConfig, password string, walletAddresses []string) *Messenger {
messenger := newTestCommunitiesMessenger(&s.Suite, s.shh, testCommunitiesMessengerConfig{
testMessengerConfig: config,
password: password,
walletAddresses: walletAddresses,
mockedBalances: &s.mockedBalances,
collectiblesService: s.collectiblesServiceMock,
collectiblesManager: s.collectiblesManagerMock,
})
publicKey := messenger.IdentityPublicKeyString()
s.accountsTestData[publicKey] = walletAddresses
s.accountsPasswords[publicKey] = password
return messenger
}
func (s *CommunitiesMessengerTestSuiteBase) joinCommunity(community *communities.Community, controlNode *Messenger, user *Messenger) {
userPk := user.IdentityPublicKeyString()
addresses, exists := s.accountsTestData[userPk]
s.Require().True(exists)
password, exists := s.accountsPasswords[userPk]
s.Require().True(exists)
joinCommunity(&s.Suite, community.ID(), controlNode, user, password, addresses)
}
func (s *CommunitiesMessengerTestSuiteBase) joinOnRequestCommunity(community *communities.Community, controlNode *Messenger, user *Messenger) {
userPk := user.IdentityPublicKeyString()
addresses, exists := s.accountsTestData[userPk]
s.Require().True(exists)
password, exists := s.accountsPasswords[userPk]
s.Require().True(exists)
joinOnRequestCommunity(&s.Suite, community.ID(), controlNode, user, password, addresses)
}
func (s *CommunitiesMessengerTestSuiteBase) createRequestToJoinCommunity(communityID types.HexBytes, user *Messenger) *requests.RequestToJoinCommunity {
userPk := user.IdentityPublicKeyString()
addresses, exists := s.accountsTestData[userPk]
s.Require().True(exists)
password, exists := s.accountsPasswords[userPk]
s.Require().True(exists)
return createRequestToJoinCommunity(&s.Suite, communityID, user, password, addresses)
}
func (s *CommunitiesMessengerTestSuiteBase) makeAddressSatisfyTheCriteria(chainID uint64, address string, criteria *protobuf.TokenCriteria) {
walletAddress := gethcommon.HexToAddress(address)
contractAddress := gethcommon.HexToAddress(criteria.ContractAddresses[chainID])
balance, ok := new(big.Int).SetString(criteria.AmountInWei, 10)
s.Require().True(ok)
if _, exists := s.mockedBalances[chainID]; !exists {
s.mockedBalances[chainID] = make(map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big)
}
if _, exists := s.mockedBalances[chainID][walletAddress]; !exists {
s.mockedBalances[chainID][walletAddress] = make(map[gethcommon.Address]*hexutil.Big)
}
if _, exists := s.mockedBalances[chainID][walletAddress][contractAddress]; !exists {
s.mockedBalances[chainID][walletAddress][contractAddress] = (*hexutil.Big)(balance)
}
makeAddressSatisfyTheCriteria(&s.Suite, s.mockedBalances, s.mockedCollectibles, chainID, address, criteria)
}