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{
|
|
|
|
Access: protobuf.CommunityPermissions_NO_MEMBERSHIP,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, 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{
|
|
|
|
Access: protobuf.CommunityPermissions_NO_MEMBERSHIP,
|
|
|
|
Private: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := org.CreateChat(newChatID, &protobuf.CommunityChat{
|
|
|
|
Identity: identity,
|
|
|
|
Permissions: permissions,
|
|
|
|
})
|
|
|
|
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{
|
|
|
|
Access: protobuf.CommunityPermissions_NO_MEMBERSHIP,
|
|
|
|
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{
|
|
|
|
Identity: editedIdentity,
|
|
|
|
Permissions: editedPermissions,
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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: "invitation-only",
|
|
|
|
config: s.configInvitationOnly(),
|
|
|
|
signer: signer,
|
|
|
|
request: request,
|
|
|
|
err: ErrCantRequestAccess,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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-INVITATION_ONLY = error as it's invitation only
|
|
|
|
{
|
|
|
|
name: "no-membership org with no-membeship chat",
|
|
|
|
config: s.configNoMembershipOrgInvitationOnlyChat(),
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
// INVITATION_ONLY-INVITATION_ONLY error as it's invitation only
|
|
|
|
{
|
|
|
|
name: "invitation-only org with invitation-only chat",
|
|
|
|
config: s.configInvitationOnlyOrgInvitationOnlyChat(),
|
|
|
|
signer: signer,
|
|
|
|
request: requestWithChatID,
|
|
|
|
err: ErrCantRequestAccess,
|
|
|
|
},
|
|
|
|
// ON_REQUEST-INVITATION_ONLY error as it's invitation only
|
|
|
|
{
|
|
|
|
name: "on-request org with invitation-only chat",
|
|
|
|
config: s.configOnRequestOrgInvitationOnlyChat(),
|
|
|
|
signer: signer,
|
|
|
|
request: requestWithChatID,
|
|
|
|
err: ErrCantRequestAccess,
|
|
|
|
},
|
|
|
|
// ON_REQUEST-INVITATION_ONLY error as it's invitation only
|
|
|
|
{
|
|
|
|
name: "on-request org with invitation-only chat",
|
|
|
|
config: s.configOnRequestOrgInvitationOnlyChat(),
|
|
|
|
signer: signer,
|
|
|
|
request: requestWithChatID,
|
|
|
|
err: ErrCantRequestAccess,
|
|
|
|
},
|
|
|
|
// 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-09-28 15:37:03 +00:00
|
|
|
org, err := New(tc.config, &TimeSourceStub{})
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) TestCanPost() {
|
|
|
|
validGrant := 1
|
|
|
|
invalidGrant := 2
|
|
|
|
|
|
|
|
notMember := &s.member3.PublicKey
|
|
|
|
member := &s.member1.PublicKey
|
|
|
|
|
|
|
|
// MEMBERSHIP-NO-MEMBERSHIP-Member-> User can post
|
|
|
|
// MEMBERSHIP-NO-MEMEBRESHIP->NON member -> User can't post
|
|
|
|
// MEMBERSHIP-NO-MEMBERSHIP-Grant -> user can post
|
|
|
|
// MEMBERSHIP-NO-MEMBERSHIP-old-grant -> user can't post
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
config Config
|
|
|
|
member *ecdsa.PublicKey
|
|
|
|
err error
|
|
|
|
grant int
|
|
|
|
canPost bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no-membership org with no-membeship chat",
|
|
|
|
config: s.configNoMembershipOrgNoMembershipChat(),
|
|
|
|
member: notMember,
|
|
|
|
canPost: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-membership org with invitation only chat-not-a-member",
|
|
|
|
config: s.configNoMembershipOrgInvitationOnlyChat(),
|
|
|
|
member: notMember,
|
|
|
|
canPost: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-membership org with invitation only chat member",
|
|
|
|
config: s.configNoMembershipOrgInvitationOnlyChat(),
|
|
|
|
member: member,
|
|
|
|
canPost: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-membership org with invitation only chat-not-a-member valid grant",
|
|
|
|
config: s.configNoMembershipOrgInvitationOnlyChat(),
|
|
|
|
member: notMember,
|
|
|
|
canPost: true,
|
|
|
|
grant: validGrant,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-membership org with invitation only chat-not-a-member invalid grant",
|
|
|
|
config: s.configNoMembershipOrgInvitationOnlyChat(),
|
|
|
|
member: notMember,
|
|
|
|
canPost: false,
|
|
|
|
grant: invalidGrant,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "membership org with no-membership chat not-a-member valid grant",
|
|
|
|
config: s.configOnRequestOrgNoMembershipChat(),
|
|
|
|
member: notMember,
|
|
|
|
canPost: true,
|
|
|
|
grant: validGrant,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "membership org with no-membership chat not-a-member invalid grant",
|
|
|
|
config: s.configOnRequestOrgNoMembershipChat(),
|
|
|
|
member: notMember,
|
|
|
|
canPost: false,
|
|
|
|
grant: invalidGrant,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "monsier creator can always post of course",
|
|
|
|
config: s.configOnRequestOrgNoMembershipChat(),
|
|
|
|
member: &s.identity.PublicKey,
|
|
|
|
canPost: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.name, func() {
|
|
|
|
var grant []byte
|
|
|
|
var err error
|
2023-09-28 15:37:03 +00:00
|
|
|
org, err := New(tc.config, &TimeSourceStub{})
|
2020-11-18 09:16:51 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
if tc.grant == validGrant {
|
|
|
|
grant, err = org.buildGrant(tc.member, testChatID1)
|
|
|
|
// We lower the clock of the description to simulate
|
|
|
|
// a valid use case
|
|
|
|
org.config.CommunityDescription.Clock--
|
|
|
|
s.Require().NoError(err)
|
|
|
|
} else if tc.grant == invalidGrant {
|
|
|
|
grant, err = org.buildGrant(&s.member2.PublicKey, testChatID1)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
canPost, err := org.CanPost(tc.member, testChatID1, grant)
|
|
|
|
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"},
|
|
|
|
Permissions: &protobuf.CommunityPermissions{Access: protobuf.CommunityPermissions_INVITATION_ONLY},
|
|
|
|
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-08-22 17:48:42 +00:00
|
|
|
actualChanges, err := org.UpdateCommunityDescription(tc.description(org), []byte{0x01})
|
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)
|
|
|
|
}
|
|
|
|
|
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,
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (s *CommunitySuite) configOnRequest() Config {
|
|
|
|
description := s.emptyCommunityDescription()
|
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_ON_REQUEST
|
|
|
|
return s.newConfig(s.identity, description)
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
func (s *CommunitySuite) configInvitationOnly() Config {
|
|
|
|
description := s.emptyCommunityDescription()
|
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_INVITATION_ONLY
|
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()
|
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_NO_MEMBERSHIP
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_NO_MEMBERSHIP
|
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) configNoMembershipOrgInvitationOnlyChat() Config {
|
|
|
|
description := s.emptyCommunityDescriptionWithChat()
|
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_NO_MEMBERSHIP
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_INVITATION_ONLY
|
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) configInvitationOnlyOrgInvitationOnlyChat() Config {
|
|
|
|
description := s.emptyCommunityDescriptionWithChat()
|
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_INVITATION_ONLY
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_INVITATION_ONLY
|
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()
|
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_NO_MEMBERSHIP
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_ON_REQUEST
|
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()
|
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_ON_REQUEST
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_ON_REQUEST
|
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) configOnRequestOrgInvitationOnlyChat() Config {
|
|
|
|
description := s.emptyCommunityDescriptionWithChat()
|
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_ON_REQUEST
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_INVITATION_ONLY
|
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()
|
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_ON_REQUEST
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_NO_MEMBERSHIP
|
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()
|
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_ON_REQUEST
|
|
|
|
description.Chats[testChatID1].Permissions.Access = protobuf.CommunityPermissions_ON_REQUEST
|
|
|
|
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()
|
|
|
|
description.Permissions.Access = protobuf.CommunityPermissions_ON_REQUEST
|
|
|
|
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 {
|
|
|
|
config := s.configOnRequestOrgInvitationOnlyChat()
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommunitySuite) buildCommunityDescription() *protobuf.CommunityDescription {
|
|
|
|
config := s.configOnRequestOrgInvitationOnlyChat()
|
|
|
|
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-09-28 15:37:03 +00:00
|
|
|
org, err := New(config, &TimeSourceStub{})
|
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"},
|
|
|
|
Permissions: &protobuf.CommunityPermissions{Access: protobuf.CommunityPermissions_INVITATION_ONLY},
|
|
|
|
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
|
|
|
|
}
|