Added multiaccounts.Database tests for new funcs
This commit is contained in:
parent
83db7fc795
commit
344458d74a
|
@ -63,3 +63,10 @@ func (i IdentityImage) MarshalJSON() ([]byte, error) {
|
||||||
|
|
||||||
return json.Marshal(temp)
|
return json.Marshal(temp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i IdentityImage) IsEmpty() bool {
|
||||||
|
if i.KeyUID == "" && i.Name == "" && len(i.Payload) == 0 && i.Width == 0 && i.Height == 0 && i.FileSize == 0 && i.ResizeTarget == 0 && i.Clock == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
|
@ -5,8 +5,8 @@ import (
|
||||||
"image"
|
"image"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateImageVariants(cImg image.Image) ([]*IdentityImage, error) {
|
func GenerateImageVariants(cImg image.Image) ([]IdentityImage, error) {
|
||||||
var iis []*IdentityImage
|
var iis []IdentityImage
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
for _, s := range ResizeDimensions {
|
for _, s := range ResizeDimensions {
|
||||||
|
@ -18,7 +18,7 @@ func GenerateImageVariants(cImg image.Image) ([]*IdentityImage, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ii := &IdentityImage{
|
ii := IdentityImage{
|
||||||
Name: ResizeDimensionToName[s],
|
Name: ResizeDimensionToName[s],
|
||||||
Payload: bb.Bytes(),
|
Payload: bb.Bytes(),
|
||||||
Width: rImg.Bounds().Dx(),
|
Width: rImg.Bounds().Dx(),
|
||||||
|
@ -33,7 +33,7 @@ func GenerateImageVariants(cImg image.Image) ([]*IdentityImage, error) {
|
||||||
return iis, nil
|
return iis, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateIdentityImages(filepath string, aX, aY, bX, bY int) ([]*IdentityImage, error) {
|
func GenerateIdentityImages(filepath string, aX, aY, bX, bY int) ([]IdentityImage, error) {
|
||||||
img, err := Decode(filepath)
|
img, err := Decode(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -51,7 +51,7 @@ func GenerateIdentityImages(filepath string, aX, aY, bX, bY int) ([]*IdentityIma
|
||||||
return GenerateImageVariants(cImg)
|
return GenerateImageVariants(cImg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateIdentityImagesFromURL(url string) ([]*IdentityImage, error) {
|
func GenerateIdentityImagesFromURL(url string) ([]IdentityImage, error) {
|
||||||
img, err := DecodeFromURL(url)
|
img, err := DecodeFromURL(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -9,8 +9,8 @@ var (
|
||||||
testAacBytes = []byte{0xff, 0xf1, 0x50, 0x80, 0x1c, 0x3f, 0xfc, 0xda, 0x00, 0x4c, 0x61, 0x76, 0x63, 0x35}
|
testAacBytes = []byte{0xff, 0xf1, 0x50, 0x80, 0x1c, 0x3f, 0xfc, 0xda, 0x00, 0x4c, 0x61, 0x76, 0x63, 0x35}
|
||||||
)
|
)
|
||||||
|
|
||||||
func SampleIdentityImages() []*IdentityImage {
|
func SampleIdentityImages() []IdentityImage {
|
||||||
return []*IdentityImage{
|
return []IdentityImage{
|
||||||
{
|
{
|
||||||
Name: SmallDimName,
|
Name: SmallDimName,
|
||||||
Payload: testJpegBytes,
|
Payload: testJpegBytes,
|
||||||
|
|
|
@ -138,8 +138,7 @@ func (db *Database) GetAccounts() (rst []Account, err error) {
|
||||||
return rst, nil
|
return rst, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *Database) GetAccount(keyUID string) (acc *Account, err error) {
|
func (db *Database) GetAccount(keyUID string) (*Account, error) {
|
||||||
// TODO add test for this
|
|
||||||
rows, err := db.db.Query("SELECT a.name, a.loginTimestamp, a.identicon, a.colorHash, a.colorId, a.keycardPairing, a.keyUid, 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.keycardPairing, a.keyUid, 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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -149,6 +148,8 @@ func (db *Database) GetAccount(keyUID string) (acc *Account, err error) {
|
||||||
err = valueOr(err, errClose)
|
err = valueOr(err, errClose)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
acc := new(Account)
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
accLoginTimestamp := sql.NullInt64{}
|
accLoginTimestamp := sql.NullInt64{}
|
||||||
accIdenticon := sql.NullString{}
|
accIdenticon := sql.NullString{}
|
||||||
|
@ -200,12 +201,8 @@ func (db *Database) GetAccount(keyUID string) (acc *Account, err error) {
|
||||||
ii.ResizeTarget = int(iiResizeTarget.Int64)
|
ii.ResizeTarget = int(iiResizeTarget.Int64)
|
||||||
ii.Clock = uint64(iiClock.Int64)
|
ii.Clock = uint64(iiClock.Int64)
|
||||||
|
|
||||||
if ii.Name == "" && len(ii.Payload) == 0 && ii.Width == 0 && ii.Height == 0 && ii.FileSize == 0 && ii.ResizeTarget == 0 {
|
// Don't process empty identity images
|
||||||
ii = nil
|
if !ii.IsEmpty() {
|
||||||
}
|
|
||||||
|
|
||||||
// Don't process nil identity images
|
|
||||||
if ii != nil {
|
|
||||||
acc.Images = append(acc.Images, *ii)
|
acc.Images = append(acc.Images, *ii)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -228,11 +225,7 @@ func (db *Database) SaveAccount(account Account) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var iis []*images.IdentityImage
|
return db.StoreIdentityImages(account.KeyUID, account.Images, false)
|
||||||
for _, ii := range account.Images {
|
|
||||||
iis = append(iis, &ii)
|
|
||||||
}
|
|
||||||
return db.StoreIdentityImages(account.KeyUID, iis, false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *Database) UpdateAccount(account Account) error {
|
func (db *Database) UpdateAccount(account Account) error {
|
||||||
|
@ -295,7 +288,7 @@ func (db *Database) GetIdentityImage(keyUID, it string) (*images.IdentityImage,
|
||||||
return &ii, nil
|
return &ii, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *Database) StoreIdentityImages(keyUID string, iis []*images.IdentityImage, publish bool) (err error) {
|
func (db *Database) StoreIdentityImages(keyUID string, iis []images.IdentityImage, publish bool) (err error) {
|
||||||
// Because SQL INSERTs are triggered in a loop use a tx to ensure a single call to the DB.
|
// Because SQL INSERTs are triggered in a loop use a tx to ensure a single call to the DB.
|
||||||
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
|
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -312,7 +305,7 @@ func (db *Database) StoreIdentityImages(keyUID string, iis []*images.IdentityIma
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for _, ii := range iis {
|
for _, ii := range iis {
|
||||||
if ii == nil {
|
if ii.IsEmpty() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -176,3 +176,60 @@ func TestDatabase_GetAccountsWithIdentityImages(t *testing.T) {
|
||||||
|
|
||||||
require.Exactly(t, expected, string(accJSON))
|
require.Exactly(t, expected, string(accJSON))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDatabase_GetAccount(t *testing.T) {
|
||||||
|
db, stop := setupTestDB(t)
|
||||||
|
defer stop()
|
||||||
|
|
||||||
|
expected := Account{Name: "string", KeyUID: keyUID, ColorHash: [][]int{{4, 3}, {4, 0}, {4, 3}, {4, 0}}, ColorID: 10}
|
||||||
|
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,
|
||||||
|
ColorHash: [][]int{{4, 3}, {4, 0}, {4, 3}, {4, 0}},
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -543,14 +543,14 @@ func (m *Messenger) HandleSyncProfilePictures(state *ReceivedMessageState, messa
|
||||||
for _, img := range dbImages {
|
for _, img := range dbImages {
|
||||||
dbImageMap[img.Name] = img
|
dbImageMap[img.Name] = img
|
||||||
}
|
}
|
||||||
idImages := make([]*images.IdentityImage, len(message.Pictures))
|
idImages := make([]images.IdentityImage, len(message.Pictures))
|
||||||
i := 0
|
i := 0
|
||||||
for _, message := range message.Pictures {
|
for _, message := range message.Pictures {
|
||||||
dbImg := dbImageMap[message.Name]
|
dbImg := dbImageMap[message.Name]
|
||||||
if dbImg != nil && message.Clock <= dbImg.Clock {
|
if dbImg != nil && message.Clock <= dbImg.Clock {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
image := &images.IdentityImage{
|
image := images.IdentityImage{
|
||||||
Name: message.Name,
|
Name: message.Name,
|
||||||
Payload: message.Payload,
|
Payload: message.Payload,
|
||||||
Width: int(message.Width),
|
Width: int(message.Width),
|
||||||
|
|
|
@ -126,7 +126,7 @@ func (s *MessengerProfilePictureHandlerSuite) setupMultiAccount(m *Messenger) {
|
||||||
s.NoError(err)
|
s.NoError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MessengerProfilePictureHandlerSuite) generateAndStoreIdentityImages(m *Messenger) []*images.IdentityImage {
|
func (s *MessengerProfilePictureHandlerSuite) generateAndStoreIdentityImages(m *Messenger) []images.IdentityImage {
|
||||||
keyUID := s.generateKeyUID(&m.identity.PublicKey)
|
keyUID := s.generateKeyUID(&m.identity.PublicKey)
|
||||||
iis := images.SampleIdentityImages()
|
iis := images.SampleIdentityImages()
|
||||||
s.Require().NoError(m.multiAccounts.StoreIdentityImages(keyUID, iis, false))
|
s.Require().NoError(m.multiAccounts.StoreIdentityImages(keyUID, iis, false))
|
||||||
|
|
|
@ -39,7 +39,7 @@ type MessengerResponse struct {
|
||||||
Mailservers []mailservers.Mailserver
|
Mailservers []mailservers.Mailserver
|
||||||
Bookmarks []*browsers.Bookmark
|
Bookmarks []*browsers.Bookmark
|
||||||
Settings []*settings.SyncSettingField
|
Settings []*settings.SyncSettingField
|
||||||
IdentityImages []*images.IdentityImage
|
IdentityImages []images.IdentityImage
|
||||||
Accounts []*accounts.Account
|
Accounts []*accounts.Account
|
||||||
VerificationRequests []*verification.Request
|
VerificationRequests []*verification.Request
|
||||||
DiscordCategories []*discord.Category
|
DiscordCategories []*discord.Category
|
||||||
|
@ -90,7 +90,7 @@ func (r *MessengerResponse) MarshalJSON() ([]byte, error) {
|
||||||
CurrentStatus *UserStatus `json:"currentStatus,omitempty"`
|
CurrentStatus *UserStatus `json:"currentStatus,omitempty"`
|
||||||
StatusUpdates []UserStatus `json:"statusUpdates,omitempty"`
|
StatusUpdates []UserStatus `json:"statusUpdates,omitempty"`
|
||||||
Settings []*settings.SyncSettingField `json:"settings,omitempty"`
|
Settings []*settings.SyncSettingField `json:"settings,omitempty"`
|
||||||
IdentityImages []*images.IdentityImage `json:"identityImages,omitempty"`
|
IdentityImages []images.IdentityImage `json:"identityImages,omitempty"`
|
||||||
Accounts []*accounts.Account `json:"accounts,omitempty"`
|
Accounts []*accounts.Account `json:"accounts,omitempty"`
|
||||||
DiscordCategories []*discord.Category `json:"discordCategories,omitempty"`
|
DiscordCategories []*discord.Category `json:"discordCategories,omitempty"`
|
||||||
DiscordChannels []*discord.Channel `json:"discordChannels,omitempty"`
|
DiscordChannels []*discord.Channel `json:"discordChannels,omitempty"`
|
||||||
|
|
|
@ -110,8 +110,8 @@ func (s *MessengerSyncProfilePictureSuite) TestSyncProfilePicture() {
|
||||||
)
|
)
|
||||||
|
|
||||||
iis := images.SampleIdentityImages()
|
iis := images.SampleIdentityImages()
|
||||||
for _, img := range iis {
|
for i := range iis {
|
||||||
img.Clock = highClock
|
iis[i].Clock = highClock
|
||||||
}
|
}
|
||||||
s.Require().NoError(s.m.multiAccounts.StoreIdentityImages(keyUID, iis, true))
|
s.Require().NoError(s.m.multiAccounts.StoreIdentityImages(keyUID, iis, true))
|
||||||
|
|
||||||
|
@ -131,7 +131,7 @@ func (s *MessengerSyncProfilePictureSuite) TestSyncProfilePicture() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.New("Not received all identity images")
|
return errors.New("not received all identity images")
|
||||||
})
|
})
|
||||||
|
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
@ -143,13 +143,13 @@ func (s *MessengerSyncProfilePictureSuite) TestSyncProfilePicture() {
|
||||||
|
|
||||||
// Check that we don't update images with earlier clock values
|
// Check that we don't update images with earlier clock values
|
||||||
|
|
||||||
for _, img := range iis {
|
for i := range iis {
|
||||||
img.Clock = lowClock
|
iis[i].Clock = lowClock
|
||||||
}
|
}
|
||||||
iis2 := images.SampleIdentityImages()
|
iis2 := images.SampleIdentityImages()
|
||||||
for i, img := range iis2 {
|
for i := range iis2 {
|
||||||
img.Name = fmt.Sprintf("newimg%d", i)
|
iis2[i].Name = fmt.Sprintf("newimg%d", i)
|
||||||
img.Clock = highClock
|
iis2[i].Clock = highClock
|
||||||
}
|
}
|
||||||
iis = append(iis, iis2...)
|
iis = append(iis, iis2...)
|
||||||
s.Require().NoError(s.m.multiAccounts.StoreIdentityImages(keyUID, iis, true))
|
s.Require().NoError(s.m.multiAccounts.StoreIdentityImages(keyUID, iis, true))
|
||||||
|
@ -169,7 +169,7 @@ func (s *MessengerSyncProfilePictureSuite) TestSyncProfilePicture() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.New("Not received all identity images")
|
return errors.New("not received all identity images")
|
||||||
})
|
})
|
||||||
|
|
||||||
syncedImages, err = theirMessenger.multiAccounts.GetIdentityImages(keyUID)
|
syncedImages, err = theirMessenger.multiAccounts.GetIdentityImages(keyUID)
|
||||||
|
@ -180,5 +180,4 @@ func (s *MessengerSyncProfilePictureSuite) TestSyncProfilePicture() {
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Require().NoError(theirMessenger.Shutdown())
|
s.Require().NoError(theirMessenger.Shutdown())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,7 +104,7 @@ func (c *CreateCommunity) ToCommunityDescription() (*protobuf.CommunityDescripti
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, img := range imgs {
|
for _, img := range imgs {
|
||||||
ciis[img.Name] = adaptIdentityImageToProtobuf(img)
|
ciis[img.Name] = adaptIdentityImageToProtobuf(&img)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if c.Banner.ImagePath != "" {
|
if c.Banner.ImagePath != "" {
|
||||||
|
|
|
@ -43,7 +43,7 @@ func (api *MultiAccountsAPI) GetIdentityImage(keyUID, name string) (*images.Iden
|
||||||
// The resulting image(s) will be stored in the DB along with other user account information.
|
// The resulting image(s) will be stored in the DB along with other user account information.
|
||||||
// aX and aY represent the pixel coordinates of the upper left corner of the image's cropping area
|
// aX and aY represent the pixel coordinates of the upper left corner of the image's cropping area
|
||||||
// bX and bY represent the pixel coordinates of the lower right corner of the image's cropping area
|
// bX and bY represent the pixel coordinates of the lower right corner of the image's cropping area
|
||||||
func (api *MultiAccountsAPI) StoreIdentityImage(keyUID, filepath string, aX, aY, bX, bY int) ([]*images.IdentityImage, error) {
|
func (api *MultiAccountsAPI) StoreIdentityImage(keyUID, filepath string, aX, aY, bX, bY int) ([]images.IdentityImage, error) {
|
||||||
iis, err := images.GenerateIdentityImages(filepath, aX, aY, bX, bY)
|
iis, err := images.GenerateIdentityImages(filepath, aX, aY, bX, bY)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -57,7 +57,7 @@ func (api *MultiAccountsAPI) StoreIdentityImage(keyUID, filepath string, aX, aY,
|
||||||
return iis, err
|
return iis, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *MultiAccountsAPI) StoreIdentityImageFromURL(keyUID, url string) ([]*images.IdentityImage, error) {
|
func (api *MultiAccountsAPI) StoreIdentityImageFromURL(keyUID, url string) ([]images.IdentityImage, error) {
|
||||||
iis, err := images.GenerateIdentityImagesFromURL(url)
|
iis, err := images.GenerateIdentityImagesFromURL(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in New Issue