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

     1  package protocol
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/google/uuid"
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/status-im/status-go/multiaccounts/accounts"
    11  	"github.com/status-im/status-go/multiaccounts/settings"
    12  	"github.com/status-im/status-go/protocol/encryption/multidevice"
    13  	"github.com/status-im/status-go/protocol/requests"
    14  	"github.com/status-im/status-go/protocol/tt"
    15  )
    16  
    17  func TestMessengerPairingTest(t *testing.T) {
    18  	suite.Run(t, new(MessengerPairingSuite))
    19  }
    20  
    21  type MessengerPairingSuite struct {
    22  	MessengerBaseTestSuite
    23  }
    24  
    25  func (s *MessengerPairingSuite) TestEnableNonExistingInstallation() {
    26  	installationID := uuid.New().String()
    27  	_, err := s.m.EnableInstallationAndPair(&requests.EnableInstallationAndPair{InstallationID: installationID})
    28  	s.Require().NoError(err)
    29  
    30  	installations := s.m.Installations()
    31  	s.Require().NoError(err)
    32  
    33  	s.Require().Len(installations, 2)
    34  	var theirInstallation *multidevice.Installation
    35  	for _, i := range installations {
    36  		if i.ID == installationID {
    37  			theirInstallation = i
    38  			break
    39  		} else {
    40  			s.Require().NotNil(i.InstallationMetadata)
    41  		}
    42  	}
    43  	s.Require().NotNil(theirInstallation)
    44  	s.Require().True(theirInstallation.Enabled)
    45  
    46  	installationsFromDB, err := s.m.encryptor.GetOurActiveInstallations(&s.m.identity.PublicKey)
    47  	s.Require().NoError(err)
    48  	s.Require().Len(installationsFromDB, 2)
    49  	for _, i := range installationsFromDB {
    50  		s.Require().True(i.Enabled)
    51  		if i.ID == installationID {
    52  			theirInstallation = i
    53  			break
    54  		}
    55  	}
    56  	s.Require().NotNil(theirInstallation)
    57  	s.Require().True(theirInstallation.Enabled)
    58  
    59  }
    60  
    61  // TestMessengerPairAfterSeedPhrase tests the scenario where alice2 wants to sync with alice1
    62  // alice1 generated the connection string for bootstraping alice2
    63  // alice2 failed to connect to alice1 and restored from seed phrase
    64  // alice2 get the installationID1 from alice1 via parsing the connection string
    65  // alice2 should get the display name from alice1 after pairing
    66  func (s *MessengerPairingSuite) TestMessengerPairAfterSeedPhrase() {
    67  	alice1 := s.m
    68  	alice2, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil)
    69  	s.Require().NoError(err)
    70  	defer TearDownMessenger(&s.Suite, alice2)
    71  
    72  	alice1ProfileKp := accounts.GetProfileKeypairForTest(true, false, false)
    73  	alice1ProfileKp.KeyUID = alice1.account.KeyUID
    74  	alice1ProfileKp.Accounts[0].KeyUID = alice1.account.KeyUID
    75  	err = alice1.settings.SaveOrUpdateKeypair(alice1ProfileKp)
    76  	s.Require().NoError(err)
    77  
    78  	expectedDisplayName := "alice1"
    79  	s.Require().NoError(alice1.SetDisplayName(expectedDisplayName))
    80  	ss, err := alice2.getSettings()
    81  	s.Require().NoError(err)
    82  	s.Require().NotEqual(expectedDisplayName, ss.DisplayName)
    83  	installationID1 := alice1.installationID
    84  	installationID2 := alice2.installationID
    85  	s.Require().NotEqual(installationID1, installationID2)
    86  
    87  	_, err = alice2.EnableInstallationAndPair(&requests.EnableInstallationAndPair{InstallationID: installationID1})
    88  	s.Require().NoError(err)
    89  
    90  	// alice1 should get the installationID1 from alice2
    91  	_, err = WaitOnMessengerResponse(
    92  		alice1,
    93  		func(r *MessengerResponse) bool {
    94  			for _, i := range r.Installations() {
    95  				if i.ID == installationID2 {
    96  					return true
    97  				}
    98  			}
    99  			return false
   100  		},
   101  		"no messages",
   102  	)
   103  	s.Require().NoError(err)
   104  
   105  	s.Require().NoError(alice1.EnableAndSyncInstallation(&requests.EnableAndSyncInstallation{InstallationID: installationID2}))
   106  
   107  	// check if the display name is synced
   108  	err = tt.RetryWithBackOff(func() error {
   109  		r, err := alice2.RetrieveAll()
   110  		s.Require().NoError(err)
   111  		for _, ss := range r.Settings {
   112  			if ss.GetDBName() == settings.DisplayName.GetDBName() {
   113  				return nil
   114  			}
   115  		}
   116  		return errors.New("display name setting not received yet")
   117  	})
   118  	s.Require().NoError(err)
   119  	ss, err = alice2.getSettings()
   120  	s.Require().NoError(err)
   121  	s.Require().Equal(expectedDisplayName, ss.DisplayName)
   122  }