Skip to content

Commit

Permalink
chore(multiaccount)_: Adapt database functions to handle new column
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmotta committed Aug 24, 2024
1 parent b1f196d commit cb5f243
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 10 deletions.
24 changes: 20 additions & 4 deletions multiaccounts/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Account struct {
Images []images.IdentityImage `json:"images"`
KDFIterations int `json:"kdfIterations,omitempty"`
CustomizationColorClock uint64 `json:"-"`

// HasAcceptedTerms will be set to true when the first account is created.
HasAcceptedTerms bool `json:"hasAcceptedTerms"`
}

func (a *Account) RefersToKeycard() bool {
Expand Down Expand Up @@ -145,7 +148,7 @@ func (db *Database) GetAccountKDFIterationsNumber(keyUID string) (kdfIterationsN
}

func (db *Database) GetAccounts() (rst []Account, err error) {
rows, err := db.db.Query("SELECT a.name, a.loginTimestamp, a.identicon, a.colorHash, a.colorId, a.customizationColor, a.customizationColorClock, a.keycardPairing, a.keyUid, a.kdfIterations, ii.name, ii.image_payload, ii.width, ii.height, ii.file_size, ii.resize_target, ii.clock FROM accounts AS a LEFT JOIN identity_images AS ii ON ii.key_uid = a.keyUid ORDER BY loginTimestamp DESC")
rows, err := db.db.Query("SELECT a.name, a.loginTimestamp, a.identicon, a.colorHash, a.colorId, a.customizationColor, a.customizationColorClock, a.keycardPairing, a.keyUid, a.kdfIterations, a.hasAcceptedTerms, ii.name, ii.image_payload, ii.width, ii.height, ii.file_size, ii.resize_target, ii.clock FROM accounts AS a LEFT JOIN identity_images AS ii ON ii.key_uid = a.keyUid ORDER BY loginTimestamp DESC")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -179,6 +182,7 @@ func (db *Database) GetAccounts() (rst []Account, err error) {
&acc.KeycardPairing,
&acc.KeyUID,
&acc.KDFIterations,
&acc.HasAcceptedTerms,
&iiName,
&ii.Payload,
&iiWidth,
Expand Down Expand Up @@ -236,8 +240,14 @@ func (db *Database) GetAccounts() (rst []Account, err error) {
return rst, nil
}

func (db *Database) GetAccountsCount() (int, error) {
var count int
err := db.db.QueryRow("SELECT COUNT(1) FROM accounts").Scan(&count)
return count, err
}

func (db *Database) GetAccount(keyUID string) (*Account, error) {
rows, err := db.db.Query("SELECT a.name, a.loginTimestamp, a.identicon, a.colorHash, a.colorId, a.customizationColor, a.customizationColorClock, a.keycardPairing, a.keyUid, a.kdfIterations, ii.key_uid, ii.name, ii.image_payload, ii.width, ii.height, ii.file_size, ii.resize_target, ii.clock FROM accounts AS a LEFT JOIN identity_images AS ii ON ii.key_uid = a.keyUid WHERE a.keyUid = ? ORDER BY loginTimestamp DESC", keyUID)
rows, err := db.db.Query("SELECT a.name, a.loginTimestamp, a.identicon, a.colorHash, a.colorId, a.customizationColor, a.customizationColorClock, a.keycardPairing, a.keyUid, a.kdfIterations, a.hasAcceptedTerms, ii.key_uid, ii.name, ii.image_payload, ii.width, ii.height, ii.file_size, ii.resize_target, ii.clock FROM accounts AS a LEFT JOIN identity_images AS ii ON ii.key_uid = a.keyUid WHERE a.keyUid = ? ORDER BY loginTimestamp DESC", keyUID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -273,6 +283,7 @@ func (db *Database) GetAccount(keyUID string) (*Account, error) {
&acc.KeycardPairing,
&acc.KeyUID,
&acc.KDFIterations,
&acc.HasAcceptedTerms,
&iiKeyUID,
&iiName,
&ii.Payload,
Expand Down Expand Up @@ -323,7 +334,7 @@ func (db *Database) SaveAccount(account Account) error {
account.KDFIterations = dbsetup.ReducedKDFIterationsNumber
}

_, err = db.db.Exec("INSERT OR REPLACE INTO accounts (name, identicon, colorHash, colorId, customizationColor, customizationColorClock, keycardPairing, keyUid, kdfIterations, loginTimestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", account.Name, account.Identicon, colorHash, account.ColorID, account.CustomizationColor, account.CustomizationColorClock, account.KeycardPairing, account.KeyUID, account.KDFIterations, account.Timestamp)
_, err = db.db.Exec("INSERT OR REPLACE INTO accounts (name, identicon, colorHash, colorId, customizationColor, customizationColorClock, keycardPairing, keyUid, kdfIterations, loginTimestamp, hasAcceptedTerms) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", account.Name, account.Identicon, colorHash, account.ColorID, account.CustomizationColor, account.CustomizationColorClock, account.KeycardPairing, account.KeyUID, account.KDFIterations, account.Timestamp, account.HasAcceptedTerms)
if err != nil {
return err
}
Expand All @@ -340,6 +351,11 @@ func (db *Database) UpdateDisplayName(keyUID string, displayName string) error {
return err
}

func (db *Database) UpdateHasAcceptedTerms(keyUID string, hasAcceptedTerms bool) error {
_, err := db.db.Exec("UPDATE accounts SET hasAcceptedTerms = ? WHERE keyUid = ?", hasAcceptedTerms, keyUID)
return err
}

func (db *Database) UpdateAccount(account Account) error {
colorHash, err := json.Marshal(account.ColorHash)
if err != nil {
Expand All @@ -350,7 +366,7 @@ func (db *Database) UpdateAccount(account Account) error {
account.KDFIterations = dbsetup.ReducedKDFIterationsNumber
}

_, err = db.db.Exec("UPDATE accounts SET name = ?, identicon = ?, colorHash = ?, colorId = ?, customizationColor = ?, customizationColorClock = ?, keycardPairing = ?, kdfIterations = ? WHERE keyUid = ?", account.Name, account.Identicon, colorHash, account.ColorID, account.CustomizationColor, account.CustomizationColorClock, account.KeycardPairing, account.KDFIterations, account.KeyUID)
_, err = db.db.Exec("UPDATE accounts SET name = ?, identicon = ?, colorHash = ?, colorId = ?, customizationColor = ?, customizationColorClock = ?, keycardPairing = ?, kdfIterations = ?, hasAcceptedTerms = ? WHERE keyUid = ?", account.Name, account.Identicon, colorHash, account.ColorID, account.CustomizationColor, account.CustomizationColorClock, account.KeycardPairing, account.KDFIterations, account.HasAcceptedTerms, account.KeyUID)
return err
}

Expand Down
175 changes: 169 additions & 6 deletions multiaccounts/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io/ioutil"
"os"
"strings"
"testing"

"github.com/status-im/status-go/common/dbsetup"
Expand Down Expand Up @@ -39,17 +40,71 @@ func TestAccounts(t *testing.T) {
func TestAccountsUpdate(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
expected := Account{KeyUID: "string", CustomizationColor: common.CustomizationColorBlue, ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}}, ColorID: 10, KDFIterations: dbsetup.ReducedKDFIterationsNumber}
expected := Account{
KeyUID: "string",
CustomizationColor: common.CustomizationColorBlue,
ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}},
ColorID: 10,
KDFIterations: dbsetup.ReducedKDFIterationsNumber,
}
require.NoError(t, db.SaveAccount(expected))
expected.Name = "chars"
expected.CustomizationColor = common.CustomizationColorMagenta
expected.HasAcceptedTerms = true
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 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)
}

func TestLoginUpdate(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
Expand Down Expand Up @@ -148,20 +203,26 @@ func TestDatabase_DeleteIdentityImage(t *testing.T) {
require.Empty(t, oii)
}

func removeAllWhitespace(s string) string {
tmp := strings.ReplaceAll(s, " ", "")
tmp = strings.ReplaceAll(tmp, "\n", "")
tmp = strings.ReplaceAll(tmp, "\t", "")
return tmp
}

func TestDatabase_GetAccountsWithIdentityImages(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()

testAccs := []Account{
{Name: "string", KeyUID: keyUID, Identicon: "data"},
{Name: "string", KeyUID: keyUID, Identicon: "data", HasAcceptedTerms: true},
{Name: "string", KeyUID: keyUID2},
{Name: "string", KeyUID: keyUID2 + "2"},
{Name: "string", KeyUID: keyUID2 + "3"},
}
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},{"name":"string","timestamp":10,"identicon":"","colorHash":null,"colorId":0,"keycard-pairing":"","key-uid":"0x1337beef","images":null,"kdfIterations":3200},{"name":"string","timestamp":0,"identicon":"","colorHash":null,"colorId":0,"keycard-pairing":"","key-uid":"0x1337beef2","images":null,"kdfIterations":3200},{"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}]`

for _, a := range testAccs {
require.NoError(t, db.SaveAccount(a))
require.NoError(t, db.SaveAccount(a), a.KeyUID)
}

seedTestDBWithIdentityImages(t, db, keyUID)
Expand All @@ -178,14 +239,116 @@ func TestDatabase_GetAccountsWithIdentityImages(t *testing.T) {
accJSON, err := json.Marshal(accs)
require.NoError(t, err)

require.Exactly(t, expected, string(accJSON))
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))
}

func TestDatabase_GetAccount(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()

expected := Account{Name: "string", KeyUID: keyUID, ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}}, ColorID: 10, KDFIterations: dbsetup.ReducedKDFIterationsNumber}
expected := Account{
Name: "string",
KeyUID: keyUID,
ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}},
ColorID: 10,
KDFIterations: dbsetup.ReducedKDFIterationsNumber,
HasAcceptedTerms: true,
}
require.NoError(t, db.SaveAccount(expected))

account, err := db.GetAccount(expected.KeyUID)
Expand Down

0 comments on commit cb5f243

Please sign in to comment.