Skip to content

Commit

Permalink
feat: fix and test CreateCommunityURLWithData
Browse files Browse the repository at this point in the history
  • Loading branch information
MishkaRogachev committed Jun 23, 2023
1 parent 2cbcf2a commit 6217aed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
11 changes: 5 additions & 6 deletions protocol/messenger_share_urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,25 @@ func (m *Messenger) DeserializePublicKey(compressedKey string) (types.HexBytes,
return crypto.CompressPubkey(pubKey), nil
}

func (m *Messenger) CreateCommunityURLWithChatKey(communityID string) (string, error) {
func (m *Messenger) CreateCommunityURLWithChatKey(communityID types.HexBytes) (string, error) {
if len(communityID) == 0 {
return "", ErrChatIDEmpty
}

community, err := m.GetCommunityByID(types.HexBytes(communityID))

community, err := m.GetCommunityByID(communityID)
if err != nil {
return "", err
}

if community == nil {
return "", errors.New("community is nil")
return "", fmt.Errorf("no community found with id: %s", communityID)
}

pubKey, err := common.HexToPubkey(communityID)
pubKey, err := m.SerializePublicKey(communityID)
if err != nil {
return "", err
}
return fmt.Sprintf(baseShareUrl, "/c", "#", types.EncodeHex(crypto.CompressPubkey(pubKey))), nil
return fmt.Sprintf(baseShareUrl, "c", "#", pubKey), nil
}

func (m *Messenger) CreateCommunityURLWithData(communityID string) (string, error) {
Expand Down
32 changes: 32 additions & 0 deletions protocol/messenger_share_urls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package protocol

import (
"crypto/ecdsa"
"fmt"
"strings"
"testing"

Expand All @@ -11,6 +12,9 @@ import (
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
Expand Down Expand Up @@ -57,6 +61,21 @@ func (s *MessengerShareUrlsSuite) newMessenger() *Messenger {
return messenger
}

func (s *MessengerShareUrlsSuite) createCommunity() *communities.Community {
description := &requests.CreateCommunity{
Membership: protobuf.CommunityPermissions_NO_MEMBERSHIP,
Name: "status",
Color: "#ffffff",
Description: "status community description",
}

// Create an community chat
response, err := s.m.CreateCommunity(description, true)
s.Require().NoError(err)
s.Require().NotNil(response)
return response.Communities()[0]
}

func (s *MessengerShareUrlsSuite) TestSerializePublicKey() {
key, err := crypto.GenerateKey()
s.Require().NoError(err)
Expand All @@ -77,3 +96,16 @@ func (s *MessengerShareUrlsSuite) TestDeserializePublicKey() {
s.Require().Len(publicKey, 33)
s.Require().True(strings.HasPrefix(publicKey.String(), "0x"))
}

func (s *MessengerShareUrlsSuite) TestCreateCommunityURLWithData() {
community := s.createCommunity()

url, err := s.m.CreateCommunityURLWithChatKey(community.ID())
s.Require().NoError(err)

publicKey, err := s.m.SerializePublicKey(community.ID())
s.Require().NoError(err)

expectedUrl := fmt.Sprintf(baseShareUrl, "c", "#", publicKey)
s.Require().Equal(expectedUrl, url)
}

0 comments on commit 6217aed

Please sign in to comment.