2024-01-08 18:18:57 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-05-01 05:54:47 +00:00
|
|
|
"strings"
|
2024-03-08 13:48:22 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
2024-01-08 18:18:57 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
"github.com/status-im/status-go/server"
|
|
|
|
)
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
func TestMessengerPrepareMessage(t *testing.T) {
|
|
|
|
suite.Run(t, new(TestMessengerPrepareMessageSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestMessengerPrepareMessageSuite struct {
|
|
|
|
MessengerBaseTestSuite
|
|
|
|
chatID string
|
|
|
|
p *sqlitePersistence
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TestMessengerPrepareMessageSuite) SetupTest() {
|
|
|
|
s.MessengerBaseTestSuite.SetupTest()
|
|
|
|
s.chatID, s.p = s.setUpTestDatabase()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TestMessengerPrepareMessageSuite) setUpTestDatabase() (string, *sqlitePersistence) {
|
2024-01-08 18:18:57 +00:00
|
|
|
chat := CreatePublicChat("test-chat", s.m.transport)
|
|
|
|
err := s.m.SaveChat(chat)
|
|
|
|
s.NoError(err)
|
2024-03-08 13:48:22 +00:00
|
|
|
|
2024-01-08 18:18:57 +00:00
|
|
|
db, err := openTestDB()
|
|
|
|
s.NoError(err)
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
p := newSQLitePersistence(db)
|
2024-01-08 18:18:57 +00:00
|
|
|
return chat.ID, p
|
|
|
|
}
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
func (s *TestMessengerPrepareMessageSuite) generateTextMessage(ID string, From string, Clock uint64, responseTo string) *common.Message {
|
|
|
|
return &common.Message{
|
|
|
|
ID: ID,
|
|
|
|
From: From,
|
|
|
|
LocalChatID: s.chatID,
|
2024-01-08 18:18:57 +00:00
|
|
|
ChatMessage: &protobuf.ChatMessage{
|
2024-03-08 13:48:22 +00:00
|
|
|
Text: RandomLettersString(5),
|
|
|
|
Clock: Clock,
|
|
|
|
ResponseTo: responseTo,
|
2024-01-08 18:18:57 +00:00
|
|
|
},
|
|
|
|
}
|
2024-03-08 13:48:22 +00:00
|
|
|
}
|
2024-01-08 18:18:57 +00:00
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
func (s *TestMessengerPrepareMessageSuite) testMessageContainsImage(testAlbum bool) {
|
|
|
|
message1 := s.generateTextMessage("id-1", "1", 1, "")
|
|
|
|
message1.ContentType = protobuf.ChatMessage_IMAGE
|
|
|
|
message1.Payload = &protobuf.ChatMessage_Image{
|
|
|
|
Image: &protobuf.ImageMessage{
|
|
|
|
Format: 1,
|
|
|
|
Payload: RandomBytes(10),
|
|
|
|
},
|
2024-01-08 18:18:57 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
message2 := s.generateTextMessage("id-2", "2", 2, message1.ID)
|
2024-01-08 18:18:57 +00:00
|
|
|
messages := []*common.Message{message1, message2}
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
var message3 *common.Message
|
|
|
|
|
|
|
|
if testAlbum {
|
|
|
|
albumID := RandomLettersString(5)
|
|
|
|
message1.GetImage().AlbumId = albumID
|
|
|
|
|
|
|
|
message3 = s.generateTextMessage("id-3", "1", 0, "")
|
|
|
|
message3.ContentType = protobuf.ChatMessage_IMAGE
|
|
|
|
message3.Payload = &protobuf.ChatMessage_Image{
|
|
|
|
Image: &protobuf.ImageMessage{
|
|
|
|
Format: 1,
|
|
|
|
Payload: RandomBytes(10),
|
|
|
|
AlbumId: albumID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
messages = append(messages, message3)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.m.SaveMessages(messages)
|
2024-01-08 18:18:57 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
err = s.p.SaveMessages(messages)
|
2024-01-08 18:18:57 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
mediaServer, err := server.NewMediaServer(s.m.database, nil, nil, nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NoError(mediaServer.Start())
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
retrievedMessages, _, err := s.p.MessageByChatID(s.chatID, "", 10)
|
2024-01-08 18:18:57 +00:00
|
|
|
s.Require().NoError(err)
|
2024-03-08 13:48:22 +00:00
|
|
|
if testAlbum {
|
|
|
|
s.Require().Len(retrievedMessages, 3)
|
|
|
|
} else {
|
|
|
|
s.Require().Len(retrievedMessages, 2)
|
|
|
|
}
|
|
|
|
s.Require().Equal(message2.ID, retrievedMessages[0].ID)
|
|
|
|
s.Require().Equal(message1.ID, retrievedMessages[0].ResponseTo)
|
2024-01-08 18:18:57 +00:00
|
|
|
|
|
|
|
err = s.m.prepareMessage(retrievedMessages[0], mediaServer)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
mediaServerImageLink := func(messageID string) string {
|
|
|
|
return fmt.Sprintf(`https://Localhost:%d/messages/images?messageId=%s`,
|
|
|
|
mediaServer.GetPort(),
|
|
|
|
messageID)
|
|
|
|
}
|
2024-01-08 18:18:57 +00:00
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
if testAlbum {
|
|
|
|
expectedJSON := fmt.Sprintf(`["%s","%s"]`,
|
|
|
|
mediaServerImageLink(message1.ID),
|
|
|
|
mediaServerImageLink(message3.ID),
|
|
|
|
)
|
|
|
|
s.Require().Equal(json.RawMessage(expectedJSON), retrievedMessages[0].QuotedMessage.AlbumImages)
|
|
|
|
} else {
|
|
|
|
expectedURL := mediaServerImageLink(message1.ID)
|
|
|
|
s.Require().Equal(expectedURL, retrievedMessages[0].QuotedMessage.ImageLocalURL)
|
|
|
|
}
|
2024-01-08 18:18:57 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
func (s *TestMessengerPrepareMessageSuite) Test_WHEN_MessageContainsImage_THEN_preparedMessageAddsAlbumImageWithImageGeneratedLink() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
album bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "single image",
|
|
|
|
album: false,
|
2024-01-08 18:18:57 +00:00
|
|
|
},
|
2024-03-08 13:48:22 +00:00
|
|
|
{
|
|
|
|
name: "album",
|
|
|
|
album: true,
|
2024-01-08 18:18:57 +00:00
|
|
|
},
|
2024-03-08 13:48:22 +00:00
|
|
|
}
|
2024-01-08 18:18:57 +00:00
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.name, func() {
|
|
|
|
s.testMessageContainsImage(tc.album)
|
|
|
|
})
|
2024-01-08 18:18:57 +00:00
|
|
|
}
|
2024-03-08 13:48:22 +00:00
|
|
|
}
|
2024-01-08 18:18:57 +00:00
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
func (s *TestMessengerPrepareMessageSuite) Test_WHEN_NoQuotedMessage_THEN_RetrievedMessageDoesNotContainQuotedMessage() {
|
|
|
|
message1 := s.generateTextMessage("id-1", "1", 1, "")
|
|
|
|
message2 := s.generateTextMessage("id-2", "2", 2, "")
|
2024-01-08 18:18:57 +00:00
|
|
|
messages := []*common.Message{message1, message2}
|
|
|
|
|
|
|
|
err := s.m.SaveMessages([]*common.Message{message1, message2})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
err = s.p.SaveMessages(messages)
|
2024-01-08 18:18:57 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
mediaServer, err := server.NewMediaServer(s.m.database, nil, nil, nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NoError(mediaServer.Start())
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
retrievedMessages, _, err := s.p.MessageByChatID(s.chatID, "", 10)
|
2024-01-08 18:18:57 +00:00
|
|
|
s.Require().NoError(err)
|
2024-03-08 13:48:22 +00:00
|
|
|
s.Require().Equal(message2.ID, retrievedMessages[0].ID)
|
|
|
|
s.Require().Empty(retrievedMessages[0].ResponseTo)
|
2024-01-08 18:18:57 +00:00
|
|
|
|
|
|
|
err = s.m.prepareMessage(retrievedMessages[0], mediaServer)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require().Equal((*common.QuotedMessage)(nil), retrievedMessages[0].QuotedMessage)
|
|
|
|
}
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
func (s *TestMessengerPrepareMessageSuite) Test_WHEN_QuotedMessageDoesNotContainsImage_THEN_RetrievedMessageContainsNoImages() {
|
|
|
|
message1 := s.generateTextMessage("id-1", "1", 1, "")
|
|
|
|
message2 := s.generateTextMessage("id-2", "2", 2, message1.ID)
|
2024-01-08 18:18:57 +00:00
|
|
|
messages := []*common.Message{message1, message2}
|
|
|
|
|
|
|
|
err := s.m.SaveMessages([]*common.Message{message1, message2})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
err = s.p.SaveMessages(messages)
|
2024-01-08 18:18:57 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
mediaServer, err := server.NewMediaServer(s.m.database, nil, nil, nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NoError(mediaServer.Start())
|
|
|
|
|
2024-03-08 13:48:22 +00:00
|
|
|
retrievedMessages, _, err := s.p.MessageByChatID(s.chatID, "", 10)
|
2024-01-08 18:18:57 +00:00
|
|
|
s.Require().NoError(err)
|
2024-03-08 13:48:22 +00:00
|
|
|
s.Require().Equal(message2.ID, retrievedMessages[0].ID)
|
|
|
|
s.Require().Equal(message1.ID, retrievedMessages[0].ResponseTo)
|
2024-01-08 18:18:57 +00:00
|
|
|
|
|
|
|
err = s.m.prepareMessage(retrievedMessages[0], mediaServer)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require().Equal(json.RawMessage(nil), retrievedMessages[0].QuotedMessage.AlbumImages)
|
|
|
|
}
|
2024-05-01 05:54:47 +00:00
|
|
|
|
|
|
|
func (s *TestMessengerPrepareMessageSuite) Test_WHEN_MessageContainsImage_THEN_PreparedMessageByIDContainsLink() {
|
|
|
|
// GIVEN: message with image saved to DB
|
|
|
|
message := s.generateTextMessage("id-1", "1", 1, "")
|
|
|
|
message.ContentType = protobuf.ChatMessage_IMAGE
|
|
|
|
message.Payload = &protobuf.ChatMessage_Image{
|
|
|
|
Image: &protobuf.ImageMessage{
|
|
|
|
Format: 1,
|
|
|
|
Payload: RandomBytes(100),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.m.SaveMessages([]*common.Message{message})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
mediaServer, err := server.NewMediaServer(s.m.database, nil, nil, nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NoError(mediaServer.Start())
|
2024-07-01 18:52:57 +00:00
|
|
|
s.m.SetMediaServer(mediaServer)
|
2024-05-01 05:54:47 +00:00
|
|
|
|
|
|
|
// WHEN: message is prepared
|
|
|
|
loadedMessage, err := s.m.MessageByID(message.ID)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// THEN: message contains image link
|
|
|
|
s.Require().True(strings.HasPrefix(loadedMessage.ImageLocalURL, "https://Localhost"))
|
|
|
|
}
|