github.com/status-im/status-go@v1.1.0/images/identity_test.go (about)

     1  package images
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestIdentityImage_GetDataURI(t *testing.T) {
    12  	cs := []struct {
    13  		II    IdentityImage
    14  		URI   string
    15  		Error error
    16  	}{
    17  		{
    18  			IdentityImage{Payload: testJpegBytes},
    19  			"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=",
    20  			nil,
    21  		},
    22  		{
    23  			IdentityImage{Payload: testPngBytes},
    24  			"data:image/png;base64,iVBORw0KGgoAAAANSUg=",
    25  			nil,
    26  		},
    27  		{
    28  			IdentityImage{Payload: testGifBytes},
    29  			"data:image/gif;base64,R0lGODlhAAEAAYQfAP8=",
    30  			nil,
    31  		},
    32  		{
    33  			IdentityImage{Payload: testWebpBytes},
    34  			"data:image/webp;base64,UklGRpBJAABXRUJQVlA=",
    35  			nil,
    36  		},
    37  		{
    38  			IdentityImage{Payload: testAacBytes},
    39  			"",
    40  			errors.New("image format not supported"),
    41  		},
    42  	}
    43  
    44  	for _, c := range cs {
    45  		u, err := c.II.GetDataURI()
    46  
    47  		if c.Error == nil {
    48  			require.NoError(t, err)
    49  		} else {
    50  			require.EqualError(t, err, c.Error.Error())
    51  		}
    52  
    53  		require.Exactly(t, c.URI, u)
    54  	}
    55  }
    56  
    57  func TestIdentityImage_MarshalJSON(t *testing.T) {
    58  	ii := IdentityImage{
    59  		Name:         "thumbnail",
    60  		Payload:      testJpegBytes,
    61  		Width:        80,
    62  		Height:       80,
    63  		FileSize:     256,
    64  		ResizeTarget: 80,
    65  	}
    66  	expected := `{"keyUid":"","type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"fileSize":256,"resizeTarget":80,"clock":0}`
    67  
    68  	js, err := json.Marshal(ii)
    69  	require.NoError(t, err)
    70  	require.Exactly(t, expected, string(js))
    71  }