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
|
|
|
|
2016-11-10 16:24:48 +00:00
|
|
|
"code.gitea.io/gitea/modules/markdown"
|
2017-02-06 15:18:36 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-04-22 22:28:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepo(t *testing.T) {
|
2017-02-08 06:29:07 +00:00
|
|
|
repo := &Repository{Name: "testRepo"}
|
|
|
|
repo.Owner = &User{Name: "testOwner"}
|
|
|
|
|
|
|
|
repo.Units = nil
|
|
|
|
assert.Nil(t, repo.ComposeMetas())
|
|
|
|
|
|
|
|
externalTracker := RepoUnit{
|
|
|
|
Type: UnitTypeExternalTracker,
|
|
|
|
Config: &ExternalTrackerConfig{
|
|
|
|
ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testSuccess := func(expectedStyle string) {
|
|
|
|
repo.Units = []*RepoUnit{&externalTracker}
|
|
|
|
repo.ExternalMetas = 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(markdown.IssueNameStyleNumeric)
|
|
|
|
|
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markdown.IssueNameStyleAlphanumeric
|
|
|
|
testSuccess(markdown.IssueNameStyleAlphanumeric)
|
|
|
|
|
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markdown.IssueNameStyleNumeric
|
|
|
|
testSuccess(markdown.IssueNameStyleNumeric)
|
2016-04-22 22:28:08 +00:00
|
|
|
}
|
2017-02-06 15:18:36 +00:00
|
|
|
|
|
|
|
func TestGetRepositoryCount(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
|
|
|
|
count, err1 := GetRepositoryCount(&User{ID: int64(10)})
|
|
|
|
privateCount, err2 := GetPrivateRepositoryCount(&User{ID: int64(10)})
|
|
|
|
publicCount, err3 := GetPublicRepositoryCount(&User{ID: int64(10)})
|
|
|
|
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, PrepareTestDatabase())
|
|
|
|
|
|
|
|
count, err := GetPublicRepositoryCount(&User{ID: int64(10)})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1), count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPrivateRepositoryCount(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
|
|
|
|
count, err := GetPrivateRepositoryCount(&User{ID: int64(10)})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(2), count)
|
|
|
|
}
|
2017-02-11 10:57:57 +00:00
|
|
|
|
|
|
|
func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
|
|
|
|
// Get sample repo and change visibility
|
|
|
|
repo, err := GetRepositoryByID(9)
|
|
|
|
repo.IsPrivate = true
|
|
|
|
|
|
|
|
// Update it
|
|
|
|
err = UpdateRepository(repo, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Check visibility of action has become private
|
|
|
|
act := Action{}
|
|
|
|
_, err = x.ID(3).Get(&act)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, true, act.IsPrivate)
|
|
|
|
}
|
2017-02-15 15:24:23 +00:00
|
|
|
|
|
|
|
func TestGetUserFork(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestForkRepository(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
|
|
|
|
// User13 has repo 11 forked from repo10
|
|
|
|
repo, err := GetRepositoryByID(10)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, repo)
|
|
|
|
|
|
|
|
repo, err = ForkRepository(&User{ID: 13}, repo, "test", "test")
|
|
|
|
assert.Nil(t, repo)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrRepoAlreadyExist(err))
|
|
|
|
}
|
2017-02-22 13:15:14 +00:00
|
|
|
|
|
|
|
func TestSearchRepositoryByName(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
|
|
|
|
// test search public repository on explore page
|
|
|
|
repos, count, err := SearchRepositoryByName(&SearchRepoOptions{
|
|
|
|
Keyword: "repo_12",
|
|
|
|
Page: 1,
|
|
|
|
PageSize: 10,
|
|
|
|
Searcher: nil,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotNil(t, repos)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test_repo_12", repos[0].Name)
|
|
|
|
assert.Equal(t, int64(1), count)
|
|
|
|
|
|
|
|
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
|
|
|
|
Keyword: "test_repo",
|
|
|
|
Page: 1,
|
|
|
|
PageSize: 10,
|
|
|
|
Searcher: nil,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotNil(t, repos)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(2), count)
|
|
|
|
|
|
|
|
// test search private repository on explore page
|
|
|
|
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
|
|
|
|
Keyword: "repo_13",
|
|
|
|
Page: 1,
|
|
|
|
PageSize: 10,
|
|
|
|
Searcher: &User{ID: 14},
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotNil(t, repos)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test_repo_13", repos[0].Name)
|
|
|
|
assert.Equal(t, int64(1), count)
|
|
|
|
|
|
|
|
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
|
|
|
|
Keyword: "test_repo",
|
|
|
|
Page: 1,
|
|
|
|
PageSize: 10,
|
|
|
|
Searcher: &User{ID: 14},
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotNil(t, repos)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(3), count)
|
|
|
|
}
|