2021-09-10 18:08:22 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
2022-09-27 20:27:20 +00:00
|
|
|
"github.com/status-im/status-go/sqlite"
|
2021-09-10 18:08:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupTestSavedAddressesDB(t *testing.T) (*SavedAddressesManager, func()) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "wallet-saved_addresses-tests-")
|
|
|
|
require.NoError(t, err)
|
2022-09-27 20:27:20 +00:00
|
|
|
db, err := appdatabase.InitializeDB(tmpfile.Name(), "wallet-saved_addresses-tests", sqlite.ReducedKDFIterationsNumber)
|
2021-09-10 18:08:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return &SavedAddressesManager{db}, func() {
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
require.NoError(t, os.Remove(tmpfile.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSavedAddresses(t *testing.T) {
|
|
|
|
manager, stop := setupTestSavedAddressesDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
rst, err := manager.GetSavedAddresses(777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, rst)
|
|
|
|
|
|
|
|
sa := SavedAddress{
|
2022-08-18 14:51:13 +00:00
|
|
|
Address: common.Address{1},
|
|
|
|
Name: "Zilliqa",
|
|
|
|
Favourite: true,
|
|
|
|
ChainID: 777,
|
2021-09-10 18:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = manager.AddSavedAddress(sa)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rst, err = manager.GetSavedAddresses(777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rst))
|
2022-05-10 07:48:05 +00:00
|
|
|
require.Equal(t, sa, rst[0])
|
2021-09-10 18:08:22 +00:00
|
|
|
|
|
|
|
err = manager.DeleteSavedAddress(777, sa.Address)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rst, err = manager.GetSavedAddresses(777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(rst))
|
|
|
|
}
|