status-go/protocol/messenger_mute_test.go

138 lines
3.0 KiB
Go
Raw Normal View History

2020-06-26 07:46:14 +00:00
package protocol
import (
"testing"
2023-04-16 15:06:00 +00:00
"time"
2020-06-26 07:46:14 +00:00
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
"github.com/status-im/status-go/deprecation"
2020-06-26 07:46:14 +00:00
"github.com/status-im/status-go/eth-node/crypto"
2023-04-16 15:06:00 +00:00
"github.com/status-im/status-go/protocol/requests"
2020-06-26 07:46:14 +00:00
)
func TestMessengerMuteSuite(t *testing.T) {
suite.Run(t, new(MessengerMuteSuite))
}
type MessengerMuteSuite struct {
2023-07-13 10:28:34 +00:00
MessengerBaseTestSuite
2020-06-26 07:46:14 +00:00
}
func (s *MessengerMuteSuite) TestSetMute() {
key, err := crypto.GenerateKey()
s.Require().NoError(err)
2020-12-21 11:57:47 +00:00
theirMessenger, err := newMessengerWithKey(s.shh, key, s.logger, nil)
s.Require().NoError(err)
2020-06-26 07:46:14 +00:00
2023-04-16 15:06:00 +00:00
chatID := publicChatName
2020-06-26 07:46:14 +00:00
chat := CreatePublicChat(chatID, s.m.transport)
2021-01-11 10:32:51 +00:00
err = s.m.SaveChat(chat)
2020-06-26 07:46:14 +00:00
s.Require().NoError(err)
2021-01-11 10:32:51 +00:00
_, err = s.m.Join(chat)
2020-06-26 07:46:14 +00:00
s.Require().NoError(err)
2021-01-11 10:32:51 +00:00
err = theirMessenger.SaveChat(chat)
2020-06-26 07:46:14 +00:00
s.Require().NoError(err)
2023-04-16 15:06:00 +00:00
_, error := s.m.MuteChat(&requests.MuteChat{ChatID: chatID, MutedType: 5})
s.NoError(error)
2020-06-26 07:46:14 +00:00
2021-05-14 10:55:42 +00:00
allChats := s.m.Chats()
s.Require().Len(allChats, deprecation.AddChatsCount(1))
2021-05-14 10:55:42 +00:00
var actualChat *Chat
for idx := range allChats {
if chat.ID == allChats[idx].ID {
actualChat = allChats[idx]
}
}
s.Require().NotNil(actualChat)
s.Require().True(actualChat.Muted)
2020-06-26 07:46:14 +00:00
s.Require().NoError(s.m.UnmuteChat(chatID))
2021-05-14 10:55:42 +00:00
allChats = s.m.Chats()
for idx := range allChats {
if chat.ID == allChats[idx].ID {
actualChat = allChats[idx]
}
}
s.Require().False(actualChat.Muted)
2020-07-31 09:46:38 +00:00
s.Require().NoError(theirMessenger.Shutdown())
2020-06-26 07:46:14 +00:00
}
2023-04-16 15:06:00 +00:00
func (s *MessengerMuteSuite) TestSetMuteForDuration() {
key, err := crypto.GenerateKey()
mockTimeOneMinuteAgo := time.Now().Add(-time.Minute)
s.Require().NoError(err)
theirMessenger, err := newMessengerWithKey(s.shh, key, s.logger, nil)
s.Require().NoError(err)
chatID := publicChatName
chat := CreatePublicChat(chatID, s.m.transport)
err = s.m.SaveChat(chat)
s.Require().NoError(err)
_, err = s.m.Join(chat)
s.Require().NoError(err)
err = theirMessenger.SaveChat(chat)
s.Require().NoError(err)
allChats := s.m.Chats()
s.Require().Len(allChats, deprecation.AddChatsCount(1))
2023-04-16 15:06:00 +00:00
var actualChat *Chat
for idx := range allChats {
if chat.ID == allChats[idx].ID {
actualChat = allChats[idx]
}
}
var contact *Contact
if actualChat.OneToOne() {
contact, _ = s.m.allContacts.Load(chatID)
}
_, error := s.m.muteChat(actualChat, contact, mockTimeOneMinuteAgo)
s.NoError(error)
// Mock Routine
for _, chat := range allChats {
chatMuteTill, chatMuteTillErr := time.Parse(time.RFC3339, chat.MuteTill.Format(time.RFC3339))
currTime, currTimeErr := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
if chatMuteTillErr != nil {
s.logger.Info("err", zap.Any("Couldn't parse muteTill", err))
return
}
if currTimeErr != nil {
s.logger.Info("err", zap.Any("Couldn't parse current time", err))
return
}
if currTime.After(chatMuteTill) && !chatMuteTill.Equal(time.Time{}) && chat.Muted {
_ = s.m.UnmuteChat(chat.ID)
}
}
s.Require().NotNil(actualChat)
s.Require().False(actualChat.Muted)
s.Require().NoError(theirMessenger.Shutdown())
}