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.

83 lines
3.1 KiB

  1. // Copyright 2019 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. "net/http"
  7. "testing"
  8. "code.gitea.io/gitea/models"
  9. api "code.gitea.io/gitea/modules/structs"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestAPIListStopWatches(t *testing.T) {
  13. defer prepareTestEnv(t)()
  14. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  15. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  16. session := loginUser(t, owner.Name)
  17. token := getTokenForLoggedInUser(t, session)
  18. req := NewRequestf(t, "GET", "/api/v1/user/stopwatches?token=%s", token)
  19. resp := session.MakeRequest(t, req, http.StatusOK)
  20. var apiWatches []*api.StopWatch
  21. DecodeJSON(t, resp, &apiWatches)
  22. expect := models.AssertExistsAndLoadBean(t, &models.Stopwatch{UserID: owner.ID}).(*models.Stopwatch)
  23. expectAPI, _ := expect.APIFormat()
  24. assert.Len(t, apiWatches, 1)
  25. assert.EqualValues(t, expectAPI.IssueIndex, apiWatches[0].IssueIndex)
  26. assert.EqualValues(t, expectAPI.Created.Unix(), apiWatches[0].Created.Unix())
  27. }
  28. func TestAPIStopStopWatches(t *testing.T) {
  29. defer prepareTestEnv(t)()
  30. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
  31. _ = issue.LoadRepo()
  32. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User)
  33. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  34. session := loginUser(t, user.Name)
  35. token := getTokenForLoggedInUser(t, session)
  36. req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/issues/%d/stopwatch/stop?token=%s", owner.Name, issue.Repo.Name, issue.Index, token)
  37. session.MakeRequest(t, req, http.StatusCreated)
  38. session.MakeRequest(t, req, http.StatusConflict)
  39. }
  40. func TestAPICancelStopWatches(t *testing.T) {
  41. defer prepareTestEnv(t)()
  42. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue)
  43. _ = issue.LoadRepo()
  44. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User)
  45. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  46. session := loginUser(t, user.Name)
  47. token := getTokenForLoggedInUser(t, session)
  48. req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/stopwatch/delete?token=%s", owner.Name, issue.Repo.Name, issue.Index, token)
  49. session.MakeRequest(t, req, http.StatusNoContent)
  50. session.MakeRequest(t, req, http.StatusConflict)
  51. }
  52. func TestAPIStartStopWatches(t *testing.T) {
  53. defer prepareTestEnv(t)()
  54. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue)
  55. _ = issue.LoadRepo()
  56. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User)
  57. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  58. session := loginUser(t, user.Name)
  59. token := getTokenForLoggedInUser(t, session)
  60. req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/issues/%d/stopwatch/start?token=%s", owner.Name, issue.Repo.Name, issue.Index, token)
  61. session.MakeRequest(t, req, http.StatusCreated)
  62. session.MakeRequest(t, req, http.StatusConflict)
  63. }