2021-12-12 16:48:20 +01:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-12-12 16:48:20 +01:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"image"
|
|
|
|
"image/png"
|
|
|
|
"testing"
|
|
|
|
|
2023-02-28 23:17:51 +01:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-12 16:48:20 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2023-01-02 22:46:39 +01:00
|
|
|
"code.gitea.io/gitea/modules/avatar"
|
2021-12-12 16:48:20 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:41:27 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-12-12 16:48:20 +01: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 21:41:27 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 04:22:25 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
|
2021-12-12 16:48:20 +01:00
|
|
|
|
2023-02-28 23:17:51 +01:00
|
|
|
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, err)
|
2023-01-02 22:46:39 +01:00
|
|
|
assert.Equal(t, avatar.HashAvatar(10, buff.Bytes()), repo.Avatar)
|
2021-12-12 16:48:20 +01: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 21:41:27 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 04:22:25 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
|
2021-12-12 16:48:20 +01:00
|
|
|
|
2023-02-28 23:17:51 +01:00
|
|
|
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
2024-07-30 21:41:27 +02:00
|
|
|
require.Error(t, err)
|
2021-12-12 16:48:20 +01: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 21:41:27 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 04:22:25 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
|
2021-12-12 16:48:20 +01:00
|
|
|
|
2023-02-28 23:17:51 +01:00
|
|
|
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, err)
|
2021-12-12 16:48:20 +01:00
|
|
|
|
2023-02-28 23:17:51 +01:00
|
|
|
err = DeleteAvatar(db.DefaultContext, repo)
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, err)
|
2021-12-12 16:48:20 +01:00
|
|
|
|
|
|
|
assert.Equal(t, "", repo.Avatar)
|
|
|
|
}
|