notifications_controller_spec.rb 4.1 KB

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