status-go/protocol/messenger_contact_update_te...

203 lines
6.0 KiB
Go
Raw Normal View History

package protocol
import (
"context"
"testing"
2020-01-15 11:36:49 +00:00
"github.com/stretchr/testify/suite"
"github.com/status-im/status-go/deprecation"
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"
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
func TestMessengerContactUpdateSuite(t *testing.T) {
suite.Run(t, new(MessengerContactUpdateSuite))
}
type MessengerContactUpdateSuite struct {
2023-07-13 10:28:34 +00:00
MessengerBaseTestSuite
}
func (s *MessengerContactUpdateSuite) SetupTest() {
s.logger = tt.MustCreateTestLogger()
config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
shh := waku.New(&config, s.logger)
s.shh = gethbridge.NewGethWakuWrapper(shh)
s.Require().NoError(shh.Start())
2023-07-13 10:44:09 +00:00
s.m = s.newMessenger()
s.privateKey = s.m.identity
_, err := s.m.Start()
s.Require().NoError(err)
2020-07-31 09:46:38 +00:00
}
func (s *MessengerContactUpdateSuite) TestReceiveContactUpdate() {
theirName := "ens-name.stateofus.eth"
contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
2023-07-13 10:44:09 +00:00
theirMessenger := s.newMessenger()
_, err := theirMessenger.Start()
s.Require().NoError(err)
defer theirMessenger.Shutdown() // nolint: errcheck
// Set ENS name
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
err = theirMessenger.settings.SaveSettingField(settings.PreferredName, theirName)
s.Require().NoError(err)
theirContactID := types.EncodeHex(crypto.FromECDSAPub(&theirMessenger.identity.PublicKey))
2022-10-14 08:50:36 +00:00
response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: contactID})
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Contacts, 1)
contact := response.Contacts[0]
// It should add the contact
2023-01-20 14:28:30 +00:00
s.Require().True(contact.added())
if deprecation.ChatProfileDeprecated {
// It should a one to one chat
s.Require().Len(response.Chats(), 1)
s.Require().False(response.Chats()[0].Active)
} else {
// It should create a profile chat & a one to one chat
s.Require().Len(response.Chats(), 2)
chats := response.Chats()
if chats[0].ChatType == ChatTypeOneToOne {
s.Require().False(chats[0].Active)
} else {
s.Require().False(chats[1].Active)
}
}
//// It should a one to one chat
//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,
func(r *MessengerResponse) bool { return len(r.Contacts) > 0 },
"contact request not received",
)
s.Require().NoError(err)
receivedContact := response.Contacts[0]
2022-02-17 15:13:10 +00:00
s.Require().Equal(theirName, receivedContact.EnsName)
s.Require().False(receivedContact.ENSVerified)
s.Require().NotEmpty(receivedContact.LastUpdated)
2023-01-20 14:28:30 +00:00
s.Require().True(receivedContact.hasAddedUs())
newPicture := "new-picture"
2022-02-17 15:13:10 +00:00
err = theirMessenger.SendContactUpdates(context.Background(), newEnsName, newPicture)
s.Require().NoError(err)
// Wait for the message to reach its destination
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool {
return len(r.Contacts) > 0 && response.Contacts[0].ID == theirContactID
},
"contact request not received",
)
s.Require().NoError(err)
receivedContact = response.Contacts[0]
s.Require().Equal(theirContactID, receivedContact.ID)
2022-02-17 15:13:10 +00:00
s.Require().Equal(newEnsName, receivedContact.EnsName)
s.Require().False(receivedContact.ENSVerified)
s.Require().NotEmpty(receivedContact.LastUpdated)
}
func (s *MessengerContactUpdateSuite) TestAddContact() {
contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
2023-07-13 10:44:09 +00:00
theirMessenger := s.newMessenger()
_, err := theirMessenger.Start()
s.Require().NoError(err)
defer theirMessenger.Shutdown() // nolint: errcheck
2022-10-14 08:50:36 +00:00
response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: contactID})
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Contacts, 1)
contact := response.Contacts[0]
if deprecation.ChatProfileDeprecated {
// It adds the one to one chat
s.Require().Len(response.Chats(), 1)
} else {
// It adds the profile chat and the one to one chat
s.Require().Len(response.Chats(), 2)
}
// It should add the contact
2023-01-20 14:28:30 +00:00
s.Require().True(contact.added())
// Wait for the message to reach its destination
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Contacts) > 0 },
"contact request not received",
)
s.Require().NoError(err)
receivedContact := response.Contacts[0]
s.Require().NotEmpty(receivedContact.LastUpdated)
}
func (s *MessengerContactUpdateSuite) TestAddContactWithENS() {
contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
ensName := "blah.stateofus.eth"
2023-07-13 10:44:09 +00:00
theirMessenger := s.newMessenger()
_, err := theirMessenger.Start()
s.Require().NoError(err)
defer theirMessenger.Shutdown() // nolint: errcheck
s.Require().NoError(theirMessenger.ENSVerified(contactID, ensName))
2022-10-14 08:50:36 +00:00
response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: contactID})
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Contacts, 1)
2022-02-17 15:13:10 +00:00
s.Require().Equal(ensName, response.Contacts[0].EnsName)
s.Require().True(response.Contacts[0].ENSVerified)
s.Require().Len(response.Contacts, 1)
contact := response.Contacts[0]
if deprecation.ChatProfileDeprecated {
// It adds the one to one chat
s.Require().Len(response.Chats(), 1)
} else {
// It adds the profile chat and the one to one chat
s.Require().Len(response.Chats(), 2)
}
// It should add the contact
2023-01-20 14:28:30 +00:00
s.Require().True(contact.added())
// Wait for the message to reach its destination
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Contacts) > 0 },
"contact request not received",
)
s.Require().NoError(err)
receivedContact := response.Contacts[0]
s.Require().NotEmpty(receivedContact.LastUpdated)
}