chore(communities)_: cover ERC721 based permissions reevaluation with tests

This commit is contained in:
Patryk Osmaczko 2024-06-05 12:20:37 +02:00 committed by osmaczko
parent ec9e29ef92
commit 2d4ef8b2e0
7 changed files with 112 additions and 41 deletions

View File

@ -27,6 +27,8 @@ func (s *CommunityEventsEventualConsistencySuite) SetupTest() {
s.logger = tt.MustCreateTestLogger()
s.collectiblesServiceMock = &CollectiblesServiceMock{}
s.mockedBalances = createMockedWalletBalance(&s.Suite)
config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
shh := waku.New(&config, s.logger)
@ -48,7 +50,6 @@ func (s *CommunityEventsEventualConsistencySuite) SetupTest() {
_, err = s.alice.Start()
s.Require().NoError(err)
s.mockedBalances = createMockedWalletBalance(&s.Suite)
}
func (s *CommunityEventsEventualConsistencySuite) TearDownTest() {

View File

@ -61,6 +61,8 @@ func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) SetupTest() {
s.logger = tt.MustCreateTestLogger()
s.collectiblesServiceMock = &CollectiblesServiceMock{}
s.mockedBalances = createMockedWalletBalance(&s.Suite)
config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
shh := waku.New(&config, s.logger)
@ -76,8 +78,6 @@ func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) SetupTest() {
s.Require().NoError(err)
_, err = s.alice.Start()
s.Require().NoError(err)
s.mockedBalances = createMockedWalletBalance(&s.Suite)
}
func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) TearDownTest() {

View File

@ -61,6 +61,8 @@ func (s *TokenMasterCommunityEventsSuite) SetupTest() {
s.logger = tt.MustCreateTestLogger()
s.collectiblesServiceMock = &CollectiblesServiceMock{}
s.mockedBalances = createMockedWalletBalance(&s.Suite)
config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
shh := waku.New(&config, s.logger)
@ -77,7 +79,6 @@ func (s *TokenMasterCommunityEventsSuite) SetupTest() {
_, err = s.alice.Start()
s.Require().NoError(err)
s.mockedBalances = createMockedWalletBalance(&s.Suite)
}
func (s *TokenMasterCommunityEventsSuite) TearDownTest() {

View File

@ -67,6 +67,8 @@ func (s *AdminCommunityEventsSuiteBase) SetupTest() {
s.logger = tt.MustCreateTestLogger()
s.collectiblesServiceMock = &CollectiblesServiceMock{}
s.mockedBalances = createMockedWalletBalance(&s.Suite)
config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
shh := waku.New(&config, s.logger)
@ -82,8 +84,6 @@ func (s *AdminCommunityEventsSuiteBase) SetupTest() {
s.Require().NoError(err)
_, err = s.alice.Start()
s.Require().NoError(err)
s.mockedBalances = createMockedWalletBalance(&s.Suite)
}
func (s *AdminCommunityEventsSuiteBase) TearDownTest() {

View File

@ -5,6 +5,7 @@ import (
"crypto/ecdsa"
"encoding/json"
"errors"
"math/big"
"sync"
"time"
@ -24,6 +25,7 @@ import (
"github.com/status-im/status-go/protocol/communities/token"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/services/wallet/bigint"
walletCommon "github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/services/wallet/thirdparty"
walletToken "github.com/status-im/status-go/services/wallet/token"
@ -75,34 +77,84 @@ func (m *TokenManagerMock) FindOrCreateTokenByAddress(ctx context.Context, chain
}
type CollectiblesManagerMock struct {
response map[thirdparty.CollectibleUniqueID][]thirdparty.AccountBalance
Balances *map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big
collectibleOwnershipResponse map[string][]thirdparty.AccountBalance
}
func (m *CollectiblesManagerMock) FetchBalancesByOwnerAndContractAddress(ctx context.Context, chainID walletCommon.ChainID,
ownerAddress gethcommon.Address, contractAddresses []gethcommon.Address) (thirdparty.TokenBalancesPerContractAddress, error) {
return nil, errors.New("FetchBalancesByOwnerAndContractAddress is not implemented for testCollectiblesManager")
ret := make(thirdparty.TokenBalancesPerContractAddress)
accountsBalances, ok := (*m.Balances)[uint64(chainID)]
if !ok {
return ret, nil
}
balances, ok := accountsBalances[ownerAddress]
if !ok {
return ret, nil
}
for _, contractAddress := range contractAddresses {
balance, ok := balances[contractAddress]
if ok {
ret[contractAddress] = []thirdparty.TokenBalance{
{
TokenID: &bigint.BigInt{},
Balance: &bigint.BigInt{
Int: (*big.Int)(balance),
},
},
}
}
}
return ret, nil
}
func (m *CollectiblesManagerMock) GetCollectibleOwnership(requestedID thirdparty.CollectibleUniqueID) ([]thirdparty.AccountBalance, error) {
// NOTE: TokenID inside of thirdparty.CollectibleUniqueID is a pointer so m.response[id] is now working
for id, balances := range m.response {
if id.ContractID.Address == requestedID.ContractID.Address &&
id.ContractID.ChainID == requestedID.ContractID.ChainID {
for id, balances := range m.collectibleOwnershipResponse {
if id == requestedID.HashKey() {
return balances, nil
}
}
return []thirdparty.AccountBalance{}, nil
}
func (m *CollectiblesManagerMock) SetResponse(id thirdparty.CollectibleUniqueID, balances []thirdparty.AccountBalance) {
if m.response == nil {
m.response = map[thirdparty.CollectibleUniqueID][]thirdparty.AccountBalance{}
func (m *CollectiblesManagerMock) FetchCollectibleOwnersByContractAddress(ctx context.Context, chainID walletCommon.ChainID, contractAddress gethcommon.Address) (*thirdparty.CollectibleContractOwnership, error) {
ret := &thirdparty.CollectibleContractOwnership{
ContractAddress: contractAddress,
Owners: []thirdparty.CollectibleOwner{},
}
m.response[id] = balances
accountsBalances, ok := (*m.Balances)[uint64(chainID)]
if !ok {
return ret, nil
}
for wallet, collectiblesBalance := range accountsBalances {
balance, ok := collectiblesBalance[contractAddress]
if ok {
ret.Owners = append(ret.Owners, thirdparty.CollectibleOwner{
OwnerAddress: wallet,
TokenBalances: []thirdparty.TokenBalance{
{
TokenID: &bigint.BigInt{},
Balance: &bigint.BigInt{
Int: (*big.Int)(balance),
},
},
},
})
}
}
return ret, nil
}
func (m *CollectiblesManagerMock) FetchCollectibleOwnersByContractAddress(ctx context.Context, chainID walletCommon.ChainID, contractAddress gethcommon.Address) (*thirdparty.CollectibleContractOwnership, error) {
return nil, errors.New("FetchCollectibleOwnersByContractAddress is not implemented for CollectiblesManagerMock")
func (m *CollectiblesManagerMock) SetCollectibleOwnershipResponse(id thirdparty.CollectibleUniqueID, balances []thirdparty.AccountBalance) {
if m.collectibleOwnershipResponse == nil {
m.collectibleOwnershipResponse = map[string][]thirdparty.AccountBalance{}
}
m.collectibleOwnershipResponse[id.HashKey()] = balances
}
type CollectiblesServiceMock struct {
@ -260,7 +312,9 @@ func newTestCommunitiesMessenger(s *suite.Suite, waku types.Waku, config testCom
Balances: config.mockedBalances,
}
collectiblesManagerMock := &CollectiblesManagerMock{}
collectiblesManagerMock := &CollectiblesManagerMock{
Balances: config.mockedBalances,
}
options := []Option{
WithAccountManager(accountsManagerMock),

View File

@ -158,6 +158,8 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) SetupTest() {
s.bobWaku = nil
s.aliceWaku = nil
s.resetMockedBalances()
s.logger = tt.MustCreateTestLogger()
wakuNodes := CreateWakuV2Network(&s.Suite, s.logger, false, []string{"owner", "bob", "alice"})
@ -178,9 +180,6 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) SetupTest() {
s.Require().NoError(err)
_, err = s.alice.Start()
s.Require().NoError(err)
s.resetMockedBalances()
}
func (s *MessengerCommunitiesTokenPermissionsSuite) TearDownTest() {
@ -1602,15 +1601,15 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestMemberRoleGetUpdatedWhen
s.Require().Equal(msg.Text, response.Messages()[0].Text)
}
func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivilegedRoleInOpenCommunity(permissionType protobuf.CommunityTokenPermission_Type) {
func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivilegedRoleInOpenCommunity(permissionType protobuf.CommunityTokenPermission_Type, tokenType protobuf.CommunityTokenType) {
community, _ := s.createCommunity()
createTokenPermission := &requests.CreateCommunityTokenPermission{
CommunityID: community.ID(),
Type: permissionType,
TokenCriteria: []*protobuf.TokenCriteria{
&protobuf.TokenCriteria{
Type: protobuf.CommunityTokenType_ERC20,
{
Type: tokenType,
ContractAddresses: map[uint64]string{testChainID1: "0x123"},
Symbol: "TEST",
AmountInWei: "100000000000000000000",
@ -1705,23 +1704,31 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivileg
s.Require().False(checkRoleBasedOnThePermissionType(permissionType, &s.alice.identity.PublicKey, community))
}
func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberAdminRoleInOpenCommunity() {
s.testReevaluateMemberPrivilegedRoleInOpenCommunity(protobuf.CommunityTokenPermission_BECOME_ADMIN)
func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberAdminRoleInOpenCommunity_ERC20() {
s.testReevaluateMemberPrivilegedRoleInOpenCommunity(protobuf.CommunityTokenPermission_BECOME_ADMIN, protobuf.CommunityTokenType_ERC20)
}
func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberTokenMasterRoleInOpenCommunity() {
s.testReevaluateMemberPrivilegedRoleInOpenCommunity(protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER)
func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberAdminRoleInOpenCommunity_ERC721() {
s.testReevaluateMemberPrivilegedRoleInOpenCommunity(protobuf.CommunityTokenPermission_BECOME_ADMIN, protobuf.CommunityTokenType_ERC721)
}
func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivilegedRoleInClosedCommunity(permissionType protobuf.CommunityTokenPermission_Type) {
func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberTokenMasterRoleInOpenCommunity_ERC20() {
s.testReevaluateMemberPrivilegedRoleInOpenCommunity(protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER, protobuf.CommunityTokenType_ERC20)
}
func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberTokenMasterRoleInOpenCommunity_ERC721() {
s.testReevaluateMemberPrivilegedRoleInOpenCommunity(protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER, protobuf.CommunityTokenType_ERC721)
}
func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivilegedRoleInClosedCommunity(permissionType protobuf.CommunityTokenPermission_Type, tokenType protobuf.CommunityTokenType) {
community, _ := s.createCommunity()
createTokenPermission := &requests.CreateCommunityTokenPermission{
CommunityID: community.ID(),
Type: permissionType,
TokenCriteria: []*protobuf.TokenCriteria{
&protobuf.TokenCriteria{
Type: protobuf.CommunityTokenType_ERC20,
{
Type: tokenType,
ContractAddresses: map[uint64]string{testChainID1: "0x123"},
Symbol: "TEST",
AmountInWei: "100000000000000000000",
@ -1740,8 +1747,8 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivileg
CommunityID: community.ID(),
Type: protobuf.CommunityTokenPermission_BECOME_MEMBER,
TokenCriteria: []*protobuf.TokenCriteria{
&protobuf.TokenCriteria{
Type: protobuf.CommunityTokenType_ERC20,
{
Type: tokenType,
ContractAddresses: map[uint64]string{testChainID1: "0x124"},
Symbol: "TEST2",
AmountInWei: "100000000000000000000",
@ -1876,12 +1883,20 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivileg
s.Require().False(checkRoleBasedOnThePermissionType(permissionType, &s.alice.identity.PublicKey, community))
}
func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberAdminRoleInClosedCommunity() {
s.testReevaluateMemberPrivilegedRoleInClosedCommunity(protobuf.CommunityTokenPermission_BECOME_ADMIN)
func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberAdminRoleInClosedCommunity_ERC20() {
s.testReevaluateMemberPrivilegedRoleInClosedCommunity(protobuf.CommunityTokenPermission_BECOME_ADMIN, protobuf.CommunityTokenType_ERC20)
}
func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberTokenMasterRoleInClosedCommunity() {
s.testReevaluateMemberPrivilegedRoleInClosedCommunity(protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER)
func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberAdminRoleInClosedCommunity_ERC721() {
s.testReevaluateMemberPrivilegedRoleInClosedCommunity(protobuf.CommunityTokenPermission_BECOME_ADMIN, protobuf.CommunityTokenType_ERC721)
}
func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberTokenMasterRoleInClosedCommunity_ERC20() {
s.testReevaluateMemberPrivilegedRoleInClosedCommunity(protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER, protobuf.CommunityTokenType_ERC20)
}
func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberTokenMasterRoleInClosedCommunity_ERC721() {
s.testReevaluateMemberPrivilegedRoleInClosedCommunity(protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER, protobuf.CommunityTokenType_ERC721)
}
func checkRoleBasedOnThePermissionType(permissionType protobuf.CommunityTokenPermission_Type, member *ecdsa.PublicKey, community *communities.Community) bool {

View File

@ -187,7 +187,7 @@ func (s *TestMessengerProfileShowcase) TestSaveAndGetProfileShowcasePreferences(
TxTimestamp: 0,
},
}
s.collectiblesMock.SetResponse(collectibleID, balances)
s.collectiblesMock.SetCollectibleOwnershipResponse(collectibleID, balances)
err = s.m.SetProfileShowcasePreferences(request, false)
s.Require().NoError(err)
@ -258,7 +258,7 @@ func (s *TestMessengerProfileShowcase) TestFailToSaveProfileShowcasePreferencesW
TxTimestamp: 0,
},
}
s.collectiblesMock.SetResponse(collectibleID, balances)
s.collectiblesMock.SetCollectibleOwnershipResponse(collectibleID, balances)
err = s.m.SetProfileShowcasePreferences(request, false)
s.Require().Equal(errorAccountVisibilityLowerThanCollectible, err)
@ -441,7 +441,7 @@ func (s *TestMessengerProfileShowcase) TestShareShowcasePreferences() {
TxTimestamp: 32443424,
},
}
s.collectiblesMock.SetResponse(collectibleID, balances)
s.collectiblesMock.SetCollectibleOwnershipResponse(collectibleID, balances)
err = s.m.SetProfileShowcasePreferences(request, false)
s.Require().NoError(err)