本站源代码
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.

215 lines
5.8KB

  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. "bytes"
  7. "crypto/md5"
  8. "fmt"
  9. "image"
  10. "image/png"
  11. "testing"
  12. "code.gitea.io/gitea/modules/markup"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/unknwon/com"
  15. )
  16. func TestRepo(t *testing.T) {
  17. repo := &Repository{Name: "testRepo"}
  18. repo.Owner = &User{Name: "testOwner"}
  19. repo.Units = nil
  20. metas := repo.ComposeMetas()
  21. assert.Equal(t, "testRepo", metas["repo"])
  22. assert.Equal(t, "testOwner", metas["user"])
  23. externalTracker := RepoUnit{
  24. Type: UnitTypeExternalTracker,
  25. Config: &ExternalTrackerConfig{
  26. ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
  27. },
  28. }
  29. testSuccess := func(expectedStyle string) {
  30. repo.Units = []*RepoUnit{&externalTracker}
  31. repo.ExternalMetas = nil
  32. metas := repo.ComposeMetas()
  33. assert.Equal(t, expectedStyle, metas["style"])
  34. assert.Equal(t, "testRepo", metas["repo"])
  35. assert.Equal(t, "testOwner", metas["user"])
  36. assert.Equal(t, "https://someurl.com/{user}/{repo}/{issue}", metas["format"])
  37. }
  38. testSuccess(markup.IssueNameStyleNumeric)
  39. externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleAlphanumeric
  40. testSuccess(markup.IssueNameStyleAlphanumeric)
  41. externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleNumeric
  42. testSuccess(markup.IssueNameStyleNumeric)
  43. }
  44. func TestGetRepositoryCount(t *testing.T) {
  45. assert.NoError(t, PrepareTestDatabase())
  46. count, err1 := GetRepositoryCount(&User{ID: int64(10)})
  47. privateCount, err2 := GetPrivateRepositoryCount(&User{ID: int64(10)})
  48. publicCount, err3 := GetPublicRepositoryCount(&User{ID: int64(10)})
  49. assert.NoError(t, err1)
  50. assert.NoError(t, err2)
  51. assert.NoError(t, err3)
  52. assert.Equal(t, int64(3), count)
  53. assert.Equal(t, (privateCount + publicCount), count)
  54. }
  55. func TestGetPublicRepositoryCount(t *testing.T) {
  56. assert.NoError(t, PrepareTestDatabase())
  57. count, err := GetPublicRepositoryCount(&User{ID: int64(10)})
  58. assert.NoError(t, err)
  59. assert.Equal(t, int64(1), count)
  60. }
  61. func TestGetPrivateRepositoryCount(t *testing.T) {
  62. assert.NoError(t, PrepareTestDatabase())
  63. count, err := GetPrivateRepositoryCount(&User{ID: int64(10)})
  64. assert.NoError(t, err)
  65. assert.Equal(t, int64(2), count)
  66. }
  67. func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
  68. assert.NoError(t, PrepareTestDatabase())
  69. // Get sample repo and change visibility
  70. repo, err := GetRepositoryByID(9)
  71. assert.NoError(t, err)
  72. repo.IsPrivate = true
  73. // Update it
  74. err = UpdateRepository(repo, true)
  75. assert.NoError(t, err)
  76. // Check visibility of action has become private
  77. act := Action{}
  78. _, err = x.ID(3).Get(&act)
  79. assert.NoError(t, err)
  80. assert.Equal(t, true, act.IsPrivate)
  81. }
  82. func TestGetUserFork(t *testing.T) {
  83. assert.NoError(t, PrepareTestDatabase())
  84. // User13 has repo 11 forked from repo10
  85. repo, err := GetRepositoryByID(10)
  86. assert.NoError(t, err)
  87. assert.NotNil(t, repo)
  88. repo, err = repo.GetUserFork(13)
  89. assert.NoError(t, err)
  90. assert.NotNil(t, repo)
  91. repo, err = GetRepositoryByID(9)
  92. assert.NoError(t, err)
  93. assert.NotNil(t, repo)
  94. repo, err = repo.GetUserFork(13)
  95. assert.NoError(t, err)
  96. assert.Nil(t, repo)
  97. }
  98. func TestForkRepository(t *testing.T) {
  99. assert.NoError(t, PrepareTestDatabase())
  100. // user 13 has already forked repo10
  101. user := AssertExistsAndLoadBean(t, &User{ID: 13}).(*User)
  102. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  103. fork, err := ForkRepository(user, user, repo, "test", "test")
  104. assert.Nil(t, fork)
  105. assert.Error(t, err)
  106. assert.True(t, IsErrForkAlreadyExist(err))
  107. }
  108. func TestRepoAPIURL(t *testing.T) {
  109. assert.NoError(t, PrepareTestDatabase())
  110. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  111. assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
  112. }
  113. func TestTransferOwnership(t *testing.T) {
  114. assert.NoError(t, PrepareTestDatabase())
  115. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  116. repo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  117. repo.Owner = AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  118. assert.NoError(t, TransferOwnership(doer, "user2", repo))
  119. transferredRepo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  120. assert.EqualValues(t, 2, transferredRepo.OwnerID)
  121. assert.False(t, com.IsExist(RepoPath("user3", "repo3")))
  122. assert.True(t, com.IsExist(RepoPath("user2", "repo3")))
  123. AssertExistsAndLoadBean(t, &Action{
  124. OpType: ActionTransferRepo,
  125. ActUserID: 2,
  126. RepoID: 3,
  127. Content: "user3/repo3",
  128. })
  129. CheckConsistencyFor(t, &Repository{}, &User{}, &Team{})
  130. }
  131. func TestUploadAvatar(t *testing.T) {
  132. // Generate image
  133. myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
  134. var buff bytes.Buffer
  135. png.Encode(&buff, myImage)
  136. assert.NoError(t, PrepareTestDatabase())
  137. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  138. err := repo.UploadAvatar(buff.Bytes())
  139. assert.NoError(t, err)
  140. assert.Equal(t, fmt.Sprintf("%d-%x", 10, md5.Sum(buff.Bytes())), repo.Avatar)
  141. }
  142. func TestUploadBigAvatar(t *testing.T) {
  143. // Generate BIG image
  144. myImage := image.NewRGBA(image.Rect(0, 0, 5000, 1))
  145. var buff bytes.Buffer
  146. png.Encode(&buff, myImage)
  147. assert.NoError(t, PrepareTestDatabase())
  148. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  149. err := repo.UploadAvatar(buff.Bytes())
  150. assert.Error(t, err)
  151. }
  152. func TestDeleteAvatar(t *testing.T) {
  153. // Generate image
  154. myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
  155. var buff bytes.Buffer
  156. png.Encode(&buff, myImage)
  157. assert.NoError(t, PrepareTestDatabase())
  158. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  159. err := repo.UploadAvatar(buff.Bytes())
  160. assert.NoError(t, err)
  161. err = repo.DeleteAvatar()
  162. assert.NoError(t, err)
  163. assert.Equal(t, "", repo.Avatar)
  164. }
上海开阖软件有限公司 沪ICP备12045867号-1