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

     1  package protocol
     2  
     3  import (
     4  	_ "github.com/mutecomm/go-sqlcipher/v4" // require go-sqlcipher that overrides default implementation
     5  
     6  	"github.com/status-im/status-go/eth-node/crypto"
     7  	"github.com/status-im/status-go/eth-node/types"
     8  	"github.com/status-im/status-go/images"
     9  	"github.com/status-im/status-go/multiaccounts/settings"
    10  	"github.com/status-im/status-go/protocol/protobuf"
    11  )
    12  
    13  func isImageWithNamePresent(imgs map[string]*protobuf.IdentityImage, name string) bool {
    14  	for k, v := range imgs {
    15  		if k == name && len(v.Payload) > 0 {
    16  			return true
    17  		}
    18  	}
    19  
    20  	return false
    21  }
    22  
    23  func (s *MessengerSuite) retrieveIdentityImages(alice, bob *Messenger, chat *Chat) map[string]*protobuf.IdentityImage {
    24  	s.Require().NoError(alice.settings.SaveSettingField(settings.DisplayName, "alice"))
    25  
    26  	identityImages := images.SampleIdentityImages()
    27  	identityImagesMap := make(map[string]images.IdentityImage)
    28  	for _, img := range identityImages {
    29  		img.KeyUID = s.m.account.KeyUID
    30  		identityImagesMap[img.Name] = img
    31  	}
    32  
    33  	err := s.m.multiAccounts.StoreIdentityImages(s.m.account.KeyUID, identityImages, true)
    34  	s.Require().NoError(err)
    35  	s.Require().NoError(alice.SaveChat(chat))
    36  	s.Require().NoError(bob.settings.SaveSettingField(settings.DisplayName, "bob"))
    37  	s.Require().NoError(bob.SaveChat(chat))
    38  
    39  	chatContext := GetChatContextFromChatType(chat.ChatType)
    40  
    41  	chatIdentity, err := alice.createChatIdentity(chatContext)
    42  	s.Require().NoError(err)
    43  
    44  	imgs := chatIdentity.Images
    45  	s.Require().NoError(err)
    46  
    47  	return imgs
    48  }
    49  
    50  func (s *MessengerSuite) TestTwoImagesAreAddedToChatIdentityForPrivateChat() {
    51  	alice := s.m
    52  	bob := s.newMessenger()
    53  	defer TearDownMessenger(&s.Suite, bob)
    54  
    55  	bobPkString := types.EncodeHex(crypto.FromECDSAPub(&bob.identity.PublicKey))
    56  
    57  	chat := CreateOneToOneChat(bobPkString, &bob.identity.PublicKey, alice.transport)
    58  	s.Require().Equal(privateChat, GetChatContextFromChatType(chat.ChatType))
    59  
    60  	imgs := s.retrieveIdentityImages(alice, bob, chat)
    61  	s.Require().Len(imgs, 2)
    62  	s.Require().Equal(true, isImageWithNamePresent(imgs, "thumbnail"))
    63  	s.Require().Equal(true, isImageWithNamePresent(imgs, "large"))
    64  }
    65  
    66  func (s *MessengerSuite) TestOneImageIsAddedToChatIdentityForPublicChat() {
    67  	alice := s.m
    68  	bob := s.newMessenger()
    69  	defer TearDownMessenger(&s.Suite, bob)
    70  
    71  	chat := CreatePublicChat("alic-and-bob-chat", &testTimeSource{})
    72  	s.Require().Equal(publicChat, GetChatContextFromChatType(chat.ChatType))
    73  
    74  	imgs := s.retrieveIdentityImages(alice, bob, chat)
    75  	s.Require().Len(imgs, 1)
    76  	s.Require().Equal(true, isImageWithNamePresent(imgs, "thumbnail"))
    77  	s.Require().Equal(false, isImageWithNamePresent(imgs, "large"))
    78  }