2020-06-30 08:30:58 +00:00
|
|
|
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"
|
|
|
|
"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 {
|
|
|
|
mutedChatListHashes = append(mutedChatListHashes, shake256(chatID))
|
|
|
|
}
|
|
|
|
|
|
|
|
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)))
|
|
|
|
|
2020-06-30 08:30:58 +00:00
|
|
|
config := &Config{
|
2020-06-30 07:50:59 +00:00
|
|
|
Identity: identity,
|
|
|
|
RemoteNotificationsEnabled: true,
|
|
|
|
MutedChatIDs: mutedChatList,
|
|
|
|
ContactIDs: contactIDs,
|
|
|
|
InstallationID: myInstallationID,
|
|
|
|
}
|
|
|
|
|
2020-06-30 08:30:58 +00:00
|
|
|
client := &Client{}
|
|
|
|
client.config = config
|
|
|
|
client.DeviceToken = myDeviceToken
|
2020-06-30 07:50:59 +00:00
|
|
|
// Set reader
|
2020-06-30 08:30:58 +00:00
|
|
|
client.reader = bytes.NewReader([]byte(expectedUUID))
|
2020-06-30 07:50:59 +00:00
|
|
|
|
|
|
|
options := &protobuf.PushNotificationOptions{
|
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
|
|
|
}
|
|
|
|
|
2020-07-01 08:37:54 +00:00
|
|
|
actualMessage, err := client.buildPushNotificationOptionsMessage()
|
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
|
|
|
}
|