2020-10-26 17:45:51 +00:00
|
|
|
package images
|
|
|
|
|
2020-10-27 11:29:14 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2020-10-27 13:57:28 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-10-27 11:29:14 +00:00
|
|
|
"testing"
|
2020-10-27 13:57:28 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-27 11:29:14 +00:00
|
|
|
)
|
2020-10-26 17:45:51 +00:00
|
|
|
|
2020-10-27 14:42:42 +00:00
|
|
|
func TestIdentityImage_GetDataURI(t *testing.T) {
|
|
|
|
cs := []struct {
|
|
|
|
II IdentityImage
|
|
|
|
URI string
|
|
|
|
Error error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
IdentityImage{Payload: testJpegBytes},
|
|
|
|
"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IdentityImage{Payload: testPngBytes},
|
|
|
|
"data:image/png;base64,iVBORw0KGgoAAAANSUg=",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IdentityImage{Payload: testGifBytes},
|
|
|
|
"data:image/gif;base64,R0lGODlhAAEAAYQfAP8=",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IdentityImage{Payload: testWebpBytes},
|
|
|
|
"data:image/webp;base64,UklGRpBJAABXRUJQVlA=",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IdentityImage{Payload: testAacBytes},
|
|
|
|
"",
|
|
|
|
errors.New("image format not supported"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cs {
|
|
|
|
u, err := c.II.GetDataURI()
|
|
|
|
|
|
|
|
if c.Error == nil {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, c.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Exactly(t, c.URI, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityImage_MarshalJSON(t *testing.T) {
|
|
|
|
ii := IdentityImage{
|
2020-10-27 15:41:20 +00:00
|
|
|
Name: "thumbnail",
|
2020-10-27 14:42:42 +00:00
|
|
|
Payload: testJpegBytes,
|
|
|
|
Width: 80,
|
|
|
|
Height: 80,
|
|
|
|
FileSize: 256,
|
|
|
|
ResizeTarget: 80,
|
|
|
|
}
|
|
|
|
expected := `{"type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}`
|
|
|
|
|
|
|
|
js, err := json.Marshal(ii)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Exactly(t, expected, string(js))
|
|
|
|
}
|
|
|
|
|
2020-10-27 13:57:28 +00:00
|
|
|
func setupTestDB(t *testing.T) (Database, func()) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "images-tests-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
db, err := appdatabase.InitializeDB(tmpfile.Name(), "images-tests")
|
|
|
|
require.NoError(t, err)
|
|
|
|
return NewDatabase(db), func() {
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
require.NoError(t, os.Remove(tmpfile.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 14:33:27 +00:00
|
|
|
func seedTestDB(t *testing.T, db Database) {
|
2020-10-27 15:14:00 +00:00
|
|
|
iis := []*IdentityImage{
|
2020-10-27 13:57:28 +00:00
|
|
|
{
|
2020-10-27 15:41:20 +00:00
|
|
|
Name: smallDimName,
|
2020-10-27 13:57:28 +00:00
|
|
|
Payload: testJpegBytes,
|
|
|
|
Width: 80,
|
|
|
|
Height: 80,
|
|
|
|
FileSize: 256,
|
|
|
|
ResizeTarget: 80,
|
|
|
|
},
|
|
|
|
{
|
2020-10-27 15:41:20 +00:00
|
|
|
Name: largeDimName,
|
2020-10-27 13:57:28 +00:00
|
|
|
Payload: testPngBytes,
|
|
|
|
Width: 240,
|
|
|
|
Height: 300,
|
|
|
|
FileSize: 1024,
|
|
|
|
ResizeTarget: 240,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-27 14:33:27 +00:00
|
|
|
require.NoError(t, db.StoreIdentityImages(iis))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDatabase_GetIdentityImages(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
seedTestDB(t, db)
|
|
|
|
|
2020-10-27 15:41:20 +00:00
|
|
|
expected := `[{"name":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"file_size":1024,"resize_target":240},{"name":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}]`
|
2020-10-27 13:57:28 +00:00
|
|
|
|
|
|
|
oiis, err := db.GetIdentityImages()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
joiis, err := json.Marshal(oiis)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Exactly(t, expected, string(joiis))
|
|
|
|
}
|
|
|
|
|
2020-10-27 14:20:15 +00:00
|
|
|
func TestDatabase_GetIdentityImage(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2020-10-27 14:33:27 +00:00
|
|
|
seedTestDB(t, db)
|
2020-10-27 14:20:15 +00:00
|
|
|
|
2020-10-27 14:42:42 +00:00
|
|
|
cs := []struct {
|
|
|
|
Name string
|
2020-10-27 14:20:15 +00:00
|
|
|
Expected string
|
|
|
|
}{
|
|
|
|
{
|
2020-10-27 15:41:20 +00:00
|
|
|
smallDimName,
|
|
|
|
`{"name":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}`,
|
2020-10-27 14:20:15 +00:00
|
|
|
},
|
|
|
|
{
|
2020-10-27 15:41:20 +00:00
|
|
|
largeDimName,
|
|
|
|
`{"name":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"file_size":1024,"resize_target":240}`,
|
2020-10-27 14:20:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cs {
|
|
|
|
oii, err := db.GetIdentityImage(c.Name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
joii, err := json.Marshal(oii)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Exactly(t, c.Expected, string(joii))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 14:33:27 +00:00
|
|
|
func TestDatabase_DeleteIdentityImage(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
seedTestDB(t, db)
|
|
|
|
|
2020-10-27 15:41:20 +00:00
|
|
|
require.NoError(t, db.DeleteIdentityImage(smallDimName))
|
2020-10-27 14:33:27 +00:00
|
|
|
|
2020-10-27 15:41:20 +00:00
|
|
|
oii, err := db.GetIdentityImage(smallDimName)
|
2020-10-27 14:33:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, oii)
|
|
|
|
}
|