github.com/status-im/status-go@v1.1.0/protocol/messenger_mute_test.go (about) 1 package protocol 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/suite" 8 "go.uber.org/zap" 9 10 "github.com/status-im/status-go/deprecation" 11 "github.com/status-im/status-go/eth-node/crypto" 12 "github.com/status-im/status-go/protocol/requests" 13 ) 14 15 func TestMessengerMuteSuite(t *testing.T) { 16 suite.Run(t, new(MessengerMuteSuite)) 17 } 18 19 type MessengerMuteSuite struct { 20 MessengerBaseTestSuite 21 } 22 23 func (s *MessengerMuteSuite) TestSetMute() { 24 key, err := crypto.GenerateKey() 25 s.Require().NoError(err) 26 27 theirMessenger, err := newMessengerWithKey(s.shh, key, s.logger, nil) 28 s.Require().NoError(err) 29 30 chatID := publicChatName 31 32 chat := CreatePublicChat(chatID, s.m.transport) 33 34 err = s.m.SaveChat(chat) 35 s.Require().NoError(err) 36 37 _, err = s.m.Join(chat) 38 s.Require().NoError(err) 39 40 err = theirMessenger.SaveChat(chat) 41 s.Require().NoError(err) 42 43 _, error := s.m.MuteChat(&requests.MuteChat{ChatID: chatID, MutedType: 5}) 44 s.NoError(error) 45 46 allChats := s.m.Chats() 47 s.Require().Len(allChats, deprecation.AddChatsCount(1)) 48 49 var actualChat *Chat 50 51 for idx := range allChats { 52 if chat.ID == allChats[idx].ID { 53 actualChat = allChats[idx] 54 } 55 } 56 57 s.Require().NotNil(actualChat) 58 s.Require().True(actualChat.Muted) 59 60 s.Require().NoError(s.m.UnmuteChat(chatID)) 61 62 allChats = s.m.Chats() 63 64 for idx := range allChats { 65 if chat.ID == allChats[idx].ID { 66 actualChat = allChats[idx] 67 } 68 } 69 70 s.Require().False(actualChat.Muted) 71 s.Require().NoError(theirMessenger.Shutdown()) 72 } 73 74 func (s *MessengerMuteSuite) TestSetMuteForDuration() { 75 key, err := crypto.GenerateKey() 76 mockTimeOneMinuteAgo := time.Now().Add(-time.Minute) 77 78 s.Require().NoError(err) 79 80 theirMessenger, err := newMessengerWithKey(s.shh, key, s.logger, nil) 81 s.Require().NoError(err) 82 83 chatID := publicChatName 84 85 chat := CreatePublicChat(chatID, s.m.transport) 86 87 err = s.m.SaveChat(chat) 88 s.Require().NoError(err) 89 90 _, err = s.m.Join(chat) 91 s.Require().NoError(err) 92 93 err = theirMessenger.SaveChat(chat) 94 s.Require().NoError(err) 95 96 allChats := s.m.Chats() 97 s.Require().Len(allChats, deprecation.AddChatsCount(1)) 98 99 var actualChat *Chat 100 101 for idx := range allChats { 102 if chat.ID == allChats[idx].ID { 103 actualChat = allChats[idx] 104 } 105 } 106 107 var contact *Contact 108 if actualChat.OneToOne() { 109 contact, _ = s.m.allContacts.Load(chatID) 110 } 111 112 _, error := s.m.muteChat(actualChat, contact, mockTimeOneMinuteAgo) 113 s.NoError(error) 114 // Mock Routine 115 for _, chat := range allChats { 116 chatMuteTill, chatMuteTillErr := time.Parse(time.RFC3339, chat.MuteTill.Format(time.RFC3339)) 117 currTime, currTimeErr := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) 118 119 if chatMuteTillErr != nil { 120 s.logger.Info("err", zap.Any("Couldn't parse muteTill", err)) 121 return 122 } 123 if currTimeErr != nil { 124 s.logger.Info("err", zap.Any("Couldn't parse current time", err)) 125 return 126 } 127 128 if currTime.After(chatMuteTill) && !chatMuteTill.Equal(time.Time{}) && chat.Muted { 129 _ = s.m.UnmuteChat(chat.ID) 130 } 131 } 132 133 s.Require().NotNil(actualChat) 134 s.Require().False(actualChat.Muted) 135 136 s.Require().NoError(theirMessenger.Shutdown()) 137 }