Skip to content

Commit fb68890

Browse files
committed
Add integration test for file editing
1 parent 9ddc35e commit fb68890

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

integrations/editor_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"net/http"
1010
"net/url"
11+
"path"
1112
"testing"
1213

1314
"github.com/stretchr/testify/assert"
@@ -104,3 +105,47 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
104105
// Check body for error message
105106
assert.Contains(t, string(resp.Body), "Can not commit to protected branch 'master'.")
106107
}
108+
109+
func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath string) {
110+
111+
newContent := "Hello, World (Edited)\n"
112+
113+
// Get to the 'edit this file' page
114+
req, err := http.NewRequest("GET", path.Join(user, repo, "_edit", branch, filePath), nil)
115+
assert.NoError(t, err)
116+
resp := session.MakeRequest(t, req)
117+
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
118+
119+
htmlDoc, err := NewHtmlParser(resp.Body)
120+
assert.NoError(t, err)
121+
lastCommit := htmlDoc.GetInputValueByName("last_commit")
122+
assert.NotEmpty(t, lastCommit)
123+
124+
// Submit the edits
125+
req, err = http.NewRequest("POST", path.Join(user, repo, "_edit", branch, filePath),
126+
bytes.NewBufferString(url.Values{
127+
"_csrf": []string{htmlDoc.GetInputValueByName("_csrf")},
128+
"last_commit": []string{lastCommit},
129+
"tree_path": []string{filePath},
130+
"content": []string{newContent},
131+
"commit_choice": []string{"direct"},
132+
}.Encode()),
133+
)
134+
assert.NoError(t, err)
135+
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
136+
resp = session.MakeRequest(t, req)
137+
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
138+
139+
// Verify the change
140+
req, err = http.NewRequest("GET", path.Join(user, repo, "raw", branch, filePath), nil)
141+
assert.NoError(t, err)
142+
resp = session.MakeRequest(t, req)
143+
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
144+
assert.EqualValues(t, newContent, string(resp.Body))
145+
}
146+
147+
func TestEditFile(t *testing.T) {
148+
prepareTestEnv(t)
149+
session := loginUser(t, "user2", "password")
150+
testEditFile(t, session, "user2", "repo1", "master", "README.md")
151+
}

0 commit comments

Comments
 (0)