2020-11-18 09:16:51 +00:00
|
|
|
package communities
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"testing"
|
2023-09-28 15:37:03 +00:00
|
|
|
"time"
|
2020-11-18 09:16:51 +00:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommunitySuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(CommunitySuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
const testChatID1 = "chat-id-1"
|
2021-05-23 13:34:17 +00:00
|
|
|
const testCategoryID1 = "category-id-1"
|
|
|
|
const testCategoryName1 = "category-name-1"
|
2020-11-18 09:16:51 +00:00
|
|
|
const testChatID2 = "chat-id-2"
|
|
|
|
|
2023-09-28 15:37:03 +00:00
|
|
|
type TimeSourceStub struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TimeSourceStub) GetCurrentTime() uint64 {
|
|
|
|
return uint64(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
type CommunitySuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
identity *ecdsa.PrivateKey
|
|
|
|
communityID []byte
|
|
|
|
|
|
|
|
member1 *ecdsa.PrivateKey
|
|
|
|
member2 *ecdsa.PrivateKey
|
|
|
|
member3 *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
member1Key string
|
|
|
|
member2Key string
|
|
|
|
member3Key string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) SetupTest() {
|
|
|
|
identity, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.identity = identity
|
|
|
|
s.communityID = crypto.CompressPubkey(&identity.PublicKey)
|
|
|
|
|
|
|
|
member1, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.member1 = member1
|
|
|
|
|
|
|
|
member2, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.member2 = member2
|
|
|
|
|
|
|
|
member3, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.member3 = member3
|
|
|
|
|
|
|
|
s.member1Key = common.PubkeyToHex(&s.member1.PublicKey)
|
|
|
|
s.member2Key = common.PubkeyToHex(&s.member2.PublicKey)
|
|
|
|
s.member3Key = common.PubkeyToHex(&s.member3.PublicKey)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-12 08:52:32 +00:00
|
|
|
func (s *CommunitySuite) TestHasPermission() {
|
|
|
|
// returns false if empty public key is passed
|
|
|
|
community := &Community{}
|
|
|
|
ownerKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
nonMemberKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
memberKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2023-08-02 18:59:26 +00:00
|
|
|
s.Require().False(community.hasRoles(nil, adminRole()))
|
2023-07-12 08:52:32 +00:00
|
|
|
|
|
|
|
// returns false if key is passed, but config is nil
|
2023-08-02 18:59:26 +00:00
|
|
|
s.Require().False(community.hasRoles(&nonMemberKey.PublicKey, adminRole()))
|
2023-07-12 08:52:32 +00:00
|
|
|
|
|
|
|
// returns true if the user is the owner
|
|
|
|
|
|
|
|
communityDescription := &protobuf.CommunityDescription{}
|
|
|
|
communityDescription.Members = make(map[string]*protobuf.CommunityMember)
|
2023-07-18 15:06:12 +00:00
|
|
|
communityDescription.Members[common.PubkeyToHex(&ownerKey.PublicKey)] = &protobuf.CommunityMember{Roles: []protobuf.CommunityMember_Roles{protobuf.CommunityMember_ROLE_OWNER}}
|
2023-07-12 08:52:32 +00:00
|
|
|
communityDescription.Members[common.PubkeyToHex(&memberKey.PublicKey)] = &protobuf.CommunityMember{Roles: []protobuf.CommunityMember_Roles{protobuf.CommunityMember_ROLE_ADMIN}}
|
|
|
|
|
|
|
|
community.config = &Config{ID: &ownerKey.PublicKey, CommunityDescription: communityDescription}
|
|
|
|
|
2023-08-02 18:59:26 +00:00
|
|
|
s.Require().True(community.hasRoles(&ownerKey.PublicKey, ownerRole()))
|
2023-07-12 08:52:32 +00:00
|
|
|
|
|
|
|
// return false if user is not a member
|
2023-08-02 18:59:26 +00:00
|
|
|
s.Require().False(community.hasRoles(&nonMemberKey.PublicKey, adminRole()))
|
2023-07-12 08:52:32 +00:00
|
|
|
|
|
|
|
// return true if user is a member and has permissions
|
2023-08-02 18:59:26 +00:00
|
|
|
s.Require().True(community.hasRoles(&memberKey.PublicKey, adminRole()))
|
2023-07-12 08:52:32 +00:00
|
|
|
|
|
|
|
// return false if user is a member and does not have permissions
|
2023-08-02 18:59:26 +00:00
|
|
|
s.Require().False(community.hasRoles(&memberKey.PublicKey, ownerRole()))
|
2023-07-12 08:52:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
func (s *CommunitySuite) TestCreateChat() {
|
|
|
|
newChatID := "new-chat-id"
|
|
|
|
org := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
org.config.PrivateKey = nil
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = nil
|
2020-11-18 09:16:51 +00:00
|
|
|
|
|
|
|
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,
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := org.CreateChat(newChatID, &protobuf.CommunityChat{
|
|
|
|
Identity: identity,
|
|
|
|
Permissions: permissions,
|
|
|
|
})
|
|
|
|
|
2023-08-16 12:54:55 +00:00
|
|
|
s.Require().Equal(ErrNotAuthorized, err)
|
2020-11-18 09:16:51 +00:00
|
|
|
|
|
|
|
org.config.PrivateKey = s.identity
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = &s.identity.PublicKey
|
2020-11-18 09:16:51 +00:00
|
|
|
|
|
|
|
changes, err := org.CreateChat(newChatID, &protobuf.CommunityChat{
|
|
|
|
Identity: identity,
|
|
|
|
Permissions: permissions,
|
|
|
|
})
|
|
|
|
|
|
|
|
description := org.config.CommunityDescription
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(description)
|
|
|
|
s.Require().NotNil(description.Chats[newChatID])
|
|
|
|
s.Require().NotEmpty(description.Clock)
|
2021-05-23 13:34:17 +00:00
|
|
|
s.Require().Equal(len(description.Chats)-1, int(description.Chats[newChatID].Position))
|
2020-11-18 09:16:51 +00:00
|
|
|
s.Require().Equal(permissions, description.Chats[newChatID].Permissions)
|
|
|
|
s.Require().Equal(identity, description.Chats[newChatID].Identity)
|
|
|
|
|
|
|
|
s.Require().NotNil(changes)
|
|
|
|
s.Require().NotNil(changes.ChatsAdded[newChatID])
|
2021-06-02 08:54:53 +00:00
|
|
|
|
|
|
|
// Add a community with the same name
|
|
|
|
|
|
|
|
_, err = org.CreateChat("different-chat-id", &protobuf.CommunityChat{
|
|
|
|
Identity: identity,
|
|
|
|
Permissions: permissions,
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Require().Error(err)
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 12:13:17 +00:00
|
|
|
func (s *CommunitySuite) TestEditChat() {
|
|
|
|
newChatID := "new-chat-id"
|
|
|
|
org := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
|
|
|
|
identity := &protobuf.ChatIdentity{
|
|
|
|
DisplayName: "new-chat-display-name",
|
|
|
|
Description: "new-chat-description",
|
2021-10-04 13:02:25 +00:00
|
|
|
Emoji: "😎",
|
|
|
|
Color: "#000000",
|
2021-06-01 12:13:17 +00:00
|
|
|
}
|
|
|
|
permissions := &protobuf.CommunityPermissions{
|
2023-10-25 13:03:26 +00:00
|
|
|
Access: protobuf.CommunityPermissions_AUTO_ACCEPT,
|
2021-06-01 12:13:17 +00:00
|
|
|
Private: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := org.CreateChat(newChatID, &protobuf.CommunityChat{
|
2024-02-28 11:48:37 +00:00
|
|
|
Identity: identity,
|
|
|
|
Permissions: permissions,
|
|
|
|
HideIfPermissionsNotMet: false,
|
2021-06-01 12:13:17 +00:00
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
org.config.PrivateKey = nil
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = nil
|
2021-06-01 12:13:17 +00:00
|
|
|
editedIdentity := &protobuf.ChatIdentity{
|
|
|
|
DisplayName: "edited-new-chat-display-name",
|
|
|
|
Description: "edited-new-chat-description",
|
2021-10-04 13:02:25 +00:00
|
|
|
Emoji: "🤘",
|
|
|
|
Color: "#FFFFFF",
|
2021-06-01 12:13:17 +00:00
|
|
|
}
|
|
|
|
editedPermissions := &protobuf.CommunityPermissions{
|
2023-10-25 13:03:26 +00:00
|
|
|
Access: protobuf.CommunityPermissions_AUTO_ACCEPT,
|
2021-06-01 12:13:17 +00:00
|
|
|
Private: true,
|
|
|
|
}
|
|
|
|
_, err = org.EditChat(newChatID, &protobuf.CommunityChat{
|
|
|
|
Identity: editedIdentity,
|
|
|
|
Permissions: editedPermissions,
|
|
|
|
})
|
2023-08-16 12:54:55 +00:00
|
|
|
s.Require().Equal(ErrNotAuthorized, err)
|
2021-06-01 12:13:17 +00:00
|
|
|
|
|
|
|
description := org.config.CommunityDescription
|
|
|
|
org.config.PrivateKey = s.identity
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = &s.identity.PublicKey
|
2021-06-01 12:13:17 +00:00
|
|
|
editChanges, err := org.EditChat(newChatID, &protobuf.CommunityChat{
|
2024-02-28 11:48:37 +00:00
|
|
|
Identity: editedIdentity,
|
|
|
|
Permissions: editedPermissions,
|
|
|
|
HideIfPermissionsNotMet: true,
|
2021-06-01 12:13:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require().NotNil(description.Chats[newChatID])
|
|
|
|
s.Require().NotEmpty(description.Clock)
|
|
|
|
s.Require().Equal(editedPermissions, description.Chats[newChatID].Permissions)
|
|
|
|
s.Require().Equal(editedIdentity, description.Chats[newChatID].Identity)
|
|
|
|
|
|
|
|
s.Require().NotNil(editChanges)
|
|
|
|
s.Require().NotNil(editChanges.ChatsModified[newChatID])
|
|
|
|
s.Require().Equal(editChanges.ChatsModified[newChatID].ChatModified.Identity, editedIdentity)
|
|
|
|
s.Require().Equal(editChanges.ChatsModified[newChatID].ChatModified.Permissions, editedPermissions)
|
2024-02-28 11:48:37 +00:00
|
|
|
s.Require().Equal(editChanges.ChatsModified[newChatID].ChatModified.HideIfPermissionsNotMet, true)
|
2021-06-01 12:13:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
func (s *CommunitySuite) TestDeleteChat() {
|
|
|
|
org := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
org.config.PrivateKey = nil
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = nil
|
2020-11-18 09:16:51 +00:00
|
|
|
|
|
|
|
_, err := org.DeleteChat(testChatID1)
|
2023-08-16 12:54:55 +00:00
|
|
|
s.Require().Equal(ErrNotAuthorized, err)
|
2023-09-28 15:37:03 +00:00
|
|
|
change1Clock := org.Clock()
|
2020-11-18 09:16:51 +00:00
|
|
|
|
|
|
|
org.config.PrivateKey = s.identity
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = &s.identity.PublicKey
|
2020-11-18 09:16:51 +00:00
|
|
|
|
2023-06-14 14:15:46 +00:00
|
|
|
changes, err := org.DeleteChat(testChatID1)
|
2020-11-18 09:16:51 +00:00
|
|
|
s.Require().NoError(err)
|
2023-06-14 14:15:46 +00:00
|
|
|
s.Require().NotNil(changes)
|
2023-09-28 15:37:03 +00:00
|
|
|
change2Clock := org.Clock()
|
2020-11-18 09:16:51 +00:00
|
|
|
|
2023-06-14 14:15:46 +00:00
|
|
|
s.Require().Nil(org.Chats()[testChatID1])
|
|
|
|
s.Require().Len(changes.ChatsRemoved, 1)
|
2023-09-28 15:37:03 +00:00
|
|
|
s.Require().Greater(change2Clock, change1Clock)
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) TestRemoveUserFromChat() {
|
|
|
|
org := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
org.config.PrivateKey = nil
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = nil
|
2020-11-18 09:16:51 +00:00
|
|
|
// Not an admin
|
|
|
|
_, err := org.RemoveUserFromOrg(&s.member1.PublicKey)
|
2023-08-16 12:54:55 +00:00
|
|
|
s.Require().Equal(ErrNotAuthorized, err)
|
2020-11-18 09:16:51 +00:00
|
|
|
|
|
|
|
// Add admin to community
|
|
|
|
org.config.PrivateKey = s.identity
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = &s.identity.PublicKey
|
2020-11-18 09:16:51 +00:00
|
|
|
|
|
|
|
actualCommunity, err := org.RemoveUserFromChat(&s.member1.PublicKey, testChatID1)
|
|
|
|
s.Require().Nil(err)
|
|
|
|
s.Require().NotNil(actualCommunity)
|
|
|
|
|
|
|
|
// Check member has not been removed
|
|
|
|
s.Require().True(org.HasMember(&s.member1.PublicKey))
|
|
|
|
|
|
|
|
// Check member has not been removed from org
|
|
|
|
_, ok := actualCommunity.Members[common.PubkeyToHex(&s.member1.PublicKey)]
|
|
|
|
s.Require().True(ok)
|
|
|
|
|
|
|
|
// Check member has been removed from chat
|
|
|
|
_, ok = actualCommunity.Chats[testChatID1].Members[common.PubkeyToHex(&s.member1.PublicKey)]
|
|
|
|
s.Require().False(ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) TestRemoveUserFormOrg() {
|
|
|
|
org := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
org.config.PrivateKey = nil
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = nil
|
2020-11-18 09:16:51 +00:00
|
|
|
// Not an admin
|
|
|
|
_, err := org.RemoveUserFromOrg(&s.member1.PublicKey)
|
2023-08-16 12:54:55 +00:00
|
|
|
s.Require().Equal(ErrNotAuthorized, err)
|
2020-11-18 09:16:51 +00:00
|
|
|
|
|
|
|
// Add admin to community
|
|
|
|
org.config.PrivateKey = s.identity
|
2023-06-14 14:15:46 +00:00
|
|
|
org.config.ID = &s.identity.PublicKey
|
2020-11-18 09:16:51 +00:00
|
|
|
|
|
|
|
actualCommunity, err := org.RemoveUserFromOrg(&s.member1.PublicKey)
|
|
|
|
s.Require().Nil(err)
|
|
|
|
s.Require().NotNil(actualCommunity)
|
|
|
|
|
|
|
|
// Check member has been removed
|
|
|
|
s.Require().False(org.HasMember(&s.member1.PublicKey))
|
|
|
|
|
|
|
|
// Check member has been removed from org
|
|
|
|
_, ok := actualCommunity.Members[common.PubkeyToHex(&s.member1.PublicKey)]
|
|
|
|
s.Require().False(ok)
|
|
|
|
|
|
|
|
// Check member has been removed from chat
|
|
|
|
_, ok = actualCommunity.Chats[testChatID1].Members[common.PubkeyToHex(&s.member1.PublicKey)]
|
|
|
|
s.Require().False(ok)
|
|
|
|
}
|
|
|
|
|
2022-09-14 12:39:55 +00:00
|
|
|
func (s *CommunitySuite) TestRemoveOurselvesFormOrg() {
|
|
|
|
org := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
|
|
|
|
// We don't need to be an admin to remove ourselves from community
|
|
|
|
org.config.PrivateKey = nil
|
|
|
|
|
|
|
|
org.RemoveOurselvesFromOrg(&s.member1.PublicKey)
|
|
|
|
|
|
|
|
// Check member has been removed from org
|
|
|
|
s.Require().False(org.HasMember(&s.member1.PublicKey))
|
|
|
|
|
|
|
|
// Check member has been removed from chat
|
|
|
|
_, ok := org.config.CommunityDescription.Chats[testChatID1].Members[common.PubkeyToHex(&s.member1.PublicKey)]
|
|
|
|
s.Require().False(ok)
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
func (s *CommunitySuite) TestAcceptRequestToJoin() {
|
|
|
|
// WHAT TO DO WITH ENS
|
|
|
|
// TEST CASE 1: Not an admin
|
|
|
|
// TEST CASE 2: No request to join
|
|
|
|
// TEST CASE 3: Valid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) TestDeclineRequestToJoin() {
|
|
|
|
// TEST CASE 1: Not an admin
|
|
|
|
// TEST CASE 2: No request to join
|
|
|
|
// TEST CASE 3: Valid
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (s *CommunitySuite) TestValidateRequestToJoin() {
|
2020-11-18 09:16:51 +00:00
|
|
|
description := &protobuf.CommunityDescription{}
|
|
|
|
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
signer := &key.PublicKey
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
request := &protobuf.CommunityRequestToJoin{
|
2020-11-18 09:16:51 +00:00
|
|
|
EnsName: "donvanvliet.stateofus.eth",
|
|
|
|
CommunityId: s.communityID,
|
2023-10-12 21:42:03 +00:00
|
|
|
Clock: uint64(time.Now().Unix()),
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
requestWithChatID := &protobuf.CommunityRequestToJoin{
|
2020-11-18 09:16:51 +00:00
|
|
|
EnsName: "donvanvliet.stateofus.eth",
|
|
|
|
CommunityId: s.communityID,
|
|
|
|
ChatId: testChatID1,
|
2023-10-12 21:42:03 +00:00
|
|
|
Clock: uint64(time.Now().Unix()),
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
requestWithoutENS := &protobuf.CommunityRequestToJoin{
|
2020-11-18 09:16:51 +00:00
|
|
|
CommunityId: s.communityID,
|
2023-10-12 21:42:03 +00:00
|
|
|
Clock: uint64(time.Now().Unix()),
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
requestWithChatWithoutENS := &protobuf.CommunityRequestToJoin{
|
2020-11-18 09:16:51 +00:00
|
|
|
CommunityId: s.communityID,
|
|
|
|
ChatId: testChatID1,
|
2023-10-12 21:42:03 +00:00
|
|
|
Clock: uint64(time.Now().Unix()),
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MATRIX
|
|
|
|
// NO_MEMBERHSIP - NO_MEMBERSHIP -> Error -> Anyone can join org, chat is read/write for anyone
|
|
|
|
// NO_MEMBRISHIP - INVITATION_ONLY -> Error -> Anyone can join org, chat is invitation only
|
|
|
|
// NO_MEMBERSHIP - ON_REQUEST -> Success -> Anyone can join org, chat is on request and needs approval
|
|
|
|
// INVITATION_ONLY - NO_MEMBERSHIP -> TODO -> Org is invitation only, chat is read-write for members
|
|
|
|
// INVITATION_ONLY - INVITATION_ONLY -> Error -> Org is invitation only, chat is invitation only
|
|
|
|
// INVITATION_ONLY - ON_REQUEST -> TODO -> Error -> Org is invitation only, member of the org need to request access for chat
|
|
|
|
// ON_REQUEST - NO_MEMBRERSHIP -> TODO -> Error -> Org is on request, chat is read write for members
|
|
|
|
// ON_REQUEST - INVITATION_ONLY -> Error -> Org is on request, chat is invitation only for members
|
|
|
|
// ON_REQUEST - ON_REQUEST -> Fine -> Org is on request, chat is on request
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
config Config
|
2021-01-11 10:32:51 +00:00
|
|
|
request *protobuf.CommunityRequestToJoin
|
2020-11-18 09:16:51 +00:00
|
|
|
signer *ecdsa.PublicKey
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "on-request access to community",
|
|
|
|
config: s.configOnRequest(),
|
|
|
|
signer: signer,
|
|
|
|
request: request,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not admin",
|
2021-01-11 10:32:51 +00:00
|
|
|
config: Config{MemberIdentity: signer, CommunityDescription: description},
|
2020-11-18 09:16:51 +00:00
|
|
|
signer: signer,
|
|
|
|
request: request,
|
|
|
|
err: ErrNotAdmin,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ens-only org and missing ens",
|
|
|
|
config: s.configENSOnly(),
|
|
|
|
signer: signer,
|
|
|
|
request: requestWithoutENS,
|
|
|
|
err: ErrCantRequestAccess,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ens-only chat and missing ens",
|
|
|
|
config: s.configChatENSOnly(),
|
|
|
|
signer: signer,
|
|
|
|
request: requestWithChatWithoutENS,
|
|
|
|
err: ErrCantRequestAccess,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing chat",
|
|
|
|
config: s.configOnRequest(),
|
|
|
|
signer: signer,
|
|
|
|
request: requestWithChatID,
|
|
|
|
err: ErrChatNotFound,
|
|
|
|
},
|
|
|
|
// Org-Chat combinations
|
|
|
|
// NO_MEMBERSHIP-NO_MEMBERSHIP = error as you should not be
|
|
|
|
// requesting access
|
|
|
|
{
|
|
|
|
name: "no-membership org with no-membeship chat",
|
|
|
|
config: s.configNoMembershipOrgNoMembershipChat(),
|
|
|
|
signer: signer,
|
|
|
|
request: requestWithChatID,
|
|
|
|
err: ErrCantRequestAccess,
|
|
|
|
},
|
|
|
|
// NO_MEMBERSHIP-ON_REQUEST = this is a valid case
|
|
|
|
{
|
|
|
|
name: "no-membership org with on-request chat",
|
|
|
|
config: s.configNoMembershipOrgOnRequestChat(),
|
|
|
|
signer: signer,
|
|
|
|
request: requestWithChatID,
|
|
|
|
},
|
|
|
|
// ON_REQUEST-ON_REQUEST success
|
|
|
|
{
|
|
|
|
name: "on-request org with on-request chat",
|
|
|
|
config: s.configOnRequestOrgOnRequestChat(),
|
|
|
|
signer: signer,
|
|
|
|
request: requestWithChatID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.name, func() {
|
2023-11-29 17:21:21 +00:00
|
|
|
org, err := New(tc.config, &TimeSourceStub{}, &DescriptionEncryptorMock{})
|
2020-11-18 09:16:51 +00:00
|
|
|
s.Require().NoError(err)
|
2021-01-11 10:32:51 +00:00
|
|
|
err = org.ValidateRequestToJoin(tc.signer, tc.request)
|
2020-11-18 09:16:51 +00:00
|
|
|
s.Require().Equal(tc.err, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 17:14:24 +00:00
|
|
|
func (s *CommunitySuite) TestCanPostCanView() {
|
|
|
|
chatID := "chat-id"
|
|
|
|
memberKey := common.PubkeyToHex(&s.member1.PublicKey)
|
|
|
|
// Member has no channel role
|
|
|
|
description := &protobuf.CommunityDescription{
|
|
|
|
Members: map[string]*protobuf.CommunityMember{
|
|
|
|
memberKey: &protobuf.CommunityMember{},
|
|
|
|
},
|
|
|
|
Chats: map[string]*protobuf.CommunityChat{
|
|
|
|
chatID: &protobuf.CommunityChat{
|
|
|
|
Members: map[string]*protobuf.CommunityMember{
|
|
|
|
memberKey: &protobuf.CommunityMember{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
community := &Community{config: &Config{ID: &s.member2.PublicKey}}
|
|
|
|
community.config.CommunityDescription = description
|
|
|
|
|
|
|
|
result, err := community.CanPost(&s.member1.PublicKey, chatID, protobuf.ApplicationMetadataMessage_CHAT_MESSAGE)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().True(result)
|
|
|
|
|
|
|
|
result = community.CanView(&s.member1.PublicKey, chatID)
|
|
|
|
s.Require().True(result)
|
|
|
|
|
|
|
|
// member has view channel permissions
|
|
|
|
description.Chats[chatID].Members[memberKey].ChannelRole = protobuf.CommunityMember_CHANNEL_ROLE_VIEWER
|
|
|
|
|
|
|
|
result, err = community.CanPost(&s.member1.PublicKey, chatID, protobuf.ApplicationMetadataMessage_CHAT_MESSAGE)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().False(result)
|
|
|
|
|
|
|
|
result = community.CanView(&s.member1.PublicKey, chatID)
|
|
|
|
s.Require().True(result)
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
func (s *CommunitySuite) TestCanPost() {
|
|
|
|
notMember := &s.member3.PublicKey
|
|
|
|
member := &s.member1.PublicKey
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
config Config
|
|
|
|
member *ecdsa.PublicKey
|
|
|
|
err error
|
|
|
|
canPost bool
|
|
|
|
}{
|
|
|
|
{
|
2024-03-01 17:15:38 +00:00
|
|
|
name: "no-membership org with no-membership chat",
|
2020-11-18 09:16:51 +00:00
|
|
|
config: s.configNoMembershipOrgNoMembershipChat(),
|
|
|
|
member: notMember,
|
2023-10-25 14:26:18 +00:00
|
|
|
canPost: false,
|
2020-11-18 09:16:51 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "membership org with no-membership chat-not-a-member",
|
|
|
|
config: s.configOnRequestOrgNoMembershipChat(),
|
|
|
|
member: notMember,
|
|
|
|
canPost: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "membership org with no-membership chat",
|
|
|
|
config: s.configOnRequestOrgNoMembershipChat(),
|
|
|
|
member: member,
|
|
|
|
canPost: true,
|
|
|
|
},
|
|
|
|
{
|
2024-03-01 17:15:38 +00:00
|
|
|
name: "creator can always post of course",
|
2020-11-18 09:16:51 +00:00
|
|
|
config: s.configOnRequestOrgNoMembershipChat(),
|
|
|
|
member: &s.identity.PublicKey,
|
|
|
|
canPost: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.name, func() {
|
|
|
|
var err error
|
2023-11-29 17:21:21 +00:00
|
|
|
org, err := New(tc.config, &TimeSourceStub{}, &DescriptionEncryptorMock{})
|
2020-11-18 09:16:51 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2024-03-01 17:15:38 +00:00
|
|
|
canPost, err := org.CanPost(tc.member, testChatID1, protobuf.ApplicationMetadataMessage_CHAT_MESSAGE)
|
2020-11-18 09:16:51 +00:00
|
|
|
s.Require().Equal(tc.err, err)
|
|
|
|
s.Require().Equal(tc.canPost, canPost)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) TestHandleCommunityDescription() {
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
signer := &key.PublicKey
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
buildChanges := func(c *Community) *CommunityChanges {
|
|
|
|
return c.emptyCommunityChanges()
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
description func(*Community) *protobuf.CommunityDescription
|
|
|
|
changes func(*Community) *CommunityChanges
|
|
|
|
signer *ecdsa.PublicKey
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "updated version but no changes",
|
|
|
|
description: s.identicalCommunityDescription,
|
|
|
|
signer: signer,
|
2021-01-11 10:32:51 +00:00
|
|
|
changes: buildChanges,
|
2020-11-18 09:16:51 +00:00
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "updated version but lower clock",
|
|
|
|
description: s.oldCommunityDescription,
|
|
|
|
signer: signer,
|
2021-01-11 10:32:51 +00:00
|
|
|
changes: buildChanges,
|
2020-11-18 09:16:51 +00:00
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "removed member from org",
|
|
|
|
description: s.removedMemberCommunityDescription,
|
|
|
|
signer: signer,
|
|
|
|
changes: func(org *Community) *CommunityChanges {
|
2021-01-11 10:32:51 +00:00
|
|
|
changes := org.emptyCommunityChanges()
|
2020-11-18 09:16:51 +00:00
|
|
|
changes.MembersRemoved[s.member1Key] = &protobuf.CommunityMember{}
|
|
|
|
changes.ChatsModified[testChatID1] = &CommunityChatChanges{
|
|
|
|
MembersAdded: make(map[string]*protobuf.CommunityMember),
|
|
|
|
MembersRemoved: make(map[string]*protobuf.CommunityMember),
|
|
|
|
}
|
|
|
|
changes.ChatsModified[testChatID1].MembersRemoved[s.member1Key] = &protobuf.CommunityMember{}
|
|
|
|
|
|
|
|
return changes
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "added member from org",
|
|
|
|
description: s.addedMemberCommunityDescription,
|
|
|
|
signer: signer,
|
|
|
|
changes: func(org *Community) *CommunityChanges {
|
2021-01-11 10:32:51 +00:00
|
|
|
changes := org.emptyCommunityChanges()
|
2020-11-18 09:16:51 +00:00
|
|
|
changes.MembersAdded[s.member3Key] = &protobuf.CommunityMember{}
|
|
|
|
changes.ChatsModified[testChatID1] = &CommunityChatChanges{
|
|
|
|
MembersAdded: make(map[string]*protobuf.CommunityMember),
|
|
|
|
MembersRemoved: make(map[string]*protobuf.CommunityMember),
|
|
|
|
}
|
|
|
|
changes.ChatsModified[testChatID1].MembersAdded[s.member3Key] = &protobuf.CommunityMember{}
|
|
|
|
|
|
|
|
return changes
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "chat added to org",
|
|
|
|
description: s.addedChatCommunityDescription,
|
|
|
|
signer: signer,
|
|
|
|
changes: func(org *Community) *CommunityChanges {
|
2021-01-11 10:32:51 +00:00
|
|
|
changes := org.emptyCommunityChanges()
|
2020-11-18 09:16:51 +00:00
|
|
|
changes.MembersAdded[s.member3Key] = &protobuf.CommunityMember{}
|
2021-06-02 08:54:53 +00:00
|
|
|
changes.ChatsAdded[testChatID2] = &protobuf.CommunityChat{
|
|
|
|
Identity: &protobuf.ChatIdentity{DisplayName: "added-chat", Description: "description"},
|
2023-10-25 13:22:12 +00:00
|
|
|
Permissions: &protobuf.CommunityPermissions{Access: protobuf.CommunityPermissions_MANUAL_ACCEPT},
|
2021-06-02 08:54:53 +00:00
|
|
|
Members: make(map[string]*protobuf.CommunityMember)}
|
2020-11-18 09:16:51 +00:00
|
|
|
changes.ChatsAdded[testChatID2].Members[s.member3Key] = &protobuf.CommunityMember{}
|
|
|
|
|
|
|
|
return changes
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "chat removed from the org",
|
|
|
|
description: s.removedChatCommunityDescription,
|
|
|
|
signer: signer,
|
|
|
|
changes: func(org *Community) *CommunityChanges {
|
2021-01-11 10:32:51 +00:00
|
|
|
changes := org.emptyCommunityChanges()
|
2020-11-18 09:16:51 +00:00
|
|
|
changes.ChatsRemoved[testChatID1] = org.config.CommunityDescription.Chats[testChatID1]
|
|
|
|
|
|
|
|
return changes
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.name, func() {
|
|
|
|
org := s.buildCommunity(signer)
|
|
|
|
org.Join()
|
|
|
|
expectedChanges := tc.changes(org)
|
2023-10-31 14:20:40 +00:00
|
|
|
actualChanges, err := org.UpdateCommunityDescription(tc.description(org), []byte{0x01}, nil)
|
2020-11-18 09:16:51 +00:00
|
|
|
s.Require().Equal(tc.err, err)
|
|
|
|
s.Require().Equal(expectedChanges, actualChanges)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) TestValidateCommunityDescription() {
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
description *protobuf.CommunityDescription
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid",
|
|
|
|
description: s.buildCommunityDescription(),
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty description",
|
|
|
|
err: ErrInvalidCommunityDescription,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty org permissions",
|
|
|
|
description: s.emptyPermissionsCommunityDescription(),
|
|
|
|
err: ErrInvalidCommunityDescriptionNoOrgPermissions,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty chat permissions",
|
|
|
|
description: s.emptyChatPermissionsCommunityDescription(),
|
|
|
|
err: ErrInvalidCommunityDescriptionNoChatPermissions,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unknown org permissions",
|
|
|
|
description: s.unknownOrgPermissionsCommunityDescription(),
|
|
|
|
err: ErrInvalidCommunityDescriptionUnknownOrgAccess,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unknown chat permissions",
|
|
|
|
description: s.unknownChatPermissionsCommunityDescription(),
|
|
|
|
err: ErrInvalidCommunityDescriptionUnknownChatAccess,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "member in chat but not in org",
|
|
|
|
description: s.memberInChatNotInOrgCommunityDescription(),
|
|
|
|
err: ErrInvalidCommunityDescriptionMemberInChatButNotInOrg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.name, func() {
|
|
|
|
err := ValidateCommunityDescription(tc.description)
|
|
|
|
s.Require().Equal(tc.err, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 09:58:05 +00:00
|
|
|
func (s *CommunitySuite) TestChatIDs() {
|
|
|
|
community := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
chatIDs := community.ChatIDs()
|
|
|
|
|
|
|
|
s.Require().Len(chatIDs, 1)
|
|
|
|
}
|
|
|
|
|
2023-06-12 15:17:37 +00:00
|
|
|
func (s *CommunitySuite) TestChannelTokenPermissionsByType() {
|
|
|
|
org := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
|
|
|
|
viewOnlyPermissions := []*protobuf.CommunityTokenPermission{
|
|
|
|
&protobuf.CommunityTokenPermission{
|
|
|
|
Id: "some-id",
|
|
|
|
Type: protobuf.CommunityTokenPermission_CAN_VIEW_CHANNEL,
|
|
|
|
TokenCriteria: make([]*protobuf.TokenCriteria, 0),
|
|
|
|
ChatIds: []string{"some-chat-id"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
viewAndPostPermissions := []*protobuf.CommunityTokenPermission{
|
|
|
|
&protobuf.CommunityTokenPermission{
|
|
|
|
Id: "some-other-id",
|
|
|
|
Type: protobuf.CommunityTokenPermission_CAN_VIEW_AND_POST_CHANNEL,
|
|
|
|
TokenCriteria: make([]*protobuf.TokenCriteria, 0),
|
|
|
|
ChatIds: []string{"some-chat-id-2"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, viewOnlyPermission := range viewOnlyPermissions {
|
2023-08-17 15:19:18 +00:00
|
|
|
_, err := org.UpsertTokenPermission(viewOnlyPermission)
|
2023-06-12 15:17:37 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
for _, viewAndPostPermission := range viewAndPostPermissions {
|
2023-08-17 15:19:18 +00:00
|
|
|
_, err := org.UpsertTokenPermission(viewAndPostPermission)
|
2023-06-12 15:17:37 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := org.ChannelTokenPermissionsByType("some-chat-id", protobuf.CommunityTokenPermission_CAN_VIEW_CHANNEL)
|
|
|
|
s.Require().Len(result, 1)
|
|
|
|
s.Require().Equal(result[0].Id, viewOnlyPermissions[0].Id)
|
|
|
|
s.Require().Equal(result[0].TokenCriteria, viewOnlyPermissions[0].TokenCriteria)
|
|
|
|
s.Require().Equal(result[0].ChatIds, viewOnlyPermissions[0].ChatIds)
|
|
|
|
|
|
|
|
result = org.ChannelTokenPermissionsByType("some-chat-id-2", protobuf.CommunityTokenPermission_CAN_VIEW_AND_POST_CHANNEL)
|
|
|
|
s.Require().Len(result, 1)
|
|
|
|
s.Require().Equal(result[0].Id, viewAndPostPermissions[0].Id)
|
|
|
|
s.Require().Equal(result[0].TokenCriteria, viewAndPostPermissions[0].TokenCriteria)
|
|
|
|
s.Require().Equal(result[0].ChatIds, viewAndPostPermissions[0].ChatIds)
|
|
|
|
}
|
|
|
|
|
2024-05-15 08:43:15 +00:00
|
|
|
func (s *CommunitySuite) TestChannelEncrypted() {
|
|
|
|
org := s.buildCommunity(&s.identity.PublicKey)
|
|
|
|
someChannelID := "some-channel-id"
|
|
|
|
someChatID := org.ChatID(someChannelID)
|
|
|
|
|
|
|
|
s.Require().False(org.ChannelEncrypted(someChannelID))
|
|
|
|
|
|
|
|
_, err := org.UpsertTokenPermission(&protobuf.CommunityTokenPermission{
|
|
|
|
Id: "A",
|
|
|
|
Type: protobuf.CommunityTokenPermission_CAN_VIEW_AND_POST_CHANNEL,
|
|
|
|
TokenCriteria: []*protobuf.TokenCriteria{},
|
|
|
|
ChatIds: []string{someChatID},
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().True(org.channelEncrypted(someChannelID))
|
|
|
|
|
|
|
|
_, err = org.UpsertTokenPermission(&protobuf.CommunityTokenPermission{
|
|
|
|
Id: "B",
|
|
|
|
Type: protobuf.CommunityTokenPermission_CAN_VIEW_CHANNEL,
|
|
|
|
TokenCriteria: []*protobuf.TokenCriteria{&protobuf.TokenCriteria{}},
|
|
|
|
ChatIds: []string{someChatID},
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().True(org.channelEncrypted(someChannelID))
|
|
|
|
|
|
|
|
// Channels with `view` permission without token requirements shouldn't be encrypted.
|
|
|
|
// See: https://github.com/status-im/status-desktop/issues/14748
|
|
|
|
_, err = org.UpsertTokenPermission(&protobuf.CommunityTokenPermission{
|
|
|
|
Id: "C",
|
|
|
|
Type: protobuf.CommunityTokenPermission_CAN_VIEW_CHANNEL,
|
|
|
|
TokenCriteria: []*protobuf.TokenCriteria{},
|
|
|
|
ChatIds: []string{someChatID},
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().False(org.channelEncrypted(someChannelID))
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
func (s *CommunitySuite) emptyCommunityDescription() *protobuf.CommunityDescription {
|
|
|
|
return &protobuf.CommunityDescription{
|
|
|
|
Permissions: &protobuf.CommunityPermissions{},
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) emptyCommunityDescriptionWithChat() *protobuf.CommunityDescription {
|
|
|
|
desc := &protobuf.CommunityDescription{
|
|
|
|
Members: make(map[string]*protobuf.CommunityMember),
|
|
|
|
Clock: 1,
|
|
|
|
Chats: make(map[string]*protobuf.CommunityChat),
|
2021-05-23 13:34:17 +00:00
|
|
|
Categories: make(map[string]*protobuf.CommunityCategory),
|
2020-11-18 09:16:51 +00:00
|
|
|
Permissions: &protobuf.CommunityPermissions{},
|
|
|
|
}
|
|
|
|
|
2021-05-23 13:34:17 +00:00
|
|
|
desc.Categories[testCategoryID1] = &protobuf.CommunityCategory{CategoryId: testCategoryID1, Name: testCategoryName1, Position: 0}
|
|
|
|
desc.Chats[testChatID1] = &protobuf.CommunityChat{Position: 0, Permissions: &protobuf.CommunityPermissions{}, Members: make(map[string]*protobuf.CommunityMember)}
|
2020-11-18 09:16:51 +00:00
|
|
|
desc.Members[common.PubkeyToHex(&s.member1.PublicKey)] = &protobuf.CommunityMember{}
|
|
|
|
desc.Chats[testChatID1].Members[common.PubkeyToHex(&s.member1.PublicKey)] = &protobuf.CommunityMember{}
|
|
|
|
|
|
|
|
return desc
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (s *CommunitySuite) newConfig(identity *ecdsa.PrivateKey, description *protobuf.CommunityDescription) Config {
|
2020-11-18 09:16:51 +00:00
|
|
|
return Config{
|
2021-01-11 10:32:51 +00:00
|
|
|
MemberIdentity: &identity.PublicKey,
|
|
|
|
ID: &identity.PublicKey,
|
2020-11-18 09:16:51 +00:00
|
|
|
CommunityDescription: description,
|
2021-01-11 10:32:51 +00:00
|
|
|
PrivateKey: identity,
|
2023-10-18 15:04:02 +00:00
|
|
|
ControlNode: &identity.PublicKey,
|
2023-09-21 11:16:05 +00:00
|
|
|
ControlDevice: true,
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (s *CommunitySuite) configOnRequest() Config {
|
|
|
|
description := s.emptyCommunityDescription()
|
2023-10-25 13:03:26 +00:00
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_MANUAL_ACCEPT
|
2021-01-11 10:32:51 +00:00
|
|
|
return s.newConfig(s.identity, description)
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
func (s *CommunitySuite) configNoMembershipOrgNoMembershipChat() Config {
|
|
|
|
description := s.emptyCommunityDescriptionWithChat()
|
2023-10-25 13:03:26 +00:00
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_AUTO_ACCEPT
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_AUTO_ACCEPT
|
2021-01-11 10:32:51 +00:00
|
|
|
return s.newConfig(s.identity, description)
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) configNoMembershipOrgOnRequestChat() Config {
|
|
|
|
description := s.emptyCommunityDescriptionWithChat()
|
2023-10-25 13:03:26 +00:00
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_AUTO_ACCEPT
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_MANUAL_ACCEPT
|
2021-01-11 10:32:51 +00:00
|
|
|
return s.newConfig(s.identity, description)
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) configOnRequestOrgOnRequestChat() Config {
|
|
|
|
description := s.emptyCommunityDescriptionWithChat()
|
2023-10-25 13:03:26 +00:00
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_MANUAL_ACCEPT
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_MANUAL_ACCEPT
|
2021-01-11 10:32:51 +00:00
|
|
|
return s.newConfig(s.identity, description)
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) configOnRequestOrgNoMembershipChat() Config {
|
|
|
|
description := s.emptyCommunityDescriptionWithChat()
|
2023-10-25 13:03:26 +00:00
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_MANUAL_ACCEPT
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_AUTO_ACCEPT
|
2021-01-11 10:32:51 +00:00
|
|
|
return s.newConfig(s.identity, description)
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) configChatENSOnly() Config {
|
|
|
|
description := s.emptyCommunityDescriptionWithChat()
|
2023-10-25 13:03:26 +00:00
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_MANUAL_ACCEPT
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_MANUAL_ACCEPT
|
2020-11-18 09:16:51 +00:00
|
|
|
description.Chats[testChatID1].Permissions.EnsOnly = true
|
2021-01-11 10:32:51 +00:00
|
|
|
return s.newConfig(s.identity, description)
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) configENSOnly() Config {
|
|
|
|
description := s.emptyCommunityDescription()
|
2023-10-25 13:03:26 +00:00
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_MANUAL_ACCEPT
|
2020-11-18 09:16:51 +00:00
|
|
|
description.Permissions.EnsOnly = true
|
2021-01-11 10:32:51 +00:00
|
|
|
return s.newConfig(s.identity, description)
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) config() Config {
|
2023-10-25 13:22:12 +00:00
|
|
|
config := s.configOnRequestOrgOnRequestChat()
|
2020-11-18 09:16:51 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) buildCommunityDescription() *protobuf.CommunityDescription {
|
2023-10-25 13:22:12 +00:00
|
|
|
config := s.configOnRequestOrgOnRequestChat()
|
2020-11-18 09:16:51 +00:00
|
|
|
desc := config.CommunityDescription
|
|
|
|
desc.Clock = 1
|
|
|
|
desc.Members = make(map[string]*protobuf.CommunityMember)
|
|
|
|
desc.Members[s.member1Key] = &protobuf.CommunityMember{}
|
|
|
|
desc.Members[s.member2Key] = &protobuf.CommunityMember{}
|
|
|
|
desc.Chats[testChatID1].Members = make(map[string]*protobuf.CommunityMember)
|
|
|
|
desc.Chats[testChatID1].Members[s.member1Key] = &protobuf.CommunityMember{}
|
2021-06-02 08:54:53 +00:00
|
|
|
desc.Chats[testChatID1].Identity = &protobuf.ChatIdentity{
|
|
|
|
DisplayName: "display-name",
|
|
|
|
Description: "description",
|
|
|
|
}
|
2020-11-18 09:16:51 +00:00
|
|
|
return desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) emptyPermissionsCommunityDescription() *protobuf.CommunityDescription {
|
|
|
|
desc := s.buildCommunityDescription()
|
|
|
|
desc.Permissions = nil
|
|
|
|
return desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) emptyChatPermissionsCommunityDescription() *protobuf.CommunityDescription {
|
|
|
|
desc := s.buildCommunityDescription()
|
|
|
|
desc.Chats[testChatID1].Permissions = nil
|
|
|
|
return desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) unknownOrgPermissionsCommunityDescription() *protobuf.CommunityDescription {
|
|
|
|
desc := s.buildCommunityDescription()
|
|
|
|
desc.Permissions.Access = protobuf.CommunityPermissions_UNKNOWN_ACCESS
|
|
|
|
return desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) unknownChatPermissionsCommunityDescription() *protobuf.CommunityDescription {
|
|
|
|
desc := s.buildCommunityDescription()
|
|
|
|
desc.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_UNKNOWN_ACCESS
|
|
|
|
return desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) memberInChatNotInOrgCommunityDescription() *protobuf.CommunityDescription {
|
|
|
|
desc := s.buildCommunityDescription()
|
|
|
|
desc.Chats[testChatID1].Members[s.member3Key] = &protobuf.CommunityMember{}
|
|
|
|
return desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) buildCommunity(owner *ecdsa.PublicKey) *Community {
|
|
|
|
config := s.config()
|
|
|
|
config.ID = owner
|
|
|
|
config.CommunityDescription = s.buildCommunityDescription()
|
|
|
|
|
2023-11-29 17:21:21 +00:00
|
|
|
org, err := New(config, &TimeSourceStub{}, &DescriptionEncryptorMock{})
|
2020-11-18 09:16:51 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
return org
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) identicalCommunityDescription(org *Community) *protobuf.CommunityDescription {
|
|
|
|
description := proto.Clone(org.config.CommunityDescription).(*protobuf.CommunityDescription)
|
|
|
|
description.Clock++
|
|
|
|
return description
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) oldCommunityDescription(org *Community) *protobuf.CommunityDescription {
|
|
|
|
description := proto.Clone(org.config.CommunityDescription).(*protobuf.CommunityDescription)
|
|
|
|
description.Clock--
|
|
|
|
delete(description.Members, s.member1Key)
|
|
|
|
delete(description.Chats[testChatID1].Members, s.member1Key)
|
|
|
|
return description
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) removedMemberCommunityDescription(org *Community) *protobuf.CommunityDescription {
|
|
|
|
description := proto.Clone(org.config.CommunityDescription).(*protobuf.CommunityDescription)
|
|
|
|
description.Clock++
|
|
|
|
delete(description.Members, s.member1Key)
|
|
|
|
delete(description.Chats[testChatID1].Members, s.member1Key)
|
|
|
|
return description
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) addedMemberCommunityDescription(org *Community) *protobuf.CommunityDescription {
|
|
|
|
description := proto.Clone(org.config.CommunityDescription).(*protobuf.CommunityDescription)
|
|
|
|
description.Clock++
|
|
|
|
description.Members[s.member3Key] = &protobuf.CommunityMember{}
|
|
|
|
description.Chats[testChatID1].Members[s.member3Key] = &protobuf.CommunityMember{}
|
|
|
|
|
|
|
|
return description
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) addedChatCommunityDescription(org *Community) *protobuf.CommunityDescription {
|
|
|
|
description := proto.Clone(org.config.CommunityDescription).(*protobuf.CommunityDescription)
|
|
|
|
description.Clock++
|
|
|
|
description.Members[s.member3Key] = &protobuf.CommunityMember{}
|
2021-06-02 08:54:53 +00:00
|
|
|
description.Chats[testChatID2] = &protobuf.CommunityChat{
|
|
|
|
Identity: &protobuf.ChatIdentity{DisplayName: "added-chat", Description: "description"},
|
2023-10-25 13:22:12 +00:00
|
|
|
Permissions: &protobuf.CommunityPermissions{Access: protobuf.CommunityPermissions_MANUAL_ACCEPT},
|
2021-06-02 08:54:53 +00:00
|
|
|
Members: make(map[string]*protobuf.CommunityMember)}
|
2020-11-18 09:16:51 +00:00
|
|
|
description.Chats[testChatID2].Members[s.member3Key] = &protobuf.CommunityMember{}
|
|
|
|
|
|
|
|
return description
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) removedChatCommunityDescription(org *Community) *protobuf.CommunityDescription {
|
|
|
|
description := proto.Clone(org.config.CommunityDescription).(*protobuf.CommunityDescription)
|
|
|
|
description.Clock++
|
|
|
|
delete(description.Chats, testChatID1)
|
|
|
|
|
|
|
|
return description
|
|
|
|
}
|