Skip to content

Commit 58bc786

Browse files
committed
chore: improve createAttachment
1 parent 60b4259 commit 58bc786

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

tests/integration/attachment_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func generateImg() bytes.Buffer {
2929
return buff
3030
}
3131

32-
func createAttachment(t *testing.T, session *TestSession, repoURL, filename string, buff bytes.Buffer, expectedStatus int) string {
32+
func createAttachment(t *testing.T, session *TestSession, csrf string, repoURL, filename string, buff bytes.Buffer, expectedStatus int) string {
3333
body := &bytes.Buffer{}
3434

3535
// Setup multi-part
@@ -41,16 +41,6 @@ func createAttachment(t *testing.T, session *TestSession, repoURL, filename stri
4141
err = writer.Close()
4242
assert.NoError(t, err)
4343

44-
var csrf string
45-
// FIXME: It's quite hacky to determine if it's a logged in user or not and use the right URL to get the CSRF token
46-
if expectedStatus == http.StatusSeeOther {
47-
// the session is not logged in
48-
csrf = GetCSRF(t, session, "/user/login")
49-
} else {
50-
// the session is logged in
51-
csrf = GetCSRF(t, session, repoURL)
52-
}
53-
5444
req := NewRequestWithBody(t, "POST", repoURL+"/issues/attachments", body)
5545
req.Header.Add("X-Csrf-Token", csrf)
5646
req.Header.Add("Content-Type", writer.FormDataContentType())
@@ -67,14 +57,14 @@ func createAttachment(t *testing.T, session *TestSession, repoURL, filename stri
6757
func TestCreateAnonymousAttachment(t *testing.T) {
6858
defer tests.PrepareTestEnv(t)()
6959
session := emptyTestSession(t)
70-
createAttachment(t, session, "user2/repo1", "image.png", generateImg(), http.StatusSeeOther)
60+
createAttachment(t, session, GetCSRF(t, session, "/user/login"), "user2/repo1", "image.png", generateImg(), http.StatusSeeOther)
7161
}
7262

7363
func TestCreateIssueAttachment(t *testing.T) {
7464
defer tests.PrepareTestEnv(t)()
7565
const repoURL = "user2/repo1"
7666
session := loginUser(t, "user2")
77-
uuid := createAttachment(t, session, repoURL, "image.png", generateImg(), http.StatusOK)
67+
uuid := createAttachment(t, session, GetCSRF(t, session, repoURL), repoURL, "image.png", generateImg(), http.StatusOK)
7868

7969
req := NewRequest(t, "GET", repoURL+"/issues/new")
8070
resp := session.MakeRequest(t, req, http.StatusOK)

0 commit comments

Comments
 (0)