Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

repo_test.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "path"
  7. "testing"
  8. "code.gitea.io/gitea/modules/markup"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/Unknwon/com"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestRepo(t *testing.T) {
  14. repo := &Repository{Name: "testRepo"}
  15. repo.Owner = &User{Name: "testOwner"}
  16. repo.Units = nil
  17. assert.Nil(t, repo.ComposeMetas())
  18. externalTracker := RepoUnit{
  19. Type: UnitTypeExternalTracker,
  20. Config: &ExternalTrackerConfig{
  21. ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
  22. },
  23. }
  24. testSuccess := func(expectedStyle string) {
  25. repo.Units = []*RepoUnit{&externalTracker}
  26. repo.ExternalMetas = nil
  27. metas := repo.ComposeMetas()
  28. assert.Equal(t, expectedStyle, metas["style"])
  29. assert.Equal(t, "testRepo", metas["repo"])
  30. assert.Equal(t, "testOwner", metas["user"])
  31. assert.Equal(t, "https://someurl.com/{user}/{repo}/{issue}", metas["format"])
  32. }
  33. testSuccess(markup.IssueNameStyleNumeric)
  34. externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleAlphanumeric
  35. testSuccess(markup.IssueNameStyleAlphanumeric)
  36. externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleNumeric
  37. testSuccess(markup.IssueNameStyleNumeric)
  38. }
  39. func TestGetRepositoryCount(t *testing.T) {
  40. assert.NoError(t, PrepareTestDatabase())
  41. count, err1 := GetRepositoryCount(&User{ID: int64(10)})
  42. privateCount, err2 := GetPrivateRepositoryCount(&User{ID: int64(10)})
  43. publicCount, err3 := GetPublicRepositoryCount(&User{ID: int64(10)})
  44. assert.NoError(t, err1)
  45. assert.NoError(t, err2)
  46. assert.NoError(t, err3)
  47. assert.Equal(t, int64(3), count)
  48. assert.Equal(t, (privateCount + publicCount), count)
  49. }
  50. func TestGetPublicRepositoryCount(t *testing.T) {
  51. assert.NoError(t, PrepareTestDatabase())
  52. count, err := GetPublicRepositoryCount(&User{ID: int64(10)})
  53. assert.NoError(t, err)
  54. assert.Equal(t, int64(1), count)
  55. }
  56. func TestGetPrivateRepositoryCount(t *testing.T) {
  57. assert.NoError(t, PrepareTestDatabase())
  58. count, err := GetPrivateRepositoryCount(&User{ID: int64(10)})
  59. assert.NoError(t, err)
  60. assert.Equal(t, int64(2), count)
  61. }
  62. func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
  63. assert.NoError(t, PrepareTestDatabase())
  64. // Get sample repo and change visibility
  65. repo, err := GetRepositoryByID(9)
  66. repo.IsPrivate = true
  67. // Update it
  68. err = UpdateRepository(repo, true)
  69. assert.NoError(t, err)
  70. // Check visibility of action has become private
  71. act := Action{}
  72. _, err = x.ID(3).Get(&act)
  73. assert.NoError(t, err)
  74. assert.Equal(t, true, act.IsPrivate)
  75. }
  76. func TestGetUserFork(t *testing.T) {
  77. assert.NoError(t, PrepareTestDatabase())
  78. // User13 has repo 11 forked from repo10
  79. repo, err := GetRepositoryByID(10)
  80. assert.NoError(t, err)
  81. assert.NotNil(t, repo)
  82. repo, err = repo.GetUserFork(13)
  83. assert.NoError(t, err)
  84. assert.NotNil(t, repo)
  85. repo, err = GetRepositoryByID(9)
  86. assert.NoError(t, err)
  87. assert.NotNil(t, repo)
  88. repo, err = repo.GetUserFork(13)
  89. assert.NoError(t, err)
  90. assert.Nil(t, repo)
  91. }
  92. func TestForkRepository(t *testing.T) {
  93. assert.NoError(t, PrepareTestDatabase())
  94. // user 13 has already forked repo10
  95. user := AssertExistsAndLoadBean(t, &User{ID: 13}).(*User)
  96. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  97. fork, err := ForkRepository(user, user, repo, "test", "test")
  98. assert.Nil(t, fork)
  99. assert.Error(t, err)
  100. assert.True(t, IsErrRepoAlreadyExist(err))
  101. }
  102. func TestRepoAPIURL(t *testing.T) {
  103. assert.NoError(t, PrepareTestDatabase())
  104. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  105. assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
  106. }
  107. func TestRepoLocalCopyPath(t *testing.T) {
  108. assert.NoError(t, PrepareTestDatabase())
  109. repo, err := GetRepositoryByID(10)
  110. assert.NoError(t, err)
  111. assert.NotNil(t, repo)
  112. // test default
  113. repoID := com.ToStr(repo.ID)
  114. expected := path.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath, repoID)
  115. assert.Equal(t, expected, repo.LocalCopyPath())
  116. // test absolute setting
  117. tempPath := "/tmp/gitea/local-copy-path"
  118. expected = path.Join(tempPath, repoID)
  119. setting.Repository.Local.LocalCopyPath = tempPath
  120. assert.Equal(t, expected, repo.LocalCopyPath())
  121. }