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.

195 lines
5.0 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/stretchr/testify/assert"
  14. )
  15. func TestMetas(t *testing.T) {
  16. assert.NoError(t, PrepareTestDatabase())
  17. repo := &Repository{Name: "testRepo"}
  18. repo.Owner = &User{Name: "testOwner"}
  19. repo.OwnerName = repo.Owner.Name
  20. repo.Units = nil
  21. metas := repo.ComposeMetas()
  22. assert.Equal(t, "testRepo", metas["repo"])
  23. assert.Equal(t, "testOwner", metas["user"])
  24. externalTracker := RepoUnit{
  25. Type: UnitTypeExternalTracker,
  26. Config: &ExternalTrackerConfig{
  27. ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
  28. },
  29. }
  30. testSuccess := func(expectedStyle string) {
  31. repo.Units = []*RepoUnit{&externalTracker}
  32. repo.RenderingMetas = nil
  33. metas := repo.ComposeMetas()
  34. assert.Equal(t, expectedStyle, metas["style"])
  35. assert.Equal(t, "testRepo", metas["repo"])
  36. assert.Equal(t, "testOwner", metas["user"])
  37. assert.Equal(t, "https://someurl.com/{user}/{repo}/{issue}", metas["format"])
  38. }
  39. testSuccess(markup.IssueNameStyleNumeric)
  40. externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleAlphanumeric
  41. testSuccess(markup.IssueNameStyleAlphanumeric)
  42. externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleNumeric
  43. testSuccess(markup.IssueNameStyleNumeric)
  44. repo, err := GetRepositoryByID(3)
  45. assert.NoError(t, err)
  46. metas = repo.ComposeMetas()
  47. assert.Contains(t, metas, "org")
  48. assert.Contains(t, metas, "teams")
  49. assert.Equal(t, metas["org"], "user3")
  50. assert.Equal(t, metas["teams"], ",owners,team1,")
  51. }
  52. func TestGetRepositoryCount(t *testing.T) {
  53. assert.NoError(t, PrepareTestDatabase())
  54. count, err1 := GetRepositoryCount(&User{ID: int64(10)})
  55. privateCount, err2 := GetPrivateRepositoryCount(&User{ID: int64(10)})
  56. publicCount, err3 := GetPublicRepositoryCount(&User{ID: int64(10)})
  57. assert.NoError(t, err1)
  58. assert.NoError(t, err2)
  59. assert.NoError(t, err3)
  60. assert.Equal(t, int64(3), count)
  61. assert.Equal(t, (privateCount + publicCount), count)
  62. }
  63. func TestGetPublicRepositoryCount(t *testing.T) {
  64. assert.NoError(t, PrepareTestDatabase())
  65. count, err := GetPublicRepositoryCount(&User{ID: int64(10)})
  66. assert.NoError(t, err)
  67. assert.Equal(t, int64(1), count)
  68. }
  69. func TestGetPrivateRepositoryCount(t *testing.T) {
  70. assert.NoError(t, PrepareTestDatabase())
  71. count, err := GetPrivateRepositoryCount(&User{ID: int64(10)})
  72. assert.NoError(t, err)
  73. assert.Equal(t, int64(2), count)
  74. }
  75. func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
  76. assert.NoError(t, PrepareTestDatabase())
  77. // Get sample repo and change visibility
  78. repo, err := GetRepositoryByID(9)
  79. assert.NoError(t, err)
  80. repo.IsPrivate = true
  81. // Update it
  82. err = UpdateRepository(repo, true)
  83. assert.NoError(t, err)
  84. // Check visibility of action has become private
  85. act := Action{}
  86. _, err = x.ID(3).Get(&act)
  87. assert.NoError(t, err)
  88. assert.Equal(t, true, act.IsPrivate)
  89. }
  90. func TestGetUserFork(t *testing.T) {
  91. assert.NoError(t, PrepareTestDatabase())
  92. // User13 has repo 11 forked from repo10
  93. repo, err := GetRepositoryByID(10)
  94. assert.NoError(t, err)
  95. assert.NotNil(t, repo)
  96. repo, err = repo.GetUserFork(13)
  97. assert.NoError(t, err)
  98. assert.NotNil(t, repo)
  99. repo, err = GetRepositoryByID(9)
  100. assert.NoError(t, err)
  101. assert.NotNil(t, repo)
  102. repo, err = repo.GetUserFork(13)
  103. assert.NoError(t, err)
  104. assert.Nil(t, repo)
  105. }
  106. func TestRepoAPIURL(t *testing.T) {
  107. assert.NoError(t, PrepareTestDatabase())
  108. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  109. assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
  110. }
  111. func TestUploadAvatar(t *testing.T) {
  112. // Generate image
  113. myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
  114. var buff bytes.Buffer
  115. png.Encode(&buff, myImage)
  116. assert.NoError(t, PrepareTestDatabase())
  117. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  118. err := repo.UploadAvatar(buff.Bytes())
  119. assert.NoError(t, err)
  120. assert.Equal(t, fmt.Sprintf("%d-%x", 10, md5.Sum(buff.Bytes())), repo.Avatar)
  121. }
  122. func TestUploadBigAvatar(t *testing.T) {
  123. // Generate BIG image
  124. myImage := image.NewRGBA(image.Rect(0, 0, 5000, 1))
  125. var buff bytes.Buffer
  126. png.Encode(&buff, myImage)
  127. assert.NoError(t, PrepareTestDatabase())
  128. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  129. err := repo.UploadAvatar(buff.Bytes())
  130. assert.Error(t, err)
  131. }
  132. func TestDeleteAvatar(t *testing.T) {
  133. // Generate image
  134. myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
  135. var buff bytes.Buffer
  136. png.Encode(&buff, myImage)
  137. assert.NoError(t, PrepareTestDatabase())
  138. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  139. err := repo.UploadAvatar(buff.Bytes())
  140. assert.NoError(t, err)
  141. err = repo.DeleteAvatar()
  142. assert.NoError(t, err)
  143. assert.Equal(t, "", repo.Avatar)
  144. }
  145. func TestDoctorUserStarNum(t *testing.T) {
  146. assert.NoError(t, PrepareTestDatabase())
  147. assert.NoError(t, DoctorUserStarNum())
  148. }