Skip to content

Commit

Permalink
Less verbose integration tests (go-gitea#2123)
Browse files Browse the repository at this point in the history
* Helper functions for intergration test boilerplate
  • Loading branch information
ethantkoenig authored and bkcsoft committed Jul 7, 2017
1 parent 5651cc7 commit f1adaef
Show file tree
Hide file tree
Showing 24 changed files with 122 additions and 223 deletions.
2 changes: 1 addition & 1 deletion integrations/api_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func testAPIGetBranch(t *testing.T, branchName string, exists bool) {

session := loginUser(t, "user2")
req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/branches/%s", branchName)
resp := session.MakeRequest(t, req)
resp := session.MakeRequest(t, req, NoExpectedStatus)
if !exists {
assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode)
return
Expand Down
3 changes: 1 addition & 2 deletions integrations/api_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ func TestAPIListComments(t *testing.T) {
session := loginUser(t, repoOwner.Name)
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/comments",
repoOwner.Name, repo.Name, issue.Index)
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp := session.MakeRequest(t, req, http.StatusOK)

var comments []*api.Comment
DecodeJSON(t, resp, &comments)
Expand Down
6 changes: 2 additions & 4 deletions integrations/api_issue_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func TestAPIAddIssueLabels(t *testing.T) {
Labels: []int64{label.ID},
})
session := loginUser(t, owner.Name)
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp := session.MakeRequest(t, req, http.StatusOK)
var apiLabels []*api.Label
DecodeJSON(t, resp, &apiLabels)
assert.Len(t, apiLabels, models.GetCount(t, &models.IssueLabel{IssueID: issue.ID}))
Expand All @@ -52,8 +51,7 @@ func TestAPIReplaceIssueLabels(t *testing.T) {
Labels: []int64{label.ID},
})
session := loginUser(t, owner.Name)
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp := session.MakeRequest(t, req, http.StatusOK)
var apiLabels []*api.Label
DecodeJSON(t, resp, &apiLabels)
assert.Len(t, apiLabels, 1)
Expand Down
6 changes: 2 additions & 4 deletions integrations/api_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ func TestAPIListIssues(t *testing.T) {
session := loginUser(t, owner.Name)
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues?state=all",
owner.Name, repo.Name)
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp := session.MakeRequest(t, req, http.StatusOK)
var apiIssues []*api.Issue
DecodeJSON(t, resp, &apiIssues)
assert.Len(t, apiIssues, models.GetCount(t, &models.Issue{RepoID: repo.ID}))
Expand All @@ -49,8 +48,7 @@ func TestAPICreateIssue(t *testing.T) {
Title: title,
Assignee: owner.Name,
})
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusCreated, resp.HeaderCode)
resp := session.MakeRequest(t, req, http.StatusCreated)
var apiIssue api.Issue
DecodeJSON(t, resp, &apiIssue)
assert.Equal(t, apiIssue.Body, body)
Expand Down
3 changes: 1 addition & 2 deletions integrations/api_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func TestAPIViewPulls(t *testing.T) {

session := loginUser(t, "user2")
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/pulls?state=all", owner.Name, repo.Name)
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp := session.MakeRequest(t, req, http.StatusOK)

var pulls []*api.PullRequest
DecodeJSON(t, resp, &pulls)
Expand Down
8 changes: 2 additions & 6 deletions integrations/api_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@ package integrations
import (
"net/http"
"testing"

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

func TestAPIUserReposNotLogin(t *testing.T) {
prepareTestEnv(t)

req := NewRequest(t, "GET", "/api/v1/users/user2/repos")
resp := MakeRequest(req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
MakeRequest(t, req, http.StatusOK)
}

func TestAPISearchRepoNotLogin(t *testing.T) {
prepareTestEnv(t)

req := NewRequest(t, "GET", "/api/v1/repos/search?q=Test")
resp := MakeRequest(req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
MakeRequest(t, req, http.StatusOK)
}
3 changes: 1 addition & 2 deletions integrations/api_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ func TestAPITeam(t *testing.T) {

session := loginUser(t, user.Name)
req := NewRequestf(t, "GET", "/api/v1/teams/%d", teamUser.TeamID)
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp := session.MakeRequest(t, req, http.StatusOK)

var apiTeam api.Team
DecodeJSON(t, resp, &apiTeam)
Expand Down
26 changes: 7 additions & 19 deletions integrations/change_default_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"testing"

"code.gitea.io/gitea/models"

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

func TestChangeDefaultBranch(t *testing.T) {
Expand All @@ -22,29 +20,19 @@ func TestChangeDefaultBranch(t *testing.T) {
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 := NewHTMLParser(t, resp.Body)

req = NewRequestWithValues(t, "POST", branchesURL, map[string]string{
"_csrf": doc.GetCSRF(),
csrf := GetCSRF(t, session, branchesURL)
req := NewRequestWithValues(t, "POST", branchesURL, map[string]string{
"_csrf": csrf,
"action": "default_branch",
"branch": "DefaultBranch",
})
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 = NewHTMLParser(t, resp.Body)
session.MakeRequest(t, req, http.StatusFound)

csrf = GetCSRF(t, session, branchesURL)
req = NewRequestWithValues(t, "POST", branchesURL, map[string]string{
"_csrf": doc.GetInputValueByName("_csrf"),
"_csrf": csrf,
"action": "default_branch",
"branch": "does_not_exist",
})
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode)
session.MakeRequest(t, req, http.StatusNotFound)
}
15 changes: 4 additions & 11 deletions integrations/delete_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,18 @@ import (
"testing"

"code.gitea.io/gitea/models"

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

func TestDeleteUser(t *testing.T) {
prepareTestEnv(t)

session := loginUser(t, "user1")

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

doc := NewHTMLParser(t, resp.Body)
req = NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{
"_csrf": doc.GetCSRF(),
csrf := GetCSRF(t, session, "/admin/users/8")
req := NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{
"_csrf": csrf,
})
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
session.MakeRequest(t, req, http.StatusOK)

models.AssertNotExistsBean(t, &models.User{ID: 8})
models.CheckConsistencyFor(t, &models.User{})
Expand Down
47 changes: 15 additions & 32 deletions integrations/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ func TestCreateFile(t *testing.T) {

// Request editor page
req := NewRequest(t, "GET", "/user2/repo1/_new/master/")
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp := session.MakeRequest(t, req, http.StatusOK)

doc := NewHTMLParser(t, resp.Body)
lastCommit := doc.GetInputValueByName("last_commit")
Expand All @@ -34,41 +33,32 @@ func TestCreateFile(t *testing.T) {
"content": "Content",
"commit_choice": "direct",
})
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
resp = session.MakeRequest(t, req, http.StatusFound)
}

func TestCreateFileOnProtectedBranch(t *testing.T) {
prepareTestEnv(t)

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 := NewHTMLParser(t, resp.Body)

csrf := GetCSRF(t, session, "/user2/repo1/settings/branches")
// Change master branch to protected
req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", map[string]string{
"_csrf": doc.GetCSRF(),
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", map[string]string{
"_csrf": csrf,
"branchName": "master",
"canPush": "true",
})
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp := session.MakeRequest(t, req, http.StatusOK)
// Check if master branch has been locked successfully
flashCookie := session.GetCookie("macaron_flash")
assert.NotNil(t, flashCookie)
assert.EqualValues(t, flashCookie.Value, "success%3Dmaster%2BLocked%2Bsuccessfully")

// Request editor page
req = NewRequest(t, "GET", "/user2/repo1/_new/master/")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp = session.MakeRequest(t, req, http.StatusOK)

doc = NewHTMLParser(t, resp.Body)
doc := NewHTMLParser(t, resp.Body)
lastCommit := doc.GetInputValueByName("last_commit")
assert.NotEmpty(t, lastCommit)

Expand All @@ -81,8 +71,7 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
"commit_choice": "direct",
})

resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp = session.MakeRequest(t, req, http.StatusOK)
// Check body for error message
assert.Contains(t, string(resp.Body), "Can not commit to protected branch 'master'.")
}
Expand All @@ -93,8 +82,7 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa

// Get to the 'edit this file' page
req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp := session.MakeRequest(t, req, http.StatusOK)

htmlDoc := NewHTMLParser(t, resp.Body)
lastCommit := htmlDoc.GetInputValueByName("last_commit")
Expand All @@ -110,13 +98,11 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
"commit_choice": "direct",
},
)
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
resp = session.MakeRequest(t, req, http.StatusFound)

// Verify the change
req = NewRequest(t, "GET", path.Join(user, repo, "raw", branch, filePath))
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp = session.MakeRequest(t, req, http.StatusOK)
assert.EqualValues(t, newContent, string(resp.Body))

return resp
Expand All @@ -128,8 +114,7 @@ func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, bra

// Get to the 'edit this file' page
req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp := session.MakeRequest(t, req, http.StatusOK)

htmlDoc := NewHTMLParser(t, resp.Body)
lastCommit := htmlDoc.GetInputValueByName("last_commit")
Expand All @@ -146,13 +131,11 @@ func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, bra
"new_branch_name": targetBranch,
},
)
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
resp = session.MakeRequest(t, req, http.StatusFound)

// Verify the change
req = NewRequest(t, "GET", path.Join(user, repo, "raw", targetBranch, filePath))
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
resp = session.MakeRequest(t, req, http.StatusOK)
assert.EqualValues(t, newContent, string(resp.Body))

return resp
Expand Down
30 changes: 23 additions & 7 deletions integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ func (s *TestSession) GetCookie(name string) *http.Cookie {
return nil
}

func (s *TestSession) MakeRequest(t testing.TB, req *http.Request) *TestResponse {
func (s *TestSession) MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *TestResponse {
baseURL, err := url.Parse(setting.AppURL)
assert.NoError(t, err)
for _, c := range s.jar.Cookies(baseURL) {
req.AddCookie(c)
}
resp := MakeRequest(req)
resp := MakeRequest(t, req, expectedStatus)

ch := http.Header{}
ch.Add("Cookie", strings.Join(resp.Headers["Set-Cookie"], ";"))
Expand All @@ -164,17 +164,15 @@ func loginUser(t testing.TB, userName string) *TestSession {

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

doc := NewHTMLParser(t, resp.Body)
req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{
"_csrf": doc.GetCSRF(),
"user_name": userName,
"password": password,
})
resp = MakeRequest(req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
resp = MakeRequest(t, req, http.StatusFound)

ch := http.Header{}
ch.Add("Cookie", strings.Join(resp.Headers["Set-Cookie"], ";"))
Expand Down Expand Up @@ -246,13 +244,18 @@ func NewRequestWithBody(t testing.TB, method, urlStr string, body io.Reader) *ht
return request
}

func MakeRequest(req *http.Request) *TestResponse {
const NoExpectedStatus = -1

func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *TestResponse {
buffer := bytes.NewBuffer(nil)
respWriter := &TestResponseWriter{
Writer: buffer,
Headers: make(map[string][]string),
}
mac.ServeHTTP(respWriter, req)
if expectedStatus != NoExpectedStatus {
assert.EqualValues(t, expectedStatus, respWriter.HeaderCode)
}
return &TestResponse{
HeaderCode: respWriter.HeaderCode,
Body: buffer.Bytes(),
Expand All @@ -264,3 +267,16 @@ func DecodeJSON(t testing.TB, resp *TestResponse, v interface{}) {
decoder := json.NewDecoder(bytes.NewBuffer(resp.Body))
assert.NoError(t, decoder.Decode(v))
}

func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
req := NewRequest(t, "GET", urlStr)
resp := session.MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body)
return doc.GetCSRF()
}

func RedirectURL(t testing.TB, resp *TestResponse) string {
urlSlice := resp.Headers["Location"]
assert.NotEmpty(t, urlSlice, "No redirect URL founds")
return urlSlice[0]
}
4 changes: 2 additions & 2 deletions integrations/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func assertProtectedBranch(t *testing.T, repoID int64, branchName string, isErr,
t.Log(reqURL)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken))

resp := MakeRequest(req)
resp := MakeRequest(t, req, NoExpectedStatus)
if isErr {
assert.EqualValues(t, 500, resp.HeaderCode)
assert.EqualValues(t, http.StatusInternalServerError, resp.HeaderCode)
} else {
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
var branch models.ProtectedBranch
Expand Down
Loading

0 comments on commit f1adaef

Please sign in to comment.