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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. user_model "code.gitea.io/gitea/models/user"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestAPINotification(t *testing.T) {
  16. defer prepareTestEnv(t)()
  17. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  18. repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  19. thread5 := unittest.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification)
  20. assert.NoError(t, thread5.LoadAttributes())
  21. session := loginUser(t, user2.Name)
  22. token := getTokenForLoggedInUser(t, session)
  23. // -- GET /notifications --
  24. // test filter
  25. since := "2000-01-01T00%3A50%3A01%2B00%3A00" //946687801
  26. req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?since=%s&token=%s", since, token))
  27. resp := session.MakeRequest(t, req, http.StatusOK)
  28. var apiNL []api.NotificationThread
  29. DecodeJSON(t, resp, &apiNL)
  30. assert.Len(t, apiNL, 1)
  31. assert.EqualValues(t, 5, apiNL[0].ID)
  32. // test filter
  33. before := "2000-01-01T01%3A06%3A59%2B00%3A00" //946688819
  34. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?all=%s&before=%s&token=%s", "true", before, token))
  35. resp = session.MakeRequest(t, req, http.StatusOK)
  36. DecodeJSON(t, resp, &apiNL)
  37. assert.Len(t, apiNL, 3)
  38. assert.EqualValues(t, 4, apiNL[0].ID)
  39. assert.True(t, apiNL[0].Unread)
  40. assert.False(t, apiNL[0].Pinned)
  41. assert.EqualValues(t, 3, apiNL[1].ID)
  42. assert.False(t, apiNL[1].Unread)
  43. assert.True(t, apiNL[1].Pinned)
  44. assert.EqualValues(t, 2, apiNL[2].ID)
  45. assert.False(t, apiNL[2].Unread)
  46. assert.False(t, apiNL[2].Pinned)
  47. // -- GET /repos/{owner}/{repo}/notifications --
  48. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?status-types=unread&token=%s", user2.Name, repo1.Name, token))
  49. resp = session.MakeRequest(t, req, http.StatusOK)
  50. DecodeJSON(t, resp, &apiNL)
  51. assert.Len(t, apiNL, 1)
  52. assert.EqualValues(t, 4, apiNL[0].ID)
  53. // -- GET /repos/{owner}/{repo}/notifications -- multiple status-types
  54. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?status-types=unread&status-types=pinned&token=%s", user2.Name, repo1.Name, token))
  55. resp = session.MakeRequest(t, req, http.StatusOK)
  56. DecodeJSON(t, resp, &apiNL)
  57. assert.Len(t, apiNL, 2)
  58. assert.EqualValues(t, 4, apiNL[0].ID)
  59. assert.True(t, apiNL[0].Unread)
  60. assert.False(t, apiNL[0].Pinned)
  61. assert.EqualValues(t, 3, apiNL[1].ID)
  62. assert.False(t, apiNL[1].Unread)
  63. assert.True(t, apiNL[1].Pinned)
  64. // -- GET /notifications/threads/{id} --
  65. // get forbidden
  66. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/threads/%d?token=%s", 1, token))
  67. session.MakeRequest(t, req, http.StatusForbidden)
  68. // get own
  69. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/threads/%d?token=%s", thread5.ID, token))
  70. resp = session.MakeRequest(t, req, http.StatusOK)
  71. var apiN api.NotificationThread
  72. DecodeJSON(t, resp, &apiN)
  73. assert.EqualValues(t, 5, apiN.ID)
  74. assert.False(t, apiN.Pinned)
  75. assert.True(t, apiN.Unread)
  76. assert.EqualValues(t, "issue4", apiN.Subject.Title)
  77. assert.EqualValues(t, "Issue", apiN.Subject.Type)
  78. assert.EqualValues(t, thread5.Issue.APIURL(), apiN.Subject.URL)
  79. assert.EqualValues(t, thread5.Repository.HTMLURL(), apiN.Repository.HTMLURL)
  80. new := struct {
  81. New int64 `json:"new"`
  82. }{}
  83. // -- check notifications --
  84. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/new?token=%s", token))
  85. resp = session.MakeRequest(t, req, http.StatusOK)
  86. DecodeJSON(t, resp, &new)
  87. assert.True(t, new.New > 0)
  88. // -- mark notifications as read --
  89. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?status-types=unread&token=%s", token))
  90. resp = session.MakeRequest(t, req, http.StatusOK)
  91. DecodeJSON(t, resp, &apiNL)
  92. assert.Len(t, apiNL, 2)
  93. lastReadAt := "2000-01-01T00%3A50%3A01%2B00%3A00" //946687801 <- only Notification 4 is in this filter ...
  94. req = NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?last_read_at=%s&token=%s", user2.Name, repo1.Name, lastReadAt, token))
  95. session.MakeRequest(t, req, http.StatusResetContent)
  96. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?status-types=unread&token=%s", token))
  97. resp = session.MakeRequest(t, req, http.StatusOK)
  98. DecodeJSON(t, resp, &apiNL)
  99. assert.Len(t, apiNL, 1)
  100. // -- PATCH /notifications/threads/{id} --
  101. req = NewRequest(t, "PATCH", fmt.Sprintf("/api/v1/notifications/threads/%d?token=%s", thread5.ID, token))
  102. session.MakeRequest(t, req, http.StatusResetContent)
  103. assert.Equal(t, models.NotificationStatusUnread, thread5.Status)
  104. thread5 = unittest.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification)
  105. assert.Equal(t, models.NotificationStatusRead, thread5.Status)
  106. // -- check notifications --
  107. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/new?token=%s", token))
  108. resp = session.MakeRequest(t, req, http.StatusOK)
  109. DecodeJSON(t, resp, &new)
  110. assert.True(t, new.New == 0)
  111. }
  112. func TestAPINotificationPUT(t *testing.T) {
  113. defer prepareTestEnv(t)()
  114. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  115. thread5 := unittest.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification)
  116. assert.NoError(t, thread5.LoadAttributes())
  117. session := loginUser(t, user2.Name)
  118. token := getTokenForLoggedInUser(t, session)
  119. // Check notifications are as expected
  120. req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?all=true&token=%s", token))
  121. resp := session.MakeRequest(t, req, http.StatusOK)
  122. var apiNL []api.NotificationThread
  123. DecodeJSON(t, resp, &apiNL)
  124. assert.Len(t, apiNL, 4)
  125. assert.EqualValues(t, 5, apiNL[0].ID)
  126. assert.True(t, apiNL[0].Unread)
  127. assert.False(t, apiNL[0].Pinned)
  128. assert.EqualValues(t, 4, apiNL[1].ID)
  129. assert.True(t, apiNL[1].Unread)
  130. assert.False(t, apiNL[1].Pinned)
  131. assert.EqualValues(t, 3, apiNL[2].ID)
  132. assert.False(t, apiNL[2].Unread)
  133. assert.True(t, apiNL[2].Pinned)
  134. assert.EqualValues(t, 2, apiNL[3].ID)
  135. assert.False(t, apiNL[3].Unread)
  136. assert.False(t, apiNL[3].Pinned)
  137. //
  138. // Notification ID 2 is the only one with status-type read & pinned
  139. // change it to unread.
  140. //
  141. req = NewRequest(t, "PUT", fmt.Sprintf("/api/v1/notifications?status-types=read&status-type=pinned&to-status=unread&token=%s", token))
  142. resp = session.MakeRequest(t, req, http.StatusResetContent)
  143. DecodeJSON(t, resp, &apiNL)
  144. assert.Len(t, apiNL, 1)
  145. assert.EqualValues(t, 2, apiNL[0].ID)
  146. assert.True(t, apiNL[0].Unread)
  147. assert.False(t, apiNL[0].Pinned)
  148. //
  149. // Now nofication ID 2 is the first in the list and is unread.
  150. //
  151. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?all=true&token=%s", token))
  152. resp = session.MakeRequest(t, req, http.StatusOK)
  153. DecodeJSON(t, resp, &apiNL)
  154. assert.Len(t, apiNL, 4)
  155. assert.EqualValues(t, 2, apiNL[0].ID)
  156. assert.True(t, apiNL[0].Unread)
  157. assert.False(t, apiNL[0].Pinned)
  158. }