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.

78 lines
2.3 KiB

  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. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/eventsource"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestEventSourceManagerRun(t *testing.T) {
  16. defer prepareTestEnv(t)()
  17. manager := eventsource.GetManager()
  18. eventChan := manager.Register(2)
  19. defer func() {
  20. manager.Unregister(2, eventChan)
  21. // ensure the eventChan is closed
  22. for {
  23. _, ok := <-eventChan
  24. if !ok {
  25. break
  26. }
  27. }
  28. }()
  29. expectNotificationCountEvent := func(count int64) func() bool {
  30. return func() bool {
  31. select {
  32. case event, ok := <-eventChan:
  33. if !ok {
  34. return false
  35. }
  36. data, ok := event.Data.(models.UserIDCount)
  37. if !ok {
  38. return false
  39. }
  40. return event.Name == "notification-count" && data.Count == count
  41. default:
  42. return false
  43. }
  44. }
  45. }
  46. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  47. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  48. thread5 := models.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification)
  49. assert.NoError(t, thread5.LoadAttributes())
  50. session := loginUser(t, user2.Name)
  51. token := getTokenForLoggedInUser(t, session)
  52. var apiNL []api.NotificationThread
  53. // -- mark notifications as read --
  54. req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?status-types=unread&token=%s", token))
  55. resp := session.MakeRequest(t, req, http.StatusOK)
  56. DecodeJSON(t, resp, &apiNL)
  57. assert.Len(t, apiNL, 2)
  58. lastReadAt := "2000-01-01T00%3A50%3A01%2B00%3A00" //946687801 <- only Notification 4 is in this filter ...
  59. req = NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?last_read_at=%s&token=%s", user2.Name, repo1.Name, lastReadAt, token))
  60. resp = session.MakeRequest(t, req, http.StatusResetContent)
  61. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?token=%s&status-types=unread", token))
  62. resp = session.MakeRequest(t, req, http.StatusOK)
  63. DecodeJSON(t, resp, &apiNL)
  64. assert.Len(t, apiNL, 1)
  65. assert.Eventually(t, expectNotificationCountEvent(1), 30*time.Second, 1*time.Second)
  66. }