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.

api_notification_test.go 4.6KB

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