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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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?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. // -- check notifications --
  68. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/new?token=%s", token))
  69. resp = session.MakeRequest(t, req, http.StatusOK)
  70. // -- mark notifications as read --
  71. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?token=%s", token))
  72. resp = session.MakeRequest(t, req, http.StatusOK)
  73. DecodeJSON(t, resp, &apiNL)
  74. assert.Len(t, apiNL, 2)
  75. lastReadAt := "2000-01-01T00%3A50%3A01%2B00%3A00" //946687801 <- only Notification 4 is in this filter ...
  76. req = NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?last_read_at=%s&token=%s", user2.Name, repo1.Name, lastReadAt, token))
  77. resp = session.MakeRequest(t, req, http.StatusResetContent)
  78. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?token=%s", token))
  79. resp = session.MakeRequest(t, req, http.StatusOK)
  80. DecodeJSON(t, resp, &apiNL)
  81. assert.Len(t, apiNL, 1)
  82. // -- PATCH /notifications/threads/{id} --
  83. req = NewRequest(t, "PATCH", fmt.Sprintf("/api/v1/notifications/threads/%d?token=%s", thread5.ID, token))
  84. resp = session.MakeRequest(t, req, http.StatusResetContent)
  85. assert.Equal(t, models.NotificationStatusUnread, thread5.Status)
  86. thread5 = models.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification)
  87. assert.Equal(t, models.NotificationStatusRead, thread5.Status)
  88. // -- check notifications --
  89. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/new?token=%s", token))
  90. resp = session.MakeRequest(t, req, http.StatusNoContent)
  91. }