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.

201 lines
5.3 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.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.RenderingMetas = 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. repo, err := GetRepositoryByID(3)
  44. assert.NoError(t, err)
  45. metas = repo.ComposeMetas()
  46. assert.Contains(t, metas, "org")
  47. assert.Contains(t, metas, "teams")
  48. assert.Equal(t, metas["org"], "user3")
  49. assert.Equal(t, metas["teams"], ",owners,team1,")
  50. }
  51. func TestGetRepositoryCount(t *testing.T) {
  52. assert.NoError(t, PrepareTestDatabase())
  53. count, err1 := GetRepositoryCount(&User{ID: int64(10)})
  54. privateCount, err2 := GetPrivateRepositoryCount(&User{ID: int64(10)})
  55. publicCount, err3 := GetPublicRepositoryCount(&User{ID: int64(10)})
  56. assert.NoError(t, err1)
  57. assert.NoError(t, err2)
  58. assert.NoError(t, err3)
  59. assert.Equal(t, int64(3), count)
  60. assert.Equal(t, (privateCount + publicCount), count)
  61. }
  62. func TestGetPublicRepositoryCount(t *testing.T) {
  63. assert.NoError(t, PrepareTestDatabase())
  64. count, err := GetPublicRepositoryCount(&User{ID: int64(10)})
  65. assert.NoError(t, err)
  66. assert.Equal(t, int64(1), count)
  67. }
  68. func TestGetPrivateRepositoryCount(t *testing.T) {
  69. assert.NoError(t, PrepareTestDatabase())
  70. count, err := GetPrivateRepositoryCount(&User{ID: int64(10)})
  71. assert.NoError(t, err)
  72. assert.Equal(t, int64(2), count)
  73. }
  74. func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
  75. assert.NoError(t, PrepareTestDatabase())
  76. // Get sample repo and change visibility
  77. repo, err := GetRepositoryByID(9)
  78. assert.NoError(t, err)
  79. repo.IsPrivate = true
  80. // Update it
  81. err = UpdateRepository(repo, true)
  82. assert.NoError(t, err)
  83. // Check visibility of action has become private
  84. act := Action{}
  85. _, err = x.ID(3).Get(&act)
  86. assert.NoError(t, err)
  87. assert.Equal(t, true, act.IsPrivate)
  88. }
  89. func TestGetUserFork(t *testing.T) {
  90. assert.NoError(t, PrepareTestDatabase())
  91. // User13 has repo 11 forked from repo10
  92. repo, err := GetRepositoryByID(10)
  93. assert.NoError(t, err)
  94. assert.NotNil(t, repo)
  95. repo, err = repo.GetUserFork(13)
  96. assert.NoError(t, err)
  97. assert.NotNil(t, repo)
  98. repo, err = GetRepositoryByID(9)
  99. assert.NoError(t, err)
  100. assert.NotNil(t, repo)
  101. repo, err = repo.GetUserFork(13)
  102. assert.NoError(t, err)
  103. assert.Nil(t, repo)
  104. }
  105. func TestForkRepository(t *testing.T) {
  106. assert.NoError(t, PrepareTestDatabase())
  107. // user 13 has already forked repo10
  108. user := AssertExistsAndLoadBean(t, &User{ID: 13}).(*User)
  109. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  110. fork, err := ForkRepository(user, user, repo, "test", "test")
  111. assert.Nil(t, fork)
  112. assert.Error(t, err)
  113. assert.True(t, IsErrForkAlreadyExist(err))
  114. }
  115. func TestRepoAPIURL(t *testing.T) {
  116. assert.NoError(t, PrepareTestDatabase())
  117. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  118. assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
  119. }
  120. func TestUploadAvatar(t *testing.T) {
  121. // Generate image
  122. myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
  123. var buff bytes.Buffer
  124. png.Encode(&buff, myImage)
  125. assert.NoError(t, PrepareTestDatabase())
  126. repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  127. err := repo.UploadAvatar(buff.Bytes())
  128. assert.NoError(t, err)
  129. assert.Equal(t, fmt.Sprintf("%d-%x", 10, md5.Sum(buff.Bytes())), repo.Avatar)
  130. }
  131. func TestUploadBigAvatar(t *testing.T) {
  132. // Generate BIG image
  133. myImage := image.NewRGBA(image.Rect(0, 0, 5000, 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.Error(t, err)
  140. }
  141. func TestDeleteAvatar(t *testing.T) {
  142. // Generate image
  143. myImage := image.NewRGBA(image.Rect(0, 0, 1, 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.NoError(t, err)
  150. err = repo.DeleteAvatar()
  151. assert.NoError(t, err)
  152. assert.Equal(t, "", repo.Avatar)
  153. }