2020-01-10 18:59:01 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-01-15 11:36:49 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2023-08-03 14:16:11 +00:00
|
|
|
"github.com/status-im/status-go/deprecation"
|
2020-01-10 18:59:01 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2024-04-03 14:49:57 +00:00
|
|
|
multiaccountscommon "github.com/status-im/status-go/multiaccounts/common"
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
2021-10-26 10:48:02 +00:00
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
2020-01-10 18:59:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMessengerContactUpdateSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(MessengerContactUpdateSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessengerContactUpdateSuite struct {
|
2023-07-13 10:28:34 +00:00
|
|
|
MessengerBaseTestSuite
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerContactUpdateSuite) TestReceiveContactUpdate() {
|
|
|
|
theirName := "ens-name.stateofus.eth"
|
2021-10-22 14:20:42 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
|
|
|
|
|
2023-07-13 10:44:09 +00:00
|
|
|
theirMessenger := s.newMessenger()
|
2023-11-27 17:01:13 +00:00
|
|
|
defer TearDownMessenger(&s.Suite, theirMessenger)
|
2021-10-22 14:20:42 +00:00
|
|
|
|
|
|
|
// Set ENS name
|
2024-02-26 09:12:37 +00:00
|
|
|
err := theirMessenger.settings.SaveSettingField(settings.PreferredName, theirName)
|
2021-10-22 14:20:42 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
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})
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
|
|
|
|
s.Require().Len(response.Contacts, 1)
|
|
|
|
contact := response.Contacts[0]
|
2021-10-22 14:20:42 +00:00
|
|
|
// It should add the contact
|
2023-01-20 14:28:30 +00:00
|
|
|
s.Require().True(contact.added())
|
2021-10-22 14:20:42 +00:00
|
|
|
|
2023-08-03 14:16:11 +00:00
|
|
|
if deprecation.ChatProfileDeprecated {
|
|
|
|
// It should a one to one chat
|
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().False(response.Chats()[0].Active)
|
2021-10-22 14:20:42 +00:00
|
|
|
} else {
|
2023-08-03 14:16:11 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2021-10-22 14:20:42 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2023-08-03 14:16:11 +00:00
|
|
|
//// It should a one to one chat
|
|
|
|
//s.Require().Len(response.Chats(), 1)
|
|
|
|
//s.Require().False(response.Chats()[0].Active)
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
// Wait for the message to reach its destination
|
2020-04-22 12:58:28 +00:00
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.Contacts) > 0 },
|
|
|
|
"contact request not received",
|
|
|
|
)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
receivedContact := response.Contacts[0]
|
2022-02-17 15:13:10 +00:00
|
|
|
s.Require().Equal(theirName, receivedContact.EnsName)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().False(receivedContact.ENSVerified)
|
|
|
|
s.Require().NotEmpty(receivedContact.LastUpdated)
|
2023-01-20 14:28:30 +00:00
|
|
|
s.Require().True(receivedContact.hasAddedUs())
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
newPicture := "new-picture"
|
2024-04-03 14:49:57 +00:00
|
|
|
err = theirMessenger.SendContactUpdates(context.Background(), newEnsName, newPicture, multiaccountscommon.CustomizationColorRed)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Wait for the message to reach its destination
|
2020-04-22 12:58:28 +00:00
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool {
|
|
|
|
return len(r.Contacts) > 0 && response.Contacts[0].ID == theirContactID
|
|
|
|
},
|
|
|
|
"contact request not received",
|
|
|
|
)
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
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)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().False(receivedContact.ENSVerified)
|
2024-04-03 14:49:57 +00:00
|
|
|
s.Require().Equal(receivedContact.CustomizationColor, multiaccountscommon.CustomizationColorRed)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NotEmpty(receivedContact.LastUpdated)
|
|
|
|
}
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
func (s *MessengerContactUpdateSuite) TestAddContact() {
|
|
|
|
contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
|
|
|
|
|
2023-07-13 10:44:09 +00:00
|
|
|
theirMessenger := s.newMessenger()
|
2023-11-27 17:01:13 +00:00
|
|
|
defer TearDownMessenger(&s.Suite, theirMessenger)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
2024-04-03 14:49:57 +00:00
|
|
|
theirMessenger.account.CustomizationColor = multiaccountscommon.CustomizationColorSky
|
|
|
|
response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: contactID, CustomizationColor: string(multiaccountscommon.CustomizationColorRed)})
|
2020-12-22 10:49:25 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
|
|
|
|
s.Require().Len(response.Contacts, 1)
|
|
|
|
contact := response.Contacts[0]
|
|
|
|
|
2023-08-03 14:16:11 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
// It should add the contact
|
2023-01-20 14:28:30 +00:00
|
|
|
s.Require().True(contact.added())
|
2024-04-03 14:49:57 +00:00
|
|
|
s.Require().Equal(contact.CustomizationColor, multiaccountscommon.CustomizationColorRed)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
// 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)
|
2024-04-03 14:49:57 +00:00
|
|
|
s.Require().Equal(receivedContact.CustomizationColor, multiaccountscommon.CustomizationColorSky)
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
2021-10-26 10:48:02 +00:00
|
|
|
|
|
|
|
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()
|
2023-11-27 17:01:13 +00:00
|
|
|
defer TearDownMessenger(&s.Suite, theirMessenger)
|
2021-10-26 10:48:02 +00:00
|
|
|
|
|
|
|
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})
|
2021-10-26 10:48:02 +00:00
|
|
|
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)
|
2021-10-26 10:48:02 +00:00
|
|
|
s.Require().True(response.Contacts[0].ENSVerified)
|
|
|
|
|
|
|
|
s.Require().Len(response.Contacts, 1)
|
|
|
|
contact := response.Contacts[0]
|
|
|
|
|
2023-08-03 14:16:11 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-10-26 10:48:02 +00:00
|
|
|
|
|
|
|
// It should add the contact
|
2023-01-20 14:28:30 +00:00
|
|
|
s.Require().True(contact.added())
|
2021-10-26 10:48:02 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|