1
0

notifications_spec.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Notifications' do
  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(:scopes) { 'read:notifications write:notifications' }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'GET /api/v2/notifications/unread_count', :inline_jobs do
  9. subject do
  10. get '/api/v2/notifications/unread_count', headers: headers, params: params
  11. end
  12. let(:params) { {} }
  13. before do
  14. first_status = PostStatusService.new.call(user.account, text: 'Test')
  15. ReblogService.new.call(Fabricate(:account), first_status)
  16. PostStatusService.new.call(Fabricate(:account), text: 'Hello @alice')
  17. FavouriteService.new.call(Fabricate(:account), first_status)
  18. FavouriteService.new.call(Fabricate(:account), first_status)
  19. FollowService.new.call(Fabricate(:account), user.account)
  20. end
  21. it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
  22. context 'with no options' do
  23. it 'returns expected notifications count' do
  24. subject
  25. expect(response).to have_http_status(200)
  26. expect(response.content_type)
  27. .to start_with('application/json')
  28. expect(response.parsed_body[:count]).to eq 4
  29. end
  30. end
  31. context 'with grouped_types parameter' do
  32. let(:params) { { grouped_types: %w(reblog) } }
  33. it 'returns expected notifications count' do
  34. subject
  35. expect(response).to have_http_status(200)
  36. expect(response.content_type)
  37. .to start_with('application/json')
  38. expect(response.parsed_body[:count]).to eq 5
  39. end
  40. end
  41. context 'with a read marker' do
  42. before do
  43. id = user.account.notifications.browserable.order(id: :desc).offset(2).first.id
  44. user.markers.create!(timeline: 'notifications', last_read_id: id)
  45. end
  46. it 'returns expected notifications count' do
  47. subject
  48. expect(response).to have_http_status(200)
  49. expect(response.content_type)
  50. .to start_with('application/json')
  51. expect(response.parsed_body[:count]).to eq 2
  52. end
  53. end
  54. context 'with exclude_types param' do
  55. let(:params) { { exclude_types: %w(mention) } }
  56. it 'returns expected notifications count' do
  57. subject
  58. expect(response).to have_http_status(200)
  59. expect(response.content_type)
  60. .to start_with('application/json')
  61. expect(response.parsed_body[:count]).to eq 3
  62. end
  63. end
  64. context 'with a user-provided limit' do
  65. let(:params) { { limit: 2 } }
  66. it 'returns a capped value' do
  67. subject
  68. expect(response).to have_http_status(200)
  69. expect(response.content_type)
  70. .to start_with('application/json')
  71. expect(response.parsed_body[:count]).to eq 2
  72. end
  73. end
  74. context 'when there are more notifications than the limit' do
  75. before do
  76. stub_const('Api::V2::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT', 2)
  77. end
  78. it 'returns a capped value' do
  79. subject
  80. expect(response).to have_http_status(200)
  81. expect(response.content_type)
  82. .to start_with('application/json')
  83. expect(response.parsed_body[:count]).to eq Api::V2::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT
  84. end
  85. end
  86. end
  87. describe 'GET /api/v2/notifications', :inline_jobs do
  88. subject do
  89. get '/api/v2/notifications', headers: headers, params: params
  90. end
  91. let(:bob) { Fabricate(:user) }
  92. let(:tom) { Fabricate(:user) }
  93. let(:params) { {} }
  94. before do
  95. first_status = PostStatusService.new.call(user.account, text: 'Test')
  96. ReblogService.new.call(bob.account, first_status)
  97. PostStatusService.new.call(bob.account, text: 'Hello @alice')
  98. FavouriteService.new.call(bob.account, first_status)
  99. FavouriteService.new.call(tom.account, first_status)
  100. FollowService.new.call(bob.account, user.account)
  101. end
  102. it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
  103. context 'when there are no notifications' do
  104. before do
  105. user.account.notifications.destroy_all
  106. end
  107. it 'returns 0 notifications' do
  108. subject
  109. expect(response).to have_http_status(200)
  110. expect(response.content_type)
  111. .to start_with('application/json')
  112. expect(response.parsed_body[:notification_groups]).to eq []
  113. end
  114. end
  115. context 'with no options' do
  116. it 'returns expected notification types', :aggregate_failures do
  117. subject
  118. expect(response).to have_http_status(200)
  119. expect(response.content_type)
  120. .to start_with('application/json')
  121. expect(body_json_types).to include('reblog', 'mention', 'favourite', 'follow')
  122. end
  123. end
  124. context 'with grouped_types param' do
  125. let(:params) { { grouped_types: %w(reblog) } }
  126. it 'returns everything, but does not group favourites' do
  127. subject
  128. expect(response).to have_http_status(200)
  129. expect(response.content_type)
  130. .to start_with('application/json')
  131. expect(response.parsed_body[:notification_groups]).to contain_exactly(
  132. a_hash_including(
  133. type: 'reblog',
  134. sample_account_ids: [bob.account_id.to_s]
  135. ),
  136. a_hash_including(
  137. type: 'mention',
  138. sample_account_ids: [bob.account_id.to_s]
  139. ),
  140. a_hash_including(
  141. type: 'favourite',
  142. sample_account_ids: [bob.account_id.to_s]
  143. ),
  144. a_hash_including(
  145. type: 'favourite',
  146. sample_account_ids: [tom.account_id.to_s]
  147. ),
  148. a_hash_including(
  149. type: 'follow',
  150. sample_account_ids: [bob.account_id.to_s]
  151. )
  152. )
  153. end
  154. end
  155. context 'with exclude_types param' do
  156. let(:params) { { exclude_types: %w(mention) } }
  157. it 'returns everything but excluded type', :aggregate_failures do
  158. subject
  159. expect(response).to have_http_status(200)
  160. expect(response.content_type)
  161. .to start_with('application/json')
  162. expect(response.parsed_body.size).to_not eq 0
  163. expect(body_json_types.uniq).to_not include 'mention'
  164. end
  165. end
  166. context 'with types param' do
  167. let(:params) { { types: %w(mention) } }
  168. it 'returns only requested type', :aggregate_failures do
  169. subject
  170. expect(response).to have_http_status(200)
  171. expect(response.content_type)
  172. .to start_with('application/json')
  173. expect(body_json_types.uniq).to eq ['mention']
  174. expect(response.parsed_body.dig(:notification_groups, 0, :page_min_id)).to_not be_nil
  175. end
  176. end
  177. context 'with limit param' do
  178. let(:params) { { limit: 3 } }
  179. let(:notifications) { user.account.notifications.reorder(id: :desc) }
  180. it 'returns the requested number of notifications paginated', :aggregate_failures do
  181. subject
  182. expect(response.parsed_body[:notification_groups].size)
  183. .to eq(params[:limit])
  184. expect(response)
  185. .to include_pagination_headers(
  186. prev: api_v2_notifications_url(limit: params[:limit], min_id: notifications.first.id),
  187. # TODO: one downside of the current approach is that we return the first ID matching the group,
  188. # not the last that has been skipped, so pagination is very likely to give overlap
  189. next: api_v2_notifications_url(limit: params[:limit], max_id: notifications[3].id)
  190. )
  191. expect(response.content_type)
  192. .to start_with('application/json')
  193. end
  194. end
  195. context 'with since_id param' do
  196. let(:params) { { since_id: notifications[2].id } }
  197. let(:notifications) { user.account.notifications.reorder(id: :desc) }
  198. it 'returns the requested number of notifications paginated', :aggregate_failures do
  199. subject
  200. expect(response.parsed_body[:notification_groups].size)
  201. .to eq(2)
  202. expect(response.content_type)
  203. .to start_with('application/json')
  204. expect(response)
  205. .to include_pagination_headers(
  206. prev: api_v2_notifications_url(limit: params[:limit], min_id: notifications.first.id),
  207. # TODO: one downside of the current approach is that we return the first ID matching the group,
  208. # not the last that has been skipped, so pagination is very likely to give overlap
  209. next: api_v2_notifications_url(limit: params[:limit], max_id: notifications[1].id)
  210. )
  211. end
  212. end
  213. context 'when requesting stripped-down accounts' do
  214. let(:params) { { expand_accounts: 'partial_avatars' } }
  215. let(:recent_account) { Fabricate(:account) }
  216. before do
  217. FavouriteService.new.call(recent_account, user.account.statuses.first)
  218. end
  219. it 'returns an account in "partial_accounts", with the expected keys', :aggregate_failures do
  220. subject
  221. expect(response).to have_http_status(200)
  222. expect(response.content_type)
  223. .to start_with('application/json')
  224. expect(response.parsed_body[:partial_accounts].size).to be > 0
  225. expect(response.parsed_body[:partial_accounts][0].keys.map(&:to_sym)).to contain_exactly(:acct, :avatar, :avatar_static, :bot, :id, :locked, :url)
  226. expect(response.parsed_body[:partial_accounts].pluck(:id)).to_not include(recent_account.id.to_s)
  227. expect(response.parsed_body[:accounts].pluck(:id)).to include(recent_account.id.to_s)
  228. end
  229. end
  230. context 'when passing an invalid value for "expand_accounts"' do
  231. let(:params) { { expand_accounts: 'unknown_foobar' } }
  232. it 'returns http bad request' do
  233. subject
  234. expect(response).to have_http_status(400)
  235. expect(response.content_type)
  236. .to start_with('application/json')
  237. end
  238. end
  239. def body_json_types
  240. response.parsed_body[:notification_groups].pluck(:type)
  241. end
  242. end
  243. describe 'GET /api/v2/notifications/:id' do
  244. subject do
  245. get "/api/v2/notifications/#{notification.group_key}", headers: headers
  246. end
  247. let(:notification) { Fabricate(:notification, account: user.account, group_key: 'foobar') }
  248. it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
  249. it 'returns http success' do
  250. subject
  251. expect(response).to have_http_status(200)
  252. expect(response.content_type)
  253. .to start_with('application/json')
  254. end
  255. context 'when notification belongs to someone else' do
  256. let(:notification) { Fabricate(:notification, group_key: 'foobar') }
  257. it 'returns http not found' do
  258. subject
  259. expect(response).to have_http_status(404)
  260. expect(response.content_type)
  261. .to start_with('application/json')
  262. end
  263. end
  264. end
  265. describe 'POST /api/v2/notifications/:id/dismiss' do
  266. subject do
  267. post "/api/v2/notifications/#{notification.group_key}/dismiss", headers: headers
  268. end
  269. let!(:notification) { Fabricate(:notification, account: user.account, group_key: 'foobar') }
  270. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  271. it 'destroys the notification' do
  272. subject
  273. expect(response).to have_http_status(200)
  274. expect(response.content_type)
  275. .to start_with('application/json')
  276. expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
  277. end
  278. context 'when notification belongs to someone else' do
  279. let(:notification) { Fabricate(:notification, group_key: 'foobar') }
  280. it 'leaves the notification alone' do
  281. expect { subject }
  282. .to_not change(Notification, :count)
  283. expect(response).to have_http_status(200)
  284. expect(response.content_type)
  285. .to start_with('application/json')
  286. end
  287. end
  288. end
  289. describe 'POST /api/v2/notifications/clear' do
  290. subject do
  291. post '/api/v2/notifications/clear', headers: headers
  292. end
  293. before do
  294. Fabricate(:notification, account: user.account)
  295. end
  296. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  297. it 'clears notifications for the account' do
  298. subject
  299. expect(user.account.reload.notifications).to be_empty
  300. expect(response).to have_http_status(200)
  301. expect(response.content_type)
  302. .to start_with('application/json')
  303. end
  304. end
  305. end