2019-08-20 15:38:40 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"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"
|
2019-08-20 15:38:40 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
)
|
|
|
|
|
2019-12-27 09:58:25 +00:00
|
|
|
var (
|
|
|
|
config = params.NodeConfig{
|
|
|
|
NetworkID: 10,
|
|
|
|
DataDir: "test",
|
|
|
|
}
|
|
|
|
|
|
|
|
networks = json.RawMessage("{}")
|
|
|
|
settings = Settings{
|
2020-10-27 17:35:28 +00:00
|
|
|
Address: types.HexToAddress("0xdC540f3745Ff2964AFC1171a5A0DD726d1F6B472"),
|
2021-04-13 13:00:18 +00:00
|
|
|
AnonMetricsShouldSend: false,
|
2020-10-27 17:35:28 +00:00
|
|
|
CurrentNetwork: "mainnet_rpc",
|
|
|
|
DappsAddress: types.HexToAddress("0xD1300f99fDF7346986CbC766903245087394ecd0"),
|
|
|
|
InstallationID: "d3efcff6-cffa-560e-a547-21d3858cbc51",
|
|
|
|
KeyUID: "0x4e8129f3edfc004875be17bf468a784098a9f69b53c095be1f52deff286935ab",
|
|
|
|
LatestDerivedPath: 0,
|
|
|
|
Name: "Jittery Cornflowerblue Kingbird",
|
|
|
|
Networks: &networks,
|
|
|
|
PhotoPath: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAAjklEQVR4nOzXwQmFMBAAUZXUYh32ZB32ZB02sxYQQSZGsod55/91WFgSS0RM+SyjA56ZRZhFmEWYRRT6h+M6G16zrxv6fdJpmUWYRbxsYr13dKfanpN0WmYRZhGzXz6AWYRZRIfbaX26fT9Jk07LLMIsosPt9I/dTDotswizCG+nhFmEWYRZhFnEHQAA///z1CFkYamgfQAAAABJRU5ErkJggg==",
|
|
|
|
PreviewPrivacy: false,
|
|
|
|
PublicKey: "0x04211fe0f69772ecf7eb0b5bfc7678672508a9fb01f2d699096f0d59ef7fe1a0cb1e648a80190db1c0f5f088872444d846f2956d0bd84069f3f9f69335af852ac0",
|
|
|
|
SigningPhrase: "yurt joey vibe",
|
|
|
|
SendPushNotifications: true,
|
2021-01-14 15:31:13 +00:00
|
|
|
ProfilePicturesVisibility: ProfilePicturesVisibilityContactsOnly,
|
2021-06-01 09:29:37 +00:00
|
|
|
DefaultSyncPeriod: 86400,
|
2020-10-27 17:35:28 +00:00
|
|
|
UseMailservers: true,
|
|
|
|
LinkPreviewRequestEnabled: true,
|
2021-07-22 17:41:49 +00:00
|
|
|
SendStatusUpdates: true,
|
2020-10-27 17:35:28 +00:00
|
|
|
WalletRootAddress: types.HexToAddress("0x3B591fd819F86D0A6a2EF2Bcb94f77807a7De1a6")}
|
2019-12-27 09:58:25 +00:00
|
|
|
)
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func setupTestDB(t *testing.T) (*Database, func()) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "settings-tests-")
|
|
|
|
require.NoError(t, err)
|
2019-07-25 05:35:09 +00:00
|
|
|
db, err := appdatabase.InitializeDB(tmpfile.Name(), "settings-tests")
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-27 09:58:25 +00:00
|
|
|
|
2019-07-25 05:35:09 +00:00
|
|
|
return NewDB(db), func() {
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, db.Close())
|
|
|
|
require.NoError(t, os.Remove(tmpfile.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:58:25 +00:00
|
|
|
func TestCreateSettings(t *testing.T) {
|
2019-08-20 15:38:40 +00:00
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2019-12-27 09:58:25 +00:00
|
|
|
require.NoError(t, db.CreateSettings(settings, config))
|
|
|
|
|
|
|
|
s, err := db.GetSettings()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, settings, s)
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 09:58:25 +00:00
|
|
|
func TestSaveSetting(t *testing.T) {
|
2019-08-20 15:38:40 +00:00
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2019-12-27 09:58:25 +00:00
|
|
|
|
|
|
|
require.NoError(t, db.CreateSettings(settings, config))
|
|
|
|
require.NoError(t, db.SaveSetting("currency", "usd"))
|
|
|
|
|
|
|
|
_, err := db.GetSettings()
|
2019-08-08 07:31:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:58:25 +00:00
|
|
|
func TestGetNodeConfig(t *testing.T) {
|
2019-08-08 07:31:24 +00:00
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2019-12-27 09:58:25 +00:00
|
|
|
|
|
|
|
require.NoError(t, db.CreateSettings(settings, config))
|
|
|
|
|
|
|
|
_, err := db.GetSettings()
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveAccounts(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
description string
|
|
|
|
accounts []Account
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
for _, tc := range []testCase{
|
|
|
|
{
|
|
|
|
description: "NoError",
|
|
|
|
accounts: []Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true, Wallet: true},
|
|
|
|
{Address: types.Address{0x02}},
|
2019-08-20 15:38:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "UniqueChat",
|
|
|
|
accounts: []Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true},
|
|
|
|
{Address: types.Address{0x02}, Chat: true},
|
2019-08-20 15:38:40 +00:00
|
|
|
},
|
|
|
|
err: ErrChatNotUnique,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "UniqueWallet",
|
|
|
|
accounts: []Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Wallet: true},
|
|
|
|
{Address: types.Address{0x02}, Wallet: true},
|
2019-08-20 15:38:40 +00:00
|
|
|
},
|
|
|
|
err: 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()
|
|
|
|
accounts := []Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true, Wallet: true},
|
|
|
|
{Address: types.Address{0x02}},
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-12-16 15:23:36 +00:00
|
|
|
func TestDeleteAccount(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
accounts := []Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true, Wallet: true},
|
2019-12-16 15:23:36 +00:00
|
|
|
}
|
|
|
|
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}))
|
2019-12-16 15:23:36 +00:00
|
|
|
rst2, err := db.GetAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(rst2))
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func TestGetAddresses(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
accounts := []Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true, Wallet: true},
|
|
|
|
{Address: types.Address{0x02}},
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
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)
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWalletAddress(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2019-12-11 13:59:37 +00:00
|
|
|
address := types.Address{0x01}
|
2019-08-20 15:38:40 +00:00
|
|
|
_, err := db.GetWalletAddress()
|
|
|
|
require.Equal(t, err, sql.ErrNoRows)
|
|
|
|
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}
|
2019-08-20 15:38:40 +00:00
|
|
|
_, err := db.GetChatAddress()
|
|
|
|
require.Equal(t, err, sql.ErrNoRows)
|
|
|
|
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()
|
|
|
|
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}},
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, db.SaveAccounts(accounts))
|
|
|
|
rst, err := db.GetAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, accounts, rst)
|
|
|
|
}
|
2019-08-29 08:06:22 +00:00
|
|
|
|
2020-10-28 07:56:14 +00:00
|
|
|
func TestGetAccountByAddress(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
address := types.Address{0x01}
|
|
|
|
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)
|
|
|
|
require.Equal(t, &account, rst)
|
|
|
|
}
|
|
|
|
|
2019-08-29 08:06:22 +00:00
|
|
|
func TestAddressExists(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
accounts := []Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true, Wallet: true},
|
2019-08-29 08:06:22 +00:00
|
|
|
}
|
|
|
|
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})
|
2019-08-29 08:06:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, exists)
|
|
|
|
}
|