2021-09-08 08:19:30 -07:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-09-08 08:19:30 -07:00
|
|
|
|
|
|
|
package attachment
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2022-05-20 07:08:52 -07:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-19 06:39:57 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 07:36:47 -07:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-09-08 08:19:30 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2022-04-14 06:58:21 -07:00
|
|
|
unittest.MainTest(m, &unittest.TestOptions{
|
|
|
|
GiteaRootPath: filepath.Join("..", ".."),
|
|
|
|
})
|
2021-09-08 08:19:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUploadAttachment(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-09-08 08:19:30 -07:00
|
|
|
|
2022-08-15 19:22:25 -07:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
2021-09-08 08:19:30 -07:00
|
|
|
|
|
|
|
fPath := "./attachment_test.go"
|
|
|
|
f, err := os.Open(fPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
2021-11-19 06:39:57 -07:00
|
|
|
attach, err := NewAttachment(&repo_model.Attachment{
|
2021-09-08 08:19:30 -07:00
|
|
|
RepoID: 1,
|
|
|
|
UploaderID: user.ID,
|
|
|
|
Name: filepath.Base(fPath),
|
2023-03-12 00:48:07 -07:00
|
|
|
}, f, -1)
|
2021-09-08 08:19:30 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-05-20 07:08:52 -07:00
|
|
|
attachment, err := repo_model.GetAttachmentByUUID(db.DefaultContext, attach.UUID)
|
2021-09-08 08:19:30 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, user.ID, attachment.UploaderID)
|
|
|
|
assert.Equal(t, int64(0), attachment.DownloadCount)
|
|
|
|
}
|