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

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