gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. 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. "code.gitea.io/gitea/modules/eventsource"
  15. api "code.gitea.io/gitea/modules/structs"
  16. "code.gitea.io/gitea/tests"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestEventSourceManagerRun(t *testing.T) {
  20. defer tests.PrepareTestEnv(t)()
  21. manager := eventsource.GetManager()
  22. eventChan := manager.Register(2)
  23. defer func() {
  24. manager.Unregister(2, eventChan)
  25. // ensure the eventChan is closed
  26. for {
  27. _, ok := <-eventChan
  28. if !ok {
  29. break
  30. }
  31. }
  32. }()
  33. expectNotificationCountEvent := func(count int64) func() bool {
  34. return func() bool {
  35. select {
  36. case event, ok := <-eventChan:
  37. if !ok {
  38. return false
  39. }
  40. data, ok := event.Data.(activities_model.UserIDCount)
  41. if !ok {
  42. return false
  43. }
  44. return event.Name == "notification-count" && data.Count == count
  45. default:
  46. return false
  47. }
  48. }
  49. }
  50. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
  51. repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  52. thread5 := unittest.AssertExistsAndLoadBean(t, &activities_model.Notification{ID: 5})
  53. assert.NoError(t, thread5.LoadAttributes(t.Context()))
  54. session := loginUser(t, user2.Name)
  55. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteNotification, auth_model.AccessTokenScopeWriteRepository)
  56. var apiNL []api.NotificationThread
  57. // -- mark notifications as read --
  58. req := NewRequest(t, "GET", "/api/v1/notifications?status-types=unread").
  59. AddTokenAuth(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", user2.Name, repo1.Name, lastReadAt)).
  65. AddTokenAuth(token)
  66. session.MakeRequest(t, req, http.StatusResetContent)
  67. req = NewRequest(t, "GET", "/api/v1/notifications?status-types=unread").
  68. AddTokenAuth(token)
  69. resp = session.MakeRequest(t, req, http.StatusOK)
  70. DecodeJSON(t, resp, &apiNL)
  71. assert.Len(t, apiNL, 1)
  72. assert.Eventually(t, expectNotificationCountEvent(1), 30*time.Second, 1*time.Second)
  73. }