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.1KB

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