Skip to content

Commit

Permalink
refactor: introduce newTestMessenger
Browse files Browse the repository at this point in the history
This avoids duplication and enables better configuration possibilities
for clients.

Next step is to cleanup all `newMessenger` derivatives in tests.
  • Loading branch information
osmaczko committed Jan 22, 2024
1 parent 436d229 commit bf5dcac
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 165 deletions.
2 changes: 1 addition & 1 deletion cmd/statusd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func main() {
protocol.WithTorrentConfig(&config.TorrentConfig),
protocol.WithWalletConfig(&config.WalletConfig),
protocol.WithRPCClient(backend.StatusNode().RPCClient()),
protocol.WithAccountManager(backend.AccountManager()),
}

messenger, err := protocol.NewMessenger(
Expand All @@ -240,7 +241,6 @@ func main() {
gethbridge.NewNodeBridge(backend.StatusNode().GethNode(), backend.StatusNode().WakuService(), backend.StatusNode().WakuV2Service()),
installationID.String(),
nil,
backend.AccountManager(),
options...,
)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion multiaccounts/settings/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ INSERT INTO settings (
current_network,
dapps_address,
device_name,
preferred_name,
display_name,
bio,
eip1581_address,
Expand Down Expand Up @@ -150,13 +151,14 @@ INSERT INTO settings (
wallet_collectible_preferences_group_by_collection,
wallet_collectible_preferences_group_by_community
) VALUES (
?,?,?,?,?,?,?,?,?,?,?,?,?,
?,?,?,?,?,?,?,?,?,?,?,?,?,?,
?,?,?,?,?,?,?,?,?,'id',?,?,?,?,?,?,?,?,?,?,?,?)`,
s.Address,
s.Currency,
s.CurrentNetwork,
s.DappsAddress,
s.DeviceName,
s.PreferredName,
s.DisplayName,
s.Bio,
s.EIP1581Address,
Expand Down
70 changes: 11 additions & 59 deletions protocol/communities_messenger_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ import (
"sync"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"

gethcommon "github.com/ethereum/go-ethereum/common"
hexutil "github.com/ethereum/go-ethereum/common/hexutil"

"github.com/status-im/status-go/account"
"github.com/status-im/status-go/account/generator"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/common/dbsetup"
"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/multiaccounts/accounts"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/params"
Expand All @@ -32,9 +27,7 @@ import (
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/services/communitytokens"
walletToken "github.com/status-im/status-go/services/wallet/token"
"github.com/status-im/status-go/t/helpers"
"github.com/status-im/status-go/transactions"
"github.com/status-im/status-go/walletdatabase"
)

type AccountManagerMock struct {
Expand Down Expand Up @@ -201,56 +194,6 @@ func newMessenger(s *suite.Suite, shh types.Waku, logger *zap.Logger, password s

func newCommunitiesTestMessenger(shh types.Waku, privateKey *ecdsa.PrivateKey, logger *zap.Logger, accountsManager account.Manager,
tokenManager communities.TokenManager, collectiblesService communitytokens.ServiceInterface) (*Messenger, error) {
madb, err := multiaccounts.InitializeDB(dbsetup.InMemoryPath)
if err != nil {
return nil, err
}

acc := generator.NewAccount(privateKey, nil)
iai := acc.ToIdentifiedAccountInfo("")

walletDb, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
if err != nil {
return nil, err
}

appDb, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
if err != nil {
return nil, err
}
options := []Option{
WithCustomLogger(logger),
WithDatabase(appDb),
WithWalletDatabase(walletDb),
WithMultiAccounts(madb),
WithAccount(iai.ToMultiAccount()),
WithDatasync(),
WithResendParams(3, 3),
WithTokenManager(tokenManager),
}

if collectiblesService != nil {
options = append(options, WithCommunityTokensService(collectiblesService))
}

m, err := NewMessenger(
"Test",
privateKey,
&testNode{shh: shh},
uuid.New().String(),
nil,
accountsManager,
options...,
)
if err != nil {
return nil, err
}

err = m.Init()
if err != nil {
return nil, err
}

config := params.NodeConfig{
NetworkID: 10,
DataDir: "test",
Expand Down Expand Up @@ -279,9 +222,18 @@ func newCommunitiesTestMessenger(shh types.Waku, privateKey *ecdsa.PrivateKey, l
SendStatusUpdates: true,
WalletRootAddress: types.HexToAddress("0x1122334455667788990011223344556677889900")}

_ = m.settings.CreateSettings(setting, config)
options := []Option{
WithResendParams(3, 3),
WithAccountManager(accountsManager),
WithTokenManager(tokenManager),
WithCommunityTokensService(collectiblesService),
WithAppSettings(setting, config),
}

return m, nil
return newTestMessenger(shh, testMessengerConfig{
privateKey: privateKey,
logger: logger,
}, options)
}

func createCommunity(s *suite.Suite, owner *Messenger) (*communities.Community, *Chat) {
Expand Down
5 changes: 2 additions & 3 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ func NewMessenger(
node types.Node,
installationID string,
peerStore *mailservers.PeerStore,
accountsManager account.Manager,
opts ...Option,
) (*Messenger, error) {
var messenger *Messenger
Expand Down Expand Up @@ -446,7 +445,7 @@ func NewMessenger(
}

managerOptions := []communities.ManagerOption{
communities.WithAccountManager(accountsManager),
communities.WithAccountManager(c.accountsManager),
}

if walletAPI != nil {
Expand Down Expand Up @@ -508,7 +507,7 @@ func NewMessenger(
pushNotificationServer: pushNotificationServer,
communitiesManager: communitiesManager,
communitiesKeyDistributor: communitiesKeyDistributor,
accountsManager: accountsManager,
accountsManager: c.accountsManager,
ensVerifier: ensVerifier,
featureFlags: c.featureFlags,
systemMessagesTranslations: c.systemMessagesTranslations,
Expand Down
51 changes: 4 additions & 47 deletions protocol/messenger_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@ import (

"github.com/stretchr/testify/suite"

"github.com/google/uuid"
"go.uber.org/zap"

"github.com/status-im/status-go/account/generator"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/common/dbsetup"
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/multiaccounts/settings"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/t/helpers"
"github.com/status-im/status-go/waku"
"github.com/status-im/status-go/walletdatabase"
)

const DefaultProfileDisplayName = ""
Expand Down Expand Up @@ -68,56 +61,20 @@ type MessengerBaseTestSuite struct {
}

func newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey, logger *zap.Logger, extraOptions []Option) (*Messenger, error) {
madb, err := multiaccounts.InitializeDB(dbsetup.InMemoryPath)
if err != nil {
return nil, err
}

acc := generator.NewAccount(privateKey, nil)
iai := acc.ToIdentifiedAccountInfo("")

walletDb, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
if err != nil {
return nil, err
}
appDb, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
if err != nil {
return nil, err
}

options := []Option{
WithCustomLogger(logger),
WithDatabase(appDb),
WithWalletDatabase(walletDb),
WithMultiAccounts(madb),
WithAccount(iai.ToMultiAccount()),
WithDatasync(),
WithToplevelDatabaseMigrations(),
WithAppSettings(settings.Settings{
DisplayName: DefaultProfileDisplayName,
ProfilePicturesShowTo: 1,
ProfilePicturesVisibility: 1,
URLUnfurlingMode: settings.URLUnfurlingAlwaysAsk,
}, params.NodeConfig{}),
WithBrowserDatabase(nil),
}

options = append(options, extraOptions...)

m, err := NewMessenger(
"Test",
privateKey,
&testNode{shh: shh},
uuid.New().String(),
nil,
nil,
options...,
)
if err != nil {
return nil, err
}

err = m.Init()
m, err := newTestMessenger(shh, testMessengerConfig{
privateKey: privateKey,
logger: logger,
}, options)
if err != nil {
return nil, err
}
Expand Down
99 changes: 99 additions & 0 deletions protocol/messenger_builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package protocol

import (
"crypto/ecdsa"

"github.com/google/uuid"
"go.uber.org/zap"

"github.com/status-im/status-go/account/generator"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/common/dbsetup"
"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/tt"
"github.com/status-im/status-go/t/helpers"
"github.com/status-im/status-go/walletdatabase"
)

type testMessengerConfig struct {
name string
privateKey *ecdsa.PrivateKey
logger *zap.Logger
}

func (tmc *testMessengerConfig) complete() error {
if len(tmc.name) == 0 {
tmc.name = uuid.NewString()
}

if tmc.privateKey == nil {
privateKey, err := crypto.GenerateKey()
if err != nil {
return err
}
tmc.privateKey = privateKey
}

if tmc.logger == nil {
logger := tt.MustCreateTestLogger()
tmc.logger = logger.With(zap.String("name", tmc.name))
}

return nil
}

func newTestMessenger(waku types.Waku, config testMessengerConfig, extraOptions []Option) (*Messenger, error) {
err := config.complete()
if err != nil {
return nil, err
}

acc := generator.NewAccount(config.privateKey, nil)
iai := acc.ToIdentifiedAccountInfo("")

madb, err := multiaccounts.InitializeDB(dbsetup.InMemoryPath)
if err != nil {
return nil, err
}
walletDb, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
if err != nil {
return nil, err
}
appDb, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
if err != nil {
return nil, err
}

options := []Option{
WithCustomLogger(config.logger),
WithDatabase(appDb),
WithWalletDatabase(walletDb),
WithMultiAccounts(madb),
WithAccount(iai.ToMultiAccount()),
WithDatasync(),
WithToplevelDatabaseMigrations(),
WithBrowserDatabase(nil),
}
options = append(options, extraOptions...)

m, err := NewMessenger(
config.name,
config.privateKey,
&testNode{shh: waku},
uuid.New().String(),
nil,
options...,
)
if err != nil {
return nil, err
}

err = m.Init()
if err != nil {
return nil, err
}

return m, nil
}
9 changes: 9 additions & 0 deletions protocol/messenger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"

"github.com/status-im/status-go/account"
"github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/server"
"github.com/status-im/status-go/services/browsers"
Expand Down Expand Up @@ -90,6 +91,7 @@ type config struct {
httpServer *server.MediaServer
rpcClient *rpc.Client
tokenManager communities.TokenManager
accountsManager account.Manager

verifyTransactionClient EthClient
verifyENSURL string
Expand Down Expand Up @@ -380,3 +382,10 @@ func WithTokenManager(tokenManager communities.TokenManager) Option {
return nil
}
}

func WithAccountManager(accountManager account.Manager) Option {
return func(c *config) error {
c.accountsManager = accountManager
return nil
}
}
Loading

0 comments on commit bf5dcac

Please sign in to comment.