2021-05-23 13:34:17 +00:00
|
|
|
package communities
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *CommunitySuite) TestCreateCategory() {
|
|
|
|
newCategoryID := "new-category-id"
|
|
|
|
newCategoryName := "new-category-name"
|
|
|
|
|
|
|
|
org := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
org.config.PrivateKey = nil
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = nil
|
2021-05-23 13:34:17 +00:00
|
|
|
|
|
|
|
_, err := org.CreateCategory(newCategoryID, newCategoryName, []string{})
|
2023-08-16 12:54:55 +00:00
|
|
|
s.Require().Equal(ErrNotAuthorized, err)
|
2021-05-23 13:34:17 +00:00
|
|
|
|
|
|
|
org.config.PrivateKey = s.identity
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = &s.identity.PublicKey
|
2021-05-23 13:34:17 +00:00
|
|
|
|
|
|
|
changes, err := org.CreateCategory(newCategoryID, newCategoryName, []string{})
|
|
|
|
|
|
|
|
description := org.config.CommunityDescription
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(description.Categories)
|
|
|
|
s.Require().NotNil(description.Categories[newCategoryID])
|
|
|
|
s.Require().Equal(newCategoryName, description.Categories[newCategoryID].Name)
|
|
|
|
s.Require().Equal(newCategoryID, description.Categories[newCategoryID].CategoryId)
|
|
|
|
s.Require().Equal(int32(len(description.Categories)-1), description.Categories[newCategoryID].Position)
|
|
|
|
s.Require().NotNil(changes)
|
|
|
|
s.Require().NotNil(changes.CategoriesAdded[newCategoryID])
|
|
|
|
s.Require().Equal(description.Categories[newCategoryID], changes.CategoriesAdded[newCategoryID])
|
|
|
|
s.Require().Nil(changes.CategoriesModified[newCategoryID])
|
|
|
|
|
|
|
|
_, err = org.CreateCategory(newCategoryID, newCategoryName, []string{})
|
|
|
|
s.Require().Equal(ErrCategoryAlreadyExists, err)
|
|
|
|
|
|
|
|
newCategoryID2 := "new-category-id2"
|
|
|
|
newCategoryName2 := "new-category-name2"
|
|
|
|
|
|
|
|
changes, err = org.CreateCategory(newCategoryID2, newCategoryName2, []string{})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Equal(int32(len(description.Categories)-1), description.Categories[newCategoryID2].Position)
|
|
|
|
s.Require().NotNil(changes.CategoriesAdded[newCategoryID2])
|
|
|
|
s.Require().Nil(changes.CategoriesModified[newCategoryID2])
|
|
|
|
|
|
|
|
newCategoryID3 := "new-category-id3"
|
|
|
|
newCategoryName3 := "new-category-name3"
|
|
|
|
_, err = org.CreateCategory(newCategoryID3, newCategoryName3, []string{"some-chat-id"})
|
|
|
|
s.Require().Equal(ErrChatNotFound, err)
|
|
|
|
|
|
|
|
newChatID := "new-chat-id"
|
|
|
|
identity := &protobuf.ChatIdentity{
|
|
|
|
DisplayName: "new-chat-display-name",
|
|
|
|
Description: "new-chat-description",
|
|
|
|
}
|
|
|
|
permissions := &protobuf.CommunityPermissions{
|
2023-10-25 13:03:26 +00:00
|
|
|
Access: protobuf.CommunityPermissions_AUTO_ACCEPT,
|
2021-05-23 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
_, err = org.CreateChat(newChatID, &protobuf.CommunityChat{
|
2021-05-23 13:34:17 +00:00
|
|
|
Identity: identity,
|
|
|
|
Permissions: permissions,
|
|
|
|
})
|
2021-06-02 08:54:53 +00:00
|
|
|
s.Require().NoError(err)
|
2021-05-23 13:34:17 +00:00
|
|
|
|
|
|
|
changes, err = org.CreateCategory(newCategoryID3, newCategoryName3, []string{newChatID})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(changes.ChatsModified[newChatID])
|
|
|
|
s.Require().Equal(newCategoryID3, changes.ChatsModified[newChatID].CategoryModified)
|
|
|
|
|
|
|
|
newCategoryID4 := "new-category-id4"
|
|
|
|
newCategoryName4 := "new-category-name4"
|
|
|
|
|
|
|
|
_, err = org.CreateCategory(newCategoryID4, newCategoryName4, []string{newChatID})
|
|
|
|
s.Require().Equal(ErrChatAlreadyAssigned, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) TestEditCategory() {
|
|
|
|
newCategoryID := "new-category-id"
|
|
|
|
newCategoryName := "new-category-name"
|
|
|
|
editedCategoryName := "edited-category-name"
|
|
|
|
|
|
|
|
org := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
org.config.PrivateKey = s.identity
|
2021-06-02 08:54:53 +00:00
|
|
|
_, err := org.CreateCategory(newCategoryID, newCategoryName, []string{testChatID1})
|
|
|
|
s.Require().NoError(err)
|
2021-05-23 13:34:17 +00:00
|
|
|
org.config.PrivateKey = nil
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = nil
|
2021-05-23 13:34:17 +00:00
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
_, err = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1})
|
2023-08-16 12:54:55 +00:00
|
|
|
s.Require().Equal(ErrNotAuthorized, err)
|
2021-05-23 13:34:17 +00:00
|
|
|
|
|
|
|
org.config.PrivateKey = s.identity
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = &s.identity.PublicKey
|
2021-05-23 13:34:17 +00:00
|
|
|
|
|
|
|
_, err = org.EditCategory("some-random-category", editedCategoryName, []string{testChatID1})
|
|
|
|
s.Require().Equal(ErrCategoryNotFound, err)
|
|
|
|
|
|
|
|
changes, err := org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1})
|
|
|
|
|
|
|
|
description := org.config.CommunityDescription
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Equal(editedCategoryName, description.Categories[newCategoryID].Name)
|
|
|
|
s.Require().NotNil(changes)
|
|
|
|
s.Require().NotNil(changes.CategoriesModified[newCategoryID])
|
|
|
|
s.Require().Equal(description.Categories[newCategoryID], changes.CategoriesModified[newCategoryID])
|
|
|
|
s.Require().Nil(changes.CategoriesAdded[newCategoryID])
|
|
|
|
|
|
|
|
_, err = org.EditCategory(newCategoryID, editedCategoryName, []string{"some-random-chat-id"})
|
|
|
|
s.Require().Equal(ErrChatNotFound, err)
|
|
|
|
|
|
|
|
_, err = org.EditCategory(testCategoryID1, testCategoryName1, []string{testChatID1})
|
|
|
|
s.Require().Equal(ErrChatAlreadyAssigned, err)
|
|
|
|
|
|
|
|
// Edit by removing the chats
|
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
identity1 := &protobuf.ChatIdentity{
|
2021-05-23 13:34:17 +00:00
|
|
|
DisplayName: "new-chat-display-name",
|
|
|
|
Description: "new-chat-description",
|
|
|
|
}
|
|
|
|
permissions := &protobuf.CommunityPermissions{
|
2023-10-25 13:03:26 +00:00
|
|
|
Access: protobuf.CommunityPermissions_AUTO_ACCEPT,
|
2021-05-23 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testChatID2 := "test-chat-id-2"
|
|
|
|
testChatID3 := "test-chat-id-3"
|
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
_, err = org.CreateChat(testChatID2, &protobuf.CommunityChat{
|
|
|
|
Identity: identity1,
|
2021-05-23 13:34:17 +00:00
|
|
|
Permissions: permissions,
|
|
|
|
})
|
2021-06-02 08:54:53 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
identity2 := &protobuf.ChatIdentity{
|
|
|
|
DisplayName: "identity-2",
|
|
|
|
Description: "new-chat-description",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = org.CreateChat(testChatID3, &protobuf.CommunityChat{
|
|
|
|
Identity: identity2,
|
2021-05-23 13:34:17 +00:00
|
|
|
Permissions: permissions,
|
|
|
|
})
|
2021-06-02 08:54:53 +00:00
|
|
|
s.Require().NoError(err)
|
2021-05-23 13:34:17 +00:00
|
|
|
|
|
|
|
_, err = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1, testChatID2, testChatID3})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require().Equal(newCategoryID, description.Chats[testChatID1].CategoryId)
|
|
|
|
s.Require().Equal(newCategoryID, description.Chats[testChatID2].CategoryId)
|
|
|
|
s.Require().Equal(newCategoryID, description.Chats[testChatID3].CategoryId)
|
|
|
|
|
|
|
|
s.Require().Equal(int32(0), description.Chats[testChatID1].Position)
|
|
|
|
s.Require().Equal(int32(1), description.Chats[testChatID2].Position)
|
|
|
|
s.Require().Equal(int32(2), description.Chats[testChatID3].Position)
|
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
_, err = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1, testChatID3})
|
|
|
|
s.Require().NoError(err)
|
2021-05-23 13:34:17 +00:00
|
|
|
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)
|
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
_, err = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID3})
|
|
|
|
s.Require().NoError(err)
|
2021-05-23 13:34:17 +00:00
|
|
|
s.Require().Equal("", description.Chats[testChatID1].CategoryId)
|
|
|
|
s.Require().Equal(int32(0), description.Chats[testChatID3].Position)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) TestDeleteCategory() {
|
|
|
|
org := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
org.config.PrivateKey = s.identity
|
|
|
|
permissions := &protobuf.CommunityPermissions{
|
2023-10-25 13:03:26 +00:00
|
|
|
Access: protobuf.CommunityPermissions_AUTO_ACCEPT,
|
2021-05-23 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testChatID2 := "test-chat-id-2"
|
|
|
|
testChatID3 := "test-chat-id-3"
|
|
|
|
newCategoryID := "new-category-id"
|
|
|
|
newCategoryName := "new-category-name"
|
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
identity1 := &protobuf.ChatIdentity{
|
|
|
|
DisplayName: "display-name-2",
|
|
|
|
Description: "new-chat-description",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := org.CreateChat(testChatID2, &protobuf.CommunityChat{
|
|
|
|
Identity: identity1,
|
2021-05-23 13:34:17 +00:00
|
|
|
Permissions: permissions,
|
|
|
|
})
|
2021-06-02 08:54:53 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
identity2 := &protobuf.ChatIdentity{
|
|
|
|
DisplayName: "display-name-3",
|
|
|
|
Description: "new-chat-description",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = org.CreateChat(testChatID3, &protobuf.CommunityChat{
|
|
|
|
Identity: identity2,
|
2021-05-23 13:34:17 +00:00
|
|
|
Permissions: permissions,
|
|
|
|
})
|
2021-06-02 08:54:53 +00:00
|
|
|
s.Require().NoError(err)
|
2021-05-23 13:34:17 +00:00
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
_, err = org.CreateCategory(newCategoryID, newCategoryName, []string{})
|
|
|
|
s.Require().NoError(err)
|
2021-05-23 13:34:17 +00:00
|
|
|
|
|
|
|
description := org.config.CommunityDescription
|
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
_, err = org.EditCategory(newCategoryID, newCategoryName, []string{testChatID2, testChatID1})
|
|
|
|
s.Require().NoError(err)
|
2021-05-23 13:34:17 +00:00
|
|
|
s.Require().Equal(newCategoryID, description.Chats[testChatID1].CategoryId)
|
|
|
|
s.Require().Equal(newCategoryID, description.Chats[testChatID2].CategoryId)
|
|
|
|
|
|
|
|
s.Require().Equal(int32(0), description.Chats[testChatID3].Position)
|
|
|
|
s.Require().Equal(int32(0), description.Chats[testChatID2].Position)
|
|
|
|
s.Require().Equal(int32(1), description.Chats[testChatID1].Position)
|
|
|
|
|
|
|
|
org.config.PrivateKey = nil
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = nil
|
2021-06-02 08:54:53 +00:00
|
|
|
_, err = org.DeleteCategory(testCategoryID1)
|
2023-08-16 12:54:55 +00:00
|
|
|
s.Require().Equal(ErrNotAuthorized, err)
|
2021-05-23 13:34:17 +00:00
|
|
|
|
|
|
|
org.config.PrivateKey = s.identity
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = &s.identity.PublicKey
|
2021-05-23 13:34:17 +00:00
|
|
|
_, err = org.DeleteCategory("some-category-id")
|
|
|
|
s.Require().Equal(ErrCategoryNotFound, err)
|
|
|
|
|
|
|
|
changes, err := org.DeleteCategory(newCategoryID)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(changes)
|
|
|
|
|
|
|
|
s.Require().Equal("", description.Chats[testChatID1].CategoryId)
|
|
|
|
s.Require().Equal("", description.Chats[testChatID2].CategoryId)
|
|
|
|
s.Require().Equal("", description.Chats[testChatID3].CategoryId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) TestDeleteChatOrder() {
|
|
|
|
org := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
org.config.PrivateKey = s.identity
|
|
|
|
permissions := &protobuf.CommunityPermissions{
|
2023-10-25 13:03:26 +00:00
|
|
|
Access: protobuf.CommunityPermissions_AUTO_ACCEPT,
|
2021-05-23 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testChatID2 := "test-chat-id-2"
|
|
|
|
testChatID3 := "test-chat-id-3"
|
|
|
|
newCategoryID := "new-category-id"
|
|
|
|
newCategoryName := "new-category-name"
|
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
identity1 := &protobuf.ChatIdentity{
|
|
|
|
DisplayName: "identity-1",
|
|
|
|
Description: "new-chat-description",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := org.CreateChat(testChatID2, &protobuf.CommunityChat{
|
|
|
|
Identity: identity1,
|
2021-05-23 13:34:17 +00:00
|
|
|
Permissions: permissions,
|
|
|
|
})
|
2021-06-02 08:54:53 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
identity2 := &protobuf.ChatIdentity{
|
|
|
|
DisplayName: "identity-2",
|
|
|
|
Description: "new-chat-description",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = org.CreateChat(testChatID3, &protobuf.CommunityChat{
|
|
|
|
Identity: identity2,
|
2021-05-23 13:34:17 +00:00
|
|
|
Permissions: permissions,
|
|
|
|
})
|
2021-06-02 08:54:53 +00:00
|
|
|
s.Require().NoError(err)
|
2021-05-23 13:34:17 +00:00
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
_, err = org.CreateCategory(newCategoryID, newCategoryName, []string{testChatID1, testChatID2, testChatID3})
|
|
|
|
s.Require().NoError(err)
|
2021-05-23 13:34:17 +00:00
|
|
|
|
2023-06-14 14:15:46 +00:00
|
|
|
changes, err := org.DeleteChat(testChatID2)
|
2021-06-02 08:54:53 +00:00
|
|
|
s.Require().NoError(err)
|
2023-06-14 14:15:46 +00:00
|
|
|
s.Require().Equal(int32(0), org.Chats()[testChatID1].Position)
|
|
|
|
s.Require().Equal(int32(1), org.Chats()[testChatID3].Position)
|
|
|
|
s.Require().Len(changes.ChatsRemoved, 1)
|
2021-05-23 13:34:17 +00:00
|
|
|
|
2023-06-14 14:15:46 +00:00
|
|
|
changes, err = org.DeleteChat(testChatID1)
|
2021-06-02 08:54:53 +00:00
|
|
|
s.Require().NoError(err)
|
2023-06-14 14:15:46 +00:00
|
|
|
s.Require().Equal(int32(0), org.Chats()[testChatID3].Position)
|
|
|
|
s.Require().Len(changes.ChatsRemoved, 1)
|
2021-05-23 13:34:17 +00:00
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
_, err = org.DeleteChat(testChatID3)
|
2021-05-23 13:34:17 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|