diff --git a/protocol/communities/community.go b/protocol/communities/community.go index d6bfc8d4b..d5674ccbe 100644 --- a/protocol/communities/community.go +++ b/protocol/communities/community.go @@ -262,6 +262,12 @@ func (o *Community) CreateChat(chatID string, chat *protobuf.CommunityChat) (*Co return nil, ErrChatAlreadyExists } + for _, c := range o.config.CommunityDescription.Chats { + if chat.Identity.DisplayName == c.Identity.DisplayName { + return nil, ErrInvalidCommunityDescriptionDuplicatedName + } + } + // Sets the chat position to be the last within its category chat.Position = 0 for _, c := range o.config.CommunityDescription.Chats { diff --git a/protocol/communities/community_categories_test.go b/protocol/communities/community_categories_test.go index 9fe02f38a..eebc2c81b 100644 --- a/protocol/communities/community_categories_test.go +++ b/protocol/communities/community_categories_test.go @@ -57,10 +57,11 @@ func (s *CommunitySuite) TestCreateCategory() { Access: protobuf.CommunityPermissions_NO_MEMBERSHIP, } - _, _ = org.CreateChat(newChatID, &protobuf.CommunityChat{ + _, err = org.CreateChat(newChatID, &protobuf.CommunityChat{ Identity: identity, Permissions: permissions, }) + s.Require().NoError(err) changes, err = org.CreateCategory(newCategoryID3, newCategoryName3, []string{newChatID}) s.Require().NoError(err) @@ -81,10 +82,11 @@ func (s *CommunitySuite) TestEditCategory() { org := s.buildCommunity(&s.identity.PublicKey) org.config.PrivateKey = s.identity - _, _ = org.CreateCategory(newCategoryID, newCategoryName, []string{testChatID1}) + _, err := org.CreateCategory(newCategoryID, newCategoryName, []string{testChatID1}) + s.Require().NoError(err) org.config.PrivateKey = nil - _, err := org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1}) + _, err = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1}) s.Require().Equal(ErrNotAdmin, err) org.config.PrivateKey = s.identity @@ -111,7 +113,7 @@ func (s *CommunitySuite) TestEditCategory() { // Edit by removing the chats - identity := &protobuf.ChatIdentity{ + identity1 := &protobuf.ChatIdentity{ DisplayName: "new-chat-display-name", Description: "new-chat-description", } @@ -122,14 +124,21 @@ func (s *CommunitySuite) TestEditCategory() { testChatID2 := "test-chat-id-2" testChatID3 := "test-chat-id-3" - _, _ = org.CreateChat(testChatID2, &protobuf.CommunityChat{ - Identity: identity, + _, err = org.CreateChat(testChatID2, &protobuf.CommunityChat{ + Identity: identity1, Permissions: permissions, }) - _, _ = org.CreateChat(testChatID3, &protobuf.CommunityChat{ - Identity: identity, + s.Require().NoError(err) + identity2 := &protobuf.ChatIdentity{ + DisplayName: "identity-2", + Description: "new-chat-description", + } + + _, err = org.CreateChat(testChatID3, &protobuf.CommunityChat{ + Identity: identity2, Permissions: permissions, }) + s.Require().NoError(err) _, err = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1, testChatID2, testChatID3}) s.Require().NoError(err) @@ -142,12 +151,14 @@ func (s *CommunitySuite) TestEditCategory() { s.Require().Equal(int32(1), description.Chats[testChatID2].Position) s.Require().Equal(int32(2), description.Chats[testChatID3].Position) - _, _ = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1, testChatID3}) + _, err = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1, testChatID3}) + s.Require().NoError(err) s.Require().Equal("", description.Chats[testChatID2].CategoryId) s.Require().Equal(int32(0), description.Chats[testChatID1].Position) s.Require().Equal(int32(1), description.Chats[testChatID3].Position) - _, _ = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID3}) + _, err = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID3}) + s.Require().NoError(err) s.Require().Equal("", description.Chats[testChatID1].CategoryId) s.Require().Equal(int32(0), description.Chats[testChatID3].Position) } @@ -155,10 +166,6 @@ func (s *CommunitySuite) TestEditCategory() { func (s *CommunitySuite) TestDeleteCategory() { org := s.buildCommunity(&s.identity.PublicKey) org.config.PrivateKey = s.identity - identity := &protobuf.ChatIdentity{ - DisplayName: "new-chat-display-name", - Description: "new-chat-description", - } permissions := &protobuf.CommunityPermissions{ Access: protobuf.CommunityPermissions_NO_MEMBERSHIP, } @@ -168,21 +175,35 @@ func (s *CommunitySuite) TestDeleteCategory() { newCategoryID := "new-category-id" newCategoryName := "new-category-name" - _, _ = org.CreateChat(testChatID2, &protobuf.CommunityChat{ - Identity: identity, - Permissions: permissions, - }) - _, _ = org.CreateChat(testChatID3, &protobuf.CommunityChat{ - Identity: identity, - Permissions: permissions, - }) + identity1 := &protobuf.ChatIdentity{ + DisplayName: "display-name-2", + Description: "new-chat-description", + } - _, _ = org.CreateCategory(newCategoryID, newCategoryName, []string{}) + _, err := org.CreateChat(testChatID2, &protobuf.CommunityChat{ + Identity: identity1, + Permissions: permissions, + }) + s.Require().NoError(err) + + identity2 := &protobuf.ChatIdentity{ + DisplayName: "display-name-3", + Description: "new-chat-description", + } + + _, err = org.CreateChat(testChatID3, &protobuf.CommunityChat{ + Identity: identity2, + Permissions: permissions, + }) + s.Require().NoError(err) + + _, err = org.CreateCategory(newCategoryID, newCategoryName, []string{}) + s.Require().NoError(err) description := org.config.CommunityDescription - _, _ = org.EditCategory(newCategoryID, newCategoryName, []string{testChatID2, testChatID1}) - + _, err = org.EditCategory(newCategoryID, newCategoryName, []string{testChatID2, testChatID1}) + s.Require().NoError(err) s.Require().Equal(newCategoryID, description.Chats[testChatID1].CategoryId) s.Require().Equal(newCategoryID, description.Chats[testChatID2].CategoryId) @@ -191,7 +212,7 @@ func (s *CommunitySuite) TestDeleteCategory() { s.Require().Equal(int32(1), description.Chats[testChatID1].Position) org.config.PrivateKey = nil - _, err := org.DeleteCategory(testCategoryID1) + _, err = org.DeleteCategory(testCategoryID1) s.Require().Equal(ErrNotAdmin, err) org.config.PrivateKey = s.identity @@ -210,10 +231,6 @@ func (s *CommunitySuite) TestDeleteCategory() { func (s *CommunitySuite) TestDeleteChatOrder() { org := s.buildCommunity(&s.identity.PublicKey) org.config.PrivateKey = s.identity - identity := &protobuf.ChatIdentity{ - DisplayName: "new-chat-display-name", - Description: "new-chat-description", - } permissions := &protobuf.CommunityPermissions{ Access: protobuf.CommunityPermissions_NO_MEMBERSHIP, } @@ -223,24 +240,39 @@ func (s *CommunitySuite) TestDeleteChatOrder() { newCategoryID := "new-category-id" newCategoryName := "new-category-name" - _, _ = org.CreateChat(testChatID2, &protobuf.CommunityChat{ - Identity: identity, + identity1 := &protobuf.ChatIdentity{ + DisplayName: "identity-1", + Description: "new-chat-description", + } + + _, err := org.CreateChat(testChatID2, &protobuf.CommunityChat{ + Identity: identity1, Permissions: permissions, }) - _, _ = org.CreateChat(testChatID3, &protobuf.CommunityChat{ - Identity: identity, + s.Require().NoError(err) + identity2 := &protobuf.ChatIdentity{ + DisplayName: "identity-2", + Description: "new-chat-description", + } + + _, err = org.CreateChat(testChatID3, &protobuf.CommunityChat{ + Identity: identity2, Permissions: permissions, }) + s.Require().NoError(err) - _, _ = org.CreateCategory(newCategoryID, newCategoryName, []string{testChatID1, testChatID2, testChatID3}) + _, err = org.CreateCategory(newCategoryID, newCategoryName, []string{testChatID1, testChatID2, testChatID3}) + s.Require().NoError(err) - description, _ := org.DeleteChat(testChatID2) + description, err := org.DeleteChat(testChatID2) + s.Require().NoError(err) s.Require().Equal(int32(0), description.Chats[testChatID1].Position) s.Require().Equal(int32(1), description.Chats[testChatID3].Position) - description, _ = org.DeleteChat(testChatID1) + description, err = org.DeleteChat(testChatID1) + s.Require().NoError(err) s.Require().Equal(int32(0), description.Chats[testChatID3].Position) - _, err := org.DeleteChat(testChatID3) + _, err = org.DeleteChat(testChatID3) s.Require().NoError(err) } diff --git a/protocol/communities/community_test.go b/protocol/communities/community_test.go index c2fb02725..10e8a411e 100644 --- a/protocol/communities/community_test.go +++ b/protocol/communities/community_test.go @@ -141,6 +141,15 @@ func (s *CommunitySuite) TestCreateChat() { s.Require().NotNil(changes) s.Require().NotNil(changes.ChatsAdded[newChatID]) + + // Add a community with the same name + + _, err = org.CreateChat("different-chat-id", &protobuf.CommunityChat{ + Identity: identity, + Permissions: permissions, + }) + + s.Require().Error(err) } func (s *CommunitySuite) TestEditChat() { @@ -665,7 +674,10 @@ func (s *CommunitySuite) TestHandleCommunityDescription() { changes: func(org *Community) *CommunityChanges { changes := org.emptyCommunityChanges() changes.MembersAdded[s.member3Key] = &protobuf.CommunityMember{} - changes.ChatsAdded[testChatID2] = &protobuf.CommunityChat{Permissions: &protobuf.CommunityPermissions{Access: protobuf.CommunityPermissions_INVITATION_ONLY}, Members: make(map[string]*protobuf.CommunityMember)} + changes.ChatsAdded[testChatID2] = &protobuf.CommunityChat{ + Identity: &protobuf.ChatIdentity{DisplayName: "added-chat", Description: "description"}, + Permissions: &protobuf.CommunityPermissions{Access: protobuf.CommunityPermissions_INVITATION_ONLY}, + Members: make(map[string]*protobuf.CommunityMember)} changes.ChatsAdded[testChatID2].Members[s.member3Key] = &protobuf.CommunityMember{} return changes @@ -873,6 +885,10 @@ func (s *CommunitySuite) buildCommunityDescription() *protobuf.CommunityDescript desc.Members[s.member2Key] = &protobuf.CommunityMember{} desc.Chats[testChatID1].Members = make(map[string]*protobuf.CommunityMember) desc.Chats[testChatID1].Members[s.member1Key] = &protobuf.CommunityMember{} + desc.Chats[testChatID1].Identity = &protobuf.ChatIdentity{ + DisplayName: "display-name", + Description: "description", + } return desc } @@ -952,7 +968,10 @@ func (s *CommunitySuite) addedChatCommunityDescription(org *Community) *protobuf description := proto.Clone(org.config.CommunityDescription).(*protobuf.CommunityDescription) description.Clock++ description.Members[s.member3Key] = &protobuf.CommunityMember{} - description.Chats[testChatID2] = &protobuf.CommunityChat{Permissions: &protobuf.CommunityPermissions{Access: protobuf.CommunityPermissions_INVITATION_ONLY}, Members: make(map[string]*protobuf.CommunityMember)} + description.Chats[testChatID2] = &protobuf.CommunityChat{ + Identity: &protobuf.ChatIdentity{DisplayName: "added-chat", Description: "description"}, + Permissions: &protobuf.CommunityPermissions{Access: protobuf.CommunityPermissions_INVITATION_ONLY}, + Members: make(map[string]*protobuf.CommunityMember)} description.Chats[testChatID2].Members[s.member3Key] = &protobuf.CommunityMember{} return description diff --git a/protocol/communities/errors.go b/protocol/communities/errors.go index 61d26b87c..e46ad4f6c 100644 --- a/protocol/communities/errors.go +++ b/protocol/communities/errors.go @@ -17,6 +17,8 @@ var ErrInvalidCommunityDescriptionUnknownOrgAccess = errors.New("invalid communi var ErrInvalidCommunityDescriptionMemberInChatButNotInOrg = errors.New("invalid community description member in chat but not in org") var ErrInvalidCommunityDescriptionCategoryNoID = errors.New("invalid community category id") var ErrInvalidCommunityDescriptionCategoryNoName = errors.New("invalid community category name") +var ErrInvalidCommunityDescriptionChatIdentity = errors.New("invalid community chat name, missing") +var ErrInvalidCommunityDescriptionDuplicatedName = errors.New("invalid community chat name, duplicated") var ErrInvalidCommunityDescriptionUnknownChatCategory = errors.New("invalid community category in chat") var ErrNotAdmin = errors.New("no admin privileges for this community") var ErrInvalidGrant = errors.New("invalid grant") diff --git a/protocol/communities/validator.go b/protocol/communities/validator.go index 77ba479bc..2d613774d 100644 --- a/protocol/communities/validator.go +++ b/protocol/communities/validator.go @@ -21,6 +21,10 @@ func validateCommunityChat(desc *protobuf.CommunityDescription, chat *protobuf.C } } + if chat.Identity == nil { + return ErrInvalidCommunityDescriptionChatIdentity + } + for pk := range chat.Members { if desc.Members == nil { return ErrInvalidCommunityDescriptionMemberInChatButNotInOrg