github.com/status-im/status-go@v1.1.0/protocol/messenger_sync_customization_color_test.go (about)

     1  package protocol
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/suite"
     8  	"go.uber.org/zap"
     9  
    10  	"github.com/status-im/status-go/multiaccounts/common"
    11  
    12  	gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
    13  	"github.com/status-im/status-go/eth-node/crypto"
    14  	"github.com/status-im/status-go/eth-node/types"
    15  	"github.com/status-im/status-go/protocol/encryption/multidevice"
    16  	"github.com/status-im/status-go/protocol/tt"
    17  	"github.com/status-im/status-go/waku"
    18  )
    19  
    20  func TestMessengerAccountCustomizationColor(t *testing.T) {
    21  	suite.Run(t, new(MessengerSyncAccountCustomizationColorSuite))
    22  }
    23  
    24  type MessengerSyncAccountCustomizationColorSuite struct {
    25  	suite.Suite
    26  	alice  *Messenger
    27  	alice2 *Messenger
    28  	// If one wants to send messages between different instances of Messenger,
    29  	// a single Waku service should be shared.
    30  	shh    types.Waku
    31  	logger *zap.Logger
    32  }
    33  
    34  func (s *MessengerSyncAccountCustomizationColorSuite) SetupTest() {
    35  	s.logger = tt.MustCreateTestLogger()
    36  
    37  	config := waku.DefaultConfig
    38  	config.MinimumAcceptedPoW = 0
    39  	shh := waku.New(&config, s.logger)
    40  	s.shh = gethbridge.NewGethWakuWrapper(shh)
    41  	s.Require().NoError(shh.Start())
    42  
    43  	pk, err := crypto.GenerateKey()
    44  	s.Require().NoError(err)
    45  	s.alice, err = newMessengerWithKey(s.shh, pk, s.logger, nil)
    46  	s.Require().NoError(err)
    47  
    48  	s.alice2, err = newMessengerWithKey(s.shh, s.alice.identity, s.logger, nil)
    49  	s.Require().NoError(err)
    50  
    51  	prepareAliceMessengersForPairing(&s.Suite, s.alice, s.alice2)
    52  }
    53  
    54  func (s *MessengerSyncAccountCustomizationColorSuite) TearDownTest() {
    55  	TearDownMessenger(&s.Suite, s.alice)
    56  	TearDownMessenger(&s.Suite, s.alice2)
    57  	_ = s.logger.Sync()
    58  }
    59  
    60  func prepareAliceMessengersForPairing(s *suite.Suite, alice1, alice2 *Messenger) {
    61  	// Set Alice's installation metadata
    62  	aim := &multidevice.InstallationMetadata{
    63  		Name:       "alice's-device",
    64  		DeviceType: "alice's-device-type",
    65  	}
    66  	err := alice1.SetInstallationMetadata(alice1.installationID, aim)
    67  	s.Require().NoError(err)
    68  
    69  	// Set Alice 2's installation metadata
    70  	a2im := &multidevice.InstallationMetadata{
    71  		Name:       "alice's-other-device",
    72  		DeviceType: "alice's-other-device-type",
    73  	}
    74  	err = alice2.SetInstallationMetadata(alice2.installationID, a2im)
    75  	s.Require().NoError(err)
    76  }
    77  
    78  func (s *MessengerSyncAccountCustomizationColorSuite) TestSyncCustomizationColor() {
    79  	PairDevices(&s.Suite, s.alice2, s.alice)
    80  	PairDevices(&s.Suite, s.alice, s.alice2)
    81  
    82  	s.Require().Equal(s.alice.account.KeyUID, s.alice2.account.KeyUID)
    83  
    84  	err := s.alice.multiAccounts.SaveAccount(*s.alice.account)
    85  	s.Require().NoError(err)
    86  	err = s.alice2.multiAccounts.SaveAccount(*s.alice2.account)
    87  	s.Require().NoError(err)
    88  
    89  	// check that accounts have no customization color
    90  	acc, err := s.alice.multiAccounts.GetAccount(s.alice.account.KeyUID)
    91  	s.Require().NoError(err)
    92  	acc2, err := s.alice2.multiAccounts.GetAccount(s.alice2.account.KeyUID)
    93  	s.Require().NoError(err)
    94  	s.Require().Equal(acc.CustomizationColor, common.CustomizationColor(""))
    95  	s.Require().Equal(acc.CustomizationColorClock, uint64(0))
    96  	s.Require().Equal(acc2.CustomizationColor, common.CustomizationColor(""))
    97  	s.Require().Equal(acc2.CustomizationColorClock, uint64(0))
    98  
    99  	acc.CustomizationColor = common.CustomizationColorBlue
   100  	acc.CustomizationColorClock = 1
   101  	err = s.alice.syncAccountCustomizationColor(context.TODO(), acc)
   102  	s.Require().NoError(err)
   103  	_, err = WaitOnMessengerResponse(s.alice2, func(r *MessengerResponse) bool {
   104  		return len(r.CustomizationColor) > 0
   105  	}, "message syncAccountCustomizationColor not received")
   106  	s.Require().NoError(err)
   107  	acc2, err = s.alice2.multiAccounts.GetAccount(s.alice2.account.KeyUID)
   108  	s.Require().NoError(err)
   109  	s.Require().Equal(acc.CustomizationColor, acc2.CustomizationColor)
   110  }