2021-12-12 15:48:20 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-12-12 15:48:20 +00:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"image"
|
|
|
|
"image/png"
|
|
|
|
"testing"
|
|
|
|
|
2023-02-28 22:17:51 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-12 15:48:20 +00:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2023-01-02 21:46:39 +00:00
|
|
|
"code.gitea.io/gitea/modules/avatar"
|
2021-12-12 15:48:20 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 19:41:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-12-12 15:48:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUploadAvatar(t *testing.T) {
|
|
|
|
// Generate image
|
|
|
|
myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
|
|
var buff bytes.Buffer
|
|
|
|
png.Encode(&buff, myImage)
|
|
|
|
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 02:22:25 +00:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
|
2021-12-12 15:48:20 +00:00
|
|
|
|
2023-02-28 22:17:51 +00:00
|
|
|
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2023-01-02 21:46:39 +00:00
|
|
|
assert.Equal(t, avatar.HashAvatar(10, buff.Bytes()), repo.Avatar)
|
2021-12-12 15:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUploadBigAvatar(t *testing.T) {
|
|
|
|
// Generate BIG image
|
|
|
|
myImage := image.NewRGBA(image.Rect(0, 0, 5000, 1))
|
|
|
|
var buff bytes.Buffer
|
|
|
|
png.Encode(&buff, myImage)
|
|
|
|
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 02:22:25 +00:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
|
2021-12-12 15:48:20 +00:00
|
|
|
|
2023-02-28 22:17:51 +00:00
|
|
|
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
2024-07-30 19:41:10 +00:00
|
|
|
require.Error(t, err)
|
2021-12-12 15:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteAvatar(t *testing.T) {
|
|
|
|
// Generate image
|
|
|
|
myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
|
|
var buff bytes.Buffer
|
|
|
|
png.Encode(&buff, myImage)
|
|
|
|
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 02:22:25 +00:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
|
2021-12-12 15:48:20 +00:00
|
|
|
|
2023-02-28 22:17:51 +00:00
|
|
|
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2021-12-12 15:48:20 +00:00
|
|
|
|
2023-02-28 22:17:51 +00:00
|
|
|
err = DeleteAvatar(db.DefaultContext, repo)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2021-12-12 15:48:20 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "", repo.Avatar)
|
|
|
|
}
|