Skip to content

Commit

Permalink
Tests (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
alwx committed Mar 27, 2024
1 parent 221774a commit 1ec5cd8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
7 changes: 5 additions & 2 deletions protocol/messenger_peersyncing.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ func (m *Messenger) canSyncMessageWith(message peersyncing.SyncMessage, peer *ec
if !ok {
return false, nil
}
key := common.PubkeyToHex(peer)
return chat.HasMember(key), nil
return m.canSyncOneToOneMessageWith(chat, peer)
default:
return false, nil
}
Expand All @@ -318,6 +317,10 @@ func (m *Messenger) canSyncCommunityMessageWith(chat *Chat, community *communiti
return community.IsMemberInChat(peer, chat.CommunityChatID()), nil
}

func (m *Messenger) canSyncOneToOneMessageWith(chat *Chat, peer *ecdsa.PublicKey) (bool, error) {
return chat.HasMember(common.PubkeyToHex(peer)), nil
}

func (m *Messenger) OnDatasyncRequests(requester *ecdsa.PublicKey, messageIDs [][]byte) error {
if len(messageIDs) == 0 {
return nil
Expand Down
48 changes: 46 additions & 2 deletions protocol/messenger_peersyncing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package protocol

import (
"context"
"encoding/hex"
"testing"
"time"

"github.com/stretchr/testify/suite"
"go.uber.org/zap"

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/common"
"github.com/status-im/status-go/protocol/communities"
Expand Down Expand Up @@ -263,9 +265,51 @@ func (s *MessengerPeersyncingSuite) TestCanSyncMessageWith() {
}

func (s *MessengerPeersyncingSuite) TestSyncOneToOne() {
// TODO(alwx):
s.alice.featureFlags.Peersyncing = true
s.owner.featureFlags.Peersyncing = true

pkString := hex.EncodeToString(crypto.FromECDSAPub(&s.alice.identity.PublicKey))
chat := CreateOneToOneChat(pkString, &s.alice.identity.PublicKey, s.owner.transport)

inputMessage := common.NewMessage()
inputMessage.ChatId = chat.ID
chat.LastClockValue = uint64(100000000000000)
err := s.owner.SaveChat(chat)
s.NoError(err)
response, err := s.owner.SendChatMessage(context.Background(), inputMessage)
s.NoError(err)
s.Require().Equal(1, len(response.Messages()), "it returns the message")
}

func (s *MessengerPeersyncingSuite) TestCanSyncOneToOneMessageWith() {
// TODO(alwx):
s.alice.featureFlags.Peersyncing = true
s.owner.featureFlags.Peersyncing = true

pkString := hex.EncodeToString(crypto.FromECDSAPub(&s.alice.identity.PublicKey))
chat := CreateOneToOneChat(pkString, &s.alice.identity.PublicKey, s.owner.transport)

inputMessage := common.NewMessage()
inputMessage.ChatId = chat.ID
chat.LastClockValue = uint64(100000000000000)
err := s.owner.SaveChat(chat)
s.NoError(err)
_, err = s.alice.Join(chat)
s.NoError(err)

syncMessage := peersyncing.SyncMessage{
ID: []byte("test-id"),
ChatID: []byte(chat.ID),
Type: peersyncing.SyncMessageOneToOneType,
Payload: []byte("some-payload"),
Timestamp: 1,
}
s.Require().NoError(s.owner.peersyncing.Add(syncMessage))

canSyncWithBob, err := s.owner.canSyncOneToOneMessageWith(chat, &s.bob.identity.PublicKey)
s.Require().NoError(err)
s.Require().False(canSyncWithBob)

canSyncWithAlice, err := s.owner.canSyncOneToOneMessageWith(chat, &s.alice.identity.PublicKey)
s.Require().NoError(err)
s.Require().True(canSyncWithAlice)
}

0 comments on commit 1ec5cd8

Please sign in to comment.