status-go/protocol/push_notification_client/push_notification_test.go

84 lines
2.2 KiB
Go
Raw Normal View History

package push_notification_client
2020-06-30 07:50:59 +00:00
import (
"bytes"
"crypto/ecdsa"
"math/rand"
"testing"
"github.com/google/uuid"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/crypto/ecies"
2020-07-10 07:45:40 +00:00
"github.com/status-im/status-go/protocol/common"
2020-06-30 07:50:59 +00:00
"github.com/status-im/status-go/protocol/protobuf"
"github.com/stretchr/testify/require"
)
func TestBuildPushNotificationRegisterMessage(t *testing.T) {
myDeviceToken := "device-token"
myInstallationID := "installationID"
mutedChatList := []string{"a", "b"}
// build chat lish hashes
var mutedChatListHashes [][]byte
for _, chatID := range mutedChatList {
2020-07-10 07:45:40 +00:00
mutedChatListHashes = append(mutedChatListHashes, common.Shake256([]byte(chatID)))
2020-06-30 07:50:59 +00:00
}
identity, err := crypto.GenerateKey()
contactKey, err := crypto.GenerateKey()
require.NoError(t, err)
contactIDs := []*ecdsa.PublicKey{&contactKey.PublicKey}
// Set random generator for uuid
var seed int64 = 1
uuid.SetRand(rand.New(rand.NewSource(seed)))
// Get token
expectedUUID := uuid.New().String()
// set up reader
reader := bytes.NewReader([]byte(expectedUUID))
sharedKey, err := ecies.ImportECDSA(identity).GenerateShared(
ecies.ImportECDSAPublic(&contactKey.PublicKey),
accessTokenKeyLength,
accessTokenKeyLength,
)
require.NoError(t, err)
// build encrypted token
encryptedToken, err := encryptAccessToken([]byte(expectedUUID), sharedKey, reader)
require.NoError(t, err)
// Reset random generator
uuid.SetRand(rand.New(rand.NewSource(seed)))
config := &Config{
2020-06-30 07:50:59 +00:00
Identity: identity,
RemoteNotificationsEnabled: true,
InstallationID: myInstallationID,
}
client := &Client{}
client.config = config
client.DeviceToken = myDeviceToken
2020-06-30 07:50:59 +00:00
// Set reader
client.reader = bytes.NewReader([]byte(expectedUUID))
2020-06-30 07:50:59 +00:00
options := &protobuf.PushNotificationRegistration{
2020-07-01 08:37:54 +00:00
Version: 1,
AccessToken: expectedUUID,
2020-06-30 07:50:59 +00:00
Token: myDeviceToken,
InstallationId: myInstallationID,
Enabled: true,
BlockedChatList: mutedChatListHashes,
2020-07-01 08:37:54 +00:00
AllowedUserList: [][]byte{encryptedToken},
2020-06-30 07:50:59 +00:00
}
actualMessage, err := client.buildPushNotificationRegistrationMessage(contactIDs, mutedChatList)
2020-06-30 07:50:59 +00:00
require.NoError(t, err)
2020-07-01 08:37:54 +00:00
require.Equal(t, options, actualMessage)
2020-06-30 07:50:59 +00:00
}