|
| 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 | + |
| 5 | +package integrations |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "image" |
| 10 | + "image/png" |
| 11 | + "io" |
| 12 | + "mime/multipart" |
| 13 | + "net/http" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "code.gitea.io/gitea/modules/test" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 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 | + |
| 28 | +func createAttachment(t *testing.T, session *TestSession, repoURL, filename string, buff bytes.Buffer, expectedStatus int) string { |
| 29 | + body := &bytes.Buffer{} |
| 30 | + |
| 31 | + //Setup multi-part |
| 32 | + writer := multipart.NewWriter(body) |
| 33 | + part, err := writer.CreateFormFile("file", filename) |
| 34 | + assert.NoError(t, err) |
| 35 | + _, err = io.Copy(part, &buff) |
| 36 | + assert.NoError(t, err) |
| 37 | + err = writer.Close() |
| 38 | + assert.NoError(t, err) |
| 39 | + |
| 40 | + csrf := GetCSRF(t, session, repoURL) |
| 41 | + |
| 42 | + req := NewRequestWithBody(t, "POST", "/attachments", body) |
| 43 | + req.Header.Add("X-Csrf-Token", csrf) |
| 44 | + req.Header.Add("Content-Type", writer.FormDataContentType()) |
| 45 | + resp := session.MakeRequest(t, req, expectedStatus) |
| 46 | + |
| 47 | + if expectedStatus != http.StatusOK { |
| 48 | + return "" |
| 49 | + } |
| 50 | + var obj map[string]string |
| 51 | + DecodeJSON(t, resp, &obj) |
| 52 | + return obj["uuid"] |
| 53 | +} |
| 54 | + |
| 55 | +func TestCreateAnonymousAttachment(t *testing.T) { |
| 56 | + prepareTestEnv(t) |
| 57 | + session := emptyTestSession(t) |
| 58 | + createAttachment(t, session, "user2/repo1", "image.png", generateImg(), http.StatusFound) |
| 59 | +} |
| 60 | + |
| 61 | +func TestCreateIssueAttachement(t *testing.T) { |
| 62 | + prepareTestEnv(t) |
| 63 | + const repoURL = "user2/repo1" |
| 64 | + session := loginUser(t, "user2") |
| 65 | + uuid := createAttachment(t, session, repoURL, "image.png", generateImg(), http.StatusOK) |
| 66 | + |
| 67 | + req := NewRequest(t, "GET", repoURL+"/issues/new") |
| 68 | + resp := session.MakeRequest(t, req, http.StatusOK) |
| 69 | + htmlDoc := NewHTMLParser(t, resp.Body) |
| 70 | + |
| 71 | + link, exists := htmlDoc.doc.Find("form").Attr("action") |
| 72 | + assert.True(t, exists, "The template has changed") |
| 73 | + |
| 74 | + postData := map[string]string{ |
| 75 | + "_csrf": htmlDoc.GetCSRF(), |
| 76 | + "title": "New Issue With Attachement", |
| 77 | + "content": "some content", |
| 78 | + "files[0]": uuid, |
| 79 | + } |
| 80 | + |
| 81 | + req = NewRequestWithValues(t, "POST", link, postData) |
| 82 | + resp = session.MakeRequest(t, req, http.StatusFound) |
| 83 | + test.RedirectURL(resp) // check that redirect URL exists |
| 84 | + |
| 85 | + //Validate that attachement is available |
| 86 | + req = NewRequest(t, "GET", "/attachments/"+uuid) |
| 87 | + session.MakeRequest(t, req, http.StatusOK) |
| 88 | +} |
0 commit comments