2022-01-01 02:52:00 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-01-01 02:52:00 +00:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2024-06-12 10:22:01 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
2022-01-01 02:52:00 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-06-12 10:22:01 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-01 02:52:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckUnadoptedRepositories_Add(t *testing.T) {
|
|
|
|
start := 10
|
|
|
|
end := 20
|
2022-10-31 23:16:48 +00:00
|
|
|
unadopted := &unadoptedRepositories{
|
2022-01-01 02:52:00 +00:00
|
|
|
start: start,
|
|
|
|
end: end,
|
|
|
|
index: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
total := 30
|
|
|
|
for i := 0; i < total; i++ {
|
|
|
|
unadopted.add("something")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, total, unadopted.index)
|
2023-04-22 21:56:27 +00:00
|
|
|
assert.Len(t, unadopted.repositories, end-start)
|
2022-01-01 02:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckUnadoptedRepositories(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
//
|
|
|
|
// Non existent user
|
|
|
|
//
|
2022-10-31 23:16:48 +00:00
|
|
|
unadopted := &unadoptedRepositories{start: 0, end: 100}
|
2023-02-28 22:17:51 +00:00
|
|
|
err := checkUnadoptedRepositories(db.DefaultContext, "notauser", []string{"repo"}, unadopted)
|
2022-01-01 02:52:00 +00:00
|
|
|
assert.NoError(t, err)
|
2023-04-22 21:56:27 +00:00
|
|
|
assert.Empty(t, unadopted.repositories)
|
2022-01-01 02:52:00 +00:00
|
|
|
//
|
|
|
|
// Unadopted repository is returned
|
|
|
|
// Existing (adopted) repository is not returned
|
|
|
|
//
|
|
|
|
userName := "user2"
|
|
|
|
repoName := "repo2"
|
|
|
|
unadoptedRepoName := "unadopted"
|
2022-10-31 23:16:48 +00:00
|
|
|
unadopted = &unadoptedRepositories{start: 0, end: 100}
|
2023-02-28 22:17:51 +00:00
|
|
|
err = checkUnadoptedRepositories(db.DefaultContext, userName, []string{repoName, unadoptedRepoName}, unadopted)
|
2022-01-01 02:52:00 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{path.Join(userName, unadoptedRepoName)}, unadopted.repositories)
|
|
|
|
//
|
|
|
|
// Existing (adopted) repository is not returned
|
|
|
|
//
|
2022-10-31 23:16:48 +00:00
|
|
|
unadopted = &unadoptedRepositories{start: 0, end: 100}
|
2023-02-28 22:17:51 +00:00
|
|
|
err = checkUnadoptedRepositories(db.DefaultContext, userName, []string{repoName}, unadopted)
|
2022-01-01 02:52:00 +00:00
|
|
|
assert.NoError(t, err)
|
2023-04-22 21:56:27 +00:00
|
|
|
assert.Empty(t, unadopted.repositories)
|
2022-01-01 02:52:00 +00:00
|
|
|
assert.Equal(t, 0, unadopted.index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListUnadoptedRepositories_ListOptions(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
username := "user2"
|
|
|
|
unadoptedList := []string{path.Join(username, "unadopted1"), path.Join(username, "unadopted2")}
|
|
|
|
for _, unadopted := range unadoptedList {
|
2022-01-20 17:46:10 +00:00
|
|
|
_ = os.Mkdir(path.Join(setting.RepoRootPath, unadopted+".git"), 0o755)
|
2022-01-01 02:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opts := db.ListOptions{Page: 1, PageSize: 1}
|
2023-02-28 22:17:51 +00:00
|
|
|
repoNames, count, err := ListUnadoptedRepositories(db.DefaultContext, "", &opts)
|
2022-01-01 02:52:00 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 2, count)
|
|
|
|
assert.Equal(t, unadoptedList[0], repoNames[0])
|
|
|
|
|
|
|
|
opts = db.ListOptions{Page: 2, PageSize: 1}
|
2023-02-28 22:17:51 +00:00
|
|
|
repoNames, count, err = ListUnadoptedRepositories(db.DefaultContext, "", &opts)
|
2022-01-01 02:52:00 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 2, count)
|
|
|
|
assert.Equal(t, unadoptedList[1], repoNames[0])
|
|
|
|
}
|
2024-06-12 10:22:01 +00:00
|
|
|
|
|
|
|
func TestAdoptRepository(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
username := "user2"
|
|
|
|
|
|
|
|
unadopted := "unadopted"
|
|
|
|
assert.NoError(t, unittest.CopyDir(
|
|
|
|
"../../modules/git/tests/repos/repo1_bare",
|
|
|
|
path.Join(setting.RepoRootPath, username, unadopted+".git"),
|
|
|
|
))
|
|
|
|
|
|
|
|
opts := db.ListOptions{Page: 1, PageSize: 1}
|
|
|
|
repoNames, _, err := ListUnadoptedRepositories(db.DefaultContext, "", &opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, repoNames, path.Join(username, unadopted))
|
|
|
|
|
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
repo, err := AdoptRepository(db.DefaultContext, doer, owner, CreateRepoOptions{
|
|
|
|
Name: unadopted,
|
|
|
|
Description: "description",
|
|
|
|
IsPrivate: false,
|
|
|
|
AutoInit: true,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, git.Sha1ObjectFormat.Name(), repo.ObjectFormatName)
|
|
|
|
}
|