Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate boilerplate in integration tests #1979

Merged
merged 1 commit into from
Jun 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integrations/api_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAPITeam(t *testing.T) {
team := models.AssertExistsAndLoadBean(t, &models.Team{ID: teamUser.TeamID}).(*models.Team)
user := models.AssertExistsAndLoadBean(t, &models.User{ID: teamUser.UID}).(*models.User)

session := loginUser(t, user.Name, "password")
session := loginUser(t, user.Name)
url := fmt.Sprintf("/api/v1/teams/%d", teamUser.TeamID)
req := NewRequest(t, "GET", url)
resp := session.MakeRequest(t, req)
Expand Down
36 changes: 15 additions & 21 deletions integrations/change_default_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
package integrations

import (
"bytes"
"fmt"
"net/http"
"net/url"
"testing"

"code.gitea.io/gitea/models"
Expand All @@ -21,37 +19,33 @@ func TestChangeDefaultBranch(t *testing.T) {
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)

session := loginUser(t, owner.Name, "password")
session := loginUser(t, owner.Name)
branchesURL := fmt.Sprintf("/%s/%s/settings/branches", owner.Name, repo.Name)

req := NewRequest(t, "GET", branchesURL)
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
doc, err := NewHtmlParser(resp.Body)
assert.NoError(t, err)

req = NewRequestBody(t, "POST", branchesURL,
bytes.NewBufferString(url.Values{
"_csrf": []string{doc.GetInputValueByName("_csrf")},
"action": []string{"default_branch"},
"branch": []string{"DefaultBranch"},
}.Encode()))
doc := NewHtmlParser(t, resp.Body)

req = NewRequestWithValues(t, "POST", branchesURL, map[string]string{
"_csrf": doc.GetCSRF(),
"action": "default_branch",
"branch": "DefaultBranch",
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

req = NewRequest(t, "GET", branchesURL)
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
doc, err = NewHtmlParser(resp.Body)
assert.NoError(t, err)

req = NewRequestBody(t, "POST", branchesURL,
bytes.NewBufferString(url.Values{
"_csrf": []string{doc.GetInputValueByName("_csrf")},
"action": []string{"default_branch"},
"branch": []string{"does_not_exist"},
}.Encode()))
doc = NewHtmlParser(t, resp.Body)

req = NewRequestWithValues(t, "POST", branchesURL, map[string]string{
"_csrf": doc.GetInputValueByName("_csrf"),
"action": "default_branch",
"branch": "does_not_exist",
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode)
Expand Down
14 changes: 5 additions & 9 deletions integrations/delete_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
package integrations

import (
"bytes"
"net/http"
"net/url"
"testing"

"code.gitea.io/gitea/models"
Expand All @@ -18,18 +16,16 @@ import (
func TestDeleteUser(t *testing.T) {
prepareTestEnv(t)

session := loginUser(t, "user1", "password")
session := loginUser(t, "user1")

req := NewRequest(t, "GET", "/admin/users/8")
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

doc, err := NewHtmlParser(resp.Body)
assert.NoError(t, err)
req = NewRequestBody(t, "POST", "/admin/users/8/delete",
bytes.NewBufferString(url.Values{
"_csrf": []string{doc.GetInputValueByName("_csrf")},
}.Encode()))
doc := NewHtmlParser(t, resp.Body)
req = NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{
"_csrf": doc.GetCSRF(),
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
Expand Down
82 changes: 35 additions & 47 deletions integrations/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
package integrations

import (
"bytes"
"net/http"
"net/url"
"path"
"testing"

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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")
}
15 changes: 9 additions & 6 deletions integrations/html_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ package integrations

import (
"bytes"
"testing"

"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
)

type HtmlDoc struct {
doc *goquery.Document
}

func NewHtmlParser(content []byte) (*HtmlDoc, error) {
func NewHtmlParser(t *testing.T, content []byte) *HtmlDoc {
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(content))
if err != nil {
return nil, err
}

return &HtmlDoc{doc: doc}, nil
assert.NoError(t, err)
return &HtmlDoc{doc: doc}
}

func (doc *HtmlDoc) GetInputValueById(id string) string {
Expand All @@ -32,3 +31,7 @@ func (doc *HtmlDoc) GetInputValueByName(name string) string {
text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value")
return text
}

func (doc *HtmlDoc) GetCSRF() string {
return doc.GetInputValueByName("_csrf")
}
49 changes: 33 additions & 16 deletions integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package integrations
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -155,21 +156,23 @@ func (s *TestSession) MakeRequest(t *testing.T, req *http.Request) *TestResponse
return resp
}

func loginUser(t *testing.T, userName, password string) *TestSession {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think we should do that. How should we test user login with different password?

Copy link
Member Author

@ethantkoenig ethantkoenig Jun 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about having two functions?

func loginUserDefaultPassword(...) {
	loginUser(..., "password")
}

func loginUser(...) {
	// what we originally had
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lunny Done

const userPassword = "password"

func loginUser(t *testing.T, userName string) *TestSession {
return loginUserWithPassword(t, userName, userPassword)
}

func loginUserWithPassword(t *testing.T, userName, password string) *TestSession {
req := NewRequest(t, "GET", "/user/login")
resp := MakeRequest(req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

doc, err := NewHtmlParser(resp.Body)
assert.NoError(t, err)

req = NewRequestBody(t, "POST", "/user/login",
bytes.NewBufferString(url.Values{
"_csrf": []string{doc.GetInputValueByName("_csrf")},
"user_name": []string{userName},
"password": []string{password},
}.Encode()),
)
doc := NewHtmlParser(t, resp.Body)
req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{
"_csrf": doc.GetCSRF(),
"user_name": userName,
"password": password,
})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp = MakeRequest(req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
Expand Down Expand Up @@ -211,14 +214,28 @@ type TestResponse struct {
Headers http.Header
}

func NewRequest(t *testing.T, method, url string) *http.Request {
return NewRequestBody(t, method, url, nil)
func NewRequest(t *testing.T, method, urlStr string) *http.Request {
return NewRequestWithBody(t, method, urlStr, nil)
}

func NewRequestWithValues(t *testing.T, method, urlStr string, values map[string]string) *http.Request {
urlValues := url.Values{}
for key, value := range values {
urlValues[key] = []string{value}
}
return NewRequestWithBody(t, method, urlStr, bytes.NewBufferString(urlValues.Encode()))
}

func NewRequestWithJSON(t *testing.T, method, urlStr string, v interface{}) *http.Request {
jsonBytes, err := json.Marshal(v)
assert.NoError(t, err)
return NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes))
}

func NewRequestBody(t *testing.T, method, url string, body io.Reader) *http.Request {
request, err := http.NewRequest(method, url, body)
func NewRequestWithBody(t *testing.T, method, urlStr string, body io.Reader) *http.Request {
request, err := http.NewRequest(method, urlStr, body)
assert.NoError(t, err)
request.RequestURI = url
request.RequestURI = urlStr
return request
}

Expand Down
5 changes: 2 additions & 3 deletions integrations/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ func TestNoLoginViewIssuesSortByType(t *testing.T) {
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
repo.Owner = models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)

session := loginUser(t, user.Name, "password")
session := loginUser(t, user.Name)
req := NewRequest(t, "GET", repo.RelLink()+"/issues?type=created_by")
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)
issuesSelection := getIssuesSelection(htmlDoc)
expectedNumIssues := models.GetCount(t,
&models.Issue{RepoID: repo.ID, PosterID: user.ID},
Expand Down
5 changes: 2 additions & 3 deletions integrations/pull_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import (
func TestPullCompare(t *testing.T) {
prepareTestEnv(t)

session := loginUser(t, "user2", "password")
session := loginUser(t, "user2")
req := NewRequest(t, "GET", "/user2/repo1/pulls")
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)
link, exists := htmlDoc.doc.Find(".navbar").Find(".ui.green.button").Attr("href")
assert.True(t, exists, "The template has changed")

Expand Down
Loading