notifications_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Notifications' do
  4. let(:user) { Fabricate(:user, account_attributes: { username: 'alice' }) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:scopes) { 'read:notifications write:notifications' }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'GET /api/v2_alpha/notifications', :sidekiq_inline do
  9. subject do
  10. get '/api/v2_alpha/notifications', headers: headers, params: params
  11. end
  12. let(:bob) { Fabricate(:user) }
  13. let(:tom) { Fabricate(:user) }
  14. let(:params) { {} }
  15. before do
  16. first_status = PostStatusService.new.call(user.account, text: 'Test')
  17. ReblogService.new.call(bob.account, first_status)
  18. mentioning_status = PostStatusService.new.call(bob.account, text: 'Hello @alice')
  19. mentioning_status.mentions.first
  20. FavouriteService.new.call(bob.account, first_status)
  21. FavouriteService.new.call(tom.account, first_status)
  22. FollowService.new.call(bob.account, user.account)
  23. end
  24. it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
  25. context 'with no options' do
  26. it 'returns expected notification types', :aggregate_failures do
  27. subject
  28. expect(response).to have_http_status(200)
  29. expect(body_json_types).to include('reblog', 'mention', 'favourite', 'follow')
  30. end
  31. end
  32. context 'with exclude_types param' do
  33. let(:params) { { exclude_types: %w(mention) } }
  34. it 'returns everything but excluded type', :aggregate_failures do
  35. subject
  36. expect(response).to have_http_status(200)
  37. expect(body_as_json.size).to_not eq 0
  38. expect(body_json_types.uniq).to_not include 'mention'
  39. end
  40. end
  41. context 'with types param' do
  42. let(:params) { { types: %w(mention) } }
  43. it 'returns only requested type', :aggregate_failures do
  44. subject
  45. expect(response).to have_http_status(200)
  46. expect(body_json_types.uniq).to eq ['mention']
  47. expect(body_as_json[0][:page_min_id]).to_not be_nil
  48. end
  49. end
  50. context 'with limit param' do
  51. let(:params) { { limit: 3 } }
  52. it 'returns the requested number of notifications paginated', :aggregate_failures do
  53. subject
  54. notifications = user.account.notifications
  55. expect(body_as_json.size)
  56. .to eq(params[:limit])
  57. expect(response)
  58. .to include_pagination_headers(
  59. prev: api_v2_alpha_notifications_url(limit: params[:limit], min_id: notifications.last.id),
  60. # TODO: one downside of the current approach is that we return the first ID matching the group,
  61. # not the last that has been skipped, so pagination is very likely to give overlap
  62. next: api_v2_alpha_notifications_url(limit: params[:limit], max_id: notifications[1].id)
  63. )
  64. end
  65. end
  66. def body_json_types
  67. body_as_json.pluck(:type)
  68. end
  69. end
  70. describe 'GET /api/v2_alpha/notifications/:id' do
  71. subject do
  72. get "/api/v2_alpha/notifications/#{notification.group_key}", headers: headers
  73. end
  74. let(:notification) { Fabricate(:notification, account: user.account, group_key: 'foobar') }
  75. it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
  76. it 'returns http success' do
  77. subject
  78. expect(response).to have_http_status(200)
  79. end
  80. context 'when notification belongs to someone else' do
  81. let(:notification) { Fabricate(:notification, group_key: 'foobar') }
  82. it 'returns http not found' do
  83. subject
  84. expect(response).to have_http_status(404)
  85. end
  86. end
  87. end
  88. describe 'POST /api/v2_alpha/notifications/:id/dismiss' do
  89. subject do
  90. post "/api/v2_alpha/notifications/#{notification.group_key}/dismiss", headers: headers
  91. end
  92. let!(:notification) { Fabricate(:notification, account: user.account, group_key: 'foobar') }
  93. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  94. it 'destroys the notification' do
  95. subject
  96. expect(response).to have_http_status(200)
  97. expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
  98. end
  99. context 'when notification belongs to someone else' do
  100. let(:notification) { Fabricate(:notification) }
  101. it 'returns http not found' do
  102. subject
  103. expect(response).to have_http_status(404)
  104. end
  105. end
  106. end
  107. describe 'POST /api/v2_alpha/notifications/clear' do
  108. subject do
  109. post '/api/v2_alpha/notifications/clear', headers: headers
  110. end
  111. before do
  112. Fabricate(:notification, account: user.account)
  113. end
  114. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  115. it 'clears notifications for the account' do
  116. subject
  117. expect(user.account.reload.notifications).to be_empty
  118. expect(response).to have_http_status(200)
  119. end
  120. end
  121. end