notifications_controller_spec.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Api::V1::NotificationsController, type: :controller 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 http success' do
  54. expect(response).to have_http_status(200)
  55. end
  56. it 'includes reblog' do
  57. expect(body_as_json.pluck(:type)).to include 'reblog'
  58. end
  59. it 'includes mention' do
  60. expect(body_as_json.pluck(:type)).to include 'mention'
  61. end
  62. it 'includes favourite' do
  63. expect(body_as_json.pluck(:type)).to include 'favourite'
  64. end
  65. it 'includes follow' do
  66. expect(body_as_json.pluck(:type)).to include 'follow'
  67. end
  68. end
  69. describe 'with account_id param' do
  70. before do
  71. get :index, params: { account_id: third.account.id }
  72. end
  73. it 'returns http success' do
  74. expect(response).to have_http_status(200)
  75. end
  76. it 'returns only notifications from specified user' do
  77. expect(body_as_json.map { |x| x[:account][:id] }.uniq).to eq [third.account.id.to_s]
  78. end
  79. end
  80. describe 'with invalid account_id param' do
  81. before do
  82. get :index, params: { account_id: 'foo' }
  83. end
  84. it 'returns http success' do
  85. expect(response).to have_http_status(200)
  86. end
  87. it 'returns nothing' do
  88. expect(body_as_json.size).to eq 0
  89. end
  90. end
  91. describe 'with excluded_types param' do
  92. before do
  93. get :index, params: { exclude_types: %w(mention) }
  94. end
  95. it 'returns http success' do
  96. expect(response).to have_http_status(200)
  97. end
  98. it 'returns everything but excluded type' do
  99. expect(body_as_json.size).to_not eq 0
  100. expect(body_as_json.pluck(:type).uniq).to_not include 'mention'
  101. end
  102. end
  103. describe 'with types param' do
  104. before do
  105. get :index, params: { types: %w(mention) }
  106. end
  107. it 'returns http success' do
  108. expect(response).to have_http_status(200)
  109. end
  110. it 'returns only requested type' do
  111. expect(body_as_json.pluck(:type).uniq).to eq ['mention']
  112. end
  113. end
  114. end
  115. end