github.com/status-im/status-go@v1.1.0/protocol/messenger_contact_update_test.go (about) 1 package protocol 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/suite" 8 9 "github.com/status-im/status-go/deprecation" 10 "github.com/status-im/status-go/eth-node/crypto" 11 "github.com/status-im/status-go/eth-node/types" 12 multiaccountscommon "github.com/status-im/status-go/multiaccounts/common" 13 "github.com/status-im/status-go/multiaccounts/settings" 14 "github.com/status-im/status-go/protocol/requests" 15 ) 16 17 func TestMessengerContactUpdateSuite(t *testing.T) { 18 suite.Run(t, new(MessengerContactUpdateSuite)) 19 } 20 21 type MessengerContactUpdateSuite struct { 22 MessengerBaseTestSuite 23 } 24 25 func (s *MessengerContactUpdateSuite) TestReceiveContactUpdate() { 26 theirName := "ens-name.stateofus.eth" 27 28 contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey)) 29 30 theirMessenger := s.newMessenger() 31 defer TearDownMessenger(&s.Suite, theirMessenger) 32 33 // Set ENS name 34 err := theirMessenger.settings.SaveSettingField(settings.PreferredName, theirName) 35 s.Require().NoError(err) 36 37 theirContactID := types.EncodeHex(crypto.FromECDSAPub(&theirMessenger.identity.PublicKey)) 38 39 response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: contactID}) 40 s.Require().NoError(err) 41 s.Require().NotNil(response) 42 43 s.Require().Len(response.Contacts, 1) 44 contact := response.Contacts[0] 45 // It should add the contact 46 s.Require().True(contact.added()) 47 48 if deprecation.ChatProfileDeprecated { 49 // It should a one to one chat 50 s.Require().Len(response.Chats(), 1) 51 s.Require().False(response.Chats()[0].Active) 52 } else { 53 // It should create a profile chat & a one to one chat 54 s.Require().Len(response.Chats(), 2) 55 chats := response.Chats() 56 if chats[0].ChatType == ChatTypeOneToOne { 57 s.Require().False(chats[0].Active) 58 } else { 59 s.Require().False(chats[1].Active) 60 } 61 } 62 63 //// It should a one to one chat 64 //s.Require().Len(response.Chats(), 1) 65 //s.Require().False(response.Chats()[0].Active) 66 67 // Wait for the message to reach its destination 68 response, err = WaitOnMessengerResponse( 69 s.m, 70 func(r *MessengerResponse) bool { return len(r.Contacts) > 0 }, 71 "contact request not received", 72 ) 73 s.Require().NoError(err) 74 75 receivedContact := response.Contacts[0] 76 s.Require().Equal(theirName, receivedContact.EnsName) 77 s.Require().False(receivedContact.ENSVerified) 78 s.Require().NotEmpty(receivedContact.LastUpdated) 79 s.Require().True(receivedContact.hasAddedUs()) 80 81 newPicture := "new-picture" 82 err = theirMessenger.SendContactUpdates(context.Background(), newEnsName, newPicture, multiaccountscommon.CustomizationColorRed) 83 s.Require().NoError(err) 84 85 // Wait for the message to reach its destination 86 response, err = WaitOnMessengerResponse( 87 s.m, 88 func(r *MessengerResponse) bool { 89 return len(r.Contacts) > 0 && response.Contacts[0].ID == theirContactID 90 }, 91 "contact request not received", 92 ) 93 94 s.Require().NoError(err) 95 96 receivedContact = response.Contacts[0] 97 s.Require().Equal(theirContactID, receivedContact.ID) 98 s.Require().Equal(newEnsName, receivedContact.EnsName) 99 s.Require().False(receivedContact.ENSVerified) 100 s.Require().Equal(receivedContact.CustomizationColor, multiaccountscommon.CustomizationColorRed) 101 s.Require().NotEmpty(receivedContact.LastUpdated) 102 } 103 104 func (s *MessengerContactUpdateSuite) TestAddContact() { 105 contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey)) 106 107 theirMessenger := s.newMessenger() 108 defer TearDownMessenger(&s.Suite, theirMessenger) 109 110 theirMessenger.account.CustomizationColor = multiaccountscommon.CustomizationColorSky 111 response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: contactID, CustomizationColor: string(multiaccountscommon.CustomizationColorRed)}) 112 s.Require().NoError(err) 113 s.Require().NotNil(response) 114 115 s.Require().Len(response.Contacts, 1) 116 contact := response.Contacts[0] 117 118 if deprecation.ChatProfileDeprecated { 119 // It adds the one to one chat 120 s.Require().Len(response.Chats(), 1) 121 } else { 122 // It adds the profile chat and the one to one chat 123 s.Require().Len(response.Chats(), 2) 124 } 125 126 // It should add the contact 127 s.Require().True(contact.added()) 128 s.Require().Equal(contact.CustomizationColor, multiaccountscommon.CustomizationColorRed) 129 130 // Wait for the message to reach its destination 131 response, err = WaitOnMessengerResponse( 132 s.m, 133 func(r *MessengerResponse) bool { return len(r.Contacts) > 0 }, 134 "contact request not received", 135 ) 136 s.Require().NoError(err) 137 138 receivedContact := response.Contacts[0] 139 s.Require().NotEmpty(receivedContact.LastUpdated) 140 s.Require().Equal(receivedContact.CustomizationColor, multiaccountscommon.CustomizationColorSky) 141 } 142 143 func (s *MessengerContactUpdateSuite) TestAddContactWithENS() { 144 contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey)) 145 ensName := "blah.stateofus.eth" 146 147 theirMessenger := s.newMessenger() 148 defer TearDownMessenger(&s.Suite, theirMessenger) 149 150 s.Require().NoError(theirMessenger.ENSVerified(contactID, ensName)) 151 152 response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: contactID}) 153 s.Require().NoError(err) 154 s.Require().NotNil(response) 155 s.Require().Len(response.Contacts, 1) 156 s.Require().Equal(ensName, response.Contacts[0].EnsName) 157 s.Require().True(response.Contacts[0].ENSVerified) 158 159 s.Require().Len(response.Contacts, 1) 160 contact := response.Contacts[0] 161 162 if deprecation.ChatProfileDeprecated { 163 // It adds the one to one chat 164 s.Require().Len(response.Chats(), 1) 165 } else { 166 // It adds the profile chat and the one to one chat 167 s.Require().Len(response.Chats(), 2) 168 } 169 170 // It should add the contact 171 s.Require().True(contact.added()) 172 173 // Wait for the message to reach its destination 174 response, err = WaitOnMessengerResponse( 175 s.m, 176 func(r *MessengerResponse) bool { return len(r.Contacts) > 0 }, 177 "contact request not received", 178 ) 179 s.Require().NoError(err) 180 181 receivedContact := response.Contacts[0] 182 s.Require().NotEmpty(receivedContact.LastUpdated) 183 }