2017-02-06 15:18:36 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
2016-04-22 22:28:08 +00:00
|
|
|
|
|
|
|
import (
|
2016-07-09 05:22:28 +00:00
|
|
|
"testing"
|
2016-04-22 22:28:08 +00:00
|
|
|
|
2021-09-19 11:49:59 +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-09 19:57:58 +00:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
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"
|
2017-09-16 17:17:57 +00:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2017-02-06 15:18:36 +00:00
|
|
|
|
2017-06-06 13:53:16 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-04-22 22:28:08 +00:00
|
|
|
)
|
|
|
|
|
2022-01-17 18:31:58 +00:00
|
|
|
func TestCheckRepoStats(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
assert.NoError(t, CheckRepoStats(db.DefaultContext))
|
|
|
|
}
|
|
|
|
|
2021-12-12 15:48:20 +00:00
|
|
|
func TestWatchRepo(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
const repoID = 3
|
|
|
|
const userID = 2
|
|
|
|
|
2022-05-20 14:08:52 +00:00
|
|
|
assert.NoError(t, repo_model.WatchRepo(db.DefaultContext, userID, repoID, true))
|
2021-12-12 15:48:20 +00:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{RepoID: repoID, UserID: userID})
|
|
|
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repoID})
|
|
|
|
|
2022-05-20 14:08:52 +00:00
|
|
|
assert.NoError(t, repo_model.WatchRepo(db.DefaultContext, userID, repoID, false))
|
2021-12-12 15:48:20 +00:00
|
|
|
unittest.AssertNotExistsBean(t, &repo_model.Watch{RepoID: repoID, UserID: userID})
|
|
|
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repoID})
|
|
|
|
}
|
|
|
|
|
2019-11-24 16:34:44 +00:00
|
|
|
func TestMetas(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-24 16:34:44 +00:00
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
repo := &repo_model.Repository{Name: "testRepo"}
|
2021-11-24 09:49:20 +00:00
|
|
|
repo.Owner = &user_model.User{Name: "testOwner"}
|
2020-01-12 09:36:21 +00:00
|
|
|
repo.OwnerName = repo.Owner.Name
|
2017-02-08 06:29:07 +00:00
|
|
|
|
|
|
|
repo.Units = nil
|
2019-04-12 05:53:34 +00:00
|
|
|
|
|
|
|
metas := repo.ComposeMetas()
|
|
|
|
assert.Equal(t, "testRepo", metas["repo"])
|
|
|
|
assert.Equal(t, "testOwner", metas["user"])
|
2017-02-08 06:29:07 +00:00
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
externalTracker := repo_model.RepoUnit{
|
2021-11-09 19:57:58 +00:00
|
|
|
Type: unit.TypeExternalTracker,
|
2021-12-10 01:27:50 +00:00
|
|
|
Config: &repo_model.ExternalTrackerConfig{
|
2017-02-08 06:29:07 +00:00
|
|
|
ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testSuccess := func(expectedStyle string) {
|
2021-12-10 01:27:50 +00:00
|
|
|
repo.Units = []*repo_model.RepoUnit{&externalTracker}
|
2019-11-24 16:34:44 +00:00
|
|
|
repo.RenderingMetas = nil
|
2017-02-08 06:29:07 +00:00
|
|
|
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"])
|
|
|
|
}
|
|
|
|
|
2017-09-16 17:17:57 +00:00
|
|
|
testSuccess(markup.IssueNameStyleNumeric)
|
2017-02-08 06:29:07 +00:00
|
|
|
|
2017-09-16 17:17:57 +00:00
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleAlphanumeric
|
|
|
|
testSuccess(markup.IssueNameStyleAlphanumeric)
|
2017-02-08 06:29:07 +00:00
|
|
|
|
2017-09-16 17:17:57 +00:00
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleNumeric
|
|
|
|
testSuccess(markup.IssueNameStyleNumeric)
|
2019-11-24 16:34:44 +00:00
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
repo, err := repo_model.GetRepositoryByID(3)
|
2019-11-24 16:34:44 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
metas = repo.ComposeMetas()
|
|
|
|
assert.Contains(t, metas, "org")
|
|
|
|
assert.Contains(t, metas, "teams")
|
2021-06-07 05:27:09 +00:00
|
|
|
assert.Equal(t, "user3", metas["org"])
|
|
|
|
assert.Equal(t, ",owners,team1,", metas["teams"])
|
2016-04-22 22:28:08 +00:00
|
|
|
}
|
2017-02-06 15:18:36 +00:00
|
|
|
|
2017-02-11 10:57:57 +00:00
|
|
|
func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-11 10:57:57 +00:00
|
|
|
|
|
|
|
// Get sample repo and change visibility
|
2021-12-10 01:27:50 +00:00
|
|
|
repo, err := repo_model.GetRepositoryByID(9)
|
2019-07-23 19:28:43 +00:00
|
|
|
assert.NoError(t, err)
|
2017-02-11 10:57:57 +00:00
|
|
|
repo.IsPrivate = true
|
|
|
|
|
|
|
|
// Update it
|
|
|
|
err = UpdateRepository(repo, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Check visibility of action has become private
|
|
|
|
act := Action{}
|
2021-09-23 15:45:36 +00:00
|
|
|
_, err = db.GetEngine(db.DefaultContext).ID(3).Get(&act)
|
2017-02-11 10:57:57 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2021-06-07 05:27:09 +00:00
|
|
|
assert.True(t, act.IsPrivate)
|
2017-02-11 10:57:57 +00:00
|
|
|
}
|
2017-02-15 15:24:23 +00:00
|
|
|
|
2020-07-07 19:16:34 +00:00
|
|
|
func TestDoctorUserStarNum(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-07-07 19:16:34 +00:00
|
|
|
|
|
|
|
assert.NoError(t, DoctorUserStarNum())
|
|
|
|
}
|
2020-10-12 19:55:13 +00:00
|
|
|
|
|
|
|
func TestRepoGetReviewers(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-10-12 19:55:13 +00:00
|
|
|
|
|
|
|
// test public repo
|
2021-12-10 01:27:50 +00:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
2020-10-12 19:55:13 +00:00
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
reviewers, err := GetReviewers(repo1, 2, 2)
|
2020-10-12 19:55:13 +00:00
|
|
|
assert.NoError(t, err)
|
2021-06-07 05:27:09 +00:00
|
|
|
assert.Len(t, reviewers, 4)
|
2020-10-12 19:55:13 +00:00
|
|
|
|
2022-03-08 21:48:47 +00:00
|
|
|
// should include doer if doer is not PR poster.
|
|
|
|
reviewers, err = GetReviewers(repo1, 11, 2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, reviewers, 4)
|
|
|
|
|
|
|
|
// should not include PR poster, if PR poster would be otherwise eligible
|
|
|
|
reviewers, err = GetReviewers(repo1, 11, 4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, reviewers, 3)
|
|
|
|
|
|
|
|
// test private user repo
|
2021-12-10 01:27:50 +00:00
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository)
|
2022-03-08 21:48:47 +00:00
|
|
|
|
|
|
|
reviewers, err = GetReviewers(repo2, 2, 4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, reviewers, 1)
|
|
|
|
assert.EqualValues(t, reviewers[0].ID, 2)
|
|
|
|
|
|
|
|
// test private org repo
|
|
|
|
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
|
|
|
|
|
|
|
reviewers, err = GetReviewers(repo3, 2, 1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, reviewers, 2)
|
|
|
|
|
|
|
|
reviewers, err = GetReviewers(repo3, 2, 2)
|
2020-10-12 19:55:13 +00:00
|
|
|
assert.NoError(t, err)
|
2022-03-08 21:48:47 +00:00
|
|
|
assert.Len(t, reviewers, 1)
|
2020-10-12 19:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoGetReviewerTeams(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-10-12 19:55:13 +00:00
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository)
|
|
|
|
teams, err := GetReviewerTeams(repo2)
|
2020-10-12 19:55:13 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, teams)
|
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
|
|
|
teams, err = GetReviewerTeams(repo3)
|
2020-10-12 19:55:13 +00:00
|
|
|
assert.NoError(t, err)
|
2021-06-07 05:27:09 +00:00
|
|
|
assert.Len(t, teams, 2)
|
2020-10-12 19:55:13 +00:00
|
|
|
}
|
2021-11-19 13:39:57 +00:00
|
|
|
|
|
|
|
func TestLinkedRepository(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
attachID int64
|
2021-12-10 01:27:50 +00:00
|
|
|
expectedRepo *repo_model.Repository
|
2021-11-19 13:39:57 +00:00
|
|
|
expectedUnitType unit.Type
|
|
|
|
}{
|
2021-12-10 01:27:50 +00:00
|
|
|
{"LinkedIssue", 1, &repo_model.Repository{ID: 1}, unit.TypeIssues},
|
|
|
|
{"LinkedComment", 3, &repo_model.Repository{ID: 1}, unit.TypePullRequests},
|
|
|
|
{"LinkedRelease", 9, &repo_model.Repository{ID: 1}, unit.TypeReleases},
|
2021-11-19 13:39:57 +00:00
|
|
|
{"Notlinked", 10, nil, -1},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-05-20 14:08:52 +00:00
|
|
|
attach, err := repo_model.GetAttachmentByID(db.DefaultContext, tc.attachID)
|
2021-11-19 13:39:57 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
repo, unitType, err := LinkedRepository(attach)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if tc.expectedRepo != nil {
|
|
|
|
assert.Equal(t, tc.expectedRepo.ID, repo.ID)
|
|
|
|
}
|
|
|
|
assert.Equal(t, tc.expectedUnitType, unitType)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-03-04 18:29:12 +00:00
|
|
|
|
|
|
|
func TestRepoAssignees(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository)
|
|
|
|
users, err := GetRepoAssignees(repo2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, users, 1)
|
|
|
|
assert.Equal(t, users[0].ID, int64(2))
|
|
|
|
|
|
|
|
repo21 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 21}).(*repo_model.Repository)
|
|
|
|
users, err = GetRepoAssignees(repo21)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, users, 3)
|
|
|
|
assert.Equal(t, users[0].ID, int64(15))
|
|
|
|
assert.Equal(t, users[1].ID, int64(18))
|
|
|
|
assert.Equal(t, users[2].ID, int64(16))
|
|
|
|
}
|