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.

133 lines
4.6 KiB

Add a storage layer for attachments (#11387) * Add a storage layer for attachments * Fix some bug * fix test * Fix copyright head and lint * Fix bug * Add setting for minio and flags for migrate-storage * Add documents * fix lint * Add test for minio store type on attachments * fix test * fix test * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Add warning when storage migrated successfully * Fix drone * fix test * rebase * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * remove log on xorm * Fi download bug * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * Add URL function to serve attachments directly from S3/Minio * Add ability to enable/disable redirection in attachment configuration * Fix typo * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * don't change unrelated files * Fix lint * Fix build * update go.mod and go.sum * Use github.com/minio/minio-go/v6 * Remove unused function * Upgrade minio to v7 and some other improvements * fix lint * Fix go mod Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Tyler <tystuyfzand@gmail.com>
4 years ago
Add a storage layer for attachments (#11387) * Add a storage layer for attachments * Fix some bug * fix test * Fix copyright head and lint * Fix bug * Add setting for minio and flags for migrate-storage * Add documents * fix lint * Add test for minio store type on attachments * fix test * fix test * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Add warning when storage migrated successfully * Fix drone * fix test * rebase * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * remove log on xorm * Fi download bug * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * Add URL function to serve attachments directly from S3/Minio * Add ability to enable/disable redirection in attachment configuration * Fix typo * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * don't change unrelated files * Fix lint * Fix build * update go.mod and go.sum * Use github.com/minio/minio-go/v6 * Remove unused function * Upgrade minio to v7 and some other improvements * fix lint * Fix go mod Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Tyler <tystuyfzand@gmail.com>
4 years ago
Add a storage layer for attachments (#11387) * Add a storage layer for attachments * Fix some bug * fix test * Fix copyright head and lint * Fix bug * Add setting for minio and flags for migrate-storage * Add documents * fix lint * Add test for minio store type on attachments * fix test * fix test * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Add warning when storage migrated successfully * Fix drone * fix test * rebase * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * remove log on xorm * Fi download bug * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * Add URL function to serve attachments directly from S3/Minio * Add ability to enable/disable redirection in attachment configuration * Fix typo * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * don't change unrelated files * Fix lint * Fix build * update go.mod and go.sum * Use github.com/minio/minio-go/v6 * Remove unused function * Upgrade minio to v7 and some other improvements * fix lint * Fix go mod Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Tyler <tystuyfzand@gmail.com>
4 years ago
  1. // Copyright 2019 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 integrations
  5. import (
  6. "bytes"
  7. "image"
  8. "image/png"
  9. "io"
  10. "mime/multipart"
  11. "net/http"
  12. "strings"
  13. "testing"
  14. "code.gitea.io/gitea/models"
  15. "code.gitea.io/gitea/modules/storage"
  16. "code.gitea.io/gitea/modules/test"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func generateImg() bytes.Buffer {
  20. // Generate image
  21. myImage := image.NewRGBA(image.Rect(0, 0, 32, 32))
  22. var buff bytes.Buffer
  23. png.Encode(&buff, myImage)
  24. return buff
  25. }
  26. func createAttachment(t *testing.T, session *TestSession, repoURL, filename string, buff bytes.Buffer, expectedStatus int) string {
  27. body := &bytes.Buffer{}
  28. //Setup multi-part
  29. writer := multipart.NewWriter(body)
  30. part, err := writer.CreateFormFile("file", filename)
  31. assert.NoError(t, err)
  32. _, err = io.Copy(part, &buff)
  33. assert.NoError(t, err)
  34. err = writer.Close()
  35. assert.NoError(t, err)
  36. csrf := GetCSRF(t, session, repoURL)
  37. req := NewRequestWithBody(t, "POST", "/attachments", body)
  38. req.Header.Add("X-Csrf-Token", csrf)
  39. req.Header.Add("Content-Type", writer.FormDataContentType())
  40. resp := session.MakeRequest(t, req, expectedStatus)
  41. if expectedStatus != http.StatusOK {
  42. return ""
  43. }
  44. var obj map[string]string
  45. DecodeJSON(t, resp, &obj)
  46. return obj["uuid"]
  47. }
  48. func TestCreateAnonymousAttachment(t *testing.T) {
  49. defer prepareTestEnv(t)()
  50. session := emptyTestSession(t)
  51. createAttachment(t, session, "user2/repo1", "image.png", generateImg(), http.StatusFound)
  52. }
  53. func TestCreateIssueAttachment(t *testing.T) {
  54. defer prepareTestEnv(t)()
  55. const repoURL = "user2/repo1"
  56. session := loginUser(t, "user2")
  57. uuid := createAttachment(t, session, repoURL, "image.png", generateImg(), http.StatusOK)
  58. req := NewRequest(t, "GET", repoURL+"/issues/new")
  59. resp := session.MakeRequest(t, req, http.StatusOK)
  60. htmlDoc := NewHTMLParser(t, resp.Body)
  61. link, exists := htmlDoc.doc.Find("form").Attr("action")
  62. assert.True(t, exists, "The template has changed")
  63. postData := map[string]string{
  64. "_csrf": htmlDoc.GetCSRF(),
  65. "title": "New Issue With Attachment",
  66. "content": "some content",
  67. "files": uuid,
  68. }
  69. req = NewRequestWithValues(t, "POST", link, postData)
  70. resp = session.MakeRequest(t, req, http.StatusFound)
  71. test.RedirectURL(resp) // check that redirect URL exists
  72. //Validate that attachment is available
  73. req = NewRequest(t, "GET", "/attachments/"+uuid)
  74. session.MakeRequest(t, req, http.StatusOK)
  75. }
  76. func TestGetAttachment(t *testing.T) {
  77. defer prepareTestEnv(t)()
  78. adminSession := loginUser(t, "user1")
  79. user2Session := loginUser(t, "user2")
  80. user8Session := loginUser(t, "user8")
  81. emptySession := emptyTestSession(t)
  82. testCases := []struct {
  83. name string
  84. uuid string
  85. createFile bool
  86. session *TestSession
  87. want int
  88. }{
  89. {"LinkedIssueUUID", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", true, user2Session, http.StatusOK},
  90. {"LinkedCommentUUID", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", true, user2Session, http.StatusOK},
  91. {"linked_release_uuid", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a19", true, user2Session, http.StatusOK},
  92. {"NotExistingUUID", "b0eebc99-9c0b-4ef8-bb6d-6bb9bd380a18", false, user2Session, http.StatusNotFound},
  93. {"FileMissing", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a18", false, user2Session, http.StatusInternalServerError},
  94. {"NotLinked", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a20", true, user2Session, http.StatusNotFound},
  95. {"NotLinkedAccessibleByUploader", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a20", true, user8Session, http.StatusOK},
  96. {"PublicByNonLogged", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", true, emptySession, http.StatusOK},
  97. {"PrivateByNonLogged", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12", true, emptySession, http.StatusNotFound},
  98. {"PrivateAccessibleByAdmin", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12", true, adminSession, http.StatusOK},
  99. {"PrivateAccessibleByUser", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12", true, user2Session, http.StatusOK},
  100. {"RepoNotAccessibleByUser", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12", true, user8Session, http.StatusNotFound},
  101. {"OrgNotAccessibleByUser", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a21", true, user8Session, http.StatusNotFound},
  102. }
  103. for _, tc := range testCases {
  104. t.Run(tc.name, func(t *testing.T) {
  105. //Write empty file to be available for response
  106. if tc.createFile {
  107. _, err := storage.Attachments.Save(models.AttachmentRelativePath(tc.uuid), strings.NewReader("hello world"))
  108. assert.NoError(t, err)
  109. }
  110. //Actual test
  111. req := NewRequest(t, "GET", "/attachments/"+tc.uuid)
  112. tc.session.MakeRequest(t, req, tc.want)
  113. })
  114. }
  115. }