Consolidate test methods

This commit is contained in:
Andrea Maria Piana 2020-12-21 12:57:47 +01:00
parent 4185420897
commit 84e91d85d2
6 changed files with 60 additions and 178 deletions

View File

@ -5,7 +5,6 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"testing" "testing"
"github.com/google/uuid"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"go.uber.org/zap" "go.uber.org/zap"
@ -48,32 +47,13 @@ func (s *MessengerContactUpdateSuite) TearDownTest() {
s.Require().NoError(s.m.Shutdown()) s.Require().NoError(s.m.Shutdown())
} }
func (s *MessengerContactUpdateSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(":memory:", "some-key"),
WithDatasync(),
}
m, err := NewMessenger(
privateKey,
&testNode{shh: shh},
uuid.New().String(),
options...,
)
s.Require().NoError(err)
err = m.Init()
s.Require().NoError(err)
return m
}
func (s *MessengerContactUpdateSuite) newMessenger(shh types.Waku) *Messenger { func (s *MessengerContactUpdateSuite) newMessenger(shh types.Waku) *Messenger {
privateKey, err := crypto.GenerateKey() privateKey, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)
return s.newMessengerWithKey(s.shh, privateKey) messenger, err := newMessengerWithKey(s.shh, privateKey, s.logger, nil)
s.Require().NoError(err)
return messenger
} }
func (s *MessengerContactUpdateSuite) TestReceiveContactUpdate() { func (s *MessengerContactUpdateSuite) TestReceiveContactUpdate() {

View File

@ -3,10 +3,8 @@ package protocol
import ( import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"io/ioutil"
"testing" "testing"
"github.com/google/uuid"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"go.uber.org/zap" "go.uber.org/zap"
@ -15,7 +13,6 @@ import (
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/multiaccounts" "github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku" "github.com/status-im/status-go/waku"
) )
@ -54,39 +51,13 @@ func (s *MessengerEmojiSuite) TearDownTest() {
s.Require().NoError(s.m.Shutdown()) s.Require().NoError(s.m.Shutdown())
} }
func (s *MessengerEmojiSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
tmpfile, err := ioutil.TempFile("", "accounts-tests-")
s.Require().NoError(err)
madb, err := multiaccounts.InitializeDB(tmpfile.Name())
s.Require().NoError(err)
options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(sqlite.InMemoryPath, "some-key"),
WithMultiAccounts(madb),
WithDatasync(),
}
installationID := uuid.New().String()
m, err := NewMessenger(
privateKey,
&testNode{shh: shh},
installationID,
options...,
)
s.Require().NoError(err)
err = m.Init()
s.Require().NoError(err)
return m
}
func (s *MessengerEmojiSuite) newMessenger(shh types.Waku) *Messenger { func (s *MessengerEmojiSuite) newMessenger(shh types.Waku) *Messenger {
privateKey, err := crypto.GenerateKey() privateKey, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)
return s.newMessengerWithKey(s.shh, privateKey) messenger, err := newMessengerWithKey(s.shh, privateKey, s.logger, nil)
s.Require().NoError(err)
return messenger
} }
func (s *MessengerEmojiSuite) TestSendEmoji() { func (s *MessengerEmojiSuite) TestSendEmoji() {
@ -95,8 +66,8 @@ func (s *MessengerEmojiSuite) TestSendEmoji() {
key, err := crypto.GenerateKey() key, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)
bob := s.newMessengerWithKey(s.shh, key) bob, err := newMessengerWithKey(s.shh, key, s.logger, nil)
s.Require().NoError(bob.Start()) s.Require().NoError(err)
chatID := statusChatID chatID := statusChatID

View File

@ -6,7 +6,6 @@ import (
"errors" "errors"
"testing" "testing"
"github.com/google/uuid"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"go.uber.org/zap" "go.uber.org/zap"
@ -14,7 +13,6 @@ import (
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/encryption/multidevice" "github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku" "github.com/status-im/status-go/waku"
) )
@ -56,40 +54,21 @@ func (s *MessengerInstallationSuite) TearDownTest() {
s.Require().NoError(s.m.Shutdown()) s.Require().NoError(s.m.Shutdown())
} }
func (s *MessengerInstallationSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(sqlite.InMemoryPath, "some-key"),
WithDatasync(),
}
installationID := uuid.New().String()
m, err := NewMessenger(
privateKey,
&testNode{shh: shh},
installationID,
options...,
)
s.Require().NoError(err)
err = m.Init()
s.Require().NoError(err)
return m
}
func (s *MessengerInstallationSuite) newMessenger(shh types.Waku) *Messenger { func (s *MessengerInstallationSuite) newMessenger(shh types.Waku) *Messenger {
privateKey, err := crypto.GenerateKey() privateKey, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)
return s.newMessengerWithKey(s.shh, privateKey) messenger, err := newMessengerWithKey(s.shh, privateKey, s.logger, nil)
s.Require().NoError(err)
return messenger
} }
func (s *MessengerInstallationSuite) TestReceiveInstallation() { func (s *MessengerInstallationSuite) TestReceiveInstallation() {
theirMessenger := s.newMessengerWithKey(s.shh, s.privateKey) theirMessenger, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil)
s.Require().NoError(theirMessenger.Start()) s.Require().NoError(err)
err := theirMessenger.SetInstallationMetadata(theirMessenger.installationID, &multidevice.InstallationMetadata{ err = theirMessenger.SetInstallationMetadata(theirMessenger.installationID, &multidevice.InstallationMetadata{
Name: "their-name", Name: "their-name",
DeviceType: "their-device-type", DeviceType: "their-device-type",
}) })
@ -175,8 +154,8 @@ func (s *MessengerInstallationSuite) TestSyncInstallation() {
s.Require().NoError(err) s.Require().NoError(err)
// pair // pair
theirMessenger := s.newMessengerWithKey(s.shh, s.privateKey) theirMessenger, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil)
s.Require().NoError(theirMessenger.Start()) s.Require().NoError(err)
err = theirMessenger.SetInstallationMetadata(theirMessenger.installationID, &multidevice.InstallationMetadata{ err = theirMessenger.SetInstallationMetadata(theirMessenger.installationID, &multidevice.InstallationMetadata{
Name: "their-name", Name: "their-name",
@ -251,12 +230,11 @@ func (s *MessengerInstallationSuite) TestSyncInstallationNewMessages() {
bob1 := s.m bob1 := s.m
// pair // pair
bob2 := s.newMessengerWithKey(s.shh, s.privateKey) bob2, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil)
s.Require().NoError(bob2.Start()) s.Require().NoError(err)
alice := s.newMessenger(s.shh) alice := s.newMessenger(s.shh)
s.Require().NoError(alice.Start())
err := bob2.SetInstallationMetadata(bob2.installationID, &multidevice.InstallationMetadata{ err = bob2.SetInstallationMetadata(bob2.installationID, &multidevice.InstallationMetadata{
Name: "their-name", Name: "their-name",
DeviceType: "their-device-type", DeviceType: "their-device-type",
}) })

View File

@ -4,14 +4,12 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"testing" "testing"
"github.com/google/uuid"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"go.uber.org/zap" "go.uber.org/zap"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth" 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/crypto"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku" "github.com/status-im/status-go/waku"
) )
@ -50,40 +48,21 @@ func (s *MessengerMuteSuite) TearDownTest() {
s.Require().NoError(s.m.Shutdown()) s.Require().NoError(s.m.Shutdown())
} }
func (s *MessengerMuteSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(sqlite.InMemoryPath, "some-key"),
WithDatasync(),
}
installationID := uuid.New().String()
m, err := NewMessenger(
privateKey,
&testNode{shh: shh},
installationID,
options...,
)
s.Require().NoError(err)
err = m.Init()
s.Require().NoError(err)
return m
}
func (s *MessengerMuteSuite) newMessenger(shh types.Waku) *Messenger { func (s *MessengerMuteSuite) newMessenger(shh types.Waku) *Messenger {
privateKey, err := crypto.GenerateKey() privateKey, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)
return s.newMessengerWithKey(s.shh, privateKey) messenger, err := newMessengerWithKey(s.shh, privateKey, s.logger, nil)
s.Require().NoError(err)
return messenger
} }
func (s *MessengerMuteSuite) TestSetMute() { func (s *MessengerMuteSuite) TestSetMute() {
key, err := crypto.GenerateKey() key, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)
theirMessenger := s.newMessengerWithKey(s.shh, key) theirMessenger, err := newMessengerWithKey(s.shh, key, s.logger, nil)
s.Require().NoError(theirMessenger.Start()) s.Require().NoError(err)
chatID := "status" chatID := "status"

View File

@ -112,43 +112,58 @@ func (s *MessengerSuite) SetupTest() {
s.Require().NoError(s.m.Start()) s.Require().NoError(s.m.Start())
} }
func (s *MessengerSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger { func newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey, logger *zap.Logger, extraOptions []Option) (*Messenger, error) {
tmpfile, err := ioutil.TempFile("", "accounts-tests-") tmpfile, err := ioutil.TempFile("", "accounts-tests-")
s.Require().NoError(err) if err != nil {
return nil, err
}
madb, err := multiaccounts.InitializeDB(tmpfile.Name()) madb, err := multiaccounts.InitializeDB(tmpfile.Name())
s.Require().NoError(err) if err != nil {
return nil, err
}
acc := generator.NewAccount(privateKey, nil) acc := generator.NewAccount(privateKey, nil)
iai := acc.ToIdentifiedAccountInfo("") iai := acc.ToIdentifiedAccountInfo("")
options := []Option{ options := []Option{
WithCustomLogger(s.logger), WithCustomLogger(logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(":memory:", "some-key"), WithDatabaseConfig(":memory:", "some-key"),
WithMultiAccounts(madb), WithMultiAccounts(madb),
WithAccount(iai.ToMultiAccount()), WithAccount(iai.ToMultiAccount()),
WithDatasync(),
} }
if s.enableDataSync {
options = append(options, WithDatasync()) options = append(options, extraOptions...)
}
m, err := NewMessenger( m, err := NewMessenger(
privateKey, privateKey,
&testNode{shh: shh}, &testNode{shh: shh},
uuid.New().String(), uuid.New().String(),
options..., options...,
) )
s.Require().NoError(err) if err != nil {
return nil, err
}
err = m.Init() err = m.Init()
s.Require().NoError(err) if err != nil {
return nil, err
}
return m err = m.Start()
if err != nil {
return nil, err
}
return m, nil
} }
func (s *MessengerSuite) newMessenger(shh types.Waku) *Messenger { func (s *MessengerSuite) newMessenger(shh types.Waku) *Messenger {
privateKey, err := crypto.GenerateKey() privateKey, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)
return s.newMessengerWithKey(shh, privateKey) messenger, err := newMessengerWithKey(shh, privateKey, s.logger, nil)
s.Require().NoError(err)
return messenger
} }
func (s *MessengerSuite) TearDownTest() { func (s *MessengerSuite) TearDownTest() {

View File

@ -5,23 +5,18 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"encoding/hex" "encoding/hex"
"errors" "errors"
"io/ioutil"
"testing" "testing"
"github.com/google/uuid"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"go.uber.org/zap" "go.uber.org/zap"
"github.com/status-im/status-go/account/generator"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth" 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/crypto"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/pushnotificationclient" "github.com/status-im/status-go/protocol/pushnotificationclient"
"github.com/status-im/status-go/protocol/pushnotificationserver" "github.com/status-im/status-go/protocol/pushnotificationserver"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku" "github.com/status-im/status-go/waku"
) )
@ -65,47 +60,13 @@ func (s *MessengerPushNotificationSuite) TearDownTest() {
_ = s.logger.Sync() _ = s.logger.Sync()
} }
func (s *MessengerPushNotificationSuite) newMessengerWithOptions(shh types.Waku, privateKey *ecdsa.PrivateKey, options []Option) *Messenger {
m, err := NewMessenger(
privateKey,
&testNode{shh: shh},
uuid.New().String(),
options...,
)
s.Require().NoError(err)
err = m.Init()
s.Require().NoError(err)
return m
}
func (s *MessengerPushNotificationSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
tmpfile, err := ioutil.TempFile("", "accounts-tests-")
s.Require().NoError(err)
madb, err := multiaccounts.InitializeDB(tmpfile.Name())
s.Require().NoError(err)
acc := generator.NewAccount(privateKey, nil)
iai := acc.ToIdentifiedAccountInfo("")
options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(sqlite.InMemoryPath, ""),
WithDatasync(),
WithPushNotifications(),
WithMultiAccounts(madb),
WithAccount(iai.ToMultiAccount()),
}
return s.newMessengerWithOptions(shh, privateKey, options)
}
func (s *MessengerPushNotificationSuite) newMessenger(shh types.Waku) *Messenger { func (s *MessengerPushNotificationSuite) newMessenger(shh types.Waku) *Messenger {
privateKey, err := crypto.GenerateKey() privateKey, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)
return s.newMessengerWithKey(s.shh, privateKey) messenger, err := newMessengerWithKey(s.shh, privateKey, s.logger, []Option{WithPushNotifications()})
s.Require().NoError(err)
return messenger
} }
func (s *MessengerPushNotificationSuite) newPushNotificationServer(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger { func (s *MessengerPushNotificationSuite) newPushNotificationServer(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
@ -117,20 +78,18 @@ func (s *MessengerPushNotificationSuite) newPushNotificationServer(shh types.Wak
} }
options := []Option{ options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(sqlite.InMemoryPath, "some-key"),
WithPushNotificationServerConfig(serverConfig), WithPushNotificationServerConfig(serverConfig),
WithDatasync(),
} }
return s.newMessengerWithOptions(shh, privateKey, options) messenger, err := newMessengerWithKey(shh, privateKey, s.logger, options)
s.Require().NoError(err)
return messenger
} }
func (s *MessengerPushNotificationSuite) TestReceivePushNotification() { func (s *MessengerPushNotificationSuite) TestReceivePushNotification() {
bob1 := s.m bob1 := s.m
bob2 := s.newMessengerWithKey(s.shh, s.m.identity) bob2, err := newMessengerWithKey(s.shh, s.m.identity, s.logger, []Option{WithPushNotifications()})
s.Require().NoError(bob2.Start()) s.Require().NoError(err)
serverKey, err := crypto.GenerateKey() serverKey, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)