diff options
author | Ethan Koenig <etk39@cornell.edu> | 2017-06-17 00:49:45 -0400 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-06-17 12:49:45 +0800 |
commit | ce9b86082c4824917023b1bb480648d0cc56dd04 (patch) | |
tree | ba28673c955d009492557f54953857165e2f8da8 /integrations/editor_test.go | |
parent | a3868ef5367315f52334b81819a412cad820f5eb (diff) | |
download | gitea-ce9b86082c4824917023b1bb480648d0cc56dd04.tar.gz gitea-ce9b86082c4824917023b1bb480648d0cc56dd04.zip |
Consolidate boilerplate in integration tests (#1979)
Diffstat (limited to 'integrations/editor_test.go')
-rw-r--r-- | integrations/editor_test.go | 82 |
1 files changed, 35 insertions, 47 deletions
diff --git a/integrations/editor_test.go b/integrations/editor_test.go index 540caca264..10d9dc5318 100644 --- a/integrations/editor_test.go +++ b/integrations/editor_test.go @@ -5,9 +5,7 @@ package integrations import ( - "bytes" "net/http" - "net/url" "path" "testing" @@ -17,28 +15,25 @@ import ( func TestCreateFile(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user2", "password") + session := loginUser(t, "user2") // Request editor page req := NewRequest(t, "GET", "/user2/repo1/_new/master/") resp := session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + doc := NewHtmlParser(t, resp.Body) lastCommit := doc.GetInputValueByName("last_commit") assert.NotEmpty(t, lastCommit) // Save new file to master branch - req = NewRequestBody(t, "POST", "/user2/repo1/_new/master/", - bytes.NewBufferString(url.Values{ - "_csrf": []string{doc.GetInputValueByName("_csrf")}, - "last_commit": []string{lastCommit}, - "tree_path": []string{"test.txt"}, - "content": []string{"Content"}, - "commit_choice": []string{"direct"}, - }.Encode()), - ) + req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{ + "_csrf": doc.GetCSRF(), + "last_commit": lastCommit, + "tree_path": "test.txt", + "content": "Content", + "commit_choice": "direct", + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusFound, resp.HeaderCode) @@ -47,25 +42,21 @@ func TestCreateFile(t *testing.T) { func TestCreateFileOnProtectedBranch(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user2", "password") + session := loginUser(t, "user2") // Open repository branch settings req := NewRequest(t, "GET", "/user2/repo1/settings/branches") resp := session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + doc := NewHtmlParser(t, resp.Body) // Change master branch to protected - req = NewRequestBody(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", - bytes.NewBufferString(url.Values{ - "_csrf": []string{doc.GetInputValueByName("_csrf")}, - "branchName": []string{"master"}, - "canPush": []string{"true"}, - }.Encode()), - ) - assert.NoError(t, err) + req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", map[string]string{ + "_csrf": doc.GetCSRF(), + "branchName": "master", + "canPush": "true", + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) @@ -79,21 +70,19 @@ func TestCreateFileOnProtectedBranch(t *testing.T) { resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc, err = NewHtmlParser(resp.Body) - assert.NoError(t, err) + doc = NewHtmlParser(t, resp.Body) lastCommit := doc.GetInputValueByName("last_commit") assert.NotEmpty(t, lastCommit) // Save new file to master branch - req = NewRequestBody(t, "POST", "/user2/repo1/_new/master/", - bytes.NewBufferString(url.Values{ - "_csrf": []string{doc.GetInputValueByName("_csrf")}, - "last_commit": []string{lastCommit}, - "tree_path": []string{"test.txt"}, - "content": []string{"Content"}, - "commit_choice": []string{"direct"}, - }.Encode()), - ) + req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{ + "_csrf": doc.GetCSRF(), + "last_commit": lastCommit, + "tree_path": "test.txt", + "content": "Content", + "commit_choice": "direct", + }) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) @@ -110,20 +99,19 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa resp := session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - htmlDoc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + htmlDoc := NewHtmlParser(t, resp.Body) lastCommit := htmlDoc.GetInputValueByName("last_commit") assert.NotEmpty(t, lastCommit) // Submit the edits - req = NewRequestBody(t, "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()), + req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath), + map[string]string{ + "_csrf": htmlDoc.GetCSRF(), + "last_commit": lastCommit, + "tree_path": filePath, + "content": newContent, + "commit_choice": "direct", + }, ) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) @@ -140,6 +128,6 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa func TestEditFile(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user2", "password") + session := loginUser(t, "user2") testEditFile(t, session, "user2", "repo1", "master", "README.md") } |