1
0

notifications_controller_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Api::V1::NotificationsController do
  4. render_views
  5. let(:user) { Fabricate(:user, account_attributes: { username: 'alice' }) }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. let(:other) { Fabricate(:user) }
  8. let(:third) { Fabricate(:user) }
  9. before do
  10. allow(controller).to receive(:doorkeeper_token) { token }
  11. end
  12. describe 'GET #show' do
  13. let(:scopes) { 'read:notifications' }
  14. it 'returns http success' do
  15. notification = Fabricate(:notification, account: user.account)
  16. get :show, params: { id: notification.id }
  17. expect(response).to have_http_status(200)
  18. end
  19. end
  20. describe 'POST #dismiss' do
  21. let(:scopes) { 'write:notifications' }
  22. it 'destroys the notification' do
  23. notification = Fabricate(:notification, account: user.account)
  24. post :dismiss, params: { id: notification.id }
  25. expect(response).to have_http_status(200)
  26. expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
  27. end
  28. end
  29. describe 'POST #clear' do
  30. let(:scopes) { 'write:notifications' }
  31. it 'clears notifications for the account' do
  32. notification = Fabricate(:notification, account: user.account)
  33. post :clear
  34. expect(notification.account.reload.notifications).to be_empty
  35. expect(response).to have_http_status(200)
  36. end
  37. end
  38. describe 'GET #index' do
  39. let(:scopes) { 'read:notifications' }
  40. before do
  41. first_status = PostStatusService.new.call(user.account, text: 'Test')
  42. @reblog_of_first_status = ReblogService.new.call(other.account, first_status)
  43. mentioning_status = PostStatusService.new.call(other.account, text: 'Hello @alice')
  44. @mention_from_status = mentioning_status.mentions.first
  45. @favourite = FavouriteService.new.call(other.account, first_status)
  46. @second_favourite = FavouriteService.new.call(third.account, first_status)
  47. @follow = FollowService.new.call(other.account, user.account)
  48. end
  49. describe 'with no options' do
  50. before do
  51. get :index
  52. end
  53. it 'returns expected notification types', :aggregate_failures do
  54. expect(response).to have_http_status(200)
  55. expect(body_json_types).to include 'reblog'
  56. expect(body_json_types).to include 'mention'
  57. expect(body_json_types).to include 'favourite'
  58. expect(body_json_types).to include 'follow'
  59. end
  60. end
  61. describe 'with account_id param' do
  62. before do
  63. get :index, params: { account_id: third.account.id }
  64. end
  65. it 'returns only notifications from specified user', :aggregate_failures do
  66. expect(response).to have_http_status(200)
  67. expect(body_json_account_ids.uniq).to eq [third.account.id.to_s]
  68. end
  69. def body_json_account_ids
  70. body_as_json.map { |x| x[:account][:id] }
  71. end
  72. end
  73. describe 'with invalid account_id param' do
  74. before do
  75. get :index, params: { account_id: 'foo' }
  76. end
  77. it 'returns nothing', :aggregate_failures do
  78. expect(response).to have_http_status(200)
  79. expect(body_as_json.size).to eq 0
  80. end
  81. end
  82. describe 'with exclude_types param' do
  83. before do
  84. get :index, params: { exclude_types: %w(mention) }
  85. end
  86. it 'returns everything but excluded type', :aggregate_failures do
  87. expect(response).to have_http_status(200)
  88. expect(body_as_json.size).to_not eq 0
  89. expect(body_json_types.uniq).to_not include 'mention'
  90. end
  91. end
  92. describe 'with types param' do
  93. before do
  94. get :index, params: { types: %w(mention) }
  95. end
  96. it 'returns only requested type', :aggregate_failures do
  97. expect(response).to have_http_status(200)
  98. expect(body_json_types.uniq).to eq ['mention']
  99. end
  100. end
  101. def body_json_types
  102. body_as_json.pluck(:type)
  103. end
  104. end
  105. end