Don't panic if config is nil in community

I have encountered a crash in the app after syncing, looks like a
community has been created without a Config.
This crashes the app after the user logs in.
This commit prevents the app from crashing, but does not fix the
underlaying issue (that's something I will have to investigate).
Added tests to validate the behavior.

Check community exists
This commit is contained in:
Andrea Maria Piana 2023-07-12 09:52:32 +01:00
parent 40eed0fd01
commit 3f5f87d1d1
3 changed files with 43 additions and 1 deletions

View File

@ -723,7 +723,7 @@ func (o *Community) hasMemberPermission(member *protobuf.CommunityMember, permis
} }
func (o *Community) hasPermission(pk *ecdsa.PublicKey, roles map[protobuf.CommunityMember_Roles]bool) bool { func (o *Community) hasPermission(pk *ecdsa.PublicKey, roles map[protobuf.CommunityMember_Roles]bool) bool {
if pk == nil || o.config.ID == nil { if pk == nil || o.config == nil || o.config.ID == nil {
return false return false
} }

View File

@ -61,6 +61,44 @@ func (s *CommunitySuite) SetupTest() {
} }
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)
s.Require().False(community.hasPermission(nil, adminRolePermissions()))
// returns false if key is passed, but config is nil
s.Require().False(community.hasPermission(&nonMemberKey.PublicKey, adminRolePermissions()))
// returns true if the user is the owner
communityDescription := &protobuf.CommunityDescription{}
communityDescription.Members = make(map[string]*protobuf.CommunityMember)
communityDescription.Members[common.PubkeyToHex(&memberKey.PublicKey)] = &protobuf.CommunityMember{Roles: []protobuf.CommunityMember_Roles{protobuf.CommunityMember_ROLE_ADMIN}}
community.config = &Config{ID: &ownerKey.PublicKey, CommunityDescription: communityDescription}
s.Require().True(community.hasPermission(&ownerKey.PublicKey, adminRolePermissions()))
// return false if user is not a member
s.Require().False(community.hasPermission(&nonMemberKey.PublicKey, adminRolePermissions()))
// return true if user is a member and has permissions
s.Require().True(community.hasPermission(&memberKey.PublicKey, adminRolePermissions()))
// return false if user is a member and does not have permissions
s.Require().False(community.hasPermission(&memberKey.PublicKey, ownerRolePermission()))
}
func (s *CommunitySuite) TestInviteUserToOrg() { func (s *CommunitySuite) TestInviteUserToOrg() {
newMember, err := crypto.GenerateKey() newMember, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)

View File

@ -1369,6 +1369,10 @@ func (m *Manager) HandleCommunityAdminEvent(signer *ecdsa.PublicKey, adminEvent
return nil, err return nil, err
} }
if community == nil {
return nil, ErrOrgNotFound
}
if !community.IsMemberAdmin(signer) { if !community.IsMemberAdmin(signer) {
return nil, errors.New("user is not an admin") return nil, errors.New("user is not an admin")
} }