2019-12-02 15:34:05 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2021-11-19 14:32:04 +00:00
|
|
|
"context"
|
2019-12-02 15:34:05 +00:00
|
|
|
"testing"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2021-09-27 14:02:25 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2020-01-02 09:10:19 +00:00
|
|
|
v1protocol "github.com/status-im/status-go/protocol/v1"
|
2021-09-27 14:02:25 +00:00
|
|
|
localnotifications "github.com/status-im/status-go/services/local-notifications"
|
2019-12-02 15:34:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEventToSystemMessageSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(EventToSystemMessageSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type EventToSystemMessageSuite struct {
|
2023-11-27 17:01:13 +00:00
|
|
|
MessengerBaseTestSuite
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EventToSystemMessageSuite) TestRun() {
|
|
|
|
testCases := []struct {
|
|
|
|
Name string
|
|
|
|
Event v1protocol.MembershipUpdateEvent
|
|
|
|
Expected string
|
|
|
|
From string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "chat created event",
|
2022-07-12 07:48:58 +00:00
|
|
|
Event: v1protocol.NewChatCreatedEvent("chat-name", "#7cda00", 12),
|
2019-12-02 15:34:05 +00:00
|
|
|
From: "admin",
|
|
|
|
Expected: "@admin created the group chat-name",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "chat name changed event",
|
|
|
|
Event: v1protocol.NewNameChangedEvent("chat-name-2", 12),
|
|
|
|
From: "admin",
|
|
|
|
Expected: "@admin changed the group's name to chat-name-2",
|
|
|
|
},
|
2022-07-12 07:48:58 +00:00
|
|
|
{
|
|
|
|
Name: "chat color changed event",
|
|
|
|
Event: v1protocol.NewColorChangedEvent("#7cda00", 12),
|
|
|
|
From: "admin",
|
|
|
|
Expected: "@admin changed the group's color",
|
|
|
|
},
|
2022-08-08 15:22:22 +00:00
|
|
|
{
|
|
|
|
Name: "chat image changed event",
|
|
|
|
Event: v1protocol.NewImageChangedEvent([]byte{1, 2, 3}, 12),
|
|
|
|
From: "admin",
|
|
|
|
Expected: "@admin changed the group's image",
|
|
|
|
},
|
2019-12-02 15:34:05 +00:00
|
|
|
{
|
|
|
|
Name: "members added event",
|
|
|
|
Event: v1protocol.NewMembersAddedEvent([]string{"a", "b", "c"}, 12),
|
|
|
|
From: "admin",
|
2022-05-20 10:53:28 +00:00
|
|
|
Expected: "@admin has added @a, @b, @c",
|
2019-12-02 15:34:05 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "member joined event",
|
|
|
|
Event: v1protocol.NewMemberJoinedEvent(12),
|
|
|
|
From: "admin",
|
2022-05-20 10:53:28 +00:00
|
|
|
Expected: "", // joined events are deprecated
|
2019-12-02 15:34:05 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "admins added event",
|
|
|
|
Event: v1protocol.NewAdminsAddedEvent([]string{"a", "b", "c"}, 12),
|
|
|
|
From: "admin",
|
|
|
|
Expected: "@admin has made @a, @b, @c admin",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "member removed event",
|
|
|
|
Event: v1protocol.NewMemberRemovedEvent("a", 12),
|
|
|
|
From: "admin",
|
|
|
|
Expected: "@a left the group",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "admin removed event",
|
|
|
|
Event: v1protocol.NewAdminRemovedEvent("a", 12),
|
|
|
|
From: "admin",
|
|
|
|
Expected: "@a is not admin anymore",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.Name, func() {
|
|
|
|
tc.Event.From = tc.From
|
|
|
|
|
|
|
|
systemMessage := eventToSystemMessage(tc.Event, defaultSystemMessagesTranslations)
|
|
|
|
s.Equal(systemMessage.Text, tc.Expected)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-09-27 14:02:25 +00:00
|
|
|
|
|
|
|
func (s *EventToSystemMessageSuite) TestHandleMembershipUpdate() {
|
|
|
|
adminPrivateKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
adminPublicKey := types.EncodeHex(crypto.FromECDSAPub(&adminPrivateKey.PublicKey))
|
|
|
|
ourPublicKey := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
|
|
|
|
|
|
|
|
event1 := v1protocol.MembershipUpdateEvent{
|
|
|
|
Type: protobuf.MembershipUpdateEvent_CHAT_CREATED,
|
|
|
|
Name: "test",
|
|
|
|
ChatID: "test-" + adminPublicKey,
|
|
|
|
ClockValue: 100,
|
|
|
|
}
|
|
|
|
err = event1.Sign(adminPrivateKey)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
event2 := v1protocol.MembershipUpdateEvent{
|
|
|
|
Type: protobuf.MembershipUpdateEvent_MEMBERS_ADDED,
|
|
|
|
Members: []string{adminPublicKey, ourPublicKey},
|
|
|
|
ChatID: "test-" + adminPublicKey,
|
|
|
|
ClockValue: 100,
|
|
|
|
}
|
|
|
|
err = event2.Sign(adminPrivateKey)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
testMembershipUpdateMessageStruct2 := v1protocol.MembershipUpdateMessage{
|
|
|
|
ChatID: "test-" + adminPublicKey,
|
|
|
|
Events: []v1protocol.MembershipUpdateEvent{
|
|
|
|
event1,
|
|
|
|
event2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMembershipUpdateMessage2, err := testMembershipUpdateMessageStruct2.ToProtobuf()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
contact, err := BuildContactFromPublicKey(&adminPrivateKey.PublicKey)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
contact.ContactRequestLocalState = ContactRequestStateSent
|
2021-09-27 14:02:25 +00:00
|
|
|
|
|
|
|
currentMessageState := &CurrentMessageState{
|
|
|
|
Contact: contact,
|
|
|
|
}
|
|
|
|
|
|
|
|
state := &ReceivedMessageState{
|
|
|
|
Response: &MessengerResponse{},
|
|
|
|
Timesource: s.m.transport,
|
|
|
|
CurrentMessageState: currentMessageState,
|
|
|
|
ExistingMessagesMap: map[string]bool{},
|
|
|
|
AllChats: s.m.allChats,
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
err = s.m.HandleMembershipUpdate(state, nil, rawMembershipUpdateMessage2, defaultSystemMessagesTranslations)
|
2021-09-27 14:02:25 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(state.Response.Notifications(), 1)
|
|
|
|
s.Require().Equal(state.Response.Notifications()[0].Category, localnotifications.CategoryGroupInvite)
|
2021-11-19 14:32:04 +00:00
|
|
|
s.Require().Len(state.Response.Chats(), 1)
|
|
|
|
|
|
|
|
chat := state.Response.Chats()[0]
|
|
|
|
|
|
|
|
// Decline event, setting chat inactive
|
|
|
|
response, err := s.m.LeaveGroupChat(context.Background(), chat.ID, true)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
|
|
|
|
chat = response.Chats()[0]
|
|
|
|
|
|
|
|
// If the same response is handled, it should not show another notification & the chat should remain inactive
|
|
|
|
state.Response = &MessengerResponse{}
|
2023-08-18 11:39:59 +00:00
|
|
|
err = s.m.HandleMembershipUpdate(state, chat, rawMembershipUpdateMessage2, defaultSystemMessagesTranslations)
|
2021-11-19 14:32:04 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(state.Response.Notifications(), 0)
|
|
|
|
s.Require().Len(state.Response.Chats(), 1)
|
|
|
|
s.Require().False(state.Response.Chats()[0].Active)
|
2021-09-27 14:02:25 +00:00
|
|
|
}
|