You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.9 KiB

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