2020-07-07 09:00:04 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2020-07-20 12:46:15 +00:00
|
|
|
"encoding/hex"
|
2020-07-16 08:36:17 +00:00
|
|
|
"errors"
|
2020-07-07 09:00:04 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
2023-07-13 10:44:09 +00:00
|
|
|
"go.uber.org/zap"
|
2020-07-07 09:00:04 +00:00
|
|
|
|
|
|
|
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"
|
2020-07-20 14:14:42 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
2021-03-31 16:23:45 +00:00
|
|
|
"github.com/status-im/status-go/protocol/communities"
|
2020-07-30 08:24:30 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2020-07-22 07:41:40 +00:00
|
|
|
"github.com/status-im/status-go/protocol/pushnotificationclient"
|
|
|
|
"github.com/status-im/status-go/protocol/pushnotificationserver"
|
2021-03-31 16:23:45 +00:00
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
2020-07-07 09:00:04 +00:00
|
|
|
"github.com/status-im/status-go/protocol/tt"
|
2020-07-21 15:41:10 +00:00
|
|
|
"github.com/status-im/status-go/waku"
|
2020-07-07 09:00:04 +00:00
|
|
|
)
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
const (
|
|
|
|
bob1DeviceToken = "token-1"
|
|
|
|
bob2DeviceToken = "token-2"
|
2020-07-30 08:24:30 +00:00
|
|
|
testAPNTopic = "topic"
|
2020-07-22 07:41:40 +00:00
|
|
|
)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
func TestMessengerPushNotificationSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(MessengerPushNotificationSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessengerPushNotificationSuite struct {
|
2023-07-13 10:44:09 +00:00
|
|
|
suite.Suite
|
|
|
|
m *Messenger // main instance of Messenger
|
|
|
|
privateKey *ecdsa.PrivateKey // private key for the main instance of Messenger
|
|
|
|
// If one wants to send messages between different instances of Messenger,
|
|
|
|
// a single Waku service should be shared.
|
|
|
|
shh types.Waku
|
|
|
|
logger *zap.Logger
|
2020-07-07 09:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerPushNotificationSuite) SetupTest() {
|
|
|
|
s.logger = tt.MustCreateTestLogger()
|
|
|
|
|
2020-07-21 15:41:10 +00:00
|
|
|
config := waku.DefaultConfig
|
|
|
|
config.MinimumAcceptedPoW = 0
|
|
|
|
shh := waku.New(&config, s.logger)
|
|
|
|
s.shh = gethbridge.NewGethWakuWrapper(shh)
|
2021-07-09 13:19:33 +00:00
|
|
|
s.Require().NoError(shh.Start())
|
2020-07-07 09:00:04 +00:00
|
|
|
|
|
|
|
s.m = s.newMessenger(s.shh)
|
|
|
|
s.privateKey = s.m.identity
|
2021-01-14 22:15:13 +00:00
|
|
|
_, err := s.m.Start()
|
|
|
|
s.Require().NoError(err)
|
2020-07-07 09:00:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 15:41:10 +00:00
|
|
|
func (s *MessengerPushNotificationSuite) TearDownTest() {
|
|
|
|
s.Require().NoError(s.m.Shutdown())
|
|
|
|
_ = s.logger.Sync()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerPushNotificationSuite) newMessenger(shh types.Waku) *Messenger {
|
2020-07-07 09:00:04 +00:00
|
|
|
privateKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-12-21 11:57:47 +00:00
|
|
|
messenger, err := newMessengerWithKey(s.shh, privateKey, s.logger, []Option{WithPushNotifications()})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
return messenger
|
2020-07-07 09:00:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
func (s *MessengerPushNotificationSuite) newPushNotificationServer(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
|
2020-07-07 13:55:24 +00:00
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
serverConfig := &pushnotificationserver.Config{
|
2020-08-20 07:26:00 +00:00
|
|
|
Enabled: true,
|
2020-07-07 13:55:24 +00:00
|
|
|
Logger: s.logger,
|
|
|
|
Identity: privateKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
options := []Option{
|
|
|
|
WithPushNotificationServerConfig(serverConfig),
|
|
|
|
}
|
2020-12-21 11:57:47 +00:00
|
|
|
messenger, err := newMessengerWithKey(shh, privateKey, s.logger, options)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
return messenger
|
2020-07-07 13:55:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
func (s *MessengerPushNotificationSuite) TestReceivePushNotification() {
|
2020-07-07 13:55:24 +00:00
|
|
|
|
2020-07-09 16:52:26 +00:00
|
|
|
bob1 := s.m
|
2020-12-21 11:57:47 +00:00
|
|
|
bob2, err := newMessengerWithKey(s.shh, s.m.identity, s.logger, []Option{WithPushNotifications()})
|
|
|
|
s.Require().NoError(err)
|
2023-06-29 10:28:42 +00:00
|
|
|
defer bob2.Shutdown() // nolint: errcheck
|
2020-07-22 07:41:40 +00:00
|
|
|
|
|
|
|
serverKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
server := s.newPushNotificationServer(s.shh, serverKey)
|
2023-06-29 10:28:42 +00:00
|
|
|
defer server.Shutdown() // nolint: errcheck
|
2020-07-22 07:41:40 +00:00
|
|
|
|
2020-07-10 13:26:06 +00:00
|
|
|
alice := s.newMessenger(s.shh)
|
2020-07-20 12:46:15 +00:00
|
|
|
// start alice and enable sending push notifications
|
2021-01-14 22:15:13 +00:00
|
|
|
_, err = alice.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer alice.Shutdown() // nolint: errcheck
|
2020-07-20 12:46:15 +00:00
|
|
|
s.Require().NoError(alice.EnableSendingPushNotifications())
|
2020-07-10 13:26:06 +00:00
|
|
|
bobInstallationIDs := []string{bob1.installationID, bob2.installationID}
|
2020-07-07 09:00:04 +00:00
|
|
|
|
2020-07-09 16:52:26 +00:00
|
|
|
// Register bob1
|
2020-08-20 09:05:39 +00:00
|
|
|
err = bob1.AddPushNotificationsServer(context.Background(), &server.identity.PublicKey, pushnotificationclient.ServerTypeCustom)
|
2020-07-07 09:00:04 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-30 08:24:30 +00:00
|
|
|
err = bob1.RegisterForPushNotifications(context.Background(), bob1DeviceToken, testAPNTopic, protobuf.PushNotificationRegistration_APN_TOKEN)
|
2020-07-07 13:55:24 +00:00
|
|
|
|
2020-07-17 11:41:49 +00:00
|
|
|
// Pull servers and check we registered
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
2020-07-20 08:06:24 +00:00
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = bob1.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-17 11:41:49 +00:00
|
|
|
registered, err := bob1.RegisteredForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !registered {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
2021-01-18 09:12:03 +00:00
|
|
|
bobServers, err := bob1.GetPushNotificationsServers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bobServers) == 0 {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
|
2020-07-17 11:41:49 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// Make sure we receive it
|
|
|
|
s.Require().NoError(err)
|
2020-08-20 09:05:39 +00:00
|
|
|
bob1Servers, err := bob1.GetPushNotificationsServers()
|
2020-07-17 11:41:49 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Register bob2
|
2020-08-20 09:05:39 +00:00
|
|
|
err = bob2.AddPushNotificationsServer(context.Background(), &server.identity.PublicKey, pushnotificationclient.ServerTypeCustom)
|
2020-07-17 11:41:49 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-30 08:24:30 +00:00
|
|
|
err = bob2.RegisterForPushNotifications(context.Background(), bob2DeviceToken, testAPNTopic, protobuf.PushNotificationRegistration_APN_TOKEN)
|
2020-07-17 11:41:49 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
2020-07-20 08:06:24 +00:00
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = bob2.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-17 11:41:49 +00:00
|
|
|
registered, err := bob2.RegisteredForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !registered {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
2021-01-18 09:12:03 +00:00
|
|
|
bobServers, err := bob2.GetPushNotificationsServers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bobServers) == 0 {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
|
2020-07-17 11:41:49 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// Make sure we receive it
|
|
|
|
s.Require().NoError(err)
|
2020-08-20 09:05:39 +00:00
|
|
|
bob2Servers, err := bob2.GetPushNotificationsServers()
|
2020-07-17 11:41:49 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-20 12:46:15 +00:00
|
|
|
// Create one to one chat & send message
|
|
|
|
pkString := hex.EncodeToString(crypto.FromECDSAPub(&s.m.identity.PublicKey))
|
|
|
|
chat := CreateOneToOneChat(pkString, &s.m.identity.PublicKey, alice.transport)
|
2021-01-11 10:32:51 +00:00
|
|
|
s.Require().NoError(alice.SaveChat(chat))
|
|
|
|
inputMessage := buildTestMessage(*chat)
|
2020-07-20 14:14:42 +00:00
|
|
|
response, err := alice.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.Require().NoError(err)
|
2021-06-03 13:11:55 +00:00
|
|
|
messageIDString := response.Messages()[0].ID
|
2020-07-20 14:14:42 +00:00
|
|
|
messageID, err := hex.DecodeString(messageIDString[2:])
|
2020-07-17 11:41:49 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2021-01-15 11:08:03 +00:00
|
|
|
infoMap := make(map[string]*pushnotificationclient.PushNotificationInfo)
|
2020-07-20 08:06:24 +00:00
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-17 11:41:49 +00:00
|
|
|
|
2021-01-15 11:08:03 +00:00
|
|
|
info, err := alice.pushNotificationClient.GetPushNotificationInfo(&bob1.identity.PublicKey, bobInstallationIDs)
|
2020-07-20 08:06:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-15 11:08:03 +00:00
|
|
|
for _, i := range info {
|
|
|
|
infoMap[i.AccessToken] = i
|
|
|
|
}
|
|
|
|
|
2020-07-20 08:06:24 +00:00
|
|
|
// Check we have replies for both bob1 and bob2
|
2021-01-15 11:08:03 +00:00
|
|
|
if len(infoMap) != 2 {
|
2020-07-20 08:06:24 +00:00
|
|
|
return errors.New("info not fetched")
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-17 11:41:49 +00:00
|
|
|
|
2020-07-20 08:06:24 +00:00
|
|
|
})
|
2020-07-17 11:41:49 +00:00
|
|
|
|
2021-01-15 11:08:03 +00:00
|
|
|
s.Require().Len(infoMap, 2)
|
2020-07-17 11:41:49 +00:00
|
|
|
|
|
|
|
// Check we have replies for both bob1 and bob2
|
2020-07-22 07:41:40 +00:00
|
|
|
var bob1Info, bob2Info *pushnotificationclient.PushNotificationInfo
|
2020-07-17 11:41:49 +00:00
|
|
|
|
2021-01-15 11:08:03 +00:00
|
|
|
bob1Info = infoMap[bob1Servers[0].AccessToken]
|
|
|
|
bob2Info = infoMap[bob2Servers[0].AccessToken]
|
2020-07-17 11:41:49 +00:00
|
|
|
|
|
|
|
s.Require().NotNil(bob1Info)
|
|
|
|
s.Require().Equal(bob1.installationID, bob1Info.InstallationID)
|
2020-07-20 08:32:00 +00:00
|
|
|
s.Require().Equal(bob1Servers[0].AccessToken, bob1Info.AccessToken)
|
2020-07-17 11:41:49 +00:00
|
|
|
s.Require().Equal(&bob1.identity.PublicKey, bob1Info.PublicKey)
|
|
|
|
|
|
|
|
s.Require().NotNil(bob2Info)
|
|
|
|
s.Require().Equal(bob2.installationID, bob2Info.InstallationID)
|
|
|
|
s.Require().Equal(bob2Servers[0].AccessToken, bob2Info.AccessToken)
|
|
|
|
s.Require().Equal(&bob2.identity.PublicKey, bob2Info.PublicKey)
|
|
|
|
|
|
|
|
retrievedNotificationInfo, err := alice.pushNotificationClient.GetPushNotificationInfo(&bob1.identity.PublicKey, bobInstallationIDs)
|
2020-07-20 08:32:00 +00:00
|
|
|
|
2020-07-17 11:41:49 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(retrievedNotificationInfo)
|
|
|
|
s.Require().Len(retrievedNotificationInfo, 2)
|
2020-07-20 14:14:42 +00:00
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
var sentNotification *pushnotificationclient.SentNotification
|
2020-07-20 14:14:42 +00:00
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sentNotification, err = alice.pushNotificationClient.GetSentNotification(common.HashPublicKey(&bob1.identity.PublicKey), bob1.installationID, messageID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sentNotification == nil {
|
|
|
|
return errors.New("sent notification not found")
|
|
|
|
}
|
|
|
|
if !sentNotification.Success {
|
|
|
|
return errors.New("sent notification not successul")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-07-20 14:52:55 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-17 11:41:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerPushNotificationSuite) TestReceivePushNotificationFromContactOnly() {
|
|
|
|
|
2020-07-20 08:32:00 +00:00
|
|
|
bob := s.m
|
2020-07-22 07:41:40 +00:00
|
|
|
|
|
|
|
serverKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
server := s.newPushNotificationServer(s.shh, serverKey)
|
2023-06-29 10:28:42 +00:00
|
|
|
defer server.Shutdown() // nolint: errcheck
|
2020-07-22 07:41:40 +00:00
|
|
|
|
2020-07-17 11:41:49 +00:00
|
|
|
alice := s.newMessenger(s.shh)
|
2020-07-20 12:46:15 +00:00
|
|
|
// start alice and enable push notifications
|
2021-01-14 22:15:13 +00:00
|
|
|
_, err = alice.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer alice.Shutdown() // nolint: errcheck
|
2020-07-20 12:46:15 +00:00
|
|
|
s.Require().NoError(alice.EnableSendingPushNotifications())
|
2020-07-20 13:58:54 +00:00
|
|
|
bobInstallationIDs := []string{bob.installationID}
|
2020-07-17 11:41:49 +00:00
|
|
|
|
2020-07-20 08:32:00 +00:00
|
|
|
// Register bob
|
2020-08-20 09:05:39 +00:00
|
|
|
err = bob.AddPushNotificationsServer(context.Background(), &server.identity.PublicKey, pushnotificationclient.ServerTypeCustom)
|
2020-07-17 11:41:49 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-20 08:32:00 +00:00
|
|
|
// Add alice has a contact
|
|
|
|
aliceContact := &Contact{
|
2023-01-20 14:28:30 +00:00
|
|
|
ID: types.EncodeHex(crypto.FromECDSAPub(&alice.identity.PublicKey)),
|
|
|
|
EnsName: "Some Contact",
|
|
|
|
ContactRequestLocalState: ContactRequestStateSent,
|
2020-07-20 08:32:00 +00:00
|
|
|
}
|
2020-07-09 16:52:26 +00:00
|
|
|
|
2022-10-14 08:50:36 +00:00
|
|
|
_, err = bob.AddContact(context.Background(), &requests.AddContact{ID: aliceContact.ID})
|
2020-07-16 08:36:17 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-09 16:52:26 +00:00
|
|
|
|
2020-07-20 08:32:00 +00:00
|
|
|
// Enable from contacts only
|
|
|
|
err = bob.EnablePushNotificationsFromContactsOnly()
|
2020-07-09 16:52:26 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-30 08:24:30 +00:00
|
|
|
err = bob.RegisterForPushNotifications(context.Background(), bob1DeviceToken, testAPNTopic, protobuf.PushNotificationRegistration_APN_TOKEN)
|
2020-07-16 08:36:17 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-09 16:52:26 +00:00
|
|
|
|
2020-07-20 08:32:00 +00:00
|
|
|
// Pull servers and check we registered
|
2020-07-16 08:36:17 +00:00
|
|
|
err = tt.RetryWithBackOff(func() error {
|
2020-07-20 08:06:24 +00:00
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-20 08:32:00 +00:00
|
|
|
_, err = bob.RetrieveAll()
|
2020-07-20 08:06:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-20 08:32:00 +00:00
|
|
|
registered, err := bob.RegisteredForPushNotifications()
|
2020-07-16 08:36:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !registered {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
2021-01-18 09:12:03 +00:00
|
|
|
bobServers, err := bob.GetPushNotificationsServers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bobServers) == 0 {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
|
2020-07-16 08:36:17 +00:00
|
|
|
return nil
|
|
|
|
})
|
2020-07-09 16:52:26 +00:00
|
|
|
// Make sure we receive it
|
|
|
|
s.Require().NoError(err)
|
2020-08-20 09:05:39 +00:00
|
|
|
bobServers, err := bob.GetPushNotificationsServers()
|
2020-07-16 08:36:17 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-09 16:52:26 +00:00
|
|
|
|
2020-07-20 12:46:15 +00:00
|
|
|
// Create one to one chat & send message
|
|
|
|
pkString := hex.EncodeToString(crypto.FromECDSAPub(&s.m.identity.PublicKey))
|
|
|
|
chat := CreateOneToOneChat(pkString, &s.m.identity.PublicKey, alice.transport)
|
2021-01-11 10:32:51 +00:00
|
|
|
s.Require().NoError(alice.SaveChat(chat))
|
|
|
|
inputMessage := buildTestMessage(*chat)
|
2020-07-20 14:14:42 +00:00
|
|
|
response, err := alice.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.Require().NoError(err)
|
2021-06-03 13:11:55 +00:00
|
|
|
messageIDString := response.Messages()[0].ID
|
2020-07-20 14:14:42 +00:00
|
|
|
messageID, err := hex.DecodeString(messageIDString[2:])
|
2020-07-10 13:26:06 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-09 16:52:26 +00:00
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
var info []*pushnotificationclient.PushNotificationInfo
|
2020-07-20 08:06:24 +00:00
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-09 16:52:26 +00:00
|
|
|
|
2020-07-20 08:32:00 +00:00
|
|
|
info, err = alice.pushNotificationClient.GetPushNotificationInfo(&bob.identity.PublicKey, bobInstallationIDs)
|
2020-07-20 08:06:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-20 08:32:00 +00:00
|
|
|
// Check we have replies for bob
|
|
|
|
if len(info) != 1 {
|
2020-07-20 08:06:24 +00:00
|
|
|
return errors.New("info not fetched")
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-10 13:26:06 +00:00
|
|
|
|
2020-07-20 08:06:24 +00:00
|
|
|
})
|
2020-07-07 09:00:04 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-09 16:52:26 +00:00
|
|
|
|
2020-07-20 08:32:00 +00:00
|
|
|
s.Require().NotNil(info)
|
|
|
|
s.Require().Equal(bob.installationID, info[0].InstallationID)
|
|
|
|
s.Require().Equal(bobServers[0].AccessToken, info[0].AccessToken)
|
|
|
|
s.Require().Equal(&bob.identity.PublicKey, info[0].PublicKey)
|
2020-07-09 16:52:26 +00:00
|
|
|
|
2020-07-20 08:32:00 +00:00
|
|
|
retrievedNotificationInfo, err := alice.pushNotificationClient.GetPushNotificationInfo(&bob.identity.PublicKey, bobInstallationIDs)
|
2020-07-10 13:26:06 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(retrievedNotificationInfo)
|
2020-07-20 08:32:00 +00:00
|
|
|
s.Require().Len(retrievedNotificationInfo, 1)
|
2020-07-20 14:14:42 +00:00
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
var sentNotification *pushnotificationclient.SentNotification
|
2020-07-20 14:14:42 +00:00
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sentNotification, err = alice.pushNotificationClient.GetSentNotification(common.HashPublicKey(&bob.identity.PublicKey), bob.installationID, messageID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sentNotification == nil {
|
|
|
|
return errors.New("sent notification not found")
|
|
|
|
}
|
|
|
|
if !sentNotification.Success {
|
|
|
|
return errors.New("sent notification not successul")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-07-20 14:52:55 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-07 09:00:04 +00:00
|
|
|
}
|
2020-07-20 13:58:54 +00:00
|
|
|
|
|
|
|
func (s *MessengerPushNotificationSuite) TestReceivePushNotificationRetries() {
|
|
|
|
|
|
|
|
bob := s.m
|
2020-07-22 07:41:40 +00:00
|
|
|
|
|
|
|
serverKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
server := s.newPushNotificationServer(s.shh, serverKey)
|
2023-06-29 10:28:42 +00:00
|
|
|
defer server.Shutdown() // nolint: errcheck
|
2020-07-22 07:41:40 +00:00
|
|
|
|
2020-07-20 13:58:54 +00:00
|
|
|
alice := s.newMessenger(s.shh)
|
|
|
|
// another contact to invalidate the token
|
|
|
|
frank := s.newMessenger(s.shh)
|
2021-01-14 22:15:13 +00:00
|
|
|
_, err = frank.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer frank.Shutdown() // nolint: errcheck
|
2020-07-20 13:58:54 +00:00
|
|
|
// start alice and enable push notifications
|
2021-01-14 22:15:13 +00:00
|
|
|
_, err = alice.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer alice.Shutdown() // nolint: errcheck
|
2020-07-20 13:58:54 +00:00
|
|
|
s.Require().NoError(alice.EnableSendingPushNotifications())
|
|
|
|
bobInstallationIDs := []string{bob.installationID}
|
|
|
|
|
|
|
|
// Register bob
|
2020-08-20 09:05:39 +00:00
|
|
|
err = bob.AddPushNotificationsServer(context.Background(), &server.identity.PublicKey, pushnotificationclient.ServerTypeCustom)
|
2020-07-20 13:58:54 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Add alice has a contact
|
|
|
|
aliceContact := &Contact{
|
2023-01-20 14:28:30 +00:00
|
|
|
ID: types.EncodeHex(crypto.FromECDSAPub(&alice.identity.PublicKey)),
|
|
|
|
EnsName: "Some Contact",
|
|
|
|
ContactRequestLocalState: ContactRequestStateSent,
|
2020-07-20 13:58:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 08:50:36 +00:00
|
|
|
_, err = bob.AddContact(context.Background(), &requests.AddContact{ID: aliceContact.ID})
|
2020-07-20 13:58:54 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Add frank has a contact
|
|
|
|
frankContact := &Contact{
|
2023-01-20 14:28:30 +00:00
|
|
|
ID: types.EncodeHex(crypto.FromECDSAPub(&frank.identity.PublicKey)),
|
|
|
|
EnsName: "Some Contact",
|
|
|
|
ContactRequestLocalState: ContactRequestStateSent,
|
2020-07-20 13:58:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 08:50:36 +00:00
|
|
|
_, err = bob.AddContact(context.Background(), &requests.AddContact{ID: frankContact.ID})
|
2020-07-20 13:58:54 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Enable from contacts only
|
|
|
|
err = bob.EnablePushNotificationsFromContactsOnly()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-30 08:24:30 +00:00
|
|
|
err = bob.RegisterForPushNotifications(context.Background(), bob1DeviceToken, testAPNTopic, protobuf.PushNotificationRegistration_APN_TOKEN)
|
2020-07-20 13:58:54 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Pull servers and check we registered
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = bob.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
registered, err := bob.RegisteredForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !registered {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
2021-01-18 09:12:03 +00:00
|
|
|
bobServers, err := bob.GetPushNotificationsServers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bobServers) == 0 {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:58:54 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// Make sure we receive it
|
|
|
|
s.Require().NoError(err)
|
2020-08-20 09:05:39 +00:00
|
|
|
bobServers, err := bob.GetPushNotificationsServers()
|
2020-07-20 13:58:54 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Create one to one chat & send message
|
|
|
|
pkString := hex.EncodeToString(crypto.FromECDSAPub(&s.m.identity.PublicKey))
|
|
|
|
chat := CreateOneToOneChat(pkString, &s.m.identity.PublicKey, alice.transport)
|
2021-01-11 10:32:51 +00:00
|
|
|
s.Require().NoError(alice.SaveChat(chat))
|
|
|
|
inputMessage := buildTestMessage(*chat)
|
2020-07-20 14:52:55 +00:00
|
|
|
_, err = alice.SendChatMessage(context.Background(), inputMessage)
|
2020-07-20 14:14:42 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-20 14:52:55 +00:00
|
|
|
// We check that alice retrieves the info from the server
|
2020-07-22 07:41:40 +00:00
|
|
|
var info []*pushnotificationclient.PushNotificationInfo
|
2020-07-20 14:52:55 +00:00
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err = alice.pushNotificationClient.GetPushNotificationInfo(&bob.identity.PublicKey, bobInstallationIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Check we have replies for bob
|
|
|
|
if len(info) != 1 {
|
|
|
|
return errors.New("info not fetched")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
2020-07-20 13:58:54 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-20 14:52:55 +00:00
|
|
|
s.Require().NotNil(info)
|
|
|
|
s.Require().Equal(bob.installationID, info[0].InstallationID)
|
|
|
|
s.Require().Equal(bobServers[0].AccessToken, info[0].AccessToken)
|
|
|
|
s.Require().Equal(&bob.identity.PublicKey, info[0].PublicKey)
|
|
|
|
|
2020-07-20 13:58:54 +00:00
|
|
|
// The message has been sent, but not received, now we remove a contact so that the token is invalidated
|
|
|
|
frankContact = &Contact{
|
2022-02-17 15:13:10 +00:00
|
|
|
ID: types.EncodeHex(crypto.FromECDSAPub(&frank.identity.PublicKey)),
|
|
|
|
EnsName: "Some Contact",
|
2020-07-20 13:58:54 +00:00
|
|
|
}
|
2021-10-22 14:20:42 +00:00
|
|
|
_, err = bob.RemoveContact(context.Background(), frankContact.ID)
|
2020-07-20 13:58:54 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Re-registration should be triggered, pull from server and bob to check we are correctly registered
|
|
|
|
// Pull servers and check we registered
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = bob.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
registered, err := bob.RegisteredForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !registered {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-08-20 09:05:39 +00:00
|
|
|
newBobServers, err := bob.GetPushNotificationsServers()
|
2020-07-20 13:58:54 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
// Make sure access token is not the same
|
|
|
|
s.Require().NotEqual(newBobServers[0].AccessToken, bobServers[0].AccessToken)
|
|
|
|
|
2020-07-20 14:52:55 +00:00
|
|
|
// Send another message, here the token will not be valid
|
2021-01-11 10:32:51 +00:00
|
|
|
inputMessage = buildTestMessage(*chat)
|
2020-07-20 14:52:55 +00:00
|
|
|
response, err := alice.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.Require().NoError(err)
|
2021-06-03 13:11:55 +00:00
|
|
|
messageIDString := response.Messages()[0].ID
|
2020-07-20 14:52:55 +00:00
|
|
|
messageID, err := hex.DecodeString(messageIDString[2:])
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-20 13:58:54 +00:00
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err = alice.pushNotificationClient.GetPushNotificationInfo(&bob.identity.PublicKey, bobInstallationIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Check we have replies for bob
|
|
|
|
if len(info) != 1 {
|
|
|
|
return errors.New("info not fetched")
|
|
|
|
}
|
2020-07-20 14:52:55 +00:00
|
|
|
if newBobServers[0].AccessToken != info[0].AccessToken {
|
|
|
|
return errors.New("still using the old access token")
|
|
|
|
}
|
2020-07-20 13:58:54 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require().NotNil(info)
|
|
|
|
s.Require().Equal(bob.installationID, info[0].InstallationID)
|
|
|
|
s.Require().Equal(newBobServers[0].AccessToken, info[0].AccessToken)
|
|
|
|
s.Require().Equal(&bob.identity.PublicKey, info[0].PublicKey)
|
|
|
|
|
|
|
|
retrievedNotificationInfo, err := alice.pushNotificationClient.GetPushNotificationInfo(&bob.identity.PublicKey, bobInstallationIDs)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(retrievedNotificationInfo)
|
|
|
|
s.Require().Len(retrievedNotificationInfo, 1)
|
2020-07-20 14:14:42 +00:00
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
var sentNotification *pushnotificationclient.SentNotification
|
2020-07-20 14:14:42 +00:00
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sentNotification, err = alice.pushNotificationClient.GetSentNotification(common.HashPublicKey(&bob.identity.PublicKey), bob.installationID, messageID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sentNotification == nil {
|
|
|
|
return errors.New("sent notification not found")
|
|
|
|
}
|
|
|
|
if !sentNotification.Success {
|
|
|
|
return errors.New("sent notification not successul")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-07-20 14:52:55 +00:00
|
|
|
|
|
|
|
s.Require().NoError(err)
|
2020-07-20 13:58:54 +00:00
|
|
|
}
|
2020-07-22 07:41:40 +00:00
|
|
|
|
2020-08-18 15:07:48 +00:00
|
|
|
func (s *MessengerPushNotificationSuite) TestContactCode() {
|
|
|
|
|
|
|
|
bob1 := s.m
|
|
|
|
|
|
|
|
serverKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
server := s.newPushNotificationServer(s.shh, serverKey)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer server.Shutdown() // nolint: errcheck
|
2020-08-18 15:07:48 +00:00
|
|
|
|
|
|
|
alice := s.newMessenger(s.shh)
|
|
|
|
// start alice and enable sending push notifications
|
2021-01-14 22:15:13 +00:00
|
|
|
_, err = alice.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer alice.Shutdown() // nolint: errcheck
|
2020-08-18 15:07:48 +00:00
|
|
|
s.Require().NoError(alice.EnableSendingPushNotifications())
|
|
|
|
|
|
|
|
// Register bob1
|
2020-08-20 09:05:39 +00:00
|
|
|
err = bob1.AddPushNotificationsServer(context.Background(), &server.identity.PublicKey, pushnotificationclient.ServerTypeCustom)
|
2020-08-18 15:07:48 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
err = bob1.RegisterForPushNotifications(context.Background(), bob1DeviceToken, testAPNTopic, protobuf.PushNotificationRegistration_APN_TOKEN)
|
|
|
|
|
|
|
|
// Pull servers and check we registered
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = bob1.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
registered, err := bob1.RegisteredForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !registered {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
2021-01-18 09:12:03 +00:00
|
|
|
bobServers, err := bob1.GetPushNotificationsServers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bobServers) == 0 {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
|
2020-08-18 15:07:48 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// Make sure we receive it
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
contactCodeAdvertisement, err := bob1.buildContactCodeAdvertisement()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(contactCodeAdvertisement)
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
s.Require().NoError(alice.pushNotificationClient.HandleContactCodeAdvertisement(&bob1.identity.PublicKey, contactCodeAdvertisement))
|
2020-08-18 15:07:48 +00:00
|
|
|
|
|
|
|
}
|
2020-09-02 14:11:16 +00:00
|
|
|
|
|
|
|
func (s *MessengerPushNotificationSuite) TestReceivePushNotificationMention() {
|
|
|
|
|
|
|
|
bob := s.m
|
|
|
|
|
|
|
|
serverKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
server := s.newPushNotificationServer(s.shh, serverKey)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer server.Shutdown() // nolint: errcheck
|
2020-09-02 14:11:16 +00:00
|
|
|
|
|
|
|
alice := s.newMessenger(s.shh)
|
|
|
|
// start alice and enable sending push notifications
|
2021-01-14 22:15:13 +00:00
|
|
|
_, err = alice.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer alice.Shutdown() // nolint: errcheck
|
2020-09-02 14:11:16 +00:00
|
|
|
s.Require().NoError(alice.EnableSendingPushNotifications())
|
|
|
|
bobInstallationIDs := []string{bob.installationID}
|
|
|
|
|
|
|
|
// Create public chat and join for both alice and bob
|
|
|
|
chat := CreatePublicChat("status", s.m.transport)
|
2021-01-11 10:32:51 +00:00
|
|
|
err = bob.SaveChat(chat)
|
2020-09-02 14:11:16 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
_, err = bob.Join(chat)
|
2020-09-02 14:11:16 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
err = alice.SaveChat(chat)
|
2020-09-02 14:11:16 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
_, err = alice.Join(chat)
|
2020-09-02 14:11:16 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Register bob
|
|
|
|
err = bob.AddPushNotificationsServer(context.Background(), &server.identity.PublicKey, pushnotificationclient.ServerTypeCustom)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
err = bob.RegisterForPushNotifications(context.Background(), bob1DeviceToken, testAPNTopic, protobuf.PushNotificationRegistration_APN_TOKEN)
|
|
|
|
|
|
|
|
// Pull servers and check we registered
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = bob.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
registered, err := bob.RegisteredForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !registered {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
2021-01-18 09:12:03 +00:00
|
|
|
|
|
|
|
bobServers, err := bob.GetPushNotificationsServers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bobServers) == 0 {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
|
2020-09-02 14:11:16 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// Make sure we receive it
|
|
|
|
s.Require().NoError(err)
|
|
|
|
bobServers, err := bob.GetPushNotificationsServers()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
inputMessage := buildTestMessage(*chat)
|
2020-09-02 14:11:16 +00:00
|
|
|
// message contains a mention
|
|
|
|
inputMessage.Text = "Hey @" + types.EncodeHex(crypto.FromECDSAPub(&bob.identity.PublicKey))
|
|
|
|
response, err := alice.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.Require().NoError(err)
|
2021-06-03 13:11:55 +00:00
|
|
|
messageIDString := response.Messages()[0].ID
|
2020-09-02 14:11:16 +00:00
|
|
|
messageID, err := hex.DecodeString(messageIDString[2:])
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
var bobInfo []*pushnotificationclient.PushNotificationInfo
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bobInfo, err = alice.pushNotificationClient.GetPushNotificationInfo(&bob.identity.PublicKey, bobInstallationIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Check we have replies for bob
|
|
|
|
if len(bobInfo) != 1 {
|
|
|
|
return errors.New("info not fetched")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require().NotEmpty(bobInfo)
|
|
|
|
s.Require().Equal(bob.installationID, bobInfo[0].InstallationID)
|
|
|
|
s.Require().Equal(bobServers[0].AccessToken, bobInfo[0].AccessToken)
|
|
|
|
s.Require().Equal(&bob.identity.PublicKey, bobInfo[0].PublicKey)
|
|
|
|
|
|
|
|
retrievedNotificationInfo, err := alice.pushNotificationClient.GetPushNotificationInfo(&bob.identity.PublicKey, bobInstallationIDs)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(retrievedNotificationInfo)
|
|
|
|
s.Require().Len(retrievedNotificationInfo, 1)
|
|
|
|
|
|
|
|
var sentNotification *pushnotificationclient.SentNotification
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sentNotification, err = alice.pushNotificationClient.GetSentNotification(common.HashPublicKey(&bob.identity.PublicKey), bob.installationID, messageID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sentNotification == nil {
|
|
|
|
return errors.New("sent notification not found")
|
|
|
|
}
|
|
|
|
if !sentNotification.Success {
|
|
|
|
return errors.New("sent notification not successul")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
2021-03-31 16:23:45 +00:00
|
|
|
|
|
|
|
func (s *MessengerPushNotificationSuite) TestReceivePushNotificationCommunityRequest() {
|
|
|
|
|
|
|
|
bob := s.m
|
|
|
|
|
|
|
|
serverKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
server := s.newPushNotificationServer(s.shh, serverKey)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer server.Shutdown() // nolint: errcheck
|
2021-03-31 16:23:45 +00:00
|
|
|
|
|
|
|
alice := s.newMessenger(s.shh)
|
|
|
|
// start alice and enable sending push notifications
|
|
|
|
_, err = alice.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer alice.Shutdown() // nolint: errcheck
|
2021-03-31 16:23:45 +00:00
|
|
|
s.Require().NoError(alice.EnableSendingPushNotifications())
|
|
|
|
|
|
|
|
// Register bob
|
|
|
|
err = bob.AddPushNotificationsServer(context.Background(), &server.identity.PublicKey, pushnotificationclient.ServerTypeCustom)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
err = bob.RegisterForPushNotifications(context.Background(), bob1DeviceToken, testAPNTopic, protobuf.PushNotificationRegistration_APN_TOKEN)
|
|
|
|
|
|
|
|
// Pull servers and check we registered
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = bob.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
registered, err := bob.RegisteredForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !registered {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
bobServers, err := bob.GetPushNotificationsServers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bobServers) == 0 {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// Make sure we receive it
|
|
|
|
s.Require().NoError(err)
|
|
|
|
_, err = bob.GetPushNotificationsServers()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
description := &requests.CreateCommunity{
|
2023-10-25 13:03:26 +00:00
|
|
|
Membership: protobuf.CommunityPermissions_MANUAL_ACCEPT,
|
2021-03-31 16:23:45 +00:00
|
|
|
Name: "status",
|
|
|
|
Color: "#ffffff",
|
|
|
|
Description: "status community description",
|
|
|
|
}
|
|
|
|
|
2022-07-19 09:31:52 +00:00
|
|
|
response, err := bob.CreateCommunity(description, true)
|
2021-03-31 16:23:45 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().Len(response.Communities(), 1)
|
|
|
|
community := response.Communities()[0]
|
|
|
|
|
2023-08-10 18:25:07 +00:00
|
|
|
// Send a community message
|
2021-03-31 16:23:45 +00:00
|
|
|
chat := CreateOneToOneChat(common.PubkeyToHex(&alice.identity.PublicKey), &alice.identity.PublicKey, alice.transport)
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
inputMessage := common.NewMessage()
|
2021-03-31 16:23:45 +00:00
|
|
|
inputMessage.ChatId = chat.ID
|
|
|
|
inputMessage.Text = "some text"
|
|
|
|
inputMessage.CommunityID = community.IDString()
|
|
|
|
|
|
|
|
err = bob.SaveChat(chat)
|
|
|
|
s.NoError(err)
|
|
|
|
_, err = bob.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// Pull message and make sure org is received
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
response, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(response.Communities()) == 0 {
|
|
|
|
return errors.New("community not received")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
request := &requests.RequestToJoinCommunity{CommunityID: community.ID()}
|
|
|
|
|
|
|
|
// We try to join the org
|
|
|
|
response, err = alice.RequestToJoinCommunity(request)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().Len(response.RequestsToJoinCommunity, 1)
|
|
|
|
|
|
|
|
requestToJoin1 := response.RequestsToJoinCommunity[0]
|
|
|
|
s.Require().NotNil(requestToJoin1)
|
|
|
|
s.Require().Equal(community.ID(), requestToJoin1.CommunityID)
|
|
|
|
s.Require().True(requestToJoin1.Our)
|
|
|
|
s.Require().NotEmpty(requestToJoin1.ID)
|
|
|
|
s.Require().NotEmpty(requestToJoin1.Clock)
|
|
|
|
s.Require().Equal(requestToJoin1.PublicKey, common.PubkeyToHex(&alice.identity.PublicKey))
|
|
|
|
s.Require().Equal(communities.RequestToJoinStatePending, requestToJoin1.State)
|
|
|
|
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if server.pushNotificationServer.SentRequests != 1 {
|
|
|
|
return errors.New("request not sent")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
}
|
2022-06-13 10:57:51 +00:00
|
|
|
|
|
|
|
func (s *MessengerPushNotificationSuite) TestReceivePushNotificationPairedDevices() {
|
|
|
|
|
|
|
|
bob1 := s.m
|
|
|
|
bob2, err := newMessengerWithKey(s.shh, s.m.identity, s.logger, []Option{WithPushNotifications()})
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer bob2.Shutdown() // nolint: errcheck
|
2022-06-13 10:57:51 +00:00
|
|
|
|
|
|
|
serverKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
server := s.newPushNotificationServer(s.shh, serverKey)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer server.Shutdown() // nolint: errcheck
|
2022-06-13 10:57:51 +00:00
|
|
|
|
|
|
|
alice := s.newMessenger(s.shh)
|
|
|
|
// start alice and enable sending push notifications
|
|
|
|
_, err = alice.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer alice.Shutdown() // nolint: errcheck
|
2022-06-13 10:57:51 +00:00
|
|
|
s.Require().NoError(alice.EnableSendingPushNotifications())
|
|
|
|
bobInstallationIDs := []string{bob1.installationID, bob2.installationID}
|
|
|
|
|
|
|
|
// Register bob1
|
|
|
|
err = bob1.AddPushNotificationsServer(context.Background(), &server.identity.PublicKey, pushnotificationclient.ServerTypeCustom)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
err = bob1.RegisterForPushNotifications(context.Background(), bob1DeviceToken, testAPNTopic, protobuf.PushNotificationRegistration_APN_TOKEN)
|
|
|
|
|
|
|
|
// Pull servers and check we registered
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = bob1.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
registered, err := bob1.RegisteredForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !registered {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
bobServers, err := bob1.GetPushNotificationsServers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bobServers) == 0 {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// Make sure we receive it
|
|
|
|
s.Require().NoError(err)
|
|
|
|
bob1Servers, err := bob1.GetPushNotificationsServers()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Register bob2
|
|
|
|
err = bob2.AddPushNotificationsServer(context.Background(), &server.identity.PublicKey, pushnotificationclient.ServerTypeCustom)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
err = bob2.RegisterForPushNotifications(context.Background(), bob2DeviceToken, testAPNTopic, protobuf.PushNotificationRegistration_APN_TOKEN)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = bob2.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
registered, err := bob2.RegisteredForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !registered {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
bobServers, err := bob2.GetPushNotificationsServers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bobServers) == 0 {
|
|
|
|
return errors.New("not registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// Make sure we receive it
|
|
|
|
s.Require().NoError(err)
|
|
|
|
bob2Servers, err := bob2.GetPushNotificationsServers()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Create one to one chat & send message
|
|
|
|
pkString := hex.EncodeToString(crypto.FromECDSAPub(&s.m.identity.PublicKey))
|
|
|
|
chat := CreateOneToOneChat(pkString, &s.m.identity.PublicKey, alice.transport)
|
|
|
|
s.Require().NoError(alice.SaveChat(chat))
|
|
|
|
inputMessage := buildTestMessage(*chat)
|
|
|
|
response, err := alice.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
messageIDString := response.Messages()[0].ID
|
|
|
|
messageID, err := hex.DecodeString(messageIDString[2:])
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
infoMap := make(map[string]*pushnotificationclient.PushNotificationInfo)
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := alice.pushNotificationClient.GetPushNotificationInfo(&bob1.identity.PublicKey, bobInstallationIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, i := range info {
|
|
|
|
infoMap[i.AccessToken] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check we have replies for both bob1 and bob2
|
|
|
|
if len(infoMap) != 2 {
|
|
|
|
return errors.New("info not fetched")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Require().Len(infoMap, 2)
|
|
|
|
|
|
|
|
// Check we have replies for both bob1 and bob2
|
|
|
|
var bob1Info, bob2Info *pushnotificationclient.PushNotificationInfo
|
|
|
|
|
|
|
|
bob1Info = infoMap[bob1Servers[0].AccessToken]
|
|
|
|
bob2Info = infoMap[bob2Servers[0].AccessToken]
|
|
|
|
|
|
|
|
s.Require().NotNil(bob1Info)
|
|
|
|
s.Require().Equal(bob1.installationID, bob1Info.InstallationID)
|
|
|
|
s.Require().Equal(bob1Servers[0].AccessToken, bob1Info.AccessToken)
|
|
|
|
s.Require().Equal(&bob1.identity.PublicKey, bob1Info.PublicKey)
|
|
|
|
|
|
|
|
s.Require().NotNil(bob2Info)
|
|
|
|
s.Require().Equal(bob2.installationID, bob2Info.InstallationID)
|
|
|
|
s.Require().Equal(bob2Servers[0].AccessToken, bob2Info.AccessToken)
|
|
|
|
s.Require().Equal(&bob2.identity.PublicKey, bob2Info.PublicKey)
|
|
|
|
|
|
|
|
retrievedNotificationInfo, err := alice.pushNotificationClient.GetPushNotificationInfo(&bob1.identity.PublicKey, bobInstallationIDs)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(retrievedNotificationInfo)
|
|
|
|
s.Require().Len(retrievedNotificationInfo, 2)
|
|
|
|
|
|
|
|
var sentNotification *pushnotificationclient.SentNotification
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
_, err = server.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = alice.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sentNotification, err = alice.pushNotificationClient.GetSentNotification(common.HashPublicKey(&bob1.identity.PublicKey), bob1.installationID, messageID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sentNotification == nil {
|
|
|
|
return errors.New("sent notification not found")
|
|
|
|
}
|
|
|
|
if !sentNotification.Success {
|
|
|
|
return errors.New("sent notification not successul")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|