status-go/multiaccounts/accounts/database_test.go

265 lines
8.4 KiB
Go
Raw Normal View History

package accounts
import (
"database/sql"
"testing"
2020-01-02 09:10:19 +00:00
"github.com/stretchr/testify/require"
2019-07-25 05:35:09 +00:00
"github.com/status-im/status-go/appdatabase"
2019-12-11 13:59:37 +00:00
"github.com/status-im/status-go/eth-node/types"
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
"github.com/status-im/status-go/multiaccounts/errors"
)
func setupTestDB(t *testing.T) (*Database, func()) {
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
db, stop, err := appdatabase.SetupTestSQLDB("settings-tests-")
if err != nil {
require.NoError(t, stop())
}
require.NoError(t, err)
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
d, err := NewDB(db)
if err != nil {
require.NoError(t, stop())
}
require.NoError(t, err)
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
return d, func() {
require.NoError(t, stop())
}
}
func TestSaveAccounts(t *testing.T) {
type testCase struct {
description string
2022-05-18 10:42:51 +00:00
accounts []*Account
err error
}
for _, tc := range []testCase{
{
description: "NoError",
2022-05-18 10:42:51 +00:00
accounts: []*Account{
2019-12-11 13:59:37 +00:00
{Address: types.Address{0x01}, Chat: true, Wallet: true},
{Address: types.Address{0x02}},
},
},
{
description: "UniqueChat",
2022-05-18 10:42:51 +00:00
accounts: []*Account{
2019-12-11 13:59:37 +00:00
{Address: types.Address{0x01}, Chat: true},
{Address: types.Address{0x02}, Chat: true},
},
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
err: errors.ErrChatNotUnique,
},
{
description: "UniqueWallet",
2022-05-18 10:42:51 +00:00
accounts: []*Account{
2019-12-11 13:59:37 +00:00
{Address: types.Address{0x01}, Wallet: true},
{Address: types.Address{0x02}, Wallet: true},
},
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
err: errors.ErrWalletNotUnique,
},
} {
t.Run(tc.description, func(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
require.Equal(t, tc.err, db.SaveAccounts(tc.accounts))
})
}
}
func TestUpdateAccounts(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
2022-05-18 10:42:51 +00:00
accounts := []*Account{
2019-12-11 13:59:37 +00:00
{Address: types.Address{0x01}, Chat: true, Wallet: true},
{Address: types.Address{0x02}},
}
require.NoError(t, db.SaveAccounts(accounts))
accounts[0].Chat = false
accounts[1].Chat = true
require.NoError(t, db.SaveAccounts(accounts))
rst, err := db.GetAccounts()
require.NoError(t, err)
require.Equal(t, accounts, rst)
}
func TestDeleteAccount(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
2022-05-18 10:42:51 +00:00
accounts := []*Account{
2019-12-11 13:59:37 +00:00
{Address: types.Address{0x01}, Chat: true, Wallet: true},
}
require.NoError(t, db.SaveAccounts(accounts))
rst, err := db.GetAccounts()
require.NoError(t, err)
require.Equal(t, 1, len(rst))
2019-12-11 13:59:37 +00:00
require.NoError(t, db.DeleteAccount(types.Address{0x01}))
rst2, err := db.GetAccounts()
require.NoError(t, err)
require.Equal(t, 0, len(rst2))
}
func TestGetAddresses(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
2022-05-18 10:42:51 +00:00
accounts := []*Account{
2019-12-11 13:59:37 +00:00
{Address: types.Address{0x01}, Chat: true, Wallet: true},
{Address: types.Address{0x02}},
}
require.NoError(t, db.SaveAccounts(accounts))
addresses, err := db.GetAddresses()
require.NoError(t, err)
2019-12-11 13:59:37 +00:00
require.Equal(t, []types.Address{{0x01}, {0x02}}, addresses)
}
func TestGetWalletAddress(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
2019-12-11 13:59:37 +00:00
address := types.Address{0x01}
_, err := db.GetWalletAddress()
require.Equal(t, err, sql.ErrNoRows)
2022-05-18 10:42:51 +00:00
require.NoError(t, db.SaveAccounts([]*Account{{Address: address, Wallet: true}}))
wallet, err := db.GetWalletAddress()
require.NoError(t, err)
require.Equal(t, address, wallet)
}
func TestGetChatAddress(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
2019-12-11 13:59:37 +00:00
address := types.Address{0x01}
_, err := db.GetChatAddress()
require.Equal(t, err, sql.ErrNoRows)
2022-05-18 10:42:51 +00:00
require.NoError(t, db.SaveAccounts([]*Account{{Address: address, Chat: true}}))
chat, err := db.GetChatAddress()
require.NoError(t, err)
require.Equal(t, address, chat)
}
func TestGetAccounts(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
2022-05-18 10:42:51 +00:00
accounts := []*Account{
2019-12-11 13:59:37 +00:00
{Address: types.Address{0x01}, Chat: true, Wallet: true},
{Address: types.Address{0x02}, PublicKey: types.HexBytes{0x01, 0x02}},
{Address: types.Address{0x03}, PublicKey: types.HexBytes{0x02, 0x03}},
}
require.NoError(t, db.SaveAccounts(accounts))
rst, err := db.GetAccounts()
require.NoError(t, err)
require.Equal(t, accounts, rst)
}
func TestGetAccountByAddress(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
address := types.Address{0x01}
2022-05-18 10:42:51 +00:00
account := &Account{Address: address, Chat: true, Wallet: true}
dilute := []*Account{
{Address: types.Address{0x02}, PublicKey: types.HexBytes{0x01, 0x02}},
{Address: types.Address{0x03}, PublicKey: types.HexBytes{0x02, 0x03}},
}
accounts := append(dilute, account)
require.NoError(t, db.SaveAccounts(accounts))
rst, err := db.GetAccountByAddress(address)
require.NoError(t, err)
2022-05-18 10:42:51 +00:00
require.Equal(t, account, rst)
}
func TestAddressExists(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
2022-05-18 10:42:51 +00:00
accounts := []*Account{
2019-12-11 13:59:37 +00:00
{Address: types.Address{0x01}, Chat: true, Wallet: true},
}
require.NoError(t, db.SaveAccounts(accounts))
exists, err := db.AddressExists(accounts[0].Address)
require.NoError(t, err)
require.True(t, exists)
}
func TestAddressDoesntExist(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
2019-12-11 13:59:37 +00:00
exists, err := db.AddressExists(types.Address{1, 1, 1})
require.NoError(t, err)
require.False(t, exists)
}
func TestKeypairNameAndIndexWhenAddingNewAccount(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
accountsRegular := []*Account{
// chat account
{Address: types.Address{0x01}, Chat: true, Wallet: false, KeyUID: "0x0001"},
// Status Profile keypair
{Address: types.Address{0x02}, Chat: false, Wallet: true, KeyUID: "0x0001", Path: "m/44'/60'/0'/0/0", LastUsedDerivationIndex: 0, DerivedFrom: "0x1111", KeypairName: "Status Profile"},
{Address: types.Address{0x03}, Chat: false, Wallet: false, KeyUID: "0x0001", Path: "m/44'/60'/0'/0/1", LastUsedDerivationIndex: 1, DerivedFrom: "0x1111", KeypairName: "Status Profile"},
{Address: types.Address{0x04}, Chat: false, Wallet: false, KeyUID: "0x0001", Path: "m/44'/60'/0'/0/2", LastUsedDerivationIndex: 2, DerivedFrom: "0x1111", KeypairName: "Status Profile"},
}
accountsCustom := []*Account{
// Keypair1
{Address: types.Address{0x11}, Chat: false, Wallet: false, KeyUID: "0x0002", Path: "m/44'/60'/0'/0/10", LastUsedDerivationIndex: 0, DerivedFrom: "0x2222", KeypairName: "Keypair11"},
{Address: types.Address{0x12}, Chat: false, Wallet: false, KeyUID: "0x0002", Path: "m/44'/60'/0'/0/11", LastUsedDerivationIndex: 0, DerivedFrom: "0x2222", KeypairName: "Keypair12"},
// Keypair2 out of the default Status' derivation tree
{Address: types.Address{0x22}, Chat: false, Wallet: false, KeyUID: "0x0003", Path: "m/44'/60'/0'/0/0/100", LastUsedDerivationIndex: 0, DerivedFrom: "0x3333", KeypairName: "Keypair21"},
{Address: types.Address{0x23}, Chat: false, Wallet: false, KeyUID: "0x0003", Path: "m/44'/60'/0'/0/1/100", LastUsedDerivationIndex: 0, DerivedFrom: "0x3333", KeypairName: "Keypair22"},
}
err := db.SaveAccounts(accountsRegular)
require.NoError(t, err)
err = db.SaveAccounts(accountsCustom)
require.NoError(t, err)
accs, err := db.GetAccounts()
require.NoError(t, err)
for _, acc := range accs {
if acc.Chat {
continue
}
if acc.KeyUID == accountsRegular[0].KeyUID {
require.Equal(t, uint64(2), acc.LastUsedDerivationIndex)
require.Equal(t, "Status Profile", acc.KeypairName)
} else if acc.KeyUID == accountsCustom[1].KeyUID {
require.Equal(t, uint64(0), acc.LastUsedDerivationIndex)
require.Equal(t, "Keypair12", acc.KeypairName)
} else if acc.KeyUID == accountsCustom[3].KeyUID {
require.Equal(t, uint64(0), acc.LastUsedDerivationIndex)
require.Equal(t, "Keypair22", acc.KeypairName)
}
}
accountsCustom = []*Account{
// Status Profile keypair
{Address: types.Address{0x05}, Chat: false, Wallet: false, KeyUID: "0x0001", Path: "m/44'/60'/0'/0/100/1", LastUsedDerivationIndex: 2, DerivedFrom: "0x1111", KeypairName: "Status Profile"},
}
err = db.SaveAccounts(accountsCustom)
require.NoError(t, err)
result, err := db.GetAccountsByKeyUID(accountsCustom[0].KeyUID)
require.NoError(t, err)
require.Equal(t, 5, len(result))
require.Equal(t, uint64(2), accountsCustom[0].LastUsedDerivationIndex)
require.Equal(t, "Status Profile", accountsCustom[0].KeypairName)
accountsRegular = []*Account{
// Status Profile keypair
{Address: types.Address{0x06}, Chat: false, Wallet: false, KeyUID: "0x0001", Path: "m/44'/60'/0'/0/3", LastUsedDerivationIndex: 3, DerivedFrom: "0x1111", KeypairName: "Status Profile"},
}
err = db.SaveAccounts(accountsRegular)
require.NoError(t, err)
result, err = db.GetAccountsByKeyUID(accountsCustom[0].KeyUID)
require.NoError(t, err)
require.Equal(t, 6, len(result))
require.Equal(t, uint64(3), accountsRegular[0].LastUsedDerivationIndex)
require.Equal(t, "Status Profile", accountsRegular[0].KeypairName)
}