Skip to content

Commit befa744

Browse files
ethantkoeniglafriks
authored andcommitted
Unit tests for routers/repo/issue_label (#3198)
1 parent bde0409 commit befa744

15 files changed

+231
-42
lines changed

integrations/integration_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,3 @@ func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
303303
doc := NewHTMLParser(t, resp.Body)
304304
return doc.GetCSRF()
305305
}
306-
307-
func RedirectURL(t testing.TB, resp *httptest.ResponseRecorder) string {
308-
urlSlice := resp.HeaderMap["Location"]
309-
assert.NotEmpty(t, urlSlice, "No redirect URL founds")
310-
return urlSlice[0]
311-
}

integrations/issue_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"code.gitea.io/gitea/models"
1515
"code.gitea.io/gitea/modules/setting"
16+
"code.gitea.io/gitea/modules/test"
1617

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

125-
issueURL := RedirectURL(t, resp)
126+
issueURL := test.RedirectURL(resp)
126127
req = NewRequest(t, "GET", issueURL)
127128
resp = session.MakeRequest(t, req, http.StatusOK)
128129

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

156-
req = NewRequest(t, "GET", RedirectURL(t, resp))
157+
req = NewRequest(t, "GET", test.RedirectURL(resp))
157158
resp = session.MakeRequest(t, req, http.StatusOK)
158159

159160
htmlDoc = NewHTMLParser(t, resp.Body)

integrations/links_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/modules/test"
1415
api "code.gitea.io/sdk/gitea"
1516

1617
"github.com/stretchr/testify/assert"
@@ -54,7 +55,7 @@ func TestRedirectsNoLogin(t *testing.T) {
5455
for link, redirectLink := range redirects {
5556
req := NewRequest(t, "GET", link)
5657
resp := MakeRequest(t, req, http.StatusFound)
57-
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), RedirectURL(t, resp))
58+
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
5859
}
5960
}
6061

integrations/pull_merge_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strings"
1212
"testing"
1313

14+
"code.gitea.io/gitea/modules/test"
15+
1416
"github.com/stretchr/testify/assert"
1517
)
1618

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

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

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

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

70-
elem := strings.Split(RedirectURL(t, resp), "/")
72+
elem := strings.Split(test.RedirectURL(resp), "/")
7173
assert.EqualValues(t, "pulls", elem[3])
7274
testPullMerge(t, session, elem[1], elem[2], elem[4])
7375

integrations/release_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"net/http"
1010
"testing"
1111

12+
"code.gitea.io/gitea/modules/test"
13+
1214
"github.com/Unknwon/i18n"
1315
"github.com/stretchr/testify/assert"
1416
)
@@ -38,7 +40,7 @@ func createNewRelease(t *testing.T, session *TestSession, repoURL, tag, title st
3840

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

41-
RedirectURL(t, resp) // check that redirect URL exists
43+
test.RedirectURL(resp) // check that redirect URL exists
4244
}
4345

4446
func checkLatestReleaseAndCount(t *testing.T, session *TestSession, repoURL, version, label string, count int) {

integrations/repo_activity_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
"testing"
1111

12+
"code.gitea.io/gitea/modules/test"
13+
1214
"github.com/stretchr/testify/assert"
1315
)
1416

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

integrations/repo_branch_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"code.gitea.io/gitea/modules/test"
14+
1315
"github.com/Unknwon/i18n"
1416
"github.com/stretchr/testify/assert"
1517
)
@@ -29,7 +31,7 @@ func testCreateBranch(t *testing.T, session *TestSession, user, repo, oldRefSubU
2931
if expectedStatus != http.StatusFound {
3032
return ""
3133
}
32-
return RedirectURL(t, resp)
34+
return test.RedirectURL(resp)
3335
}
3436

3537
func TestCreateBranch(t *testing.T) {

integrations/timetracking_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"testing"
1111
"time"
1212

13+
"code.gitea.io/gitea/modules/test"
14+
1315
"github.com/stretchr/testify/assert"
1416
)
1517

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

50-
req = NewRequest(t, "GET", RedirectURL(t, resp))
52+
req = NewRequest(t, "GET", test.RedirectURL(resp))
5153
resp = session.MakeRequest(t, req, http.StatusOK)
5254
htmlDoc = NewHTMLParser(t, resp.Body)
5355

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

68-
req = NewRequest(t, "GET", RedirectURL(t, resp))
70+
req = NewRequest(t, "GET", test.RedirectURL(resp))
6971
resp = session.MakeRequest(t, req, http.StatusOK)
7072
htmlDoc = NewHTMLParser(t, resp.Body)
7173

integrations/user_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/modules/test"
1213

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

89-
req = NewRequest(t, "GET", RedirectURL(t, resp))
90+
req = NewRequest(t, "GET", test.RedirectURL(resp))
9091
resp = session.MakeRequest(t, req, http.StatusOK)
9192
htmlDoc := NewHTMLParser(t, resp.Body)
9293
assert.Contains(t,

models/unit_tests.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/go-xorm/xorm"
1818
"github.com/stretchr/testify/assert"
1919
"gopkg.in/testfixtures.v2"
20+
"net/url"
2021
)
2122

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

4554
os.Exit(m.Run())
4655
}
@@ -140,6 +149,14 @@ func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface
140149
assert.False(t, exists)
141150
}
142151

152+
// AssertExistsIf asserts that a bean exists or does not exist, depending on
153+
// what is expected.
154+
func AssertExistsIf(t *testing.T, expected bool, bean interface{}, conditions ...interface{}) {
155+
exists, err := loadBeanIfExists(bean, conditions...)
156+
assert.NoError(t, err)
157+
assert.Equal(t, expected, exists)
158+
}
159+
143160
// AssertSuccessfulInsert assert that beans is successfully inserted
144161
func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
145162
_, err := x.Insert(beans...)

modules/test/context_tests.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/go-macaron/session"
1717
"github.com/stretchr/testify/assert"
1818
"gopkg.in/macaron.v1"
19+
"net/http/httptest"
1920
)
2021

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

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

7375
type mockResponseWriter struct {
74-
status int
75-
size int
76-
}
77-
78-
func (rw *mockResponseWriter) Header() http.Header {
79-
return map[string][]string{}
76+
httptest.ResponseRecorder
77+
size int
8078
}
8179

8280
func (rw *mockResponseWriter) Write(b []byte) (int, error) {
8381
rw.size += len(b)
84-
return len(b), nil
85-
}
86-
87-
func (rw *mockResponseWriter) WriteHeader(status int) {
88-
rw.status = status
89-
}
90-
91-
func (rw *mockResponseWriter) Flush() {
82+
return rw.ResponseRecorder.Write(b)
9283
}
9384

9485
func (rw *mockResponseWriter) Status() int {
95-
return rw.status
86+
return rw.ResponseRecorder.Code
9687
}
9788

9889
func (rw *mockResponseWriter) Written() bool {
99-
return rw.status > 0
90+
return rw.ResponseRecorder.Code > 0
10091
}
10192

10293
func (rw *mockResponseWriter) Size() int {

modules/test/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package test
6+
7+
import (
8+
"net/http"
9+
)
10+
11+
// RedirectURL returns the redirect URL of a http response.
12+
func RedirectURL(resp http.ResponseWriter) string {
13+
return resp.Header().Get("Location")
14+
}

routers/repo/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ func getActionIssues(ctx *context.Context) []*models.Issue {
776776
return nil
777777
}
778778
if err = issue.LoadAttributes(); err != nil {
779-
ctx.Handle(500, "LoadAttributes", nil)
779+
ctx.Handle(500, "LoadAttributes", err)
780780
return nil
781781
}
782782
}

routers/repo/issue_label.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,14 @@ func UpdateIssueLabel(ctx *context.Context) {
155155
}
156156

157157
if action == "toggle" {
158-
anyHaveLabel := false
158+
// detach if any issues already have label, otherwise attach
159+
action = "attach"
159160
for _, issue := range issues {
160161
if issue.HasLabel(label.ID) {
161-
anyHaveLabel = true
162+
action = "detach"
162163
break
163164
}
164165
}
165-
if anyHaveLabel {
166-
action = "detach"
167-
} else {
168-
action = "attach"
169-
}
170166
}
171167

172168
if action == "attach" {

0 commit comments

Comments
 (0)