You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.3 KiB

  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/markdown"
  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(markdown.IssueNameStyleNumeric)
  34. externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markdown.IssueNameStyleAlphanumeric
  35. testSuccess(markdown.IssueNameStyleAlphanumeric)
  36. externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markdown.IssueNameStyleNumeric
  37. testSuccess(markdown.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. // User13 has repo 11 forked from repo10
  95. repo, err := GetRepositoryByID(10)
  96. assert.NoError(t, err)
  97. assert.NotNil(t, repo)
  98. repo, err = ForkRepository(&User{ID: 13}, repo, "test", "test")
  99. assert.Nil(t, repo)
  100. assert.Error(t, err)
  101. assert.True(t, IsErrRepoAlreadyExist(err))
  102. }
  103. func TestRepoAPIURL(t *testing.T) {
  104. assert.NoError(t, PrepareTestDatabase())
  105. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  106. assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
  107. }
  108. func TestRepoLocalCopyPath(t *testing.T) {
  109. assert.NoError(t, PrepareTestDatabase())
  110. repo, err := GetRepositoryByID(10)
  111. assert.NoError(t, err)
  112. assert.NotNil(t, repo)
  113. // test default
  114. repoID := com.ToStr(repo.ID)
  115. expected := path.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath, repoID)
  116. assert.Equal(t, expected, repo.LocalCopyPath())
  117. // test absolute setting
  118. tempPath := "/tmp/gitea/local-copy-path"
  119. expected = path.Join(tempPath, repoID)
  120. setting.Repository.Local.LocalCopyPath = tempPath
  121. assert.Equal(t, expected, repo.LocalCopyPath())
  122. }