2021-12-12 18:48:20 +03:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-12-12 18:48:20 +03:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"image"
|
|
|
|
"image/png"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-01 01:17:51 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-12 18:48:20 +03:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2023-01-03 00:46:39 +03:00
|
|
|
"code.gitea.io/gitea/modules/avatar"
|
2021-12-12 18:48:20 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 22:41:10 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-12-12 18:48:20 +03: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 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 05:22:25 +03:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
|
2021-12-12 18:48:20 +03:00
|
|
|
|
2023-03-01 01:17:51 +03:00
|
|
|
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2024-12-21 22:04:13 +03:00
|
|
|
assert.Equal(t, avatar.HashAvatar(repo.ID, buff.Bytes()), repo.Avatar)
|
2021-12-12 18:48:20 +03: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 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 05:22:25 +03:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
|
2021-12-12 18:48:20 +03:00
|
|
|
|
2023-03-01 01:17:51 +03:00
|
|
|
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
2024-07-30 22:41:10 +03:00
|
|
|
require.Error(t, err)
|
2021-12-12 18:48:20 +03: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 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 05:22:25 +03:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
|
2021-12-12 18:48:20 +03:00
|
|
|
|
2023-03-01 01:17:51 +03:00
|
|
|
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2021-12-12 18:48:20 +03:00
|
|
|
|
2023-03-01 01:17:51 +03:00
|
|
|
err = DeleteAvatar(db.DefaultContext, repo)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2021-12-12 18:48:20 +03:00
|
|
|
|
|
|
|
assert.Equal(t, "", repo.Avatar)
|
|
|
|
}
|
2024-12-21 22:04:13 +03:00
|
|
|
|
|
|
|
func TestTemplateGenerateAvatar(t *testing.T) {
|
|
|
|
// Generate image
|
|
|
|
myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
|
|
var buff bytes.Buffer
|
|
|
|
png.Encode(&buff, myImage)
|
|
|
|
|
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
|
|
|
|
|
|
|
|
// Upload Avatar
|
|
|
|
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, avatar.HashAvatar(repo.ID, buff.Bytes()), repo.Avatar)
|
|
|
|
|
|
|
|
// Generate the Avatar for Another Repo
|
|
|
|
genRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 11})
|
|
|
|
err = generateAvatar(db.DefaultContext, repo, genRepo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, avatar.HashAvatar(genRepo.ID, buff.Bytes()), genRepo.Avatar)
|
|
|
|
|
|
|
|
// Make sure The 2 Hashes are not the same
|
|
|
|
assert.NotEqual(t, repo.Avatar, genRepo.Avatar)
|
|
|
|
}
|