diff options
author | Rob Watson <rfwatson@users.noreply.github.com> | 2019-05-25 13:46:14 +0200 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-05-25 14:46:14 +0300 |
commit | df2557835b2235b48d1ed979abb1a1d42607e96a (patch) | |
tree | 31ff762fc5c0023f7ddad6f7673b9a210e5021ed /modules/avatar/avatar_test.go | |
parent | 5f05aa13e00eb9f8098066c81d2cd916d91e9874 (diff) | |
download | gitea-df2557835b2235b48d1ed979abb1a1d42607e96a.tar.gz gitea-df2557835b2235b48d1ed979abb1a1d42607e96a.zip |
Improve handling of non-square avatars (#7025)
* Crop avatar before resizing (#1268)
Signed-off-by: Rob Watson <rfwatson@users.noreply.github.com>
* Fix spelling error
Signed-off-by: Rob Watson <rfwatson@users.noreply.github.com>
Diffstat (limited to 'modules/avatar/avatar_test.go')
-rw-r--r-- | modules/avatar/avatar_test.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/modules/avatar/avatar_test.go b/modules/avatar/avatar_test.go index 9eff5bc2be..662d50fadd 100644 --- a/modules/avatar/avatar_test.go +++ b/modules/avatar/avatar_test.go @@ -5,8 +5,11 @@ package avatar import ( + "io/ioutil" "testing" + "code.gitea.io/gitea/modules/setting" + "github.com/stretchr/testify/assert" ) @@ -17,3 +20,49 @@ func Test_RandomImage(t *testing.T) { _, err = RandomImageSize(0, []byte("gogs@local")) assert.Error(t, err) } + +func Test_PrepareWithPNG(t *testing.T) { + setting.AvatarMaxWidth = 4096 + setting.AvatarMaxHeight = 4096 + + data, err := ioutil.ReadFile("testdata/avatar.png") + assert.NoError(t, err) + + imgPtr, err := Prepare(data) + assert.NoError(t, err) + + assert.Equal(t, 290, (*imgPtr).Bounds().Max.X) + assert.Equal(t, 290, (*imgPtr).Bounds().Max.Y) +} + +func Test_PrepareWithJPEG(t *testing.T) { + setting.AvatarMaxWidth = 4096 + setting.AvatarMaxHeight = 4096 + + data, err := ioutil.ReadFile("testdata/avatar.jpeg") + assert.NoError(t, err) + + imgPtr, err := Prepare(data) + assert.NoError(t, err) + + assert.Equal(t, 290, (*imgPtr).Bounds().Max.X) + assert.Equal(t, 290, (*imgPtr).Bounds().Max.Y) +} + +func Test_PrepareWithInvalidImage(t *testing.T) { + setting.AvatarMaxWidth = 5 + setting.AvatarMaxHeight = 5 + + _, err := Prepare([]byte{}) + assert.EqualError(t, err, "DecodeConfig: image: unknown format") +} +func Test_PrepareWithInvalidImageSize(t *testing.T) { + setting.AvatarMaxWidth = 5 + setting.AvatarMaxHeight = 5 + + data, err := ioutil.ReadFile("testdata/avatar.png") + assert.NoError(t, err) + + _, err = Prepare(data) + assert.EqualError(t, err, "Image width is too large: 10 > 5") +} |