Skip to content

Commit 4dc3993

Browse files
sapkguillep2k
authored andcommitted
tests: add attachement tests integration (#9309)
* tests: add attachements integration * Update integrations/attachement_test.go Co-Authored-By: guillep2k <[email protected]>
1 parent d3a9c4c commit 4dc3993

File tree

4 files changed

+120
-4
lines changed

4 files changed

+120
-4
lines changed

integrations/attachement_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

integrations/release_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestCreateRelease(t *testing.T) {
7878
session := loginUser(t, "user2")
7979
createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, false)
8080

81-
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.stable"), 1)
81+
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.stable"), 2)
8282
}
8383

8484
func TestCreateReleasePreRelease(t *testing.T) {
@@ -87,7 +87,7 @@ func TestCreateReleasePreRelease(t *testing.T) {
8787
session := loginUser(t, "user2")
8888
createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", true, false)
8989

90-
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.prerelease"), 1)
90+
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.prerelease"), 2)
9191
}
9292

9393
func TestCreateReleaseDraft(t *testing.T) {
@@ -96,7 +96,7 @@ func TestCreateReleaseDraft(t *testing.T) {
9696
session := loginUser(t, "user2")
9797
createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, true)
9898

99-
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.draft"), 1)
99+
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.draft"), 2)
100100
}
101101

102102
func TestCreateReleasePaging(t *testing.T) {

models/fixtures/attachment.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,18 @@
6969
name: attach1
7070
download_count: 0
7171
created_unix: 946684800
72+
73+
-
74+
id: 9
75+
uuid: a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a19
76+
release_id: 1
77+
name: attach1
78+
download_count: 0
79+
created_unix: 946684800
80+
81+
-
82+
id: 10
83+
uuid: a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a20
84+
name: attach1
85+
download_count: 0
86+
created_unix: 946684800

models/fixtures/release.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
[] # empty
1+
-
2+
id: 1
3+
repo_id: 1
4+
publisher_id: 2
5+
tag_name: "v1.1"
6+
lower_tag_name: "v1.1"
7+
target: "master"
8+
title: "testing-release"
9+
sha1: "65f1bf27bc3bf70f64657658635e66094edbcb4d"
10+
num_commits: 10
11+
is_draft: false
12+
is_prerelease: false
13+
is_tag: false
14+
created_unix: 946684800

0 commit comments

Comments
 (0)