status-go/protocol/messenger_identity_display_...

252 lines
8.6 KiB
Go
Raw Permalink Normal View History

package protocol
import (
"context"
"errors"
"testing"
utils "github.com/status-im/status-go/common"
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/tt"
"github.com/stretchr/testify/suite"
)
const testDisplayName = "My New Display Name"
func TestMessengerProfileDisplayNameHandlerSuite(t *testing.T) {
suite.Run(t, new(MessengerProfileDisplayNameHandlerSuite))
}
type MessengerProfileDisplayNameHandlerSuite struct {
2023-07-13 10:28:34 +00:00
MessengerBaseTestSuite
}
func (s *MessengerProfileDisplayNameHandlerSuite) TestDisplayNameChange() {
// check display name for the created instance
displayName, err := s.m.settings.DisplayName()
s.Require().NoError(err)
s.Require().Equal(DefaultProfileDisplayName, displayName)
// add profile keypair
profileKp := accounts.GetProfileKeypairForTest(true, false, false)
profileKp.KeyUID = s.m.account.KeyUID
profileKp.Name = DefaultProfileDisplayName
profileKp.Accounts[0].KeyUID = s.m.account.KeyUID
err = s.m.settings.SaveOrUpdateKeypair(profileKp)
s.Require().NoError(err)
// check account is present in the db
dbProfileKp, err := s.m.settings.GetKeypairByKeyUID(profileKp.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairs(profileKp, dbProfileKp))
2024-02-01 15:43:41 +00:00
// save account will create the account
err = s.m.multiAccounts.SaveAccount(*s.m.account)
s.Require().NoError(err)
// set new display name
err = s.m.SetDisplayName(testDisplayName)
s.Require().NoError(err)
// check display name after change - mutliaccount
multiAcc, err := s.m.multiAccounts.GetAccount(s.m.account.KeyUID)
s.Require().NoError(err)
s.Require().Equal(testDisplayName, multiAcc.Name)
// check display name after change - settings
displayName, err = s.m.settings.DisplayName()
s.Require().NoError(err)
s.Require().Equal(testDisplayName, displayName)
// check display name after change - keypair
dbProfileKp, err = s.m.settings.GetKeypairByKeyUID(profileKp.KeyUID)
s.Require().NoError(err)
s.Require().Equal(testDisplayName, dbProfileKp.Name)
}
func (s *MessengerProfileDisplayNameHandlerSuite) TestDisplayNameSync() {
// check display name for the created instance
displayName, err := s.m.settings.DisplayName()
s.Require().NoError(err)
s.Require().Equal(DefaultProfileDisplayName, displayName)
// add profile keypair
profileKp := accounts.GetProfileKeypairForTest(true, true, false)
profileKp.KeyUID = s.m.account.KeyUID
profileKp.Name = DefaultProfileDisplayName
profileKp.Accounts[0].KeyUID = s.m.account.KeyUID
profileKp.Accounts[1].KeyUID = s.m.account.KeyUID
err = s.m.settings.SaveOrUpdateKeypair(profileKp)
s.Require().NoError(err, "profile keypair alicesDevice.settings.SaveOrUpdateKeypair")
// check account is present in the db
dbProfileKp, err := s.m.settings.GetKeypairByKeyUID(profileKp.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairs(profileKp, dbProfileKp))
// Create new device and add main account to
alicesOtherDevice, err := newMessengerWithKey(s.shh, s.m.identity, s.logger, nil)
s.Require().NoError(err)
// Store only chat and default wallet account on other device
profileKpOtherDevice := accounts.GetProfileKeypairForTest(true, true, false)
profileKpOtherDevice.KeyUID = s.m.account.KeyUID
profileKpOtherDevice.Name = DefaultProfileDisplayName
profileKpOtherDevice.Accounts[0].KeyUID = s.m.account.KeyUID
profileKpOtherDevice.Accounts[1].KeyUID = s.m.account.KeyUID
err = alicesOtherDevice.settings.SaveOrUpdateKeypair(profileKpOtherDevice)
s.Require().NoError(err, "profile keypair alicesOtherDevice.settings.SaveOrUpdateKeypair")
// Check account is present in the db
dbProfileKp2, err := alicesOtherDevice.settings.GetKeypairByKeyUID(profileKpOtherDevice.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairs(profileKpOtherDevice, dbProfileKp2))
// Pair devices
im1 := &multidevice.InstallationMetadata{
Name: "alice's-other-device",
DeviceType: "alice's-other-device-type",
}
err = alicesOtherDevice.SetInstallationMetadata(alicesOtherDevice.installationID, im1)
s.Require().NoError(err)
response, err := alicesOtherDevice.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)
s.Require().False(response.Chats()[0].Active)
// Wait for the message to reach its destination
response, err = WaitOnMessengerResponse(
s.m,
feat: fallback pairing seed (#5614) * feat(pairing)!: Add extra parameters and remove v2 compatibility This commit includes the following changes: I have added a flag to maintain 2.29 compatibility. Breaking change in connection string The local pairing code that was parsing the connection string had a few non-upgradable features: It was strictly checking the number of parameters, throwing an error if the number was different. This made it impossible to add parameters to it without breaking. It was strictly checking the version number. This made increasing the version number impossible as older client would just refuse to connect. The code has been changed so that: Two parameters have been added, installation-id and key-uid. Those are needed for the fallback flow. I have also removed version from the payload, since it wasn't used. This means that we don't support v1 anymore. V2 parsing is supported . Going forward there's a clear strategy on how to update the protocol (append parameters, don't change existing one). https://www.youtube.com/watch?v=oyLBGkS5ICk Is a must watch video for understanding the strategy Changed MessengerResponse to use internally a map of installations rather than an array (minor) Just moving towards maps as arrays tend to lead to subtle bugs. Moved pairing methods to messenger_pairing.go Just moved some methods Added 2 new methods for the fallback flow FinishPairingThroughSeedPhraseProcess https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R29 EnableAndSyncInstallation https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R18 Flow for clients Client A1 is logged in Client A2 is logged out Client A1 shows a QR code Client A2 scans a QR code If connection fails on A2, the user will be prompted to enter a seed phrase. If the generated account matches the key-uid from the QR code, A2 should call FinishPairingThroughSeedPhraseProcess with the installation id passed in the QR code. This will send installation information over waku. The user should be shown its own installation id and prompted to check the other device. Client A1 will receive new installation data through waku, if they are still on the qr code page, they should show a popup to the user showing the received installation id, and a way to Enable and Sync, which should call the EnableAndSyncInstallation endpoint. This should finish the fallback syncing flow. Current issues Currently I haven't tested that all the data is synced after finishing the flow. I see that the two devices are paired correctly, but for example the DisplayName is not changed on the receiving device. I haven't had time to look into it further. * test_: add more test for connection string parser * fix_: fix panic when parse old connection string * test_: add comments for TestMessengerPairAfterSeedPhrase * fix_: correct error description * feat_:rename FinishPairingThroughSeedPhraseProcess to EnableInstallationAndPair * fix_: delete leftover * fix_: add UniqueKey method * fix_: unify the response for InputConnectionStringForBootstrapping * fix_: remove fields installationID and keyUID in GethStatusBackend * fix_: rename messenger_pairing to messenger_pairing_and_syncing --------- Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2024-07-30 09:14:05 +00:00
func(r *MessengerResponse) bool { return len(r.Installations()) > 0 },
"installation not received",
)
s.Require().NoError(err)
feat: fallback pairing seed (#5614) * feat(pairing)!: Add extra parameters and remove v2 compatibility This commit includes the following changes: I have added a flag to maintain 2.29 compatibility. Breaking change in connection string The local pairing code that was parsing the connection string had a few non-upgradable features: It was strictly checking the number of parameters, throwing an error if the number was different. This made it impossible to add parameters to it without breaking. It was strictly checking the version number. This made increasing the version number impossible as older client would just refuse to connect. The code has been changed so that: Two parameters have been added, installation-id and key-uid. Those are needed for the fallback flow. I have also removed version from the payload, since it wasn't used. This means that we don't support v1 anymore. V2 parsing is supported . Going forward there's a clear strategy on how to update the protocol (append parameters, don't change existing one). https://www.youtube.com/watch?v=oyLBGkS5ICk Is a must watch video for understanding the strategy Changed MessengerResponse to use internally a map of installations rather than an array (minor) Just moving towards maps as arrays tend to lead to subtle bugs. Moved pairing methods to messenger_pairing.go Just moved some methods Added 2 new methods for the fallback flow FinishPairingThroughSeedPhraseProcess https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R29 EnableAndSyncInstallation https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R18 Flow for clients Client A1 is logged in Client A2 is logged out Client A1 shows a QR code Client A2 scans a QR code If connection fails on A2, the user will be prompted to enter a seed phrase. If the generated account matches the key-uid from the QR code, A2 should call FinishPairingThroughSeedPhraseProcess with the installation id passed in the QR code. This will send installation information over waku. The user should be shown its own installation id and prompted to check the other device. Client A1 will receive new installation data through waku, if they are still on the qr code page, they should show a popup to the user showing the received installation id, and a way to Enable and Sync, which should call the EnableAndSyncInstallation endpoint. This should finish the fallback syncing flow. Current issues Currently I haven't tested that all the data is synced after finishing the flow. I see that the two devices are paired correctly, but for example the DisplayName is not changed on the receiving device. I haven't had time to look into it further. * test_: add more test for connection string parser * fix_: fix panic when parse old connection string * test_: add comments for TestMessengerPairAfterSeedPhrase * fix_: correct error description * feat_:rename FinishPairingThroughSeedPhraseProcess to EnableInstallationAndPair * fix_: delete leftover * fix_: add UniqueKey method * fix_: unify the response for InputConnectionStringForBootstrapping * fix_: remove fields installationID and keyUID in GethStatusBackend * fix_: rename messenger_pairing to messenger_pairing_and_syncing --------- Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2024-07-30 09:14:05 +00:00
actualInstallation := response.Installations()[0]
s.Require().Equal(alicesOtherDevice.installationID, actualInstallation.ID)
s.Require().NotNil(actualInstallation.InstallationMetadata)
s.Require().Equal("alice's-other-device", actualInstallation.InstallationMetadata.Name)
s.Require().Equal("alice's-other-device-type", actualInstallation.InstallationMetadata.DeviceType)
_, err = s.m.EnableInstallation(alicesOtherDevice.installationID)
s.Require().NoError(err)
// Set new display name on alice's device
err = s.m.SetDisplayName(testDisplayName)
s.Require().NoError(err)
err = tt.RetryWithBackOff(func() error {
response, err := alicesOtherDevice.RetrieveAll()
if err != nil {
return err
}
foundKp := false
for _, kp := range response.Keypairs {
if kp.KeyUID == profileKp.KeyUID {
foundKp = true
}
}
foundSetting := false
for _, s := range response.Settings {
v, _ := s.Value.(string)
if v == testDisplayName {
foundSetting = true
}
}
if foundKp && foundSetting {
return nil
}
return errors.New("no sync data received")
})
s.Require().NoError(err)
// check display name after change - mutliaccount
multiAcc, err := alicesOtherDevice.multiAccounts.GetAccount(s.m.account.KeyUID)
s.Require().NoError(err)
s.Require().Equal(testDisplayName, multiAcc.Name)
// check display name after change - settings
displayName, err = alicesOtherDevice.settings.DisplayName()
s.Require().NoError(err)
s.Require().Equal(testDisplayName, displayName)
// check display name after change - keypair
dbProfileKp, err = alicesOtherDevice.settings.GetKeypairByKeyUID(profileKp.KeyUID)
s.Require().NoError(err)
s.Require().Equal(testDisplayName, dbProfileKp.Name)
}
func (s *MessengerProfileDisplayNameHandlerSuite) TestDisplayNameRestrictions() {
// check display name for the created instance
displayName, err := s.m.settings.DisplayName()
s.Require().NoError(err)
s.Require().Equal(DefaultProfileDisplayName, displayName)
// add profile keypair
profileKp := accounts.GetProfileKeypairForTest(true, false, false)
profileKp.KeyUID = s.m.account.KeyUID
profileKp.Name = DefaultProfileDisplayName
profileKp.Accounts[0].KeyUID = s.m.account.KeyUID
err = s.m.settings.SaveOrUpdateKeypair(profileKp)
s.Require().NoError(err)
setInvalidName := func(invalidName string, expectedErr error) {
err = s.m.SetDisplayName(invalidName)
s.Require().ErrorIs(err, expectedErr)
}
setInvalidName("dot.not", utils.ErrInvalidDisplayNameRegExp)
setInvalidName("t", utils.ErrInvalidDisplayNameRegExp)
setInvalidName("tt", utils.ErrInvalidDisplayNameRegExp)
setInvalidName("ttt", utils.ErrInvalidDisplayNameRegExp)
setInvalidName("tttt", utils.ErrInvalidDisplayNameRegExp)
setInvalidName("name is bigger than 24 symb", utils.ErrInvalidDisplayNameRegExp)
err = s.m.SetDisplayName("name with space")
s.Require().NoError(err)
displayName, err = s.m.settings.DisplayName()
s.Require().NoError(err)
s.Require().Equal("name with space", displayName)
}
func (s *MessengerProfileDisplayNameHandlerSuite) TestUpdateImageWhenEnsNameIsSet() {
err := s.m.settings.SaveSetting("preferred-name", "kounkou.stateofus.eth")
s.Require().NoError(err)
// check display name for the created instance
displayName, err := s.m.settings.GetPreferredUsername()
s.Require().NoError(err)
s.Require().Equal("kounkou.stateofus.eth", displayName)
// add profile keypair
profileKp := accounts.GetProfileKeypairForTest(true, false, false)
profileKp.KeyUID = s.m.account.KeyUID
profileKp.Name = DefaultProfileDisplayName
profileKp.Accounts[0].KeyUID = s.m.account.KeyUID
err = s.m.settings.SaveOrUpdateKeypair(profileKp)
s.Require().NoError(err)
// check account is present in the db
dbProfileKp, err := s.m.settings.GetKeypairByKeyUID(profileKp.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairs(profileKp, dbProfileKp))
// save account will create the account
err = s.m.multiAccounts.SaveAccount(*s.m.account)
s.Require().NoError(err)
// set new description
err = s.m.SetDisplayName("godfrain.stateofus.eth")
s.Require().NoError(err)
}