2019-08-20 15:38:40 +00:00
|
|
|
package multiaccounts
|
|
|
|
|
|
|
|
import (
|
2020-11-24 13:13:46 +00:00
|
|
|
"encoding/json"
|
2019-08-20 15:38:40 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2024-08-26 13:56:39 +00:00
|
|
|
"strings"
|
2019-08-20 15:38:40 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-10-02 09:28:42 +00:00
|
|
|
"github.com/status-im/status-go/common/dbsetup"
|
2020-11-24 13:13:46 +00:00
|
|
|
"github.com/status-im/status-go/images"
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-06-02 15:06:51 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/multiaccounts/common"
|
2019-08-20 15:38:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupTestDB(t *testing.T) (*Database, func()) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "accounts-tests-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
db, err := InitializeDB(tmpfile.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
return db, func() {
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
require.NoError(t, os.Remove(tmpfile.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccounts(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2024-04-18 07:34:00 +00:00
|
|
|
expected := Account{Name: "string", KeyUID: "string", CustomizationColor: common.CustomizationColorBlue, ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}}, ColorID: 10, KDFIterations: dbsetup.ReducedKDFIterationsNumber, Timestamp: 1712856359}
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, db.SaveAccount(expected))
|
|
|
|
accounts, err := db.GetAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, accounts, 1)
|
|
|
|
require.Equal(t, expected, accounts[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountsUpdate(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2024-08-26 13:56:39 +00:00
|
|
|
expected := Account{
|
|
|
|
KeyUID: "string",
|
|
|
|
CustomizationColor: common.CustomizationColorBlue,
|
|
|
|
ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}},
|
|
|
|
ColorID: 10,
|
|
|
|
KDFIterations: dbsetup.ReducedKDFIterationsNumber,
|
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, db.SaveAccount(expected))
|
2020-11-24 13:13:46 +00:00
|
|
|
expected.Name = "chars"
|
2023-06-02 15:06:51 +00:00
|
|
|
expected.CustomizationColor = common.CustomizationColorMagenta
|
2024-08-26 13:56:39 +00:00
|
|
|
expected.HasAcceptedTerms = true
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, db.UpdateAccount(expected))
|
|
|
|
rst, err := db.GetAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, rst, 1)
|
|
|
|
require.Equal(t, expected, rst[0])
|
|
|
|
}
|
|
|
|
|
2024-08-26 13:56:39 +00:00
|
|
|
func TestUpdateHasAcceptedTerms(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
keyUID := "string"
|
|
|
|
expected := Account{
|
|
|
|
KeyUID: keyUID,
|
|
|
|
KDFIterations: dbsetup.ReducedKDFIterationsNumber,
|
|
|
|
}
|
|
|
|
require.NoError(t, db.SaveAccount(expected))
|
|
|
|
accounts, err := db.GetAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []Account{expected}, accounts)
|
|
|
|
|
|
|
|
// Update from false -> true
|
|
|
|
require.NoError(t, db.UpdateHasAcceptedTerms(keyUID, true))
|
|
|
|
account, err := db.GetAccount(keyUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
expected.HasAcceptedTerms = true
|
|
|
|
require.Equal(t, &expected, account)
|
|
|
|
|
|
|
|
// Update from true -> false
|
|
|
|
require.NoError(t, db.UpdateHasAcceptedTerms(keyUID, false))
|
|
|
|
account, err = db.GetAccount(keyUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
expected.HasAcceptedTerms = false
|
|
|
|
require.Equal(t, &expected, account)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDatabase_GetAccountsCount(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
count, err := db.GetAccountsCount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, count)
|
|
|
|
|
|
|
|
account := Account{
|
|
|
|
KeyUID: keyUID,
|
|
|
|
KDFIterations: dbsetup.ReducedKDFIterationsNumber,
|
|
|
|
}
|
|
|
|
require.NoError(t, db.SaveAccount(account))
|
|
|
|
|
|
|
|
count, err = db.GetAccountsCount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, count)
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func TestLoginUpdate(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2023-10-02 09:28:42 +00:00
|
|
|
accounts := []Account{{Name: "first", KeyUID: "0x1", KDFIterations: dbsetup.ReducedKDFIterationsNumber}, {Name: "second", KeyUID: "0x2", KDFIterations: dbsetup.ReducedKDFIterationsNumber}}
|
2019-08-20 15:38:40 +00:00
|
|
|
for _, acc := range accounts {
|
|
|
|
require.NoError(t, db.SaveAccount(acc))
|
|
|
|
}
|
2019-12-05 08:00:57 +00:00
|
|
|
require.NoError(t, db.UpdateAccountTimestamp(accounts[0].KeyUID, 100))
|
|
|
|
require.NoError(t, db.UpdateAccountTimestamp(accounts[1].KeyUID, 10))
|
2019-08-20 15:38:40 +00:00
|
|
|
accounts[0].Timestamp = 100
|
|
|
|
accounts[1].Timestamp = 10
|
|
|
|
rst, err := db.GetAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, accounts, rst)
|
|
|
|
}
|
2020-11-24 13:13:46 +00:00
|
|
|
|
|
|
|
// Profile Image tests
|
|
|
|
|
|
|
|
var (
|
2020-11-24 23:16:19 +00:00
|
|
|
keyUID = "0xdeadbeef"
|
|
|
|
keyUID2 = "0x1337beef"
|
2020-11-24 13:13:46 +00:00
|
|
|
)
|
|
|
|
|
2020-12-01 18:15:53 +00:00
|
|
|
func seedTestDBWithIdentityImages(t *testing.T, db *Database, keyUID string) {
|
2020-11-24 13:13:46 +00:00
|
|
|
iis := images.SampleIdentityImages()
|
2022-03-24 09:35:56 +00:00
|
|
|
require.NoError(t, db.StoreIdentityImages(keyUID, iis, false))
|
2020-11-24 13:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDatabase_GetIdentityImages(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2020-12-01 18:15:53 +00:00
|
|
|
seedTestDBWithIdentityImages(t, db, keyUID)
|
2020-11-24 13:13:46 +00:00
|
|
|
|
2022-03-24 09:35:56 +00:00
|
|
|
expected := `[{"keyUid":"0xdeadbeef","type":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"fileSize":1024,"resizeTarget":240,"clock":0},{"keyUid":"0xdeadbeef","type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"fileSize":256,"resizeTarget":80,"clock":0}]`
|
2020-11-24 13:13:46 +00:00
|
|
|
|
2020-11-24 23:16:19 +00:00
|
|
|
oiis, err := db.GetIdentityImages(keyUID)
|
2020-11-24 13:13:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
joiis, err := json.Marshal(oiis)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Exactly(t, expected, string(joiis))
|
|
|
|
|
2020-11-24 23:16:19 +00:00
|
|
|
oiis, err = db.GetIdentityImages(keyUID2)
|
2020-11-24 13:13:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Exactly(t, 0, len(oiis))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDatabase_GetIdentityImage(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2020-12-01 18:15:53 +00:00
|
|
|
seedTestDBWithIdentityImages(t, db, keyUID)
|
2020-11-24 13:13:46 +00:00
|
|
|
|
|
|
|
cs := []struct {
|
2020-11-25 00:34:32 +00:00
|
|
|
KeyUID string
|
2020-11-24 13:13:46 +00:00
|
|
|
Name string
|
|
|
|
Expected string
|
|
|
|
}{
|
|
|
|
{
|
2020-11-24 23:16:19 +00:00
|
|
|
keyUID,
|
2020-11-24 13:13:46 +00:00
|
|
|
images.SmallDimName,
|
2022-03-24 09:35:56 +00:00
|
|
|
`{"keyUid":"0xdeadbeef","type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"fileSize":256,"resizeTarget":80,"clock":0}`,
|
2020-11-24 13:13:46 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-24 23:16:19 +00:00
|
|
|
keyUID,
|
2020-11-24 13:13:46 +00:00
|
|
|
images.LargeDimName,
|
2022-03-24 09:35:56 +00:00
|
|
|
`{"keyUid":"0xdeadbeef","type":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"fileSize":1024,"resizeTarget":240,"clock":0}`,
|
2020-11-24 13:13:46 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-24 23:16:19 +00:00
|
|
|
keyUID2,
|
2020-11-24 13:13:46 +00:00
|
|
|
images.LargeDimName,
|
2020-12-15 18:00:31 +00:00
|
|
|
"null",
|
2020-11-24 13:13:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cs {
|
2020-11-25 00:34:32 +00:00
|
|
|
oii, err := db.GetIdentityImage(c.KeyUID, c.Name)
|
2020-11-24 13:13:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
joii, err := json.Marshal(oii)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Exactly(t, c.Expected, string(joii))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDatabase_DeleteIdentityImage(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2020-12-01 18:15:53 +00:00
|
|
|
seedTestDBWithIdentityImages(t, db, keyUID)
|
2020-11-24 13:13:46 +00:00
|
|
|
|
2020-11-24 23:16:19 +00:00
|
|
|
require.NoError(t, db.DeleteIdentityImage(keyUID))
|
2020-11-24 13:13:46 +00:00
|
|
|
|
2020-11-24 23:16:19 +00:00
|
|
|
oii, err := db.GetIdentityImage(keyUID, images.SmallDimName)
|
2020-11-24 13:13:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, oii)
|
|
|
|
}
|
2020-12-01 18:15:53 +00:00
|
|
|
|
2024-08-26 13:56:39 +00:00
|
|
|
func removeAllWhitespace(s string) string {
|
|
|
|
tmp := strings.ReplaceAll(s, " ", "")
|
|
|
|
tmp = strings.ReplaceAll(tmp, "\n", "")
|
|
|
|
tmp = strings.ReplaceAll(tmp, "\t", "")
|
|
|
|
return tmp
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:15:53 +00:00
|
|
|
func TestDatabase_GetAccountsWithIdentityImages(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
testAccs := []Account{
|
2024-08-26 13:56:39 +00:00
|
|
|
{Name: "string", KeyUID: keyUID, Identicon: "data", HasAcceptedTerms: true},
|
2020-12-01 18:15:53 +00:00
|
|
|
{Name: "string", KeyUID: keyUID2},
|
|
|
|
{Name: "string", KeyUID: keyUID2 + "2"},
|
|
|
|
{Name: "string", KeyUID: keyUID2 + "3"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range testAccs {
|
2024-08-26 13:56:39 +00:00
|
|
|
require.NoError(t, db.SaveAccount(a), a.KeyUID)
|
2020-12-01 18:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
seedTestDBWithIdentityImages(t, db, keyUID)
|
2020-12-15 18:00:31 +00:00
|
|
|
seedTestDBWithIdentityImages(t, db, keyUID2+"3")
|
2020-12-01 18:15:53 +00:00
|
|
|
|
|
|
|
err := db.UpdateAccountTimestamp(keyUID, 100)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = db.UpdateAccountTimestamp(keyUID2, 10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
accs, err := db.GetAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-15 18:00:31 +00:00
|
|
|
accJSON, err := json.Marshal(accs)
|
2020-12-01 18:15:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-08-26 13:56:39 +00:00
|
|
|
expected := `
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"name": "string",
|
|
|
|
"timestamp": 100,
|
|
|
|
"identicon": "data",
|
|
|
|
"colorHash": null,
|
|
|
|
"colorId": 0,
|
|
|
|
"keycard-pairing": "",
|
|
|
|
"key-uid": "0xdeadbeef",
|
|
|
|
"images": [
|
|
|
|
{
|
|
|
|
"keyUid": "0xdeadbeef",
|
|
|
|
"type": "large",
|
|
|
|
"uri": "data:image/png;base64,iVBORw0KGgoAAAANSUg=",
|
|
|
|
"width": 240,
|
|
|
|
"height": 300,
|
|
|
|
"fileSize": 1024,
|
|
|
|
"resizeTarget": 240,
|
|
|
|
"clock": 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"keyUid": "0xdeadbeef",
|
|
|
|
"type": "thumbnail",
|
|
|
|
"uri": "data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=",
|
|
|
|
"width": 80,
|
|
|
|
"height": 80,
|
|
|
|
"fileSize": 256,
|
|
|
|
"resizeTarget": 80,
|
|
|
|
"clock": 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"kdfIterations": 3200,
|
|
|
|
"hasAcceptedTerms": true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "string",
|
|
|
|
"timestamp": 10,
|
|
|
|
"identicon": "",
|
|
|
|
"colorHash": null,
|
|
|
|
"colorId": 0,
|
|
|
|
"keycard-pairing": "",
|
|
|
|
"key-uid": "0x1337beef",
|
|
|
|
"images": null,
|
|
|
|
"kdfIterations": 3200,
|
|
|
|
"hasAcceptedTerms": false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "string",
|
|
|
|
"timestamp": 0,
|
|
|
|
"identicon": "",
|
|
|
|
"colorHash": null,
|
|
|
|
"colorId": 0,
|
|
|
|
"keycard-pairing": "",
|
|
|
|
"key-uid": "0x1337beef2",
|
|
|
|
"images": null,
|
|
|
|
"kdfIterations": 3200,
|
|
|
|
"hasAcceptedTerms": false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "string",
|
|
|
|
"timestamp": 0,
|
|
|
|
"identicon": "",
|
|
|
|
"colorHash": null,
|
|
|
|
"colorId": 0,
|
|
|
|
"keycard-pairing": "",
|
|
|
|
"key-uid": "0x1337beef3",
|
|
|
|
"images": [
|
|
|
|
{
|
|
|
|
"keyUid": "0x1337beef3",
|
|
|
|
"type": "large",
|
|
|
|
"uri": "data:image/png;base64,iVBORw0KGgoAAAANSUg=",
|
|
|
|
"width": 240,
|
|
|
|
"height": 300,
|
|
|
|
"fileSize": 1024,
|
|
|
|
"resizeTarget": 240,
|
|
|
|
"clock": 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"keyUid": "0x1337beef3",
|
|
|
|
"type": "thumbnail",
|
|
|
|
"uri": "data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=",
|
|
|
|
"width": 80,
|
|
|
|
"height": 80,
|
|
|
|
"fileSize": 256,
|
|
|
|
"resizeTarget": 80,
|
|
|
|
"clock": 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"kdfIterations": 3200,
|
|
|
|
"hasAcceptedTerms": false
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`
|
|
|
|
|
|
|
|
require.Exactly(t, removeAllWhitespace(expected), string(accJSON))
|
2020-12-01 18:15:53 +00:00
|
|
|
}
|
2022-06-27 13:48:00 +00:00
|
|
|
|
|
|
|
func TestDatabase_GetAccount(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2024-08-26 13:56:39 +00:00
|
|
|
expected := Account{
|
|
|
|
Name: "string",
|
|
|
|
KeyUID: keyUID,
|
|
|
|
ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}},
|
|
|
|
ColorID: 10,
|
|
|
|
KDFIterations: dbsetup.ReducedKDFIterationsNumber,
|
|
|
|
HasAcceptedTerms: true,
|
|
|
|
}
|
2022-06-27 13:48:00 +00:00
|
|
|
require.NoError(t, db.SaveAccount(expected))
|
|
|
|
|
|
|
|
account, err := db.GetAccount(expected.KeyUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &expected, account)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDatabase_SaveAccountWithIdentityImages(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
expected := Account{
|
|
|
|
Name: "string",
|
|
|
|
KeyUID: keyUID,
|
2023-03-22 17:48:42 +00:00
|
|
|
ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}},
|
2022-06-27 13:48:00 +00:00
|
|
|
ColorID: 10,
|
|
|
|
Images: images.SampleIdentityImages(),
|
|
|
|
}
|
|
|
|
require.NoError(t, db.SaveAccount(expected))
|
|
|
|
|
|
|
|
account, err := db.GetAccount(expected.KeyUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Exactly(t, expected.ColorHash, account.ColorHash)
|
|
|
|
require.Exactly(t, expected.ColorID, account.ColorID)
|
|
|
|
require.Exactly(t, expected.Identicon, account.Identicon)
|
|
|
|
require.Exactly(t, expected.KeycardPairing, account.KeycardPairing)
|
|
|
|
require.Exactly(t, expected.KeyUID, account.KeyUID)
|
|
|
|
require.Exactly(t, expected.Name, account.Name)
|
|
|
|
require.Exactly(t, expected.Timestamp, account.Timestamp)
|
|
|
|
require.Len(t, expected.Images, 2)
|
|
|
|
|
|
|
|
matches := 0
|
|
|
|
for _, expImg := range expected.Images {
|
|
|
|
for _, accImg := range account.Images {
|
|
|
|
if expImg.Name != accImg.Name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
matches++
|
|
|
|
|
|
|
|
require.Exactly(t, expImg.Clock, accImg.Clock)
|
|
|
|
require.Exactly(t, keyUID, accImg.KeyUID)
|
|
|
|
require.Exactly(t, expImg.Name, accImg.Name)
|
|
|
|
require.Exactly(t, expImg.ResizeTarget, accImg.ResizeTarget)
|
|
|
|
require.Exactly(t, expImg.Payload, accImg.Payload)
|
|
|
|
require.Exactly(t, expImg.Height, accImg.Height)
|
|
|
|
require.Exactly(t, expImg.Width, accImg.Width)
|
|
|
|
require.Exactly(t, expImg.FileSize, accImg.FileSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, 2, matches)
|
|
|
|
}
|