You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

attachment_test.go 4.6 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/modules/storage"
  15. "code.gitea.io/gitea/modules/test"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func generateImg() bytes.Buffer {
  19. // Generate image
  20. myImage := image.NewRGBA(image.Rect(0, 0, 32, 32))
  21. var buff bytes.Buffer
  22. png.Encode(&buff, myImage)
  23. return buff
  24. }
  25. func createAttachment(t *testing.T, session *TestSession, repoURL, filename string, buff bytes.Buffer, expectedStatus int) string {
  26. body := &bytes.Buffer{}
  27. //Setup multi-part
  28. writer := multipart.NewWriter(body)
  29. part, err := writer.CreateFormFile("file", filename)
  30. assert.NoError(t, err)
  31. _, err = io.Copy(part, &buff)
  32. assert.NoError(t, err)
  33. err = writer.Close()
  34. assert.NoError(t, err)
  35. csrf := GetCSRF(t, session, repoURL)
  36. req := NewRequestWithBody(t, "POST", "/attachments", body)
  37. req.Header.Add("X-Csrf-Token", csrf)
  38. req.Header.Add("Content-Type", writer.FormDataContentType())
  39. resp := session.MakeRequest(t, req, expectedStatus)
  40. if expectedStatus != http.StatusOK {
  41. return ""
  42. }
  43. var obj map[string]string
  44. DecodeJSON(t, resp, &obj)
  45. return obj["uuid"]
  46. }
  47. func TestCreateAnonymousAttachment(t *testing.T) {
  48. defer prepareTestEnv(t)()
  49. session := emptyTestSession(t)
  50. createAttachment(t, session, "user2/repo1", "image.png", generateImg(), http.StatusFound)
  51. }
  52. func TestCreateIssueAttachment(t *testing.T) {
  53. defer prepareTestEnv(t)()
  54. const repoURL = "user2/repo1"
  55. session := loginUser(t, "user2")
  56. uuid := createAttachment(t, session, repoURL, "image.png", generateImg(), http.StatusOK)
  57. req := NewRequest(t, "GET", repoURL+"/issues/new")
  58. resp := session.MakeRequest(t, req, http.StatusOK)
  59. htmlDoc := NewHTMLParser(t, resp.Body)
  60. link, exists := htmlDoc.doc.Find("form").Attr("action")
  61. assert.True(t, exists, "The template has changed")
  62. postData := map[string]string{
  63. "_csrf": htmlDoc.GetCSRF(),
  64. "title": "New Issue With Attachment",
  65. "content": "some content",
  66. "files": uuid,
  67. }
  68. req = NewRequestWithValues(t, "POST", link, postData)
  69. resp = session.MakeRequest(t, req, http.StatusFound)
  70. test.RedirectURL(resp) // check that redirect URL exists
  71. //Validate that attachment is available
  72. req = NewRequest(t, "GET", "/attachments/"+uuid)
  73. session.MakeRequest(t, req, http.StatusOK)
  74. }
  75. func TestGetAttachment(t *testing.T) {
  76. defer prepareTestEnv(t)()
  77. adminSession := loginUser(t, "user1")
  78. user2Session := loginUser(t, "user2")
  79. user8Session := loginUser(t, "user8")
  80. emptySession := emptyTestSession(t)
  81. testCases := []struct {
  82. name string
  83. uuid string
  84. createFile bool
  85. session *TestSession
  86. want int
  87. }{
  88. {"LinkedIssueUUID", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", true, user2Session, http.StatusOK},
  89. {"LinkedCommentUUID", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", true, user2Session, http.StatusOK},
  90. {"linked_release_uuid", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a19", true, user2Session, http.StatusOK},
  91. {"NotExistingUUID", "b0eebc99-9c0b-4ef8-bb6d-6bb9bd380a18", false, user2Session, http.StatusNotFound},
  92. {"FileMissing", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a18", false, user2Session, http.StatusInternalServerError},
  93. {"NotLinked", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a20", true, user2Session, http.StatusNotFound},
  94. {"NotLinkedAccessibleByUploader", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a20", true, user8Session, http.StatusOK},
  95. {"PublicByNonLogged", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", true, emptySession, http.StatusOK},
  96. {"PrivateByNonLogged", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12", true, emptySession, http.StatusNotFound},
  97. {"PrivateAccessibleByAdmin", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12", true, adminSession, http.StatusOK},
  98. {"PrivateAccessibleByUser", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12", true, user2Session, http.StatusOK},
  99. {"RepoNotAccessibleByUser", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12", true, user8Session, http.StatusNotFound},
  100. {"OrgNotAccessibleByUser", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a21", true, user8Session, http.StatusNotFound},
  101. }
  102. for _, tc := range testCases {
  103. t.Run(tc.name, func(t *testing.T) {
  104. //Write empty file to be available for response
  105. if tc.createFile {
  106. err = storage.Attachments.Save(tc.RelativePath(), strings.NewReader("hello world"))
  107. assert.NoError(t, err)
  108. }
  109. //Actual test
  110. req := NewRequest(t, "GET", "/attachments/"+tc.uuid)
  111. tc.session.MakeRequest(t, req, tc.want)
  112. })
  113. }
  114. }