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.

181 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. metas := repo.ComposeMetas()
  18. assert.Equal(t, "testRepo", metas["repo"])
  19. assert.Equal(t, "testOwner", metas["user"])
  20. externalTracker := RepoUnit{
  21. Type: UnitTypeExternalTracker,
  22. Config: &ExternalTrackerConfig{
  23. ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
  24. },
  25. }
  26. testSuccess := func(expectedStyle string) {
  27. repo.Units = []*RepoUnit{&externalTracker}
  28. repo.ExternalMetas = nil
  29. metas := repo.ComposeMetas()
  30. assert.Equal(t, expectedStyle, metas["style"])
  31. assert.Equal(t, "testRepo", metas["repo"])
  32. assert.Equal(t, "testOwner", metas["user"])
  33. assert.Equal(t, "https://someurl.com/{user}/{repo}/{issue}", metas["format"])
  34. }
  35. testSuccess(markup.IssueNameStyleNumeric)
  36. externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleAlphanumeric
  37. testSuccess(markup.IssueNameStyleAlphanumeric)
  38. externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleNumeric
  39. testSuccess(markup.IssueNameStyleNumeric)
  40. }
  41. func TestGetRepositoryCount(t *testing.T) {
  42. assert.NoError(t, PrepareTestDatabase())
  43. count, err1 := GetRepositoryCount(&User{ID: int64(10)})
  44. privateCount, err2 := GetPrivateRepositoryCount(&User{ID: int64(10)})
  45. publicCount, err3 := GetPublicRepositoryCount(&User{ID: int64(10)})
  46. assert.NoError(t, err1)
  47. assert.NoError(t, err2)
  48. assert.NoError(t, err3)
  49. assert.Equal(t, int64(3), count)
  50. assert.Equal(t, (privateCount + publicCount), count)
  51. }
  52. func TestGetPublicRepositoryCount(t *testing.T) {
  53. assert.NoError(t, PrepareTestDatabase())
  54. count, err := GetPublicRepositoryCount(&User{ID: int64(10)})
  55. assert.NoError(t, err)
  56. assert.Equal(t, int64(1), count)
  57. }
  58. func TestGetPrivateRepositoryCount(t *testing.T) {
  59. assert.NoError(t, PrepareTestDatabase())
  60. count, err := GetPrivateRepositoryCount(&User{ID: int64(10)})
  61. assert.NoError(t, err)
  62. assert.Equal(t, int64(2), count)
  63. }
  64. func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
  65. assert.NoError(t, PrepareTestDatabase())
  66. // Get sample repo and change visibility
  67. repo, err := GetRepositoryByID(9)
  68. repo.IsPrivate = true
  69. // Update it
  70. err = UpdateRepository(repo, true)
  71. assert.NoError(t, err)
  72. // Check visibility of action has become private
  73. act := Action{}
  74. _, err = x.ID(3).Get(&act)
  75. assert.NoError(t, err)
  76. assert.Equal(t, true, act.IsPrivate)
  77. }
  78. func TestGetUserFork(t *testing.T) {
  79. assert.NoError(t, PrepareTestDatabase())
  80. // User13 has repo 11 forked from repo10
  81. repo, err := GetRepositoryByID(10)
  82. assert.NoError(t, err)
  83. assert.NotNil(t, repo)
  84. repo, err = repo.GetUserFork(13)
  85. assert.NoError(t, err)
  86. assert.NotNil(t, repo)
  87. repo, err = GetRepositoryByID(9)
  88. assert.NoError(t, err)
  89. assert.NotNil(t, repo)
  90. repo, err = repo.GetUserFork(13)
  91. assert.NoError(t, err)
  92. assert.Nil(t, repo)
  93. }
  94. func TestForkRepository(t *testing.T) {
  95. assert.NoError(t, PrepareTestDatabase())
  96. // user 13 has already forked repo10
  97. user := AssertExistsAndLoadBean(t, &User{ID: 13}).(*User)
  98. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  99. fork, err := ForkRepository(user, user, repo, "test", "test")
  100. assert.Nil(t, fork)
  101. assert.Error(t, err)
  102. assert.True(t, IsErrRepoAlreadyExist(err))
  103. }
  104. func TestRepoAPIURL(t *testing.T) {
  105. assert.NoError(t, PrepareTestDatabase())
  106. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  107. assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
  108. }
  109. func TestRepoLocalCopyPath(t *testing.T) {
  110. assert.NoError(t, PrepareTestDatabase())
  111. repo, err := GetRepositoryByID(10)
  112. assert.NoError(t, err)
  113. assert.NotNil(t, repo)
  114. // test default
  115. repoID := com.ToStr(repo.ID)
  116. expected := path.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath, repoID)
  117. assert.Equal(t, expected, repo.LocalCopyPath())
  118. // test absolute setting
  119. tempPath := "/tmp/gitea/local-copy-path"
  120. expected = path.Join(tempPath, repoID)
  121. setting.Repository.Local.LocalCopyPath = tempPath
  122. assert.Equal(t, expected, repo.LocalCopyPath())
  123. }
  124. func TestTransferOwnership(t *testing.T) {
  125. assert.NoError(t, PrepareTestDatabase())
  126. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  127. repo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  128. repo.Owner = AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  129. assert.NoError(t, TransferOwnership(doer, "user2", repo))
  130. transferredRepo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  131. assert.EqualValues(t, 2, transferredRepo.OwnerID)
  132. assert.False(t, com.IsExist(RepoPath("user3", "repo3")))
  133. assert.True(t, com.IsExist(RepoPath("user2", "repo3")))
  134. AssertExistsAndLoadBean(t, &Action{
  135. OpType: ActionTransferRepo,
  136. ActUserID: 2,
  137. RepoID: 3,
  138. Content: "user3/repo3",
  139. })
  140. CheckConsistencyFor(t, &Repository{}, &User{}, &Team{})
  141. }