You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.4 KiB

  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. let(:third) { Fabricate(:user, account: Fabricate(:account, username: 'carol')) }
  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, 'alice')
  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(assigns(:notifications).map(&:activity)).to include(@reblog_of_first_status)
  57. end
  58. it 'includes mention' do
  59. expect(assigns(:notifications).map(&:activity)).to include(@mention_from_status)
  60. end
  61. it 'includes favourite' do
  62. expect(assigns(:notifications).map(&:activity)).to include(@favourite)
  63. end
  64. it 'includes follow' do
  65. expect(assigns(:notifications).map(&:activity)).to include(@follow)
  66. end
  67. end
  68. describe 'from specified user' 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 'includes favourite' do
  76. expect(assigns(:notifications).map(&:activity)).to include(@second_favourite)
  77. end
  78. it 'excludes favourite' do
  79. expect(assigns(:notifications).map(&:activity)).to_not include(@favourite)
  80. end
  81. it 'excludes mention' do
  82. expect(assigns(:notifications).map(&:activity)).to_not include(@mention_from_status)
  83. end
  84. it 'excludes reblog' do
  85. expect(assigns(:notifications).map(&:activity)).to_not include(@reblog_of_first_status)
  86. end
  87. it 'excludes follow' do
  88. expect(assigns(:notifications).map(&:activity)).to_not include(@follow)
  89. end
  90. end
  91. describe 'from nonexistent user' do
  92. before do
  93. get :index, params: { account_id: 'foo' }
  94. end
  95. it 'returns http success' do
  96. expect(response).to have_http_status(200)
  97. end
  98. it 'excludes favourite' do
  99. expect(assigns(:notifications).map(&:activity)).to_not include(@favourite)
  100. end
  101. it 'excludes second favourite' do
  102. expect(assigns(:notifications).map(&:activity)).to_not include(@second_favourite)
  103. end
  104. it 'excludes mention' do
  105. expect(assigns(:notifications).map(&:activity)).to_not include(@mention_from_status)
  106. end
  107. it 'excludes reblog' do
  108. expect(assigns(:notifications).map(&:activity)).to_not include(@reblog_of_first_status)
  109. end
  110. it 'excludes follow' do
  111. expect(assigns(:notifications).map(&:activity)).to_not include(@follow)
  112. end
  113. end
  114. describe 'with excluded mentions' do
  115. before do
  116. get :index, params: { exclude_types: ['mention'] }
  117. end
  118. it 'returns http success' do
  119. expect(response).to have_http_status(200)
  120. end
  121. it 'includes reblog' do
  122. expect(assigns(:notifications).map(&:activity)).to include(@reblog_of_first_status)
  123. end
  124. it 'excludes mention' do
  125. expect(assigns(:notifications).map(&:activity)).to_not include(@mention_from_status)
  126. end
  127. it 'includes favourite' do
  128. expect(assigns(:notifications).map(&:activity)).to include(@favourite)
  129. end
  130. it 'includes third favourite' do
  131. expect(assigns(:notifications).map(&:activity)).to include(@second_favourite)
  132. end
  133. it 'includes follow' do
  134. expect(assigns(:notifications).map(&:activity)).to include(@follow)
  135. end
  136. end
  137. end
  138. end