notifications_spec.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/v1/notifications', :sidekiq_inline do
  9. subject do
  10. get '/api/v1/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'
  30. expect(body_json_types).to include 'mention'
  31. expect(body_json_types).to include 'favourite'
  32. expect(body_json_types).to include 'follow'
  33. end
  34. end
  35. context 'with account_id param' do
  36. let(:params) { { account_id: tom.account.id } }
  37. it 'returns only notifications from specified user', :aggregate_failures do
  38. subject
  39. expect(response).to have_http_status(200)
  40. expect(body_json_account_ids.uniq).to eq [tom.account.id.to_s]
  41. end
  42. def body_json_account_ids
  43. body_as_json.map { |x| x[:account][:id] }
  44. end
  45. end
  46. context 'with invalid account_id param' do
  47. let(:params) { { account_id: 'foo' } }
  48. it 'returns nothing', :aggregate_failures do
  49. subject
  50. expect(response).to have_http_status(200)
  51. expect(body_as_json.size).to eq 0
  52. end
  53. end
  54. context 'with exclude_types param' do
  55. let(:params) { { exclude_types: %w(mention) } }
  56. it 'returns everything but excluded type', :aggregate_failures do
  57. subject
  58. expect(response).to have_http_status(200)
  59. expect(body_as_json.size).to_not eq 0
  60. expect(body_json_types.uniq).to_not include 'mention'
  61. end
  62. end
  63. context 'with types param' do
  64. let(:params) { { types: %w(mention) } }
  65. it 'returns only requested type', :aggregate_failures do
  66. subject
  67. expect(response).to have_http_status(200)
  68. expect(body_json_types.uniq).to eq ['mention']
  69. end
  70. end
  71. context 'with limit param' do
  72. let(:params) { { limit: 3 } }
  73. it 'returns the requested number of notifications paginated', :aggregate_failures do
  74. subject
  75. notifications = user.account.notifications
  76. expect(body_as_json.size)
  77. .to eq(params[:limit])
  78. expect(response)
  79. .to include_pagination_headers(
  80. prev: api_v1_notifications_url(limit: params[:limit], min_id: notifications.last.id),
  81. next: api_v1_notifications_url(limit: params[:limit], max_id: notifications[2].id)
  82. )
  83. end
  84. end
  85. def body_json_types
  86. body_as_json.pluck(:type)
  87. end
  88. end
  89. describe 'GET /api/v1/notifications/:id' do
  90. subject do
  91. get "/api/v1/notifications/#{notification.id}", headers: headers
  92. end
  93. let(:notification) { Fabricate(:notification, account: user.account) }
  94. it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
  95. it 'returns http success' do
  96. subject
  97. expect(response).to have_http_status(200)
  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/v1/notifications/:id/dismiss' do
  108. subject do
  109. post "/api/v1/notifications/#{notification.id}/dismiss", headers: headers
  110. end
  111. let!(:notification) { Fabricate(:notification, account: user.account) }
  112. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  113. it 'destroys the notification' do
  114. subject
  115. expect(response).to have_http_status(200)
  116. expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
  117. end
  118. context 'when notification belongs to someone else' do
  119. let(:notification) { Fabricate(:notification) }
  120. it 'returns http not found' do
  121. subject
  122. expect(response).to have_http_status(404)
  123. end
  124. end
  125. end
  126. describe 'POST /api/v1/notifications/clear' do
  127. subject do
  128. post '/api/v1/notifications/clear', headers: headers
  129. end
  130. before do
  131. Fabricate(:notification, account: user.account)
  132. end
  133. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  134. it 'clears notifications for the account' do
  135. subject
  136. expect(user.account.reload.notifications).to be_empty
  137. expect(response).to have_http_status(200)
  138. end
  139. end
  140. end