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.

eventsource_test.go 2.6KB

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