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.

213 lines
5.8 KiB

Repository avatars (#6986) * Repository avatars - first variant of code from old work for gogs - add migration 87 - add new option in app.ini - add en-US locale string - add new class in repository.less * Add changed index.css, remove unused template name * Update en-us doc about configuration options * Add comments to new functions, add new option to docker app.ini * Add comment for lint * Remove variable, not needed * Fix formatting * Update swagger api template * Check if avatar exists * Fix avatar link/path checks * Typo * TEXT column can't have a default value * Fixes: - remove old avatar file on upload - use ID in name of avatar file - users may upload same files - add simple tests * Fix fmt check * Generate PNG instead of "static" GIF * More informative comment * Fix error message * Update avatar upload checks: - add file size check - add new option - update config docs - add new string to en-us locale * Fixes: - use FileHEader field for check file size - add new test - upload big image * Fix formatting * Update comments * Update log message * Removed wrong style - not needed * Use Sync2 to migrate * Update repos list view - bigger avatar - fix html blocks alignment * A little adjust avatar size * Use small icons for explore/repo list * Use new cool avatar preparation func by @lafriks * Missing changes for new function * Remove unused import, move imports * Missed new option definition in app.ini Add file size check in user/profile avatar upload * Use smaller field length for Avatar * Use session to update repo DB data, update DeleteAvatar - use session too * Fix err variable definition * As suggested @lafriks - return as soon as possible, code readability
5 years ago
Repository avatars (#6986) * Repository avatars - first variant of code from old work for gogs - add migration 87 - add new option in app.ini - add en-US locale string - add new class in repository.less * Add changed index.css, remove unused template name * Update en-us doc about configuration options * Add comments to new functions, add new option to docker app.ini * Add comment for lint * Remove variable, not needed * Fix formatting * Update swagger api template * Check if avatar exists * Fix avatar link/path checks * Typo * TEXT column can't have a default value * Fixes: - remove old avatar file on upload - use ID in name of avatar file - users may upload same files - add simple tests * Fix fmt check * Generate PNG instead of "static" GIF * More informative comment * Fix error message * Update avatar upload checks: - add file size check - add new option - update config docs - add new string to en-us locale * Fixes: - use FileHEader field for check file size - add new test - upload big image * Fix formatting * Update comments * Update log message * Removed wrong style - not needed * Use Sync2 to migrate * Update repos list view - bigger avatar - fix html blocks alignment * A little adjust avatar size * Use small icons for explore/repo list * Use new cool avatar preparation func by @lafriks * Missing changes for new function * Remove unused import, move imports * Missed new option definition in app.ini Add file size check in user/profile avatar upload * Use smaller field length for Avatar * Use session to update repo DB data, update DeleteAvatar - use session too * Fix err variable definition * As suggested @lafriks - return as soon as possible, code readability
5 years ago
  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/Unknwon/com"
  14. "github.com/stretchr/testify/assert"
  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. repo.IsPrivate = true
  72. // Update it
  73. err = UpdateRepository(repo, true)
  74. assert.NoError(t, err)
  75. // Check visibility of action has become private
  76. act := Action{}
  77. _, err = x.ID(3).Get(&act)
  78. assert.NoError(t, err)
  79. assert.Equal(t, true, act.IsPrivate)
  80. }
  81. func TestGetUserFork(t *testing.T) {
  82. assert.NoError(t, PrepareTestDatabase())
  83. // User13 has repo 11 forked from repo10
  84. repo, err := GetRepositoryByID(10)
  85. assert.NoError(t, err)
  86. assert.NotNil(t, repo)
  87. repo, err = repo.GetUserFork(13)
  88. assert.NoError(t, err)
  89. assert.NotNil(t, repo)
  90. repo, err = GetRepositoryByID(9)
  91. assert.NoError(t, err)
  92. assert.NotNil(t, repo)
  93. repo, err = repo.GetUserFork(13)
  94. assert.NoError(t, err)
  95. assert.Nil(t, repo)
  96. }
  97. func TestForkRepository(t *testing.T) {
  98. assert.NoError(t, PrepareTestDatabase())
  99. // user 13 has already forked repo10
  100. user := AssertExistsAndLoadBean(t, &User{ID: 13}).(*User)
  101. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  102. fork, err := ForkRepository(user, user, repo, "test", "test")
  103. assert.Nil(t, fork)
  104. assert.Error(t, err)
  105. assert.True(t, IsErrRepoAlreadyExist(err))
  106. }
  107. func TestRepoAPIURL(t *testing.T) {
  108. assert.NoError(t, PrepareTestDatabase())
  109. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  110. assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
  111. }
  112. func TestTransferOwnership(t *testing.T) {
  113. assert.NoError(t, PrepareTestDatabase())
  114. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  115. repo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  116. repo.Owner = AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  117. assert.NoError(t, TransferOwnership(doer, "user2", repo))
  118. transferredRepo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  119. assert.EqualValues(t, 2, transferredRepo.OwnerID)
  120. assert.False(t, com.IsExist(RepoPath("user3", "repo3")))
  121. assert.True(t, com.IsExist(RepoPath("user2", "repo3")))
  122. AssertExistsAndLoadBean(t, &Action{
  123. OpType: ActionTransferRepo,
  124. ActUserID: 2,
  125. RepoID: 3,
  126. Content: "user3/repo3",
  127. })
  128. CheckConsistencyFor(t, &Repository{}, &User{}, &Team{})
  129. }
  130. func TestUploadAvatar(t *testing.T) {
  131. // Generate image
  132. myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
  133. var buff bytes.Buffer
  134. png.Encode(&buff, myImage)
  135. assert.NoError(t, PrepareTestDatabase())
  136. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  137. err := repo.UploadAvatar(buff.Bytes())
  138. assert.NoError(t, err)
  139. assert.Equal(t, fmt.Sprintf("%d-%x", 10, md5.Sum(buff.Bytes())), repo.Avatar)
  140. }
  141. func TestUploadBigAvatar(t *testing.T) {
  142. // Generate BIG image
  143. myImage := image.NewRGBA(image.Rect(0, 0, 5000, 1))
  144. var buff bytes.Buffer
  145. png.Encode(&buff, myImage)
  146. assert.NoError(t, PrepareTestDatabase())
  147. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  148. err := repo.UploadAvatar(buff.Bytes())
  149. assert.Error(t, err)
  150. }
  151. func TestDeleteAvatar(t *testing.T) {
  152. // Generate image
  153. myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
  154. var buff bytes.Buffer
  155. png.Encode(&buff, myImage)
  156. assert.NoError(t, PrepareTestDatabase())
  157. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  158. err := repo.UploadAvatar(buff.Bytes())
  159. assert.NoError(t, err)
  160. err = repo.DeleteAvatar()
  161. assert.NoError(t, err)
  162. assert.Equal(t, "", repo.Avatar)
  163. }