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.

121 lines
4.7 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. "fmt"
  7. "net/http"
  8. "testing"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestAPIGetTrackedTimes(t *testing.T) {
  15. defer prepareTestEnv(t)()
  16. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  17. issue2 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
  18. assert.NoError(t, issue2.LoadRepo())
  19. session := loginUser(t, user2.Name)
  20. token := getTokenForLoggedInUser(t, session)
  21. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token)
  22. resp := session.MakeRequest(t, req, http.StatusOK)
  23. var apiTimes api.TrackedTimeList
  24. DecodeJSON(t, resp, &apiTimes)
  25. expect, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{IssueID: issue2.ID})
  26. assert.NoError(t, err)
  27. assert.Len(t, apiTimes, 3)
  28. for i, time := range expect {
  29. assert.Equal(t, time.ID, apiTimes[i].ID)
  30. assert.EqualValues(t, issue2.Title, apiTimes[i].Issue.Title)
  31. assert.EqualValues(t, issue2.ID, apiTimes[i].IssueID)
  32. assert.Equal(t, time.Created.Unix(), apiTimes[i].Created.Unix())
  33. assert.Equal(t, time.Time, apiTimes[i].Time)
  34. user, err := models.GetUserByID(time.UserID)
  35. assert.NoError(t, err)
  36. assert.Equal(t, user.Name, apiTimes[i].UserName)
  37. }
  38. // test filter
  39. since := "2000-01-01T00%3A00%3A02%2B00%3A00" //946684802
  40. before := "2000-01-01T00%3A00%3A12%2B00%3A00" //946684812
  41. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/times?since=%s&before=%s&token=%s", user2.Name, issue2.Repo.Name, issue2.Index, since, before, token)
  42. resp = session.MakeRequest(t, req, http.StatusOK)
  43. var filterAPITimes api.TrackedTimeList
  44. DecodeJSON(t, resp, &filterAPITimes)
  45. assert.Len(t, filterAPITimes, 2)
  46. assert.Equal(t, int64(3), filterAPITimes[0].ID)
  47. assert.Equal(t, int64(6), filterAPITimes[1].ID)
  48. }
  49. func TestAPIDeleteTrackedTime(t *testing.T) {
  50. defer prepareTestEnv(t)()
  51. time6 := models.AssertExistsAndLoadBean(t, &models.TrackedTime{ID: 6}).(*models.TrackedTime)
  52. issue2 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
  53. assert.NoError(t, issue2.LoadRepo())
  54. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  55. session := loginUser(t, user2.Name)
  56. token := getTokenForLoggedInUser(t, session)
  57. //Deletion not allowed
  58. req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times/%d?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, time6.ID, token)
  59. session.MakeRequest(t, req, http.StatusForbidden)
  60. /* Delete own time <-- ToDo: timout without reason
  61. time3 := models.AssertExistsAndLoadBean(t, &models.TrackedTime{ID: 3}).(*models.TrackedTime)
  62. req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times/%d?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, time3.ID, token)
  63. session.MakeRequest(t, req, http.StatusNoContent)
  64. //Delete non existing time
  65. session.MakeRequest(t, req, http.StatusInternalServerError) */
  66. //Reset time of user 2 on issue 2
  67. trackedSeconds, err := models.GetTrackedSeconds(models.FindTrackedTimesOptions{IssueID: 2, UserID: 2})
  68. assert.NoError(t, err)
  69. assert.Equal(t, int64(3662), trackedSeconds)
  70. req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token)
  71. session.MakeRequest(t, req, http.StatusNoContent)
  72. session.MakeRequest(t, req, http.StatusNotFound)
  73. trackedSeconds, err = models.GetTrackedSeconds(models.FindTrackedTimesOptions{IssueID: 2, UserID: 2})
  74. assert.NoError(t, err)
  75. assert.Equal(t, int64(0), trackedSeconds)
  76. }
  77. func TestAPIAddTrackedTimes(t *testing.T) {
  78. defer prepareTestEnv(t)()
  79. issue2 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
  80. assert.NoError(t, issue2.LoadRepo())
  81. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  82. admin := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  83. session := loginUser(t, admin.Name)
  84. token := getTokenForLoggedInUser(t, session)
  85. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token)
  86. req := NewRequestWithJSON(t, "POST", urlStr, &api.AddTimeOption{
  87. Time: 33,
  88. User: user2.Name,
  89. Created: time.Unix(947688818, 0),
  90. })
  91. resp := session.MakeRequest(t, req, http.StatusOK)
  92. var apiNewTime api.TrackedTime
  93. DecodeJSON(t, resp, &apiNewTime)
  94. assert.EqualValues(t, 33, apiNewTime.Time)
  95. assert.EqualValues(t, user2.ID, apiNewTime.UserID)
  96. assert.EqualValues(t, 947688818, apiNewTime.Created.Unix())
  97. }