github.com/status-im/status-go@v1.1.0/protocol/communities/community_categories_test.go (about) 1 package communities 2 3 import ( 4 "github.com/status-im/status-go/protocol/protobuf" 5 ) 6 7 func (s *CommunitySuite) TestCreateCategory() { 8 newCategoryID := "new-category-id" 9 newCategoryName := "new-category-name" 10 11 org := s.buildCommunity(&s.identity.PublicKey) 12 org.config.PrivateKey = nil 13 org.config.ID = nil 14 15 _, err := org.CreateCategory(newCategoryID, newCategoryName, []string{}) 16 s.Require().Equal(ErrNotAuthorized, err) 17 18 org.config.PrivateKey = s.identity 19 org.config.ID = &s.identity.PublicKey 20 21 changes, err := org.CreateCategory(newCategoryID, newCategoryName, []string{}) 22 23 description := org.config.CommunityDescription 24 25 s.Require().NoError(err) 26 s.Require().NotNil(description.Categories) 27 s.Require().NotNil(description.Categories[newCategoryID]) 28 s.Require().Equal(newCategoryName, description.Categories[newCategoryID].Name) 29 s.Require().Equal(newCategoryID, description.Categories[newCategoryID].CategoryId) 30 s.Require().Equal(int32(len(description.Categories)-1), description.Categories[newCategoryID].Position) 31 s.Require().NotNil(changes) 32 s.Require().NotNil(changes.CategoriesAdded[newCategoryID]) 33 s.Require().Equal(description.Categories[newCategoryID], changes.CategoriesAdded[newCategoryID]) 34 s.Require().Nil(changes.CategoriesModified[newCategoryID]) 35 36 _, err = org.CreateCategory(newCategoryID, newCategoryName, []string{}) 37 s.Require().Equal(ErrCategoryAlreadyExists, err) 38 39 newCategoryID2 := "new-category-id2" 40 newCategoryName2 := "new-category-name2" 41 42 changes, err = org.CreateCategory(newCategoryID2, newCategoryName2, []string{}) 43 s.Require().NoError(err) 44 s.Require().Equal(int32(len(description.Categories)-1), description.Categories[newCategoryID2].Position) 45 s.Require().NotNil(changes.CategoriesAdded[newCategoryID2]) 46 s.Require().Nil(changes.CategoriesModified[newCategoryID2]) 47 48 newCategoryID3 := "new-category-id3" 49 newCategoryName3 := "new-category-name3" 50 _, err = org.CreateCategory(newCategoryID3, newCategoryName3, []string{"some-chat-id"}) 51 s.Require().Equal(ErrChatNotFound, err) 52 53 newChatID := "new-chat-id" 54 identity := &protobuf.ChatIdentity{ 55 DisplayName: "new-chat-display-name", 56 Description: "new-chat-description", 57 } 58 permissions := &protobuf.CommunityPermissions{ 59 Access: protobuf.CommunityPermissions_AUTO_ACCEPT, 60 } 61 62 _, err = org.CreateChat(newChatID, &protobuf.CommunityChat{ 63 Identity: identity, 64 Permissions: permissions, 65 }) 66 s.Require().NoError(err) 67 68 changes, err = org.CreateCategory(newCategoryID3, newCategoryName3, []string{newChatID}) 69 s.Require().NoError(err) 70 s.Require().NotNil(changes.ChatsModified[newChatID]) 71 s.Require().Equal(newCategoryID3, changes.ChatsModified[newChatID].CategoryModified) 72 73 newCategoryID4 := "new-category-id4" 74 newCategoryName4 := "new-category-name4" 75 76 _, err = org.CreateCategory(newCategoryID4, newCategoryName4, []string{newChatID}) 77 s.Require().Equal(ErrChatAlreadyAssigned, err) 78 } 79 80 func (s *CommunitySuite) TestEditCategory() { 81 newCategoryID := "new-category-id" 82 newCategoryName := "new-category-name" 83 editedCategoryName := "edited-category-name" 84 85 org := s.buildCommunity(&s.identity.PublicKey) 86 org.config.PrivateKey = s.identity 87 _, err := org.CreateCategory(newCategoryID, newCategoryName, []string{testChatID1}) 88 s.Require().NoError(err) 89 org.config.PrivateKey = nil 90 org.config.ID = nil 91 92 _, err = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1}) 93 s.Require().Equal(ErrNotAuthorized, err) 94 95 org.config.PrivateKey = s.identity 96 org.config.ID = &s.identity.PublicKey 97 98 _, err = org.EditCategory("some-random-category", editedCategoryName, []string{testChatID1}) 99 s.Require().Equal(ErrCategoryNotFound, err) 100 101 changes, err := org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1}) 102 103 description := org.config.CommunityDescription 104 105 s.Require().NoError(err) 106 s.Require().Equal(editedCategoryName, description.Categories[newCategoryID].Name) 107 s.Require().NotNil(changes) 108 s.Require().NotNil(changes.CategoriesModified[newCategoryID]) 109 s.Require().Equal(description.Categories[newCategoryID], changes.CategoriesModified[newCategoryID]) 110 s.Require().Nil(changes.CategoriesAdded[newCategoryID]) 111 112 _, err = org.EditCategory(newCategoryID, editedCategoryName, []string{"some-random-chat-id"}) 113 s.Require().Equal(ErrChatNotFound, err) 114 115 _, err = org.EditCategory(testCategoryID1, testCategoryName1, []string{testChatID1}) 116 s.Require().Equal(ErrChatAlreadyAssigned, err) 117 118 // Edit by removing the chats 119 120 identity1 := &protobuf.ChatIdentity{ 121 DisplayName: "new-chat-display-name", 122 Description: "new-chat-description", 123 } 124 permissions := &protobuf.CommunityPermissions{ 125 Access: protobuf.CommunityPermissions_AUTO_ACCEPT, 126 } 127 128 testChatID2 := "test-chat-id-2" 129 testChatID3 := "test-chat-id-3" 130 131 _, err = org.CreateChat(testChatID2, &protobuf.CommunityChat{ 132 Identity: identity1, 133 Permissions: permissions, 134 }) 135 s.Require().NoError(err) 136 identity2 := &protobuf.ChatIdentity{ 137 DisplayName: "identity-2", 138 Description: "new-chat-description", 139 } 140 141 _, err = org.CreateChat(testChatID3, &protobuf.CommunityChat{ 142 Identity: identity2, 143 Permissions: permissions, 144 }) 145 s.Require().NoError(err) 146 147 _, err = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1, testChatID2, testChatID3}) 148 s.Require().NoError(err) 149 150 s.Require().Equal(newCategoryID, description.Chats[testChatID1].CategoryId) 151 s.Require().Equal(newCategoryID, description.Chats[testChatID2].CategoryId) 152 s.Require().Equal(newCategoryID, description.Chats[testChatID3].CategoryId) 153 154 s.Require().Equal(int32(0), description.Chats[testChatID1].Position) 155 s.Require().Equal(int32(1), description.Chats[testChatID2].Position) 156 s.Require().Equal(int32(2), description.Chats[testChatID3].Position) 157 158 _, err = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID1, testChatID3}) 159 s.Require().NoError(err) 160 s.Require().Equal("", description.Chats[testChatID2].CategoryId) 161 s.Require().Equal(int32(0), description.Chats[testChatID1].Position) 162 s.Require().Equal(int32(1), description.Chats[testChatID3].Position) 163 164 _, err = org.EditCategory(newCategoryID, editedCategoryName, []string{testChatID3}) 165 s.Require().NoError(err) 166 s.Require().Equal("", description.Chats[testChatID1].CategoryId) 167 s.Require().Equal(int32(0), description.Chats[testChatID3].Position) 168 } 169 170 func (s *CommunitySuite) TestDeleteCategory() { 171 org := s.buildCommunity(&s.identity.PublicKey) 172 org.config.PrivateKey = s.identity 173 permissions := &protobuf.CommunityPermissions{ 174 Access: protobuf.CommunityPermissions_AUTO_ACCEPT, 175 } 176 177 testChatID2 := "test-chat-id-2" 178 testChatID3 := "test-chat-id-3" 179 newCategoryID := "new-category-id" 180 newCategoryName := "new-category-name" 181 182 identity1 := &protobuf.ChatIdentity{ 183 DisplayName: "display-name-2", 184 Description: "new-chat-description", 185 } 186 187 _, err := org.CreateChat(testChatID2, &protobuf.CommunityChat{ 188 Identity: identity1, 189 Permissions: permissions, 190 }) 191 s.Require().NoError(err) 192 193 identity2 := &protobuf.ChatIdentity{ 194 DisplayName: "display-name-3", 195 Description: "new-chat-description", 196 } 197 198 _, err = org.CreateChat(testChatID3, &protobuf.CommunityChat{ 199 Identity: identity2, 200 Permissions: permissions, 201 }) 202 s.Require().NoError(err) 203 204 _, err = org.CreateCategory(newCategoryID, newCategoryName, []string{}) 205 s.Require().NoError(err) 206 207 description := org.config.CommunityDescription 208 209 _, err = org.EditCategory(newCategoryID, newCategoryName, []string{testChatID2, testChatID1}) 210 s.Require().NoError(err) 211 s.Require().Equal(newCategoryID, description.Chats[testChatID1].CategoryId) 212 s.Require().Equal(newCategoryID, description.Chats[testChatID2].CategoryId) 213 214 s.Require().Equal(int32(0), description.Chats[testChatID3].Position) 215 s.Require().Equal(int32(0), description.Chats[testChatID2].Position) 216 s.Require().Equal(int32(1), description.Chats[testChatID1].Position) 217 218 org.config.PrivateKey = nil 219 org.config.ID = nil 220 _, err = org.DeleteCategory(testCategoryID1) 221 s.Require().Equal(ErrNotAuthorized, err) 222 223 org.config.PrivateKey = s.identity 224 org.config.ID = &s.identity.PublicKey 225 _, err = org.DeleteCategory("some-category-id") 226 s.Require().Equal(ErrCategoryNotFound, err) 227 228 changes, err := org.DeleteCategory(newCategoryID) 229 s.Require().NoError(err) 230 s.Require().NotNil(changes) 231 232 s.Require().Equal("", description.Chats[testChatID1].CategoryId) 233 s.Require().Equal("", description.Chats[testChatID2].CategoryId) 234 s.Require().Equal("", description.Chats[testChatID3].CategoryId) 235 } 236 237 func (s *CommunitySuite) TestDeleteChatOrder() { 238 org := s.buildCommunity(&s.identity.PublicKey) 239 org.config.PrivateKey = s.identity 240 permissions := &protobuf.CommunityPermissions{ 241 Access: protobuf.CommunityPermissions_AUTO_ACCEPT, 242 } 243 244 testChatID2 := "test-chat-id-2" 245 testChatID3 := "test-chat-id-3" 246 newCategoryID := "new-category-id" 247 newCategoryName := "new-category-name" 248 249 identity1 := &protobuf.ChatIdentity{ 250 DisplayName: "identity-1", 251 Description: "new-chat-description", 252 } 253 254 _, err := org.CreateChat(testChatID2, &protobuf.CommunityChat{ 255 Identity: identity1, 256 Permissions: permissions, 257 }) 258 s.Require().NoError(err) 259 identity2 := &protobuf.ChatIdentity{ 260 DisplayName: "identity-2", 261 Description: "new-chat-description", 262 } 263 264 _, err = org.CreateChat(testChatID3, &protobuf.CommunityChat{ 265 Identity: identity2, 266 Permissions: permissions, 267 }) 268 s.Require().NoError(err) 269 270 _, err = org.CreateCategory(newCategoryID, newCategoryName, []string{testChatID1, testChatID2, testChatID3}) 271 s.Require().NoError(err) 272 273 changes, err := org.DeleteChat(testChatID2) 274 s.Require().NoError(err) 275 s.Require().Equal(int32(0), org.Chats()[testChatID1].Position) 276 s.Require().Equal(int32(1), org.Chats()[testChatID3].Position) 277 s.Require().Len(changes.ChatsRemoved, 1) 278 279 changes, err = org.DeleteChat(testChatID1) 280 s.Require().NoError(err) 281 s.Require().Equal(int32(0), org.Chats()[testChatID3].Position) 282 s.Require().Len(changes.ChatsRemoved, 1) 283 284 _, err = org.DeleteChat(testChatID3) 285 s.Require().NoError(err) 286 }