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

     1  package protocol
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/suite"
    10  
    11  	"github.com/status-im/status-go/eth-node/types"
    12  	"github.com/status-im/status-go/protocol/common"
    13  	"github.com/status-im/status-go/protocol/protobuf"
    14  	"github.com/status-im/status-go/protocol/requests"
    15  	// "github.com/status-im/status-go/protocol/requests"
    16  )
    17  
    18  func TestMessengerShareMessageSuite(t *testing.T) {
    19  	suite.Run(t, new(MessengerShareMessageSuite))
    20  }
    21  
    22  type MessengerShareMessageSuite struct {
    23  	MessengerBaseTestSuite
    24  }
    25  
    26  func buildImageMessage(s *MessengerShareMessageSuite, chat Chat) *common.Message {
    27  	file, err := os.Open("../_assets/tests/test.jpg")
    28  	s.Require().NoError(err)
    29  	defer file.Close()
    30  
    31  	payload, err := ioutil.ReadAll(file)
    32  	s.Require().NoError(err)
    33  
    34  	clock, timestamp := chat.NextClockAndTimestamp(&testTimeSource{})
    35  	message := common.NewMessage()
    36  	message.ChatId = chat.ID
    37  	message.Clock = clock
    38  	message.Timestamp = timestamp
    39  	message.WhisperTimestamp = clock
    40  	message.LocalChatID = chat.ID
    41  	message.MessageType = protobuf.MessageType_ONE_TO_ONE
    42  	message.ContentType = protobuf.ChatMessage_IMAGE
    43  	message.Text = "An image"
    44  
    45  	image := protobuf.ImageMessage{
    46  		Payload: payload,
    47  		Format:  protobuf.ImageFormat_JPEG,
    48  		AlbumId: "some-album-id",
    49  		Width:   1200,
    50  		Height:  1000,
    51  	}
    52  	message.Payload = &protobuf.ChatMessage_Image{Image: &image}
    53  	return message
    54  }
    55  
    56  func (s *MessengerShareMessageSuite) TestImageMessageSharing() {
    57  	theirMessenger := s.newMessenger()
    58  	defer TearDownMessenger(&s.Suite, theirMessenger)
    59  
    60  	theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
    61  	err := theirMessenger.SaveChat(theirChat)
    62  	s.Require().NoError(err)
    63  
    64  	ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport)
    65  	err = s.m.SaveChat(ourChat)
    66  	s.Require().NoError(err)
    67  
    68  	inputMessage := buildImageMessage(s, *ourChat)
    69  	err = s.m.SaveChat(ourChat)
    70  	s.NoError(err)
    71  	response, err := s.m.SendChatMessage(context.Background(), inputMessage)
    72  	s.NoError(err)
    73  	s.Require().Equal(1, len(response.Messages()), "it returns the message")
    74  
    75  	outputMessage := response.Messages()[0]
    76  
    77  	MessageID := outputMessage.ID
    78  
    79  	s.Require().NoError(err)
    80  	s.Require().Len(response.Messages(), 1)
    81  
    82  	response, err = WaitOnMessengerResponse(
    83  		theirMessenger,
    84  		func(r *MessengerResponse) bool { return len(r.messages) > 0 },
    85  		"no messages",
    86  	)
    87  
    88  	s.Require().NoError(err)
    89  	s.Require().Len(response.Chats(), 1)
    90  	s.Require().Len(response.Messages(), 1)
    91  	s.Require().Equal(response.Messages()[0].Text, "An image")
    92  
    93  	shareResponse, err := s.m.ShareImageMessage(
    94  		&requests.ShareImageMessage{
    95  			MessageID: MessageID,
    96  			Users:     []types.HexBytes{common.PubkeyToHexBytes(&theirMessenger.identity.PublicKey)},
    97  		},
    98  	)
    99  
   100  	s.NoError(err)
   101  	s.Require().NotNil(shareResponse)
   102  	s.Require().Len(shareResponse.Messages(), 1)
   103  
   104  	response, err = WaitOnMessengerResponse(
   105  		theirMessenger,
   106  		func(r *MessengerResponse) bool { return len(r.messages) > 0 },
   107  		"no messages",
   108  	)
   109  
   110  	s.Require().NoError(err)
   111  	s.Require().Len(response.Chats(), 1)
   112  	s.Require().Len(response.Messages(), 1)
   113  	s.Require().Equal(response.Messages()[0].Text, "This message has been shared with you")
   114  }