From 84e91d85d222ac339b0999fa25676dceaa6328f5 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Mon, 21 Dec 2020 12:57:47 +0100 Subject: [PATCH] Consolidate test methods --- protocol/messenger_contact_update_test.go | 26 ++--------- protocol/messenger_emoji_test.go | 39 ++-------------- protocol/messenger_installations_test.go | 46 +++++------------- protocol/messenger_mute_test.go | 31 ++---------- protocol/messenger_test.go | 39 +++++++++++----- protocol/push_notification_test.go | 57 ++++------------------- 6 files changed, 60 insertions(+), 178 deletions(-) diff --git a/protocol/messenger_contact_update_test.go b/protocol/messenger_contact_update_test.go index ccb65663f..2a08e7233 100644 --- a/protocol/messenger_contact_update_test.go +++ b/protocol/messenger_contact_update_test.go @@ -5,7 +5,6 @@ import ( "crypto/ecdsa" "testing" - "github.com/google/uuid" "github.com/stretchr/testify/suite" "go.uber.org/zap" @@ -48,32 +47,13 @@ func (s *MessengerContactUpdateSuite) TearDownTest() { 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 { privateKey, err := crypto.GenerateKey() 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() { diff --git a/protocol/messenger_emoji_test.go b/protocol/messenger_emoji_test.go index 9adb57fa8..f21c0bcaf 100644 --- a/protocol/messenger_emoji_test.go +++ b/protocol/messenger_emoji_test.go @@ -3,10 +3,8 @@ package protocol import ( "context" "crypto/ecdsa" - "io/ioutil" "testing" - "github.com/google/uuid" "github.com/stretchr/testify/suite" "go.uber.org/zap" @@ -15,7 +13,6 @@ import ( "github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/multiaccounts" "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/waku" ) @@ -54,39 +51,13 @@ func (s *MessengerEmojiSuite) TearDownTest() { 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 { privateKey, err := crypto.GenerateKey() 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() { @@ -95,8 +66,8 @@ func (s *MessengerEmojiSuite) TestSendEmoji() { key, err := crypto.GenerateKey() s.Require().NoError(err) - bob := s.newMessengerWithKey(s.shh, key) - s.Require().NoError(bob.Start()) + bob, err := newMessengerWithKey(s.shh, key, s.logger, nil) + s.Require().NoError(err) chatID := statusChatID diff --git a/protocol/messenger_installations_test.go b/protocol/messenger_installations_test.go index a294da4f3..75c13862d 100644 --- a/protocol/messenger_installations_test.go +++ b/protocol/messenger_installations_test.go @@ -6,7 +6,6 @@ import ( "errors" "testing" - "github.com/google/uuid" "github.com/stretchr/testify/suite" "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/types" "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/waku" ) @@ -56,40 +54,21 @@ func (s *MessengerInstallationSuite) TearDownTest() { 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 { privateKey, err := crypto.GenerateKey() 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() { - theirMessenger := s.newMessengerWithKey(s.shh, s.privateKey) - s.Require().NoError(theirMessenger.Start()) + theirMessenger, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil) + s.Require().NoError(err) - err := theirMessenger.SetInstallationMetadata(theirMessenger.installationID, &multidevice.InstallationMetadata{ + err = theirMessenger.SetInstallationMetadata(theirMessenger.installationID, &multidevice.InstallationMetadata{ Name: "their-name", DeviceType: "their-device-type", }) @@ -175,8 +154,8 @@ func (s *MessengerInstallationSuite) TestSyncInstallation() { s.Require().NoError(err) // pair - theirMessenger := s.newMessengerWithKey(s.shh, s.privateKey) - s.Require().NoError(theirMessenger.Start()) + theirMessenger, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil) + s.Require().NoError(err) err = theirMessenger.SetInstallationMetadata(theirMessenger.installationID, &multidevice.InstallationMetadata{ Name: "their-name", @@ -251,12 +230,11 @@ func (s *MessengerInstallationSuite) TestSyncInstallationNewMessages() { bob1 := s.m // pair - bob2 := s.newMessengerWithKey(s.shh, s.privateKey) - s.Require().NoError(bob2.Start()) + bob2, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil) + s.Require().NoError(err) 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", DeviceType: "their-device-type", }) diff --git a/protocol/messenger_mute_test.go b/protocol/messenger_mute_test.go index 883bf3822..ece6c1e5b 100644 --- a/protocol/messenger_mute_test.go +++ b/protocol/messenger_mute_test.go @@ -4,14 +4,12 @@ import ( "crypto/ecdsa" "testing" - "github.com/google/uuid" "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/sqlite" "github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/waku" ) @@ -50,40 +48,21 @@ func (s *MessengerMuteSuite) TearDownTest() { 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 { privateKey, err := crypto.GenerateKey() 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() { key, err := crypto.GenerateKey() s.Require().NoError(err) - theirMessenger := s.newMessengerWithKey(s.shh, key) - s.Require().NoError(theirMessenger.Start()) + theirMessenger, err := newMessengerWithKey(s.shh, key, s.logger, nil) + s.Require().NoError(err) chatID := "status" diff --git a/protocol/messenger_test.go b/protocol/messenger_test.go index f300b4bcc..93124f558 100644 --- a/protocol/messenger_test.go +++ b/protocol/messenger_test.go @@ -112,43 +112,58 @@ func (s *MessengerSuite) SetupTest() { 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-") - s.Require().NoError(err) + if err != nil { + return nil, err + } madb, err := multiaccounts.InitializeDB(tmpfile.Name()) - s.Require().NoError(err) + if err != nil { + return nil, err + } acc := generator.NewAccount(privateKey, nil) iai := acc.ToIdentifiedAccountInfo("") options := []Option{ - WithCustomLogger(s.logger), - WithMessagesPersistenceEnabled(), + WithCustomLogger(logger), WithDatabaseConfig(":memory:", "some-key"), WithMultiAccounts(madb), WithAccount(iai.ToMultiAccount()), + WithDatasync(), } - if s.enableDataSync { - options = append(options, WithDatasync()) - } + + options = append(options, extraOptions...) + m, err := NewMessenger( privateKey, &testNode{shh: shh}, uuid.New().String(), options..., ) - s.Require().NoError(err) + if err != nil { + return nil, err + } 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 { privateKey, err := crypto.GenerateKey() 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() { diff --git a/protocol/push_notification_test.go b/protocol/push_notification_test.go index b86cac67f..aa3b1c64a 100644 --- a/protocol/push_notification_test.go +++ b/protocol/push_notification_test.go @@ -5,23 +5,18 @@ import ( "crypto/ecdsa" "encoding/hex" "errors" - "io/ioutil" "testing" - "github.com/google/uuid" "github.com/stretchr/testify/suite" "go.uber.org/zap" - "github.com/status-im/status-go/account/generator" 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/multiaccounts" "github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/pushnotificationclient" "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/waku" ) @@ -65,47 +60,13 @@ func (s *MessengerPushNotificationSuite) TearDownTest() { _ = 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 { privateKey, err := crypto.GenerateKey() 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 { @@ -117,20 +78,18 @@ func (s *MessengerPushNotificationSuite) newPushNotificationServer(shh types.Wak } options := []Option{ - WithCustomLogger(s.logger), - WithMessagesPersistenceEnabled(), - WithDatabaseConfig(sqlite.InMemoryPath, "some-key"), 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() { bob1 := s.m - bob2 := s.newMessengerWithKey(s.shh, s.m.identity) - s.Require().NoError(bob2.Start()) + bob2, err := newMessengerWithKey(s.shh, s.m.identity, s.logger, []Option{WithPushNotifications()}) + s.Require().NoError(err) serverKey, err := crypto.GenerateKey() s.Require().NoError(err)