You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6 KiB

  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package avatar
  5. import (
  6. "io/ioutil"
  7. "testing"
  8. "code.gitea.io/gitea/modules/setting"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func Test_RandomImage(t *testing.T) {
  12. _, err := RandomImage([]byte("gogs@local"))
  13. assert.NoError(t, err)
  14. _, err = RandomImageSize(0, []byte("gogs@local"))
  15. assert.Error(t, err)
  16. }
  17. func Test_PrepareWithPNG(t *testing.T) {
  18. setting.AvatarMaxWidth = 4096
  19. setting.AvatarMaxHeight = 4096
  20. data, err := ioutil.ReadFile("testdata/avatar.png")
  21. assert.NoError(t, err)
  22. imgPtr, err := Prepare(data)
  23. assert.NoError(t, err)
  24. assert.Equal(t, 290, (*imgPtr).Bounds().Max.X)
  25. assert.Equal(t, 290, (*imgPtr).Bounds().Max.Y)
  26. }
  27. func Test_PrepareWithJPEG(t *testing.T) {
  28. setting.AvatarMaxWidth = 4096
  29. setting.AvatarMaxHeight = 4096
  30. data, err := ioutil.ReadFile("testdata/avatar.jpeg")
  31. assert.NoError(t, err)
  32. imgPtr, err := Prepare(data)
  33. assert.NoError(t, err)
  34. assert.Equal(t, 290, (*imgPtr).Bounds().Max.X)
  35. assert.Equal(t, 290, (*imgPtr).Bounds().Max.Y)
  36. }
  37. func Test_PrepareWithInvalidImage(t *testing.T) {
  38. setting.AvatarMaxWidth = 5
  39. setting.AvatarMaxHeight = 5
  40. _, err := Prepare([]byte{})
  41. assert.EqualError(t, err, "DecodeConfig: image: unknown format")
  42. }
  43. func Test_PrepareWithInvalidImageSize(t *testing.T) {
  44. setting.AvatarMaxWidth = 5
  45. setting.AvatarMaxHeight = 5
  46. data, err := ioutil.ReadFile("testdata/avatar.png")
  47. assert.NoError(t, err)
  48. _, err = Prepare(data)
  49. assert.EqualError(t, err, "Image width is too large: 10 > 5")
  50. }