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"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
2019-12-05 08:00:57 +00:00
|
|
|
expected := Account{Name: "string", KeyUID: "string"}
|
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()
|
2019-12-05 08:00:57 +00:00
|
|
|
expected := Account{KeyUID: "string"}
|
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"
|
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])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoginUpdate(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2019-12-05 08:00:57 +00:00
|
|
|
accounts := []Account{{Name: "first", KeyUID: "0x1"}, {Name: "second", KeyUID: "0x2"}}
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func seedTestDB(t *testing.T, db *Database) {
|
|
|
|
iis := images.SampleIdentityImages()
|
2020-11-24 23:16:19 +00:00
|
|
|
require.NoError(t, db.StoreIdentityImages(keyUID, iis))
|
2020-11-24 13:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDatabase_GetIdentityImages(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
seedTestDB(t, db)
|
|
|
|
|
|
|
|
expected := `[{"key_uid":"0xdeadbeef","type":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"file_size":1024,"resize_target":240},{"key_uid":"0xdeadbeef","type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}]`
|
|
|
|
|
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()
|
|
|
|
seedTestDB(t, db)
|
|
|
|
|
|
|
|
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,
|
|
|
|
`{"key_uid":"0xdeadbeef","type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}`,
|
|
|
|
},
|
|
|
|
{
|
2020-11-24 23:16:19 +00:00
|
|
|
keyUID,
|
2020-11-24 13:13:46 +00:00
|
|
|
images.LargeDimName,
|
|
|
|
`{"key_uid":"0xdeadbeef","type":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"file_size":1024,"resize_target":240}`,
|
|
|
|
},
|
|
|
|
{
|
2020-11-24 23:16:19 +00:00
|
|
|
keyUID2,
|
2020-11-24 13:13:46 +00:00
|
|
|
images.LargeDimName,
|
|
|
|
`{"key_uid":"","type":"","uri":"","width":0,"height":0,"file_size":0,"resize_target":0}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
seedTestDB(t, db)
|
|
|
|
|
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)
|
|
|
|
}
|