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"
)
func TestUploadAvatar ( t * testing . T ) {
// Generate image
myImage := image . NewRGBA ( image . Rect ( 0 , 0 , 1 , 1 ) )
var buff bytes . Buffer
png . Encode ( & buff , myImage )
assert . 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 ( ) )
2021-12-12 18:48:20 +03:00
assert . NoError ( t , err )
2023-01-03 00:46:39 +03:00
assert . Equal ( t , avatar . HashAvatar ( 10 , 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 )
assert . 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 ( ) )
2021-12-12 18:48:20 +03:00
assert . Error ( t , err )
}
func TestDeleteAvatar ( t * testing . T ) {
// Generate image
myImage := image . NewRGBA ( image . Rect ( 0 , 0 , 1 , 1 ) )
var buff bytes . Buffer
png . Encode ( & buff , myImage )
assert . 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 ( ) )
2021-12-12 18:48:20 +03:00
assert . NoError ( t , err )
2023-03-01 01:17:51 +03:00
err = DeleteAvatar ( db . DefaultContext , repo )
2021-12-12 18:48:20 +03:00
assert . NoError ( t , err )
assert . Equal ( t , "" , repo . Avatar )
}