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.

137 lines
4.6 KiB

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