From 662b8bc2a2ae45b720003cf9437c70991781479b Mon Sep 17 00:00:00 2001 From: Mura Li Date: Thu, 8 Jun 2017 19:15:41 +0800 Subject: [PATCH] Add integration test for file editing (#1907) --- integrations/editor_test.go | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/integrations/editor_test.go b/integrations/editor_test.go index df0cfaa291..976def89eb 100644 --- a/integrations/editor_test.go +++ b/integrations/editor_test.go @@ -8,6 +8,7 @@ import ( "bytes" "net/http" "net/url" + "path" "testing" "github.com/stretchr/testify/assert" @@ -104,3 +105,47 @@ func TestCreateFileOnProtectedBranch(t *testing.T) { // Check body for error message assert.Contains(t, string(resp.Body), "Can not commit to protected branch 'master'.") } + +func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath string) { + + newContent := "Hello, World (Edited)\n" + + // Get to the 'edit this file' page + req, err := http.NewRequest("GET", path.Join(user, repo, "_edit", branch, filePath), nil) + assert.NoError(t, err) + resp := session.MakeRequest(t, req) + assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + + htmlDoc, err := NewHtmlParser(resp.Body) + assert.NoError(t, err) + lastCommit := htmlDoc.GetInputValueByName("last_commit") + assert.NotEmpty(t, lastCommit) + + // Submit the edits + req, err = http.NewRequest("POST", path.Join(user, repo, "_edit", branch, filePath), + bytes.NewBufferString(url.Values{ + "_csrf": []string{htmlDoc.GetInputValueByName("_csrf")}, + "last_commit": []string{lastCommit}, + "tree_path": []string{filePath}, + "content": []string{newContent}, + "commit_choice": []string{"direct"}, + }.Encode()), + ) + assert.NoError(t, err) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + resp = session.MakeRequest(t, req) + assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + + // Verify the change + req, err = http.NewRequest("GET", path.Join(user, repo, "raw", branch, filePath), nil) + assert.NoError(t, err) + resp = session.MakeRequest(t, req) + assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + assert.EqualValues(t, newContent, string(resp.Body)) +} + +func TestEditFile(t *testing.T) { + prepareTestEnv(t) + session := loginUser(t, "user2", "password") + testEditFile(t, session, "user2", "repo1", "master", "README.md") +}