Added multiaccounts.Database tests for new funcs
This commit is contained in:
parent
83db7fc795
commit
344458d74a
11 changed files with 96 additions and 40 deletions
|
@ -63,3 +63,10 @@ func (i IdentityImage) MarshalJSON() ([]byte, error) {
|
|||
|
||||
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"
|
||||
)
|
||||
|
||||
func GenerateImageVariants(cImg image.Image) ([]*IdentityImage, error) {
|
||||
var iis []*IdentityImage
|
||||
func GenerateImageVariants(cImg image.Image) ([]IdentityImage, error) {
|
||||
var iis []IdentityImage
|
||||
var err error
|
||||
|
||||
for _, s := range ResizeDimensions {
|
||||
|
@ -18,7 +18,7 @@ func GenerateImageVariants(cImg image.Image) ([]*IdentityImage, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
ii := &IdentityImage{
|
||||
ii := IdentityImage{
|
||||
Name: ResizeDimensionToName[s],
|
||||
Payload: bb.Bytes(),
|
||||
Width: rImg.Bounds().Dx(),
|
||||
|
@ -33,7 +33,7 @@ func GenerateImageVariants(cImg image.Image) ([]*IdentityImage, error) {
|
|||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -51,7 +51,7 @@ func GenerateIdentityImages(filepath string, aX, aY, bX, bY int) ([]*IdentityIma
|
|||
return GenerateImageVariants(cImg)
|
||||
}
|
||||
|
||||
func GenerateIdentityImagesFromURL(url string) ([]*IdentityImage, error) {
|
||||
func GenerateIdentityImagesFromURL(url string) ([]IdentityImage, error) {
|
||||
img, err := DecodeFromURL(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -9,8 +9,8 @@ var (
|
|||
testAacBytes = []byte{0xff, 0xf1, 0x50, 0x80, 0x1c, 0x3f, 0xfc, 0xda, 0x00, 0x4c, 0x61, 0x76, 0x63, 0x35}
|
||||
)
|
||||
|
||||
func SampleIdentityImages() []*IdentityImage {
|
||||
return []*IdentityImage{
|
||||
func SampleIdentityImages() []IdentityImage {
|
||||
return []IdentityImage{
|
||||
{
|
||||
Name: SmallDimName,
|
||||
Payload: testJpegBytes,
|
||||
|
|
|
@ -138,8 +138,7 @@ func (db *Database) GetAccounts() (rst []Account, err error) {
|
|||
return rst, nil
|
||||
}
|
||||
|
||||
func (db *Database) GetAccount(keyUID string) (acc *Account, err error) {
|
||||
// TODO add test for this
|
||||
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.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 {
|
||||
return nil, err
|
||||
|
@ -149,6 +148,8 @@ func (db *Database) GetAccount(keyUID string) (acc *Account, err error) {
|
|||
err = valueOr(err, errClose)
|
||||
}()
|
||||
|
||||
acc := new(Account)
|
||||
|
||||
for rows.Next() {
|
||||
accLoginTimestamp := sql.NullInt64{}
|
||||
accIdenticon := sql.NullString{}
|
||||
|
@ -200,12 +201,8 @@ func (db *Database) GetAccount(keyUID string) (acc *Account, err error) {
|
|||
ii.ResizeTarget = int(iiResizeTarget.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 {
|
||||
ii = nil
|
||||
}
|
||||
|
||||
// Don't process nil identity images
|
||||
if ii != nil {
|
||||
// Don't process empty identity images
|
||||
if !ii.IsEmpty() {
|
||||
acc.Images = append(acc.Images, *ii)
|
||||
}
|
||||
}
|
||||
|
@ -228,11 +225,7 @@ func (db *Database) SaveAccount(account Account) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
var iis []*images.IdentityImage
|
||||
for _, ii := range account.Images {
|
||||
iis = append(iis, &ii)
|
||||
}
|
||||
return db.StoreIdentityImages(account.KeyUID, iis, false)
|
||||
return db.StoreIdentityImages(account.KeyUID, account.Images, false)
|
||||
}
|
||||
|
||||
func (db *Database) UpdateAccount(account Account) error {
|
||||
|
@ -295,7 +288,7 @@ func (db *Database) GetIdentityImage(keyUID, it string) (*images.IdentityImage,
|
|||
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.
|
||||
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
|
||||
if err != nil {
|
||||
|
@ -312,7 +305,7 @@ func (db *Database) StoreIdentityImages(keyUID string, iis []*images.IdentityIma
|
|||
}()
|
||||
|
||||
for _, ii := range iis {
|
||||
if ii == nil {
|
||||
if ii.IsEmpty() {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
|
@ -176,3 +176,60 @@ func TestDatabase_GetAccountsWithIdentityImages(t *testing.T) {
|
|||
|
||||
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 {
|
||||
dbImageMap[img.Name] = img
|
||||
}
|
||||
idImages := make([]*images.IdentityImage, len(message.Pictures))
|
||||
idImages := make([]images.IdentityImage, len(message.Pictures))
|
||||
i := 0
|
||||
for _, message := range message.Pictures {
|
||||
dbImg := dbImageMap[message.Name]
|
||||
if dbImg != nil && message.Clock <= dbImg.Clock {
|
||||
continue
|
||||
}
|
||||
image := &images.IdentityImage{
|
||||
image := images.IdentityImage{
|
||||
Name: message.Name,
|
||||
Payload: message.Payload,
|
||||
Width: int(message.Width),
|
||||
|
|
|
@ -126,7 +126,7 @@ func (s *MessengerProfilePictureHandlerSuite) setupMultiAccount(m *Messenger) {
|
|||
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)
|
||||
iis := images.SampleIdentityImages()
|
||||
s.Require().NoError(m.multiAccounts.StoreIdentityImages(keyUID, iis, false))
|
||||
|
|
|
@ -39,7 +39,7 @@ type MessengerResponse struct {
|
|||
Mailservers []mailservers.Mailserver
|
||||
Bookmarks []*browsers.Bookmark
|
||||
Settings []*settings.SyncSettingField
|
||||
IdentityImages []*images.IdentityImage
|
||||
IdentityImages []images.IdentityImage
|
||||
Accounts []*accounts.Account
|
||||
VerificationRequests []*verification.Request
|
||||
DiscordCategories []*discord.Category
|
||||
|
@ -90,7 +90,7 @@ func (r *MessengerResponse) MarshalJSON() ([]byte, error) {
|
|||
CurrentStatus *UserStatus `json:"currentStatus,omitempty"`
|
||||
StatusUpdates []UserStatus `json:"statusUpdates,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"`
|
||||
DiscordCategories []*discord.Category `json:"discordCategories,omitempty"`
|
||||
DiscordChannels []*discord.Channel `json:"discordChannels,omitempty"`
|
||||
|
|
|
@ -110,8 +110,8 @@ func (s *MessengerSyncProfilePictureSuite) TestSyncProfilePicture() {
|
|||
)
|
||||
|
||||
iis := images.SampleIdentityImages()
|
||||
for _, img := range iis {
|
||||
img.Clock = highClock
|
||||
for i := range iis {
|
||||
iis[i].Clock = highClock
|
||||
}
|
||||
s.Require().NoError(s.m.multiAccounts.StoreIdentityImages(keyUID, iis, true))
|
||||
|
||||
|
@ -131,7 +131,7 @@ func (s *MessengerSyncProfilePictureSuite) TestSyncProfilePicture() {
|
|||
return nil
|
||||
}
|
||||
|
||||
return errors.New("Not received all identity images")
|
||||
return errors.New("not received all identity images")
|
||||
})
|
||||
|
||||
s.Require().NoError(err)
|
||||
|
@ -143,13 +143,13 @@ func (s *MessengerSyncProfilePictureSuite) TestSyncProfilePicture() {
|
|||
|
||||
// Check that we don't update images with earlier clock values
|
||||
|
||||
for _, img := range iis {
|
||||
img.Clock = lowClock
|
||||
for i := range iis {
|
||||
iis[i].Clock = lowClock
|
||||
}
|
||||
iis2 := images.SampleIdentityImages()
|
||||
for i, img := range iis2 {
|
||||
img.Name = fmt.Sprintf("newimg%d", i)
|
||||
img.Clock = highClock
|
||||
for i := range iis2 {
|
||||
iis2[i].Name = fmt.Sprintf("newimg%d", i)
|
||||
iis2[i].Clock = highClock
|
||||
}
|
||||
iis = append(iis, iis2...)
|
||||
s.Require().NoError(s.m.multiAccounts.StoreIdentityImages(keyUID, iis, true))
|
||||
|
@ -169,7 +169,7 @@ func (s *MessengerSyncProfilePictureSuite) TestSyncProfilePicture() {
|
|||
return nil
|
||||
}
|
||||
|
||||
return errors.New("Not received all identity images")
|
||||
return errors.New("not received all identity images")
|
||||
})
|
||||
|
||||
syncedImages, err = theirMessenger.multiAccounts.GetIdentityImages(keyUID)
|
||||
|
@ -180,5 +180,4 @@ func (s *MessengerSyncProfilePictureSuite) TestSyncProfilePicture() {
|
|||
}
|
||||
|
||||
s.Require().NoError(theirMessenger.Shutdown())
|
||||
|
||||
}
|
||||
|
|
|
@ -104,7 +104,7 @@ func (c *CreateCommunity) ToCommunityDescription() (*protobuf.CommunityDescripti
|
|||
return nil, err
|
||||
}
|
||||
for _, img := range imgs {
|
||||
ciis[img.Name] = adaptIdentityImageToProtobuf(img)
|
||||
ciis[img.Name] = adaptIdentityImageToProtobuf(&img)
|
||||
}
|
||||
}
|
||||
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.
|
||||
// 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
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -57,7 +57,7 @@ func (api *MultiAccountsAPI) StoreIdentityImage(keyUID, filepath string, aX, aY,
|
|||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
Loading…
Reference in a new issue