|
| 1 | +// Copyright 2020 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 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/models" |
| 13 | + api "code.gitea.io/gitea/modules/structs" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +func TestAPIIssueSubscriptions(t *testing.T) { |
| 19 | + defer prepareTestEnv(t)() |
| 20 | + |
| 21 | + issue1 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue) |
| 22 | + issue2 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) |
| 23 | + issue3 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue) |
| 24 | + issue4 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 4}).(*models.Issue) |
| 25 | + issue5 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 8}).(*models.Issue) |
| 26 | + |
| 27 | + owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue1.PosterID}).(*models.User) |
| 28 | + |
| 29 | + session := loginUser(t, owner.Name) |
| 30 | + token := getTokenForLoggedInUser(t, session) |
| 31 | + |
| 32 | + testSubscription := func(issue *models.Issue, user string, isWatching bool) { |
| 33 | + |
| 34 | + issueRepo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) |
| 35 | + |
| 36 | + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/subscriptions/%s?token=%s", issueRepo.OwnerName, issueRepo.Name, issue.Index, user, token) |
| 37 | + req := NewRequest(t, "GET", urlStr) |
| 38 | + resp := session.MakeRequest(t, req, http.StatusOK) |
| 39 | + wi := new(api.WatchInfo) |
| 40 | + DecodeJSON(t, resp, wi) |
| 41 | + |
| 42 | + assert.EqualValues(t, isWatching, wi.Subscribed) |
| 43 | + assert.EqualValues(t, !isWatching, wi.Ignored) |
| 44 | + assert.EqualValues(t, issue.APIURL()+"/subscriptions", wi.URL) |
| 45 | + assert.EqualValues(t, issue.CreatedUnix, wi.CreatedAt.Unix()) |
| 46 | + assert.EqualValues(t, issueRepo.APIURL(), wi.RepositoryURL) |
| 47 | + } |
| 48 | + |
| 49 | + testSubscription(issue1, "user3", false) |
| 50 | + testSubscription(issue2, owner.Name, true) |
| 51 | + testSubscription(issue3, owner.Name, true) |
| 52 | + testSubscription(issue4, owner.Name, false) |
| 53 | + testSubscription(issue5, owner.Name, false) |
| 54 | + |
| 55 | + issue1Repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issue1.RepoID}).(*models.Repository) |
| 56 | + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/subscriptions/%s?token=%s", issue1Repo.OwnerName, issue1Repo.Name, issue1.Index, owner.Name, token) |
| 57 | + req := NewRequest(t, "DELETE", urlStr) |
| 58 | + session.MakeRequest(t, req, http.StatusCreated) |
| 59 | + testSubscription(issue1, owner.Name, false) |
| 60 | + |
| 61 | + issue5Repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issue5.RepoID}).(*models.Repository) |
| 62 | + urlStr = fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/subscriptions/%s?token=%s", issue5Repo.OwnerName, issue5Repo.Name, issue5.Index, owner.Name, token) |
| 63 | + req = NewRequest(t, "PUT", urlStr) |
| 64 | + session.MakeRequest(t, req, http.StatusCreated) |
| 65 | + testSubscription(issue5, owner.Name, true) |
| 66 | +} |
0 commit comments