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