|
8 | 8 | "bytes"
|
9 | 9 | "net/http"
|
10 | 10 | "net/url"
|
| 11 | + "path" |
11 | 12 | "testing"
|
12 | 13 |
|
13 | 14 | "github.com/stretchr/testify/assert"
|
@@ -104,3 +105,47 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
|
104 | 105 | // Check body for error message
|
105 | 106 | assert.Contains(t, string(resp.Body), "Can not commit to protected branch 'master'.")
|
106 | 107 | }
|
| 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