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.

178 lines
5.1 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/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. }
  122. func TestTransferOwnership(t *testing.T) {
  123. assert.NoError(t, PrepareTestDatabase())
  124. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  125. repo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  126. repo.Owner = AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  127. assert.NoError(t, TransferOwnership(doer, "user2", repo))
  128. transferredRepo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  129. assert.EqualValues(t, 2, transferredRepo.OwnerID)
  130. assert.False(t, com.IsExist(RepoPath("user3", "repo3")))
  131. assert.True(t, com.IsExist(RepoPath("user2", "repo3")))
  132. AssertExistsAndLoadBean(t, &Action{
  133. OpType: ActionTransferRepo,
  134. ActUserID: 2,
  135. RepoID: 3,
  136. Content: "user3/repo3",
  137. })
  138. CheckConsistencyFor(t, &Repository{}, &User{}, &Team{})
  139. }