Skip to content

Commit 7356762

Browse files
ethantkoeniglunny
authored andcommitted
Integration tests for issues API (#2059)
1 parent 4c0e567 commit 7356762

File tree

2 files changed

+78
-15
lines changed

2 files changed

+78
-15
lines changed

integrations/api_issue_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 integrations
6+
7+
import (
8+
"net/http"
9+
"testing"
10+
11+
"code.gitea.io/gitea/models"
12+
api "code.gitea.io/sdk/gitea"
13+
14+
"fmt"
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func TestAPIListIssues(t *testing.T) {
19+
prepareTestEnv(t)
20+
21+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
22+
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
23+
24+
session := loginUser(t, owner.Name)
25+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues?state=all",
26+
owner.Name, repo.Name)
27+
resp := session.MakeRequest(t, req)
28+
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
29+
var apiIssues []*api.Issue
30+
DecodeJSON(t, resp, &apiIssues)
31+
assert.Len(t, apiIssues, models.GetCount(t, &models.Issue{RepoID: repo.ID}))
32+
for _, apiIssue := range apiIssues {
33+
models.AssertExistsAndLoadBean(t, &models.Issue{ID: apiIssue.ID, RepoID: repo.ID})
34+
}
35+
}
36+
37+
func TestAPICreateIssue(t *testing.T) {
38+
prepareTestEnv(t)
39+
const body, title = "apiTestBody", "apiTestTitle"
40+
41+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
42+
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
43+
44+
session := loginUser(t, owner.Name)
45+
46+
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues?state=all", owner.Name, repo.Name)
47+
req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
48+
Body: body,
49+
Title: title,
50+
Assignee: owner.Name,
51+
})
52+
resp := session.MakeRequest(t, req)
53+
assert.EqualValues(t, http.StatusCreated, resp.HeaderCode)
54+
var apiIssue api.Issue
55+
DecodeJSON(t, resp, &apiIssue)
56+
assert.Equal(t, apiIssue.Body, body)
57+
assert.Equal(t, apiIssue.Title, title)
58+
59+
models.AssertExistsAndLoadBean(t, &models.Issue{
60+
RepoID: repo.ID,
61+
AssigneeID: owner.ID,
62+
Content: body,
63+
Title: title,
64+
})
65+
}

routers/api/v1/repo/issue.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,25 @@ import (
1818

1919
// ListIssues list the issues of a repository
2020
func ListIssues(ctx *context.APIContext) {
21-
isClosed := ctx.Query("state") == "closed"
22-
issueOpts := models.IssuesOptions{
23-
RepoID: ctx.Repo.Repository.ID,
24-
Page: ctx.QueryInt("page"),
25-
IsClosed: util.OptionalBoolOf(isClosed),
21+
var isClosed util.OptionalBool
22+
switch ctx.Query("state") {
23+
case "closed":
24+
isClosed = util.OptionalBoolTrue
25+
case "all":
26+
isClosed = util.OptionalBoolNone
27+
default:
28+
isClosed = util.OptionalBoolFalse
2629
}
2730

28-
issues, err := models.Issues(&issueOpts)
31+
issues, err := models.Issues(&models.IssuesOptions{
32+
RepoID: ctx.Repo.Repository.ID,
33+
Page: ctx.QueryInt("page"),
34+
IsClosed: isClosed,
35+
})
2936
if err != nil {
3037
ctx.Error(500, "Issues", err)
3138
return
3239
}
33-
if ctx.Query("state") == "all" {
34-
issueOpts.IsClosed = util.OptionalBoolOf(!isClosed)
35-
tempIssues, err := models.Issues(&issueOpts)
36-
if err != nil {
37-
ctx.Error(500, "Issues", err)
38-
return
39-
}
40-
issues = append(issues, tempIssues...)
41-
}
4240

4341
err = models.IssueList(issues).LoadAttributes()
4442
if err != nil {

0 commit comments

Comments
 (0)