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.

122 lines
4.6 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 TestAPINotification(t *testing.T) {
  14. defer prepareTestEnv(t)()
  15. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  16. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  17. thread5 := models.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification)
  18. assert.NoError(t, thread5.LoadAttributes())
  19. session := loginUser(t, user2.Name)
  20. token := getTokenForLoggedInUser(t, session)
  21. // -- GET /notifications --
  22. // test filter
  23. since := "2000-01-01T00%3A50%3A01%2B00%3A00" //946687801
  24. req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?since=%s&token=%s", since, token))
  25. resp := session.MakeRequest(t, req, http.StatusOK)
  26. var apiNL []api.NotificationThread
  27. DecodeJSON(t, resp, &apiNL)
  28. assert.Len(t, apiNL, 1)
  29. assert.EqualValues(t, 5, apiNL[0].ID)
  30. // test filter
  31. before := "2000-01-01T01%3A06%3A59%2B00%3A00" //946688819
  32. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?all=%s&before=%s&token=%s", "true", before, token))
  33. resp = session.MakeRequest(t, req, http.StatusOK)
  34. DecodeJSON(t, resp, &apiNL)
  35. assert.Len(t, apiNL, 3)
  36. assert.EqualValues(t, 4, apiNL[0].ID)
  37. assert.EqualValues(t, true, apiNL[0].Unread)
  38. assert.EqualValues(t, false, apiNL[0].Pinned)
  39. assert.EqualValues(t, 3, apiNL[1].ID)
  40. assert.EqualValues(t, false, apiNL[1].Unread)
  41. assert.EqualValues(t, true, apiNL[1].Pinned)
  42. assert.EqualValues(t, 2, apiNL[2].ID)
  43. assert.EqualValues(t, false, apiNL[2].Unread)
  44. assert.EqualValues(t, false, apiNL[2].Pinned)
  45. // -- GET /repos/{owner}/{repo}/notifications --
  46. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?status-types=unread&token=%s", user2.Name, repo1.Name, token))
  47. resp = session.MakeRequest(t, req, http.StatusOK)
  48. DecodeJSON(t, resp, &apiNL)
  49. assert.Len(t, apiNL, 1)
  50. assert.EqualValues(t, 4, apiNL[0].ID)
  51. // -- GET /notifications/threads/{id} --
  52. // get forbidden
  53. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/threads/%d?token=%s", 1, token))
  54. resp = session.MakeRequest(t, req, http.StatusForbidden)
  55. // get own
  56. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/threads/%d?token=%s", thread5.ID, token))
  57. resp = session.MakeRequest(t, req, http.StatusOK)
  58. var apiN api.NotificationThread
  59. DecodeJSON(t, resp, &apiN)
  60. assert.EqualValues(t, 5, apiN.ID)
  61. assert.EqualValues(t, false, apiN.Pinned)
  62. assert.EqualValues(t, true, apiN.Unread)
  63. assert.EqualValues(t, "issue4", apiN.Subject.Title)
  64. assert.EqualValues(t, "Issue", apiN.Subject.Type)
  65. assert.EqualValues(t, thread5.Issue.APIURL(), apiN.Subject.URL)
  66. assert.EqualValues(t, thread5.Repository.HTMLURL(), apiN.Repository.HTMLURL)
  67. new := struct {
  68. New int64 `json:"new"`
  69. }{}
  70. // -- check notifications --
  71. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/new?token=%s", token))
  72. resp = session.MakeRequest(t, req, http.StatusOK)
  73. DecodeJSON(t, resp, &new)
  74. assert.True(t, new.New > 0)
  75. // -- mark notifications as read --
  76. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?status-types=unread&token=%s", token))
  77. resp = session.MakeRequest(t, req, http.StatusOK)
  78. DecodeJSON(t, resp, &apiNL)
  79. assert.Len(t, apiNL, 2)
  80. lastReadAt := "2000-01-01T00%3A50%3A01%2B00%3A00" //946687801 <- only Notification 4 is in this filter ...
  81. req = NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?last_read_at=%s&token=%s", user2.Name, repo1.Name, lastReadAt, token))
  82. resp = session.MakeRequest(t, req, http.StatusResetContent)
  83. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?status-types=unread&token=%s", token))
  84. resp = session.MakeRequest(t, req, http.StatusOK)
  85. DecodeJSON(t, resp, &apiNL)
  86. assert.Len(t, apiNL, 1)
  87. // -- PATCH /notifications/threads/{id} --
  88. req = NewRequest(t, "PATCH", fmt.Sprintf("/api/v1/notifications/threads/%d?token=%s", thread5.ID, token))
  89. resp = session.MakeRequest(t, req, http.StatusResetContent)
  90. assert.Equal(t, models.NotificationStatusUnread, thread5.Status)
  91. thread5 = models.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification)
  92. assert.Equal(t, models.NotificationStatusRead, thread5.Status)
  93. // -- check notifications --
  94. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/new?token=%s", token))
  95. resp = session.MakeRequest(t, req, http.StatusOK)
  96. DecodeJSON(t, resp, &new)
  97. assert.True(t, new.New == 0)
  98. }