2020-10-26 17:45:51 +00:00
|
|
|
package images
|
|
|
|
|
2020-10-27 11:29:14 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
2020-10-27 13:57:28 +00:00
|
|
|
|
|
|
|
"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,
|
|
|
|
}
|
2020-11-26 16:49:57 +00:00
|
|
|
expected := `{"key_uid":"","type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}`
|
2020-10-27 14:42:42 +00:00
|
|
|
|
|
|
|
js, err := json.Marshal(ii)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Exactly(t, expected, string(js))
|
|
|
|
}
|