2017-02-06 08:18:36 -07: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 16:28:08 -06:00
|
|
|
|
|
|
|
import (
|
2019-05-29 20:22:26 -06:00
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/png"
|
2016-07-08 23:22:28 -06:00
|
|
|
"testing"
|
2016-04-22 16:28:08 -06:00
|
|
|
|
2021-09-19 05:49:59 -06: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-09 12:57:58 -07:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
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"
|
2017-09-16 11:17:57 -06:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2017-02-06 08:18:36 -07:00
|
|
|
|
2017-06-06 07:53:16 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-04-22 16:28:08 -06:00
|
|
|
)
|
|
|
|
|
2019-11-24 09:34:44 -07:00
|
|
|
func TestMetas(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-24 09:34:44 -07:00
|
|
|
|
2017-02-07 23:29:07 -07:00
|
|
|
repo := &Repository{Name: "testRepo"}
|
2021-11-24 02:49:20 -07:00
|
|
|
repo.Owner = &user_model.User{Name: "testOwner"}
|
2020-01-12 02:36:21 -07:00
|
|
|
repo.OwnerName = repo.Owner.Name
|
2017-02-07 23:29:07 -07:00
|
|
|
|
|
|
|
repo.Units = nil
|
2019-04-11 23:53:34 -06:00
|
|
|
|
|
|
|
metas := repo.ComposeMetas()
|
|
|
|
assert.Equal(t, "testRepo", metas["repo"])
|
|
|
|
assert.Equal(t, "testOwner", metas["user"])
|
2017-02-07 23:29:07 -07:00
|
|
|
|
|
|
|
externalTracker := RepoUnit{
|
2021-11-09 12:57:58 -07:00
|
|
|
Type: unit.TypeExternalTracker,
|
2017-02-07 23:29:07 -07:00
|
|
|
Config: &ExternalTrackerConfig{
|
|
|
|
ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testSuccess := func(expectedStyle string) {
|
|
|
|
repo.Units = []*RepoUnit{&externalTracker}
|
2019-11-24 09:34:44 -07:00
|
|
|
repo.RenderingMetas = nil
|
2017-02-07 23:29:07 -07: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 11:17:57 -06:00
|
|
|
testSuccess(markup.IssueNameStyleNumeric)
|
2017-02-07 23:29:07 -07:00
|
|
|
|
2017-09-16 11:17:57 -06:00
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleAlphanumeric
|
|
|
|
testSuccess(markup.IssueNameStyleAlphanumeric)
|
2017-02-07 23:29:07 -07:00
|
|
|
|
2017-09-16 11:17:57 -06:00
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleNumeric
|
|
|
|
testSuccess(markup.IssueNameStyleNumeric)
|
2019-11-24 09:34:44 -07:00
|
|
|
|
|
|
|
repo, err := GetRepositoryByID(3)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
metas = repo.ComposeMetas()
|
|
|
|
assert.Contains(t, metas, "org")
|
|
|
|
assert.Contains(t, metas, "teams")
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.Equal(t, "user3", metas["org"])
|
|
|
|
assert.Equal(t, ",owners,team1,", metas["teams"])
|
2016-04-22 16:28:08 -06:00
|
|
|
}
|
2017-02-06 08:18:36 -07:00
|
|
|
|
|
|
|
func TestGetRepositoryCount(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-06 08:18:36 -07:00
|
|
|
|
2021-11-19 04:41:40 -07:00
|
|
|
count, err1 := GetRepositoryCount(db.DefaultContext, 10)
|
2021-11-24 02:49:20 -07:00
|
|
|
privateCount, err2 := GetPrivateRepositoryCount(&user_model.User{ID: int64(10)})
|
|
|
|
publicCount, err3 := GetPublicRepositoryCount(&user_model.User{ID: int64(10)})
|
2017-02-06 08:18:36 -07:00
|
|
|
assert.NoError(t, err1)
|
|
|
|
assert.NoError(t, err2)
|
|
|
|
assert.NoError(t, err3)
|
|
|
|
assert.Equal(t, int64(3), count)
|
2021-04-09 01:40:34 -06:00
|
|
|
assert.Equal(t, privateCount+publicCount, count)
|
2017-02-06 08:18:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPublicRepositoryCount(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-06 08:18:36 -07:00
|
|
|
|
2021-11-24 02:49:20 -07:00
|
|
|
count, err := GetPublicRepositoryCount(&user_model.User{ID: int64(10)})
|
2017-02-06 08:18:36 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1), count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPrivateRepositoryCount(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-06 08:18:36 -07:00
|
|
|
|
2021-11-24 02:49:20 -07:00
|
|
|
count, err := GetPrivateRepositoryCount(&user_model.User{ID: int64(10)})
|
2017-02-06 08:18:36 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(2), count)
|
|
|
|
}
|
2017-02-11 03:57:57 -07:00
|
|
|
|
|
|
|
func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-11 03:57:57 -07:00
|
|
|
|
|
|
|
// Get sample repo and change visibility
|
|
|
|
repo, err := GetRepositoryByID(9)
|
2019-07-23 13:28:43 -06:00
|
|
|
assert.NoError(t, err)
|
2017-02-11 03:57:57 -07: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 09:45:36 -06:00
|
|
|
_, err = db.GetEngine(db.DefaultContext).ID(3).Get(&act)
|
2017-02-11 03:57:57 -07:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.True(t, act.IsPrivate)
|
2017-02-11 03:57:57 -07:00
|
|
|
}
|
2017-02-15 08:24:23 -07:00
|
|
|
|
|
|
|
func TestGetUserFork(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-15 08:24:23 -07:00
|
|
|
|
|
|
|
// User13 has repo 11 forked from repo10
|
|
|
|
repo, err := GetRepositoryByID(10)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, repo)
|
|
|
|
repo, err = repo.GetUserFork(13)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, repo)
|
|
|
|
|
|
|
|
repo, err = GetRepositoryByID(9)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, repo)
|
|
|
|
repo, err = repo.GetUserFork(13)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, repo)
|
|
|
|
}
|
|
|
|
|
2017-03-03 07:35:42 -07:00
|
|
|
func TestRepoAPIURL(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 01:53:21 -07:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
|
2017-03-03 07:35:42 -07:00
|
|
|
|
|
|
|
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
|
|
|
|
}
|
2017-06-06 03:09:54 -06:00
|
|
|
|
2019-05-29 20:22:26 -06:00
|
|
|
func TestUploadAvatar(t *testing.T) {
|
|
|
|
// Generate image
|
|
|
|
myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
|
|
var buff bytes.Buffer
|
|
|
|
png.Encode(&buff, myImage)
|
|
|
|
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 01:53:21 -07:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
|
2019-05-29 20:22:26 -06:00
|
|
|
|
|
|
|
err := repo.UploadAvatar(buff.Bytes())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, fmt.Sprintf("%d-%x", 10, md5.Sum(buff.Bytes())), repo.Avatar)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUploadBigAvatar(t *testing.T) {
|
|
|
|
// Generate BIG image
|
|
|
|
myImage := image.NewRGBA(image.Rect(0, 0, 5000, 1))
|
|
|
|
var buff bytes.Buffer
|
|
|
|
png.Encode(&buff, myImage)
|
|
|
|
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 01:53:21 -07:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
|
2019-05-29 20:22:26 -06:00
|
|
|
|
|
|
|
err := repo.UploadAvatar(buff.Bytes())
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteAvatar(t *testing.T) {
|
|
|
|
// Generate image
|
|
|
|
myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
|
|
var buff bytes.Buffer
|
|
|
|
png.Encode(&buff, myImage)
|
|
|
|
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 01:53:21 -07:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
|
2019-05-29 20:22:26 -06:00
|
|
|
|
|
|
|
err := repo.UploadAvatar(buff.Bytes())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = repo.DeleteAvatar()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "", repo.Avatar)
|
|
|
|
}
|
2020-07-07 13:16:34 -06:00
|
|
|
|
|
|
|
func TestDoctorUserStarNum(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-07-07 13:16:34 -06:00
|
|
|
|
|
|
|
assert.NoError(t, DoctorUserStarNum())
|
|
|
|
}
|
2020-10-12 13:55:13 -06:00
|
|
|
|
|
|
|
func TestRepoGetReviewers(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-10-12 13:55:13 -06:00
|
|
|
|
|
|
|
// test public repo
|
2021-11-16 01:53:21 -07:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
2020-10-12 13:55:13 -06:00
|
|
|
|
|
|
|
reviewers, err := repo1.GetReviewers(2, 2)
|
|
|
|
assert.NoError(t, err)
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.Len(t, reviewers, 4)
|
2020-10-12 13:55:13 -06:00
|
|
|
|
|
|
|
// test private repo
|
2021-11-16 01:53:21 -07:00
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
|
2020-10-12 13:55:13 -06:00
|
|
|
reviewers, err = repo2.GetReviewers(2, 2)
|
|
|
|
assert.NoError(t, err)
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.Empty(t, reviewers)
|
2020-10-12 13:55:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoGetReviewerTeams(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-10-12 13:55:13 -06:00
|
|
|
|
2021-11-16 01:53:21 -07:00
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
|
2020-10-12 13:55:13 -06:00
|
|
|
teams, err := repo2.GetReviewerTeams()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, teams)
|
|
|
|
|
2021-11-16 01:53:21 -07:00
|
|
|
repo3 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
|
2020-10-12 13:55:13 -06:00
|
|
|
teams, err = repo3.GetReviewerTeams()
|
|
|
|
assert.NoError(t, err)
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.Len(t, teams, 2)
|
2020-10-12 13:55:13 -06:00
|
|
|
}
|
2021-11-19 06:39:57 -07:00
|
|
|
|
|
|
|
func TestLinkedRepository(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
attachID int64
|
|
|
|
expectedRepo *Repository
|
|
|
|
expectedUnitType unit.Type
|
|
|
|
}{
|
|
|
|
{"LinkedIssue", 1, &Repository{ID: 1}, unit.TypeIssues},
|
|
|
|
{"LinkedComment", 3, &Repository{ID: 1}, unit.TypePullRequests},
|
|
|
|
{"LinkedRelease", 9, &Repository{ID: 1}, unit.TypeReleases},
|
|
|
|
{"Notlinked", 10, nil, -1},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
attach, err := repo_model.GetAttachmentByID(tc.attachID)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|