Add Issue Subscription Check & Fix DeleteIssueSubscription (#318)

add subscription tests

make createTestIssue reusable

Add CheckIssueSubscription

fix

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/318
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
This commit is contained in:
6543 2020-04-24 14:37:28 +00:00 committed by lafriks
parent 79665cae15
commit 7dfa25bb30
3 changed files with 100 additions and 43 deletions

View file

@ -42,12 +42,21 @@ func (c *Client) DeleteIssueSubscription(owner, repo string, index int64, user s
if err != nil { if err != nil {
return err return err
} }
if status == http.StatusNoContent { if status == http.StatusCreated {
return nil return nil
} }
return fmt.Errorf("unexpected Status: %d", status) return fmt.Errorf("unexpected Status: %d", status)
} }
// CheckIssueSubscription check if current user is subscribed to an issue
func (c *Client) CheckIssueSubscription(owner, repo string, index int64) (*WatchInfo, error) {
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
return nil, err
}
wi := new(WatchInfo)
return wi, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions/check", owner, repo, index), nil, nil, wi)
}
// IssueSubscribe subscribe current user to an issue // IssueSubscribe subscribe current user to an issue
func (c *Client) IssueSubscribe(owner, repo string, index int64) error { func (c *Client) IssueSubscribe(owner, repo string, index int64) error {
u, err := c.GetMyUserInfo() u, err := c.GetMyUserInfo()

View file

@ -0,0 +1,46 @@
// Copyright 2020 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 gitea
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
// TestIssue is main func witch call all Tests for Issue API
// (to make sure they are on correct order)
func TestIssueSubscription(t *testing.T) {
log.Println("== TestCreateIssues ==")
c := newTestClient()
repo, _ := createTestRepo(t, "IssueWatch", c)
createTestIssue(t, c, repo.Name, "First Issue", "", nil, nil, 0, nil, false, false)
wi, err := c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
assert.NoError(t, err)
assert.True(t, wi.Subscribed)
assert.NoError(t, c.UnWatchRepo(repo.Owner.UserName, repo.Name))
wi, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
assert.NoError(t, err)
assert.True(t, wi.Subscribed)
assert.NoError(t, c.IssueSubscribe(repo.Owner.UserName, repo.Name, 1))
wi, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
assert.NoError(t, err)
assert.True(t, wi.Subscribed)
assert.NoError(t, c.IssueUnSubscribe(repo.Owner.UserName, repo.Name, 1))
wi, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
assert.NoError(t, err)
assert.False(t, wi.Subscribed)
assert.NoError(t, c.WatchRepo(repo.Owner.UserName, repo.Name))
wi, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
assert.NoError(t, err)
assert.False(t, wi.Subscribed)
}

View file

@ -31,53 +31,20 @@ func createIssue(t *testing.T, c *Client) {
assert.NoError(t, err) assert.NoError(t, err)
repo, _ := createTestRepo(t, "IssueTestsRepo", c) repo, _ := createTestRepo(t, "IssueTestsRepo", c)
createOne := func(title, body string, assignees []string, deadline *time.Time, milestone int64, labels []int64, closed, shouldFail bool) {
issue, e := c.CreateIssue(user.UserName, repo.Name, CreateIssueOption{
Title: title,
Body: body,
Assignees: assignees,
Deadline: deadline,
Milestone: milestone,
Labels: labels,
Closed: closed,
})
if shouldFail {
assert.Error(t, e)
return
}
assert.NoError(t, e)
assert.NotEmpty(t, issue)
assert.EqualValues(t, title, issue.Title)
assert.EqualValues(t, body, issue.Body)
assert.EqualValues(t, len(assignees), len(issue.Assignees))
for i, a := range issue.Assignees {
assert.EqualValues(t, assignees[i], a.UserName)
}
if milestone > 0 {
assert.EqualValues(t, milestone, issue.Milestone.ID)
}
assert.EqualValues(t, len(labels), len(issue.Labels))
if closed {
assert.False(t, issue.Closed.IsZero())
} else {
assert.Empty(t, issue.Closed)
}
}
nowTime := time.Now() nowTime := time.Now()
mile, _ := c.CreateMilestone(user.UserName, repo.Name, CreateMilestoneOption{Title: "mile1"}) mile, _ := c.CreateMilestone(user.UserName, repo.Name, CreateMilestoneOption{Title: "mile1"})
label1, _ := c.CreateLabel(user.UserName, repo.Name, CreateLabelOption{Name: "Label1", Description: "a", Color: "#ee0701"}) label1, _ := c.CreateLabel(user.UserName, repo.Name, CreateLabelOption{Name: "Label1", Description: "a", Color: "#ee0701"})
label2, _ := c.CreateLabel(user.UserName, repo.Name, CreateLabelOption{Name: "Label2", Description: "b", Color: "#128a0c"}) label2, _ := c.CreateLabel(user.UserName, repo.Name, CreateLabelOption{Name: "Label2", Description: "b", Color: "#128a0c"})
createOne("First Issue", "", nil, nil, 0, nil, false, false) createTestIssue(t, c, repo.Name, "First Issue", "", nil, nil, 0, nil, false, false)
createOne("Issue 2", "closed isn't it?", nil, nil, 0, nil, true, false) createTestIssue(t, c, repo.Name, "Issue 2", "closed isn't it?", nil, nil, 0, nil, true, false)
createOne("Issue 3", "", nil, nil, 0, nil, true, false) createTestIssue(t, c, repo.Name, "Issue 3", "", nil, nil, 0, nil, true, false)
createOne("Feature: spam protect 4", "explain explain explain", []string{user.UserName}, &nowTime, 0, nil, true, false) createTestIssue(t, c, repo.Name, "Feature: spam protect 4", "explain explain explain", []string{user.UserName}, &nowTime, 0, nil, true, false)
createOne("W 123", "", nil, &nowTime, mile.ID, nil, false, false) createTestIssue(t, c, repo.Name, "W 123", "", nil, &nowTime, mile.ID, nil, false, false)
createOne("First Issue", "", nil, nil, 0, nil, false, false) createTestIssue(t, c, repo.Name, "First Issue", "", nil, nil, 0, nil, false, false)
createOne("Do it soon!", "is important!", []string{user.UserName}, &nowTime, mile.ID, []int64{label1.ID, label2.ID}, false, false) createTestIssue(t, c, repo.Name, "Do it soon!", "is important!", []string{user.UserName}, &nowTime, mile.ID, []int64{label1.ID, label2.ID}, false, false)
createOne("Job Done", "you never know", nil, nil, mile.ID, []int64{label2.ID}, true, false) createTestIssue(t, c, repo.Name, "Job Done", "you never know", nil, nil, mile.ID, []int64{label2.ID}, true, false)
createOne("", "you never know", nil, nil, mile.ID, nil, true, true) createTestIssue(t, c, repo.Name, "", "you never know", nil, nil, mile.ID, nil, true, true)
} }
func editIssues(t *testing.T, c *Client) { func editIssues(t *testing.T, c *Client) {
@ -123,3 +90,38 @@ func listIssues(t *testing.T, c *Client) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, issues, 3) assert.Len(t, issues, 3)
} }
func createTestIssue(t *testing.T, c *Client, repoName, title, body string, assignees []string, deadline *time.Time, milestone int64, labels []int64, closed, shouldFail bool) {
user, err := c.GetMyUserInfo()
assert.NoError(t, err)
issue, e := c.CreateIssue(user.UserName, repoName, CreateIssueOption{
Title: title,
Body: body,
Assignees: assignees,
Deadline: deadline,
Milestone: milestone,
Labels: labels,
Closed: closed,
})
if shouldFail {
assert.Error(t, e)
return
}
assert.NoError(t, e)
assert.NotEmpty(t, issue)
assert.EqualValues(t, title, issue.Title)
assert.EqualValues(t, body, issue.Body)
assert.EqualValues(t, len(assignees), len(issue.Assignees))
for i, a := range issue.Assignees {
assert.EqualValues(t, assignees[i], a.UserName)
}
if milestone > 0 {
assert.EqualValues(t, milestone, issue.Milestone.ID)
}
assert.EqualValues(t, len(labels), len(issue.Labels))
if closed {
assert.False(t, issue.Closed.IsZero())
} else {
assert.Empty(t, issue.Closed)
}
}