Skip to content

Unit tests for routers/repo/issue_label #3198

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

Merged
merged 2 commits into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 0 additions & 6 deletions integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,3 @@ func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
doc := NewHTMLParser(t, resp.Body)
return doc.GetCSRF()
}

func RedirectURL(t testing.TB, resp *httptest.ResponseRecorder) string {
urlSlice := resp.HeaderMap["Location"]
assert.NotEmpty(t, urlSlice, "No redirect URL founds")
return urlSlice[0]
}
5 changes: 3 additions & 2 deletions integrations/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"

"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -122,7 +123,7 @@ func testNewIssue(t *testing.T, session *TestSession, user, repo, title, content
})
resp = session.MakeRequest(t, req, http.StatusFound)

issueURL := RedirectURL(t, resp)
issueURL := test.RedirectURL(resp)
req = NewRequest(t, "GET", issueURL)
resp = session.MakeRequest(t, req, http.StatusOK)

Expand Down Expand Up @@ -153,7 +154,7 @@ func testIssueAddComment(t *testing.T, session *TestSession, issueURL, content,
})
resp = session.MakeRequest(t, req, http.StatusFound)

req = NewRequest(t, "GET", RedirectURL(t, resp))
req = NewRequest(t, "GET", test.RedirectURL(resp))
resp = session.MakeRequest(t, req, http.StatusOK)

htmlDoc = NewHTMLParser(t, resp.Body)
Expand Down
3 changes: 2 additions & 1 deletion integrations/links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
api "code.gitea.io/sdk/gitea"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -54,7 +55,7 @@ func TestRedirectsNoLogin(t *testing.T) {
for link, redirectLink := range redirects {
req := NewRequest(t, "GET", link)
resp := MakeRequest(t, req, http.StatusFound)
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), RedirectURL(t, resp))
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
}
}

Expand Down
6 changes: 4 additions & 2 deletions integrations/pull_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"testing"

"code.gitea.io/gitea/modules/test"

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

Expand Down Expand Up @@ -54,7 +56,7 @@ func TestPullMerge(t *testing.T) {

resp := testPullCreate(t, session, "user1", "repo1", "master")

elem := strings.Split(RedirectURL(t, resp), "/")
elem := strings.Split(test.RedirectURL(resp), "/")
assert.EqualValues(t, "pulls", elem[3])
testPullMerge(t, session, elem[1], elem[2], elem[4])
}
Expand All @@ -67,7 +69,7 @@ func TestPullCleanUpAfterMerge(t *testing.T) {

resp := testPullCreate(t, session, "user1", "repo1", "feature/test")

elem := strings.Split(RedirectURL(t, resp), "/")
elem := strings.Split(test.RedirectURL(resp), "/")
assert.EqualValues(t, "pulls", elem[3])
testPullMerge(t, session, elem[1], elem[2], elem[4])

Expand Down
4 changes: 3 additions & 1 deletion integrations/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"testing"

"code.gitea.io/gitea/modules/test"

"github.com/Unknwon/i18n"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -38,7 +40,7 @@ func createNewRelease(t *testing.T, session *TestSession, repoURL, tag, title st

resp = session.MakeRequest(t, req, http.StatusFound)

RedirectURL(t, resp) // check that redirect URL exists
test.RedirectURL(resp) // check that redirect URL exists
}

func checkLatestReleaseAndCount(t *testing.T, session *TestSession, repoURL, version, label string, count int) {
Expand Down
4 changes: 3 additions & 1 deletion integrations/repo_activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"testing"

"code.gitea.io/gitea/modules/test"

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

Expand All @@ -20,7 +22,7 @@ func TestRepoActivity(t *testing.T) {
testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
resp := testPullCreate(t, session, "user1", "repo1", "master")
elem := strings.Split(RedirectURL(t, resp), "/")
elem := strings.Split(test.RedirectURL(resp), "/")
assert.EqualValues(t, "pulls", elem[3])
testPullMerge(t, session, elem[1], elem[2], elem[4])

Expand Down
4 changes: 3 additions & 1 deletion integrations/repo_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"testing"

"code.gitea.io/gitea/modules/test"

"github.com/Unknwon/i18n"
"github.com/stretchr/testify/assert"
)
Expand All @@ -29,7 +31,7 @@ func testCreateBranch(t *testing.T, session *TestSession, user, repo, oldRefSubU
if expectedStatus != http.StatusFound {
return ""
}
return RedirectURL(t, resp)
return test.RedirectURL(resp)
}

func TestCreateBranch(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions integrations/timetracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"testing"
"time"

"code.gitea.io/gitea/modules/test"

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

Expand Down Expand Up @@ -47,7 +49,7 @@ func testViewTimetrackingControls(t *testing.T, session *TestSession, user, repo
if canTrackTime {
resp = session.MakeRequest(t, req, http.StatusSeeOther)

req = NewRequest(t, "GET", RedirectURL(t, resp))
req = NewRequest(t, "GET", test.RedirectURL(resp))
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)

Expand All @@ -65,7 +67,7 @@ func testViewTimetrackingControls(t *testing.T, session *TestSession, user, repo
})
resp = session.MakeRequest(t, req, http.StatusSeeOther)

req = NewRequest(t, "GET", RedirectURL(t, resp))
req = NewRequest(t, "GET", test.RedirectURL(resp))
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)

Expand Down
3 changes: 2 additions & 1 deletion integrations/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/test"

"github.com/Unknwon/i18n"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -86,7 +87,7 @@ func TestRenameReservedUsername(t *testing.T) {
})
resp := session.MakeRequest(t, req, http.StatusFound)

req = NewRequest(t, "GET", RedirectURL(t, resp))
req = NewRequest(t, "GET", test.RedirectURL(resp))
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
assert.Contains(t,
Expand Down
19 changes: 18 additions & 1 deletion models/unit_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/go-xorm/xorm"
"github.com/stretchr/testify/assert"
"gopkg.in/testfixtures.v2"
"net/url"
)

// NonexistentID an ID that will never exist
Expand All @@ -28,9 +29,10 @@ var giteaRoot string
// MainTest a reusable TestMain(..) function for unit tests that need to use a
// test database. Creates the test database, and sets necessary settings.
func MainTest(m *testing.M, pathToGiteaRoot string) {
var err error
giteaRoot = pathToGiteaRoot
fixturesDir := filepath.Join(pathToGiteaRoot, "models", "fixtures")
if err := createTestEngine(fixturesDir); err != nil {
if err = createTestEngine(fixturesDir); err != nil {
fmt.Fprintf(os.Stderr, "Error creating test engine: %v\n", err)
os.Exit(1)
}
Expand All @@ -41,6 +43,13 @@ func MainTest(m *testing.M, pathToGiteaRoot string) {
setting.SSH.Domain = "try.gitea.io"
setting.RepoRootPath = filepath.Join(os.TempDir(), "repos")
setting.AppDataPath = filepath.Join(os.TempDir(), "appdata")
setting.AppWorkPath = pathToGiteaRoot
setting.StaticRootPath = pathToGiteaRoot
setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/")
if err != nil {
fmt.Fprintf(os.Stderr, "Error url.Parse: %v\n", err)
os.Exit(1)
}

os.Exit(m.Run())
}
Expand Down Expand Up @@ -140,6 +149,14 @@ func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface
assert.False(t, exists)
}

// AssertExistsIf asserts that a bean exists or does not exist, depending on
// what is expected.
func AssertExistsIf(t *testing.T, expected bool, bean interface{}, conditions ...interface{}) {
exists, err := loadBeanIfExists(bean, conditions...)
assert.NoError(t, err)
assert.Equal(t, expected, exists)
}

// AssertSuccessfulInsert assert that beans is successfully inserted
func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
_, err := x.Insert(beans...)
Expand Down
23 changes: 7 additions & 16 deletions modules/test/context_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/go-macaron/session"
"github.com/stretchr/testify/assert"
"gopkg.in/macaron.v1"
"net/http/httptest"
)

// MockContext mock context for unit tests
Expand Down Expand Up @@ -44,6 +45,7 @@ func MockContext(t *testing.T, path string) *context.Context {
func LoadRepo(t *testing.T, ctx *context.Context, repoID int64) {
ctx.Repo = &context.Repository{}
ctx.Repo.Repository = models.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository)
ctx.Repo.RepoLink = ctx.Repo.Repository.Link()
}

// LoadUser load a user into a test context.
Expand Down Expand Up @@ -71,32 +73,21 @@ func (l mockLocale) Tr(s string, _ ...interface{}) string {
}

type mockResponseWriter struct {
status int
size int
}

func (rw *mockResponseWriter) Header() http.Header {
return map[string][]string{}
httptest.ResponseRecorder
size int
}

func (rw *mockResponseWriter) Write(b []byte) (int, error) {
rw.size += len(b)
return len(b), nil
}

func (rw *mockResponseWriter) WriteHeader(status int) {
rw.status = status
}

func (rw *mockResponseWriter) Flush() {
return rw.ResponseRecorder.Write(b)
}

func (rw *mockResponseWriter) Status() int {
return rw.status
return rw.ResponseRecorder.Code
}

func (rw *mockResponseWriter) Written() bool {
return rw.status > 0
return rw.ResponseRecorder.Code > 0
}

func (rw *mockResponseWriter) Size() int {
Expand Down
14 changes: 14 additions & 0 deletions modules/test/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package test

import (
"net/http"
)

// RedirectURL returns the redirect URL of a http response.
func RedirectURL(resp http.ResponseWriter) string {
return resp.Header().Get("Location")
}
2 changes: 1 addition & 1 deletion routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ func getActionIssues(ctx *context.Context) []*models.Issue {
return nil
}
if err = issue.LoadAttributes(); err != nil {
ctx.Handle(500, "LoadAttributes", nil)
ctx.Handle(500, "LoadAttributes", err)
return nil
}
}
Expand Down
10 changes: 3 additions & 7 deletions routers/repo/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,14 @@ func UpdateIssueLabel(ctx *context.Context) {
}

if action == "toggle" {
anyHaveLabel := false
// detach if any issues already have label, otherwise attach
action = "attach"
for _, issue := range issues {
if issue.HasLabel(label.ID) {
anyHaveLabel = true
action = "detach"
break
}
}
if anyHaveLabel {
action = "detach"
} else {
action = "attach"
}
}

if action == "attach" {
Expand Down
Loading