diff --git a/protocol/communities_events_owner_without_community_key_test.go b/protocol/communities_events_owner_without_community_key_test.go index 4808d98ed..39328a834 100644 --- a/protocol/communities_events_owner_without_community_key_test.go +++ b/protocol/communities_events_owner_without_community_key_test.go @@ -33,6 +33,8 @@ type OwnerWithoutCommunityKeyCommunityEventsSuite struct { logger *zap.Logger mockedBalances map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big // chainID, account, token, balance collectiblesServiceMock *CollectiblesServiceMock + + additionalEventSenders []*Messenger } func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) GetControlNode() *Messenger { @@ -82,11 +84,33 @@ func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) TearDownTest() { TearDownMessenger(&s.Suite, s.controlNode) TearDownMessenger(&s.Suite, s.ownerWithoutCommunityKey) TearDownMessenger(&s.Suite, s.alice) + + for _, m := range s.additionalEventSenders { + TearDownMessenger(&s.Suite, m) + } + s.additionalEventSenders = nil + _ = s.logger.Sync() } +func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) SetupAdditionalMessengers(messengers []*Messenger) { + for _, m := range messengers { + s.additionalEventSenders = append(s.additionalEventSenders, m) + _, err := m.Start() + s.Require().NoError(err) + } +} + func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) newMessenger(password string, walletAddresses []string) *Messenger { - return newMessenger(&s.Suite, s.shh, s.logger, password, walletAddresses, &s.mockedBalances, s.collectiblesServiceMock) + return newTestCommunitiesMessenger(&s.Suite, s.shh, testCommunitiesMessengerConfig{ + testMessengerConfig: testMessengerConfig{ + logger: s.logger, + }, + password: password, + walletAddresses: walletAddresses, + mockedBalances: &s.mockedBalances, + collectiblesService: s.collectiblesServiceMock, + }) } func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) TestOwnerEditCommunityDescription() { @@ -118,8 +142,11 @@ func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) TestOwnerCreateEditDelete func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) TestOwnerAcceptMemberRequestToJoinResponseSharedWithOtherEventSenders() { additionalOwner := s.newMessenger("", []string{}) community := setUpOnRequestCommunityAndRoles(s, protobuf.CommunityMember_ROLE_OWNER, []*Messenger{additionalOwner}) + // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) + testAcceptMemberRequestToJoinResponseSharedWithOtherEventSenders(s, community, user, additionalOwner) } @@ -128,14 +155,19 @@ func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) TestOwnerAcceptMemberRequ // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) + testAcceptMemberRequestToJoin(s, community, user) } func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) TestOwnerRejectMemberRequestToJoinResponseSharedWithOtherEventSenders() { additionalOwner := s.newMessenger("", []string{}) community := setUpOnRequestCommunityAndRoles(s, protobuf.CommunityMember_ROLE_OWNER, []*Messenger{additionalOwner}) + // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) + testAcceptMemberRequestToJoinResponseSharedWithOtherEventSenders(s, community, user, additionalOwner) } @@ -144,6 +176,8 @@ func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) TestOwnerRejectMemberRequ // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) + testRejectMemberRequestToJoin(s, community, user) } diff --git a/protocol/communities_events_token_master_test.go b/protocol/communities_events_token_master_test.go index 9b406e92c..0fd8eae5a 100644 --- a/protocol/communities_events_token_master_test.go +++ b/protocol/communities_events_token_master_test.go @@ -33,6 +33,8 @@ type TokenMasterCommunityEventsSuite struct { logger *zap.Logger mockedBalances map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big // chainID, account, token, balance collectiblesServiceMock *CollectiblesServiceMock + + additionalEventSenders []*Messenger } func (s *TokenMasterCommunityEventsSuite) GetControlNode() *Messenger { @@ -82,11 +84,33 @@ func (s *TokenMasterCommunityEventsSuite) TearDownTest() { TearDownMessenger(&s.Suite, s.controlNode) TearDownMessenger(&s.Suite, s.tokenMaster) TearDownMessenger(&s.Suite, s.alice) + + for _, m := range s.additionalEventSenders { + TearDownMessenger(&s.Suite, m) + } + s.additionalEventSenders = nil + _ = s.logger.Sync() } +func (s *TokenMasterCommunityEventsSuite) SetupAdditionalMessengers(messengers []*Messenger) { + for _, m := range messengers { + s.additionalEventSenders = append(s.additionalEventSenders, m) + _, err := m.Start() + s.Require().NoError(err) + } +} + func (s *TokenMasterCommunityEventsSuite) newMessenger(password string, walletAddresses []string) *Messenger { - return newMessenger(&s.Suite, s.shh, s.logger, password, walletAddresses, &s.mockedBalances, s.collectiblesServiceMock) + return newTestCommunitiesMessenger(&s.Suite, s.shh, testCommunitiesMessengerConfig{ + testMessengerConfig: testMessengerConfig{ + logger: s.logger, + }, + password: password, + walletAddresses: walletAddresses, + mockedBalances: &s.mockedBalances, + collectiblesService: s.collectiblesServiceMock, + }) } func (s *TokenMasterCommunityEventsSuite) TestTokenMasterEditCommunityDescription() { @@ -144,14 +168,19 @@ func (s *TokenMasterCommunityEventsSuite) TestTokenMasterAcceptMemberRequestToJo community := setUpOnRequestCommunityAndRoles(s, protobuf.CommunityMember_ROLE_TOKEN_MASTER, []*Messenger{}) // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) + testAcceptMemberRequestToJoin(s, community, user) } func (s *TokenMasterCommunityEventsSuite) TestTokenMasterAcceptMemberRequestToJoinResponseSharedWithOtherEventSenders() { additionalTokenMaster := s.newMessenger("qwerty", []string{eventsSenderAccountAddress}) community := setUpOnRequestCommunityAndRoles(s, protobuf.CommunityMember_ROLE_TOKEN_MASTER, []*Messenger{additionalTokenMaster}) + // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) + testAcceptMemberRequestToJoinResponseSharedWithOtherEventSenders(s, community, user, additionalTokenMaster) } @@ -160,6 +189,8 @@ func (s *TokenMasterCommunityEventsSuite) TestTokenMasterRejectMemberRequestToJo community := setUpOnRequestCommunityAndRoles(s, protobuf.CommunityMember_ROLE_TOKEN_MASTER, []*Messenger{additionalTokenMaster}) // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) + testRejectMemberRequestToJoinResponseSharedWithOtherEventSenders(s, community, user, additionalTokenMaster) } @@ -167,6 +198,8 @@ func (s *TokenMasterCommunityEventsSuite) TestTokenMasterRejectMemberRequestToJo community := setUpOnRequestCommunityAndRoles(s, protobuf.CommunityMember_ROLE_TOKEN_MASTER, []*Messenger{}) // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) + testRejectMemberRequestToJoin(s, community, user) } @@ -270,12 +303,15 @@ func (s *TokenMasterCommunityEventsSuite) TestJoinedTokenMasterReceiveRequestsTo // set up additional user that will join to the community as TokenMaster newPrivilegedUser := s.newMessenger(accountPassword, []string{eventsSenderAccountAddress}) + s.SetupAdditionalMessengers([]*Messenger{bob, newPrivilegedUser}) + testJoinedPrivilegedMemberReceiveRequestsToJoin(s, community, bob, newPrivilegedUser, protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER) } func (s *TokenMasterCommunityEventsSuite) TestReceiveRequestsToJoinWithRevealedAccountsAfterGettingTokenMasterRole() { // set up additional user (bob) that will send request to join bob := s.newMessenger(accountPassword, []string{bobAccountAddress}) + s.SetupAdditionalMessengers([]*Messenger{bob}) testMemberReceiveRequestsToJoinAfterGettingNewRole(s, bob, protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER) } @@ -284,5 +320,6 @@ func (s *TokenMasterCommunityEventsSuite) TestTokenMasterAcceptsRequestToJoinAft // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) testPrivilegedMemberAcceptsRequestToJoinAfterMemberLeave(s, community, user) } diff --git a/protocol/communities_events_utils_test.go b/protocol/communities_events_utils_test.go index 334db1ceb..fbbb84077 100644 --- a/protocol/communities_events_utils_test.go +++ b/protocol/communities_events_utils_test.go @@ -26,6 +26,7 @@ type CommunityEventsTestsInterface interface { GetMember() *Messenger GetSuite() *suite.Suite GetCollectiblesServiceMock() *CollectiblesServiceMock + SetupAdditionalMessengers([]*Messenger) } const communitiesEventsTestTokenAddress = "0x0400000000000000000000000000000000000000" @@ -408,6 +409,8 @@ func assertCheckTokenPermissionCreated(s *suite.Suite, community *communities.Co } func setUpOnRequestCommunityAndRoles(base CommunityEventsTestsInterface, role protobuf.CommunityMember_Roles, additionalEventSenders []*Messenger) *communities.Community { + base.SetupAdditionalMessengers(additionalEventSenders) + tcs2, err := base.GetControlNode().communitiesManager.All() s := base.GetSuite() s.Require().NoError(err, "eventSender.communitiesManager.All") @@ -1048,14 +1051,8 @@ func advertiseCommunityToUserOldWay(s *suite.Suite, community *communities.Commu } func testAcceptMemberRequestToJoin(base CommunityEventsTestsInterface, community *communities.Community, user *Messenger) { - // set up additional user that will send request to join - _, err := user.Start() - s := base.GetSuite() - s.Require().NoError(err) - defer TearDownMessenger(s, user) - advertiseCommunityToUserOldWay(s, community, base.GetControlNode(), user) // user sends request to join @@ -1178,14 +1175,8 @@ func testAcceptMemberRequestToJoin(base CommunityEventsTestsInterface, community } func testAcceptMemberRequestToJoinResponseSharedWithOtherEventSenders(base CommunityEventsTestsInterface, community *communities.Community, user *Messenger, additionalEventSender *Messenger) { - // set up additional user that will send request to join - _, err := user.Start() - s := base.GetSuite() - s.Require().NoError(err) - defer TearDownMessenger(s, user) - advertiseCommunityToUserOldWay(s, community, base.GetControlNode(), user) // user sends request to join @@ -1256,14 +1247,8 @@ func testAcceptMemberRequestToJoinResponseSharedWithOtherEventSenders(base Commu } func testRejectMemberRequestToJoinResponseSharedWithOtherEventSenders(base CommunityEventsTestsInterface, community *communities.Community, user *Messenger, additionalEventSender *Messenger) { - // set up additional user that will send request to join - _, err := user.Start() - s := base.GetSuite() - s.Require().NoError(err) - defer TearDownMessenger(s, user) - advertiseCommunityToUserOldWay(s, community, base.GetControlNode(), user) // user sends request to join @@ -1335,11 +1320,7 @@ func testRejectMemberRequestToJoinResponseSharedWithOtherEventSenders(base Commu } func testRejectMemberRequestToJoin(base CommunityEventsTestsInterface, community *communities.Community, user *Messenger) { - _, err := user.Start() - s := base.GetSuite() - s.Require().NoError(err) - defer TearDownMessenger(s, user) advertiseCommunityToUserOldWay(s, community, base.GetControlNode(), user) @@ -2249,14 +2230,8 @@ func waitAndCheckRequestsToJoin(s *suite.Suite, user *Messenger, expectedLength } func testPrivilegedMemberAcceptsRequestToJoinAfterMemberLeave(base CommunityEventsTestsInterface, community *communities.Community, user *Messenger) { - // set up additional user that will send request to join - _, err := user.Start() - s := base.GetSuite() - s.Require().NoError(err) - defer TearDownMessenger(s, user) - advertiseCommunityToUserOldWay(s, community, base.GetControlNode(), user) // user sends request to join diff --git a/protocol/communities_messenger_admin_test.go b/protocol/communities_messenger_admin_test.go index 1f24de86f..816984964 100644 --- a/protocol/communities_messenger_admin_test.go +++ b/protocol/communities_messenger_admin_test.go @@ -37,6 +37,8 @@ type AdminCommunityEventsSuite struct { logger *zap.Logger mockedBalances map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big // chainID, account, token, balance collectiblesServiceMock *CollectiblesServiceMock + + additionalEventSenders []*Messenger } func (s *AdminCommunityEventsSuite) GetControlNode() *Messenger { @@ -86,11 +88,34 @@ func (s *AdminCommunityEventsSuite) TearDownTest() { TearDownMessenger(&s.Suite, s.owner) TearDownMessenger(&s.Suite, s.admin) TearDownMessenger(&s.Suite, s.alice) + + for _, m := range s.additionalEventSenders { + TearDownMessenger(&s.Suite, m) + } + s.additionalEventSenders = nil + _ = s.logger.Sync() } +func (s *AdminCommunityEventsSuite) SetupAdditionalMessengers(messengers []*Messenger) { + for _, m := range messengers { + s.additionalEventSenders = append(s.additionalEventSenders, m) + _, err := m.Start() + s.Require().NoError(err) + } +} + func (s *AdminCommunityEventsSuite) newMessenger(password string, walletAddresses []string) *Messenger { - return newMessenger(&s.Suite, s.shh, s.logger, password, walletAddresses, &s.mockedBalances, s.collectiblesServiceMock) + return newTestCommunitiesMessenger(&s.Suite, s.shh, testCommunitiesMessengerConfig{ + testMessengerConfig: testMessengerConfig{ + logger: s.logger, + }, + password: password, + walletAddresses: walletAddresses, + mockedBalances: &s.mockedBalances, + collectiblesService: s.collectiblesServiceMock, + }) + } func (s *AdminCommunityEventsSuite) TestAdminEditCommunityDescription() { @@ -146,8 +171,11 @@ func (s *AdminCommunityEventsSuite) TestAdminCannotDeleteBecomeTokenMasterPermis func (s *AdminCommunityEventsSuite) TestAdminAcceptMemberRequestToJoinResponseSharedWithOtherEventSenders() { additionalAdmin := s.newMessenger("qwerty", []string{eventsSenderAccountAddress}) community := setUpOnRequestCommunityAndRoles(s, protobuf.CommunityMember_ROLE_ADMIN, []*Messenger{additionalAdmin}) + // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) + testAcceptMemberRequestToJoinResponseSharedWithOtherEventSenders(s, community, user, additionalAdmin) } @@ -156,6 +184,8 @@ func (s *AdminCommunityEventsSuite) TestAdminAcceptMemberRequestToJoin() { // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) + testAcceptMemberRequestToJoin(s, community, user) } @@ -164,6 +194,8 @@ func (s *AdminCommunityEventsSuite) TestAdminRejectMemberRequestToJoinResponseSh community := setUpOnRequestCommunityAndRoles(s, protobuf.CommunityMember_ROLE_ADMIN, []*Messenger{additionalAdmin}) // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) + testRejectMemberRequestToJoinResponseSharedWithOtherEventSenders(s, community, user, additionalAdmin) } @@ -172,6 +204,8 @@ func (s *AdminCommunityEventsSuite) TestAdminRejectMemberRequestToJoin() { // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) + testRejectMemberRequestToJoin(s, community, user) } @@ -370,12 +404,15 @@ func (s *AdminCommunityEventsSuite) TestJoinedAdminReceiveRequestsToJoinWithoutR // set up additional user that will join to the community as TokenMaster newPrivilegedUser := s.newMessenger(accountPassword, []string{eventsSenderAccountAddress}) + s.SetupAdditionalMessengers([]*Messenger{bob, newPrivilegedUser}) + testJoinedPrivilegedMemberReceiveRequestsToJoin(s, community, bob, newPrivilegedUser, protobuf.CommunityTokenPermission_BECOME_ADMIN) } func (s *AdminCommunityEventsSuite) TestReceiveRequestsToJoinWithRevealedAccountsAfterGettingAdminRole() { // set up additional user (bob) that will send request to join bob := s.newMessenger(accountPassword, []string{bobAccountAddress}) + s.SetupAdditionalMessengers([]*Messenger{bob}) testMemberReceiveRequestsToJoinAfterGettingNewRole(s, bob, protobuf.CommunityTokenPermission_BECOME_ADMIN) } @@ -443,5 +480,6 @@ func (s *AdminCommunityEventsSuite) TestAdminAcceptsRequestToJoinAfterMemberLeav // set up additional user that will send request to join user := s.newMessenger("", []string{}) + s.SetupAdditionalMessengers([]*Messenger{user}) testPrivilegedMemberAcceptsRequestToJoinAfterMemberLeave(s, community, user) } diff --git a/protocol/communities_messenger_helpers_test.go b/protocol/communities_messenger_helpers_test.go index 5854b6b21..12da75aba 100644 --- a/protocol/communities_messenger_helpers_test.go +++ b/protocol/communities_messenger_helpers_test.go @@ -9,7 +9,6 @@ import ( "time" "github.com/stretchr/testify/suite" - "go.uber.org/zap" gethcommon "github.com/ethereum/go-ethereum/common" hexutil "github.com/ethereum/go-ethereum/common/hexutil" @@ -149,58 +148,44 @@ func (c *CollectiblesServiceMock) DeploymentSignatureDigest(chainID uint64, addr return gethcommon.Hex2Bytes("ccbb375343347491706cf4b43796f7b96ccc89c9e191a8b78679daeba1684ec7"), nil } -func newMessenger(s *suite.Suite, shh types.Waku, logger *zap.Logger, password string, walletAddresses []string, - mockedBalances *map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big, collectiblesService communitytokens.ServiceInterface) *Messenger { - accountsManagerMock := &AccountManagerMock{} - accountsManagerMock.AccountsMap = make(map[string]string) - for _, walletAddress := range walletAddresses { - accountsManagerMock.AccountsMap[walletAddress] = types.EncodeHex(crypto.Keccak256([]byte(password))) - } +type testCommunitiesMessengerConfig struct { + testMessengerConfig - tokenManagerMock := &TokenManagerMock{ - Balances: mockedBalances, - } + nodeConfig *params.NodeConfig + appSettings *settings.Settings - privateKey, err := crypto.GenerateKey() - s.Require().NoError(err) - - messenger, err := newCommunitiesTestMessenger(shh, privateKey, logger, accountsManagerMock, tokenManagerMock, collectiblesService) - s.Require().NoError(err) - - currentDistributorObj, ok := messenger.communitiesKeyDistributor.(*CommunitiesKeyDistributorImpl) - s.Require().True(ok) - messenger.communitiesKeyDistributor = &TestCommunitiesKeyDistributor{ - CommunitiesKeyDistributorImpl: *currentDistributorObj, - subscriptions: map[chan *CommunityAndKeyActions]bool{}, - mutex: sync.RWMutex{}, - } - - // add wallet account with keypair - for _, walletAddress := range walletAddresses { - kp := accounts.GetProfileKeypairForTest(false, true, false) - kp.Accounts[0].Address = types.HexToAddress(walletAddress) - err := messenger.settings.SaveOrUpdateKeypair(kp) - s.Require().NoError(err) - } - - walletAccounts, err := messenger.settings.GetActiveAccounts() - s.Require().NoError(err) - s.Require().Len(walletAccounts, len(walletAddresses)) - for i := range walletAddresses { - s.Require().Equal(walletAccounts[i].Type, accounts.AccountTypeGenerated) - } - return messenger + password string + walletAddresses []string + mockedBalances *map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big + collectiblesService communitytokens.ServiceInterface } -func newCommunitiesTestMessenger(shh types.Waku, privateKey *ecdsa.PrivateKey, logger *zap.Logger, accountsManager account.Manager, - tokenManager communities.TokenManager, collectiblesService communitytokens.ServiceInterface) (*Messenger, error) { - config := params.NodeConfig{ +func (tcmc *testCommunitiesMessengerConfig) complete() error { + err := tcmc.testMessengerConfig.complete() + if err != nil { + return err + } + + if tcmc.nodeConfig == nil { + tcmc.nodeConfig = defaultTestCommunitiesMessengerNodeConfig() + } + if tcmc.appSettings == nil { + tcmc.appSettings = defaultTestCommunitiesMessengerSettings() + } + + return nil +} + +func defaultTestCommunitiesMessengerNodeConfig() *params.NodeConfig { + return ¶ms.NodeConfig{ NetworkID: 10, DataDir: "test", } +} +func defaultTestCommunitiesMessengerSettings() *settings.Settings { networks := json.RawMessage("{}") - setting := settings.Settings{ + return &settings.Settings{ Address: types.HexToAddress("0x1122334455667788990011223344556677889900"), AnonMetricsShouldSend: false, CurrentNetwork: "mainnet_rpc", @@ -221,29 +206,56 @@ func newCommunitiesTestMessenger(shh types.Waku, privateKey *ecdsa.PrivateKey, l LinkPreviewRequestEnabled: true, SendStatusUpdates: true, WalletRootAddress: types.HexToAddress("0x1122334455667788990011223344556677889900")} +} + +func newTestCommunitiesMessenger(s *suite.Suite, waku types.Waku, config testCommunitiesMessengerConfig) *Messenger { + err := config.complete() + s.Require().NoError(err) + + accountsManagerMock := &AccountManagerMock{} + accountsManagerMock.AccountsMap = make(map[string]string) + for _, walletAddress := range config.walletAddresses { + accountsManagerMock.AccountsMap[walletAddress] = types.EncodeHex(crypto.Keccak256([]byte(config.password))) + } + + tokenManagerMock := &TokenManagerMock{ + Balances: config.mockedBalances, + } options := []Option{ WithResendParams(3, 3), - WithAccountManager(accountsManager), - WithTokenManager(tokenManager), - WithCommunityTokensService(collectiblesService), - WithAppSettings(setting, config), + WithAccountManager(accountsManagerMock), + WithTokenManager(tokenManagerMock), + WithCommunityTokensService(config.collectiblesService), + WithAppSettings(*config.appSettings, *config.nodeConfig), } - m, err := newTestMessenger(shh, testMessengerConfig{ - privateKey: privateKey, - logger: logger, - }, options) - if err != nil { - return nil, err + messenger, err := newTestMessenger(waku, config.testMessengerConfig, options) + s.Require().NoError(err) + + currentDistributorObj, ok := messenger.communitiesKeyDistributor.(*CommunitiesKeyDistributorImpl) + s.Require().True(ok) + messenger.communitiesKeyDistributor = &TestCommunitiesKeyDistributor{ + CommunitiesKeyDistributorImpl: *currentDistributorObj, + subscriptions: map[chan *CommunityAndKeyActions]bool{}, + mutex: sync.RWMutex{}, } - _, err = m.Start() - if err != nil { - return nil, err + // add wallet account with keypair + for _, walletAddress := range config.walletAddresses { + kp := accounts.GetProfileKeypairForTest(false, true, false) + kp.Accounts[0].Address = types.HexToAddress(walletAddress) + err := messenger.settings.SaveOrUpdateKeypair(kp) + s.Require().NoError(err) } - return m, nil + walletAccounts, err := messenger.settings.GetActiveAccounts() + s.Require().NoError(err) + s.Require().Len(walletAccounts, len(config.walletAddresses)) + for i := range config.walletAddresses { + s.Require().Equal(walletAccounts[i].Type, accounts.AccountTypeGenerated) + } + return messenger } func createEncryptedCommunity(s *suite.Suite, owner *Messenger) (*communities.Community, *Chat) { diff --git a/protocol/communities_messenger_signers_test.go b/protocol/communities_messenger_signers_test.go index 9a1899ec8..bbea5e74c 100644 --- a/protocol/communities_messenger_signers_test.go +++ b/protocol/communities_messenger_signers_test.go @@ -16,7 +16,6 @@ import ( //utils "github.com/status-im/status-go/common" 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/common" "github.com/status-im/status-go/protocol/communities" @@ -90,24 +89,16 @@ func (s *MessengerCommunitiesSignersSuite) TearDownTest() { } func (s *MessengerCommunitiesSignersSuite) newMessenger(password string, walletAddresses []string) *Messenger { - privateKey, err := crypto.GenerateKey() - s.Require().NoError(err) + return newTestCommunitiesMessenger(&s.Suite, s.shh, testCommunitiesMessengerConfig{ + testMessengerConfig: testMessengerConfig{ + logger: s.logger, + }, + password: password, + walletAddresses: walletAddresses, + mockedBalances: &s.mockedBalances, + collectiblesService: s.collectiblesServiceMock, + }) - accountsManagerMock := &AccountManagerMock{} - accountsManagerMock.AccountsMap = make(map[string]string) - - for _, walletAddress := range walletAddresses { - accountsManagerMock.AccountsMap[walletAddress] = types.EncodeHex(crypto.Keccak256([]byte(password))) - } - - tokenManagerMock := &TokenManagerMock{ - Balances: &s.mockedBalances, - } - - messenger, err := newCommunitiesTestMessenger(s.shh, privateKey, s.logger, accountsManagerMock, tokenManagerMock, s.collectiblesServiceMock) - s.Require().NoError(err) - - return messenger } func (s *MessengerCommunitiesSignersSuite) createCommunity(controlNode *Messenger) *communities.Community { diff --git a/protocol/communities_messenger_test.go b/protocol/communities_messenger_test.go index 3fc424367..671a87a9f 100644 --- a/protocol/communities_messenger_test.go +++ b/protocol/communities_messenger_test.go @@ -82,10 +82,12 @@ func (s *MessengerCommunitiesSuite) TearDownTest() { } func (s *MessengerCommunitiesSuite) newMessengerWithKey(privateKey *ecdsa.PrivateKey) *Messenger { - messenger, err := newCommunitiesTestMessenger(s.shh, privateKey, s.logger, nil, nil, nil) - s.Require().NoError(err) - - return messenger + return newTestCommunitiesMessenger(&s.Suite, s.shh, testCommunitiesMessengerConfig{ + testMessengerConfig: testMessengerConfig{ + privateKey: privateKey, + logger: s.logger, + }, + }) } func (s *MessengerCommunitiesSuite) newMessenger() *Messenger { @@ -127,8 +129,6 @@ func (s *MessengerCommunitiesSuite) TestCreateCommunity_WithoutDefaultChannel() } func (s *MessengerCommunitiesSuite) TestRetrieveCommunity() { - alice := s.newMessenger() - description := &requests.CreateCommunity{ Membership: protobuf.CommunityPermissions_AUTO_ACCEPT, Name: "status", @@ -150,7 +150,7 @@ func (s *MessengerCommunitiesSuite) TestRetrieveCommunity() { s.Require().Equal(communitySettings.HistoryArchiveSupportEnabled, false) // Send a community message - chat := CreateOneToOneChat(common.PubkeyToHex(&alice.identity.PublicKey), &alice.identity.PublicKey, s.alice.transport) + chat := CreateOneToOneChat(common.PubkeyToHex(&s.alice.identity.PublicKey), &s.alice.identity.PublicKey, s.alice.transport) inputMessage := common.NewMessage() inputMessage.ChatId = chat.ID @@ -164,7 +164,7 @@ func (s *MessengerCommunitiesSuite) TestRetrieveCommunity() { // Pull message and make sure org is received err = tt.RetryWithBackOff(func() error { - response, err = alice.RetrieveAll() + response, err = s.alice.RetrieveAll() if err != nil { return err } @@ -175,7 +175,7 @@ func (s *MessengerCommunitiesSuite) TestRetrieveCommunity() { }) s.Require().NoError(err) - communities, err := alice.Communities() + communities, err := s.alice.Communities() s.Require().NoError(err) s.Require().Len(communities, 2) s.Require().Len(response.Communities(), 1) @@ -3452,15 +3452,13 @@ func (s *MessengerCommunitiesSuite) TestStartCommunityRekeyLoop() { } func (s *MessengerCommunitiesSuite) TestCommunityRekeyAfterBan() { - owner := s.newMessenger() + s.owner.communitiesManager.RekeyInterval = 500 * time.Minute - owner.communitiesManager.RekeyInterval = 500 * time.Minute - - _, err := owner.Start() + _, err := s.owner.Start() s.Require().NoError(err) // Create a new community - response, err := owner.CreateCommunity( + response, err := s.owner.CreateCommunity( &requests.CreateCommunity{ Membership: protobuf.CommunityPermissions_AUTO_ACCEPT, Name: "status", @@ -3474,12 +3472,12 @@ func (s *MessengerCommunitiesSuite) TestCommunityRekeyAfterBan() { s.Require().Len(response.Communities(), 1) // Check community is present in the DB and has default values we care about - c, err := owner.GetCommunityByID(response.Communities()[0].ID()) + c, err := s.owner.GetCommunityByID(response.Communities()[0].ID()) s.Require().NoError(err) s.Require().False(c.Encrypted()) // TODO some check that there are no keys for the community. Alt for s.Require().Zero(c.RekeyedAt().Unix()) - _, err = owner.CreateCommunityTokenPermission(&requests.CreateCommunityTokenPermission{ + _, err = s.owner.CreateCommunityTokenPermission(&requests.CreateCommunityTokenPermission{ CommunityID: c.ID(), Type: protobuf.CommunityTokenPermission_BECOME_MEMBER, TokenCriteria: []*protobuf.TokenCriteria{{ @@ -3493,20 +3491,20 @@ func (s *MessengerCommunitiesSuite) TestCommunityRekeyAfterBan() { }) s.Require().NoError(err) - c, err = owner.GetCommunityByID(c.ID()) + c, err = s.owner.GetCommunityByID(c.ID()) s.Require().NoError(err) s.Require().True(c.Encrypted()) - s.advertiseCommunityTo(c, owner, s.bob) - s.advertiseCommunityTo(c, owner, s.alice) + s.advertiseCommunityTo(c, s.owner, s.bob) + s.advertiseCommunityTo(c, s.owner, s.alice) - owner.communitiesManager.PermissionChecker = &testPermissionChecker{} + s.owner.communitiesManager.PermissionChecker = &testPermissionChecker{} - s.joinCommunity(c, owner, s.bob) - s.joinCommunity(c, owner, s.alice) + s.joinCommunity(c, s.owner, s.bob) + s.joinCommunity(c, s.owner, s.alice) // Check the Alice and Bob are members of the community - c, err = owner.GetCommunityByID(c.ID()) + c, err = s.owner.GetCommunityByID(c.ID()) s.Require().NoError(err) s.Require().True(c.HasMember(&s.alice.identity.PublicKey)) s.Require().True(c.HasMember(&s.bob.identity.PublicKey)) @@ -3525,7 +3523,7 @@ func (s *MessengerCommunitiesSuite) TestCommunityRekeyAfterBan() { ) s.Require().NoError(err) - response, err = owner.BanUserFromCommunity(context.Background(), &requests.BanUserFromCommunity{ + response, err = s.owner.BanUserFromCommunity(context.Background(), &requests.BanUserFromCommunity{ CommunityID: c.ID(), User: common.PubkeyToHexBytes(&s.bob.identity.PublicKey), }) @@ -3556,21 +3554,17 @@ func (s *MessengerCommunitiesSuite) TestCommunityRekeyAfterBan() { "alice hasn't received updated key", ) s.Require().NoError(err) - - s.Require().NoError(owner.Shutdown()) } func (s *MessengerCommunitiesSuite) TestCommunityRekeyAfterBanDisableCompatibility() { - owner := s.newMessenger() common.RekeyCompatibility = false + s.owner.communitiesManager.RekeyInterval = 500 * time.Minute - owner.communitiesManager.RekeyInterval = 500 * time.Minute - - _, err := owner.Start() + _, err := s.owner.Start() s.Require().NoError(err) // Create a new community - response, err := owner.CreateCommunity( + response, err := s.owner.CreateCommunity( &requests.CreateCommunity{ Membership: protobuf.CommunityPermissions_AUTO_ACCEPT, Name: "status", @@ -3584,12 +3578,12 @@ func (s *MessengerCommunitiesSuite) TestCommunityRekeyAfterBanDisableCompatibili s.Require().Len(response.Communities(), 1) // Check community is present in the DB and has default values we care about - c, err := owner.GetCommunityByID(response.Communities()[0].ID()) + c, err := s.owner.GetCommunityByID(response.Communities()[0].ID()) s.Require().NoError(err) s.Require().False(c.Encrypted()) // TODO some check that there are no keys for the community. Alt for s.Require().Zero(c.RekeyedAt().Unix()) - _, err = owner.CreateCommunityTokenPermission(&requests.CreateCommunityTokenPermission{ + _, err = s.owner.CreateCommunityTokenPermission(&requests.CreateCommunityTokenPermission{ CommunityID: c.ID(), Type: protobuf.CommunityTokenPermission_BECOME_MEMBER, TokenCriteria: []*protobuf.TokenCriteria{{ @@ -3603,20 +3597,20 @@ func (s *MessengerCommunitiesSuite) TestCommunityRekeyAfterBanDisableCompatibili }) s.Require().NoError(err) - c, err = owner.GetCommunityByID(c.ID()) + c, err = s.owner.GetCommunityByID(c.ID()) s.Require().NoError(err) s.Require().True(c.Encrypted()) - s.advertiseCommunityTo(c, owner, s.bob) - s.advertiseCommunityTo(c, owner, s.alice) + s.advertiseCommunityTo(c, s.owner, s.bob) + s.advertiseCommunityTo(c, s.owner, s.alice) - owner.communitiesManager.PermissionChecker = &testPermissionChecker{} + s.owner.communitiesManager.PermissionChecker = &testPermissionChecker{} - s.joinCommunity(c, owner, s.bob) - s.joinCommunity(c, owner, s.alice) + s.joinCommunity(c, s.owner, s.bob) + s.joinCommunity(c, s.owner, s.alice) // Check the Alice and Bob are members of the community - c, err = owner.GetCommunityByID(c.ID()) + c, err = s.owner.GetCommunityByID(c.ID()) s.Require().NoError(err) s.Require().True(c.HasMember(&s.alice.identity.PublicKey)) s.Require().True(c.HasMember(&s.bob.identity.PublicKey)) @@ -3635,7 +3629,7 @@ func (s *MessengerCommunitiesSuite) TestCommunityRekeyAfterBanDisableCompatibili ) s.Require().NoError(err) - response, err = owner.BanUserFromCommunity(context.Background(), &requests.BanUserFromCommunity{ + response, err = s.owner.BanUserFromCommunity(context.Background(), &requests.BanUserFromCommunity{ CommunityID: c.ID(), User: common.PubkeyToHexBytes(&s.bob.identity.PublicKey), }) @@ -3666,8 +3660,6 @@ func (s *MessengerCommunitiesSuite) TestCommunityRekeyAfterBanDisableCompatibili "alice hasn't received updated key", ) s.Require().NoError(err) - - s.Require().NoError(owner.Shutdown()) } func (s *MessengerCommunitiesSuite) TestRetrieveBigCommunity() { diff --git a/protocol/communities_messenger_token_permissions_test.go b/protocol/communities_messenger_token_permissions_test.go index 58f35edf4..f7d0a5b0d 100644 --- a/protocol/communities_messenger_token_permissions_test.go +++ b/protocol/communities_messenger_token_permissions_test.go @@ -187,7 +187,16 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TearDownTest() { } func (s *MessengerCommunitiesTokenPermissionsSuite) newMessenger(password string, walletAddresses []string, waku types.Waku, logger *zap.Logger) *Messenger { - return newMessenger(&s.Suite, waku, logger, password, walletAddresses, &s.mockedBalances, s.collectiblesServiceMock) + return newTestCommunitiesMessenger(&s.Suite, waku, testCommunitiesMessengerConfig{ + testMessengerConfig: testMessengerConfig{ + logger: logger, + }, + password: password, + walletAddresses: walletAddresses, + mockedBalances: &s.mockedBalances, + collectiblesService: s.collectiblesServiceMock, + }) + } func (s *MessengerCommunitiesTokenPermissionsSuite) joinCommunity(community *communities.Community, user *Messenger, password string, addresses []string) { diff --git a/protocol/messenger_offline_test.go b/protocol/messenger_offline_test.go index f626988ac..72ec88b86 100644 --- a/protocol/messenger_offline_test.go +++ b/protocol/messenger_offline_test.go @@ -10,7 +10,6 @@ import ( "go.uber.org/zap" 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/common" "github.com/status-im/status-go/protocol/communities" @@ -88,12 +87,11 @@ func (s *MessengerOfflineSuite) TearDownTest() { } func (s *MessengerOfflineSuite) newMessenger(waku types.Waku, logger *zap.Logger) *Messenger { - privateKey, err := crypto.GenerateKey() - s.Require().NoError(err) - - m, err := newCommunitiesTestMessenger(waku, privateKey, logger, nil, nil, nil) - s.Require().NoError(err) - return m + return newTestCommunitiesMessenger(&s.Suite, waku, testCommunitiesMessengerConfig{ + testMessengerConfig: testMessengerConfig{ + logger: s.logger, + }, + }) } func (s *MessengerOfflineSuite) advertiseCommunityTo(community *communities.Community, owner *Messenger, user *Messenger) { diff --git a/protocol/messenger_peersyncing_test.go b/protocol/messenger_peersyncing_test.go index b66550aca..755e3986c 100644 --- a/protocol/messenger_peersyncing_test.go +++ b/protocol/messenger_peersyncing_test.go @@ -2,7 +2,6 @@ package protocol import ( "context" - "crypto/ecdsa" "testing" "time" @@ -10,7 +9,6 @@ import ( "go.uber.org/zap" 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/common" "github.com/status-im/status-go/protocol/communities" @@ -71,18 +69,12 @@ func (s *MessengerPeersyncingSuite) TearDownTest() { _ = s.logger.Sync() } -func (s *MessengerPeersyncingSuite) newMessengerWithKey(privateKey *ecdsa.PrivateKey) *Messenger { - messenger, err := newCommunitiesTestMessenger(s.shh, privateKey, s.logger, nil, nil, nil) - s.Require().NoError(err) - - return messenger -} - func (s *MessengerPeersyncingSuite) newMessenger() *Messenger { - privateKey, err := crypto.GenerateKey() - s.Require().NoError(err) - - return s.newMessengerWithKey(privateKey) + return newTestCommunitiesMessenger(&s.Suite, s.shh, testCommunitiesMessengerConfig{ + testMessengerConfig: testMessengerConfig{ + logger: s.logger, + }, + }) } func (s *MessengerPeersyncingSuite) joinCommunity(community *communities.Community, owner *Messenger, user *Messenger) {