notifications_controller_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. require 'rails_helper'
  2. RSpec.describe Api::V1::NotificationsController, type: :controller do
  3. render_views
  4. let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:other) { Fabricate(:user, account: Fabricate(:account, username: 'bob')) }
  7. before do
  8. allow(controller).to receive(:doorkeeper_token) { token }
  9. end
  10. describe 'GET #show' do
  11. let(:scopes) { 'read:notifications' }
  12. it 'returns http success' do
  13. notification = Fabricate(:notification, account: user.account)
  14. get :show, params: { id: notification.id }
  15. expect(response).to have_http_status(200)
  16. end
  17. end
  18. describe 'POST #dismiss' do
  19. let(:scopes) { 'write:notifications' }
  20. it 'destroys the notification' do
  21. notification = Fabricate(:notification, account: user.account)
  22. post :dismiss, params: { id: notification.id }
  23. expect(response).to have_http_status(200)
  24. expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
  25. end
  26. end
  27. describe 'POST #clear' do
  28. let(:scopes) { 'write:notifications' }
  29. it 'clears notifications for the account' do
  30. notification = Fabricate(:notification, account: user.account)
  31. post :clear
  32. expect(notification.account.reload.notifications).to be_empty
  33. expect(response).to have_http_status(200)
  34. end
  35. end
  36. describe 'GET #index' do
  37. let(:scopes) { 'read:notifications' }
  38. before do
  39. first_status = PostStatusService.new.call(user.account, text: 'Test')
  40. @reblog_of_first_status = ReblogService.new.call(other.account, first_status)
  41. mentioning_status = PostStatusService.new.call(other.account, text: 'Hello @alice')
  42. @mention_from_status = mentioning_status.mentions.first
  43. @favourite = FavouriteService.new.call(other.account, first_status)
  44. @follow = FollowService.new.call(other.account, 'alice')
  45. end
  46. describe 'with no options' do
  47. before do
  48. get :index
  49. end
  50. it 'returns http success' do
  51. expect(response).to have_http_status(200)
  52. end
  53. it 'includes reblog' do
  54. expect(assigns(:notifications).map(&:activity)).to include(@reblog_of_first_status)
  55. end
  56. it 'includes mention' do
  57. expect(assigns(:notifications).map(&:activity)).to include(@mention_from_status)
  58. end
  59. it 'includes favourite' do
  60. expect(assigns(:notifications).map(&:activity)).to include(@favourite)
  61. end
  62. it 'includes follow' do
  63. expect(assigns(:notifications).map(&:activity)).to include(@follow)
  64. end
  65. end
  66. describe 'with excluded mentions' do
  67. before do
  68. get :index, params: { exclude_types: ['mention'] }
  69. end
  70. it 'returns http success' do
  71. expect(response).to have_http_status(200)
  72. end
  73. it 'includes reblog' do
  74. expect(assigns(:notifications).map(&:activity)).to include(@reblog_of_first_status)
  75. end
  76. it 'excludes mention' do
  77. expect(assigns(:notifications).map(&:activity)).to_not include(@mention_from_status)
  78. end
  79. it 'includes favourite' do
  80. expect(assigns(:notifications).map(&:activity)).to include(@favourite)
  81. end
  82. it 'includes follow' do
  83. expect(assigns(:notifications).map(&:activity)).to include(@follow)
  84. end
  85. end
  86. end
  87. end