2021-12-09 20:27:50 -05:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-12-09 20:27:50 -05:00
|
|
|
|
2022-06-06 04:01:49 -04:00
|
|
|
package repo_test
|
2021-12-09 20:27:50 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-06 04:01:49 -04:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2022-06-15 03:02:00 -04:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-12-09 20:27:50 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2022-06-15 03:02:00 -04:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2022-05-20 10:08:52 -04:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-12-09 20:27:50 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
var (
|
2022-06-06 04:01:49 -04:00
|
|
|
countRepospts = repo_model.CountRepositoryOptions{OwnerID: 10}
|
|
|
|
countReposptsPublic = repo_model.CountRepositoryOptions{OwnerID: 10, Private: util.OptionalBoolFalse}
|
|
|
|
countReposptsPrivate = repo_model.CountRepositoryOptions{OwnerID: 10, Private: util.OptionalBoolTrue}
|
2022-05-20 10:08:52 -04:00
|
|
|
)
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
func TestGetRepositoryCount(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
ctx := db.DefaultContext
|
2022-06-06 04:01:49 -04:00
|
|
|
count, err1 := repo_model.CountRepositories(ctx, countRepospts)
|
|
|
|
privateCount, err2 := repo_model.CountRepositories(ctx, countReposptsPrivate)
|
|
|
|
publicCount, err3 := repo_model.CountRepositories(ctx, countReposptsPublic)
|
2021-12-09 20:27:50 -05:00
|
|
|
assert.NoError(t, err1)
|
|
|
|
assert.NoError(t, err2)
|
|
|
|
assert.NoError(t, err3)
|
|
|
|
assert.Equal(t, int64(3), count)
|
|
|
|
assert.Equal(t, privateCount+publicCount, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPublicRepositoryCount(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-06-06 04:01:49 -04:00
|
|
|
count, err := repo_model.CountRepositories(db.DefaultContext, countReposptsPublic)
|
2021-12-09 20:27:50 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1), count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPrivateRepositoryCount(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-06-06 04:01:49 -04:00
|
|
|
count, err := repo_model.CountRepositories(db.DefaultContext, countReposptsPrivate)
|
2021-12-09 20:27:50 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(2), count)
|
|
|
|
}
|
2021-12-12 10:48:20 -05:00
|
|
|
|
|
|
|
func TestRepoAPIURL(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
|
2021-12-12 10:48:20 -05:00
|
|
|
|
|
|
|
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
|
|
|
|
}
|
2022-06-15 03:02:00 -04:00
|
|
|
|
|
|
|
func TestWatchRepo(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
const repoID = 3
|
|
|
|
const userID = 2
|
|
|
|
|
|
|
|
assert.NoError(t, repo_model.WatchRepo(db.DefaultContext, userID, repoID, true))
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{RepoID: repoID, UserID: userID})
|
|
|
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repoID})
|
|
|
|
|
|
|
|
assert.NoError(t, repo_model.WatchRepo(db.DefaultContext, userID, repoID, false))
|
|
|
|
unittest.AssertNotExistsBean(t, &repo_model.Watch{RepoID: repoID, UserID: userID})
|
|
|
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repoID})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetas(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
repo := &repo_model.Repository{Name: "testRepo"}
|
|
|
|
repo.Owner = &user_model.User{Name: "testOwner"}
|
|
|
|
repo.OwnerName = repo.Owner.Name
|
|
|
|
|
|
|
|
repo.Units = nil
|
|
|
|
|
|
|
|
metas := repo.ComposeMetas()
|
|
|
|
assert.Equal(t, "testRepo", metas["repo"])
|
|
|
|
assert.Equal(t, "testOwner", metas["user"])
|
|
|
|
|
|
|
|
externalTracker := repo_model.RepoUnit{
|
|
|
|
Type: unit.TypeExternalTracker,
|
|
|
|
Config: &repo_model.ExternalTrackerConfig{
|
|
|
|
ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testSuccess := func(expectedStyle string) {
|
|
|
|
repo.Units = []*repo_model.RepoUnit{&externalTracker}
|
|
|
|
repo.RenderingMetas = nil
|
|
|
|
metas := repo.ComposeMetas()
|
|
|
|
assert.Equal(t, expectedStyle, metas["style"])
|
|
|
|
assert.Equal(t, "testRepo", metas["repo"])
|
|
|
|
assert.Equal(t, "testOwner", metas["user"])
|
|
|
|
assert.Equal(t, "https://someurl.com/{user}/{repo}/{issue}", metas["format"])
|
|
|
|
}
|
|
|
|
|
|
|
|
testSuccess(markup.IssueNameStyleNumeric)
|
|
|
|
|
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleAlphanumeric
|
|
|
|
testSuccess(markup.IssueNameStyleAlphanumeric)
|
|
|
|
|
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleNumeric
|
|
|
|
testSuccess(markup.IssueNameStyleNumeric)
|
|
|
|
|
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleRegexp
|
|
|
|
testSuccess(markup.IssueNameStyleRegexp)
|
|
|
|
|
2022-12-02 21:48:26 -05:00
|
|
|
repo, err := repo_model.GetRepositoryByID(db.DefaultContext, 3)
|
2022-06-15 03:02:00 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
metas = repo.ComposeMetas()
|
|
|
|
assert.Contains(t, metas, "org")
|
|
|
|
assert.Contains(t, metas, "teams")
|
|
|
|
assert.Equal(t, "user3", metas["org"])
|
|
|
|
assert.Equal(t, ",owners,team1,", metas["teams"])
|
|
|
|
}
|