accounts_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Accounts in grouped 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/:group_key/accounts', :inline_jobs do
  9. subject do
  10. get "/api/v2/notifications/#{user.account.notifications.first.group_key}/accounts", headers: headers, params: params
  11. end
  12. let(:params) { {} }
  13. before do
  14. first_status = PostStatusService.new.call(user.account, text: 'Test')
  15. FavouriteService.new.call(Fabricate(:account), first_status)
  16. FavouriteService.new.call(Fabricate(:account), first_status)
  17. ReblogService.new.call(Fabricate(:account), first_status)
  18. FollowService.new.call(Fabricate(:account), user.account)
  19. FavouriteService.new.call(Fabricate(:account), first_status)
  20. end
  21. it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
  22. it 'returns a list of accounts' do
  23. subject
  24. expect(response).to have_http_status(200)
  25. expect(response.content_type)
  26. .to start_with('application/json')
  27. # The group we are interested in is only favorites
  28. notifications = user.account.notifications.where(type: 'favourite').reorder(id: :desc)
  29. expect(response.parsed_body).to match(
  30. [
  31. a_hash_including(
  32. id: notifications.first.from_account_id.to_s
  33. ),
  34. a_hash_including(
  35. id: notifications.second.from_account_id.to_s
  36. ),
  37. a_hash_including(
  38. id: notifications.third.from_account_id.to_s
  39. ),
  40. ]
  41. )
  42. end
  43. context 'with limit param' do
  44. let(:params) { { limit: 2 } }
  45. it 'returns the requested number of accounts, with pagination headers' do
  46. subject
  47. expect(response).to have_http_status(200)
  48. expect(response.content_type)
  49. .to start_with('application/json')
  50. # The group we are interested in is only favorites
  51. notifications = user.account.notifications.where(type: 'favourite').reorder(id: :desc)
  52. expect(response.parsed_body).to match(
  53. [
  54. a_hash_including(
  55. id: notifications.first.from_account_id.to_s
  56. ),
  57. a_hash_including(
  58. id: notifications.second.from_account_id.to_s
  59. ),
  60. ]
  61. )
  62. expect(response)
  63. .to include_pagination_headers(
  64. prev: api_v2_notification_accounts_url(limit: params[:limit], min_id: notifications.first.id),
  65. next: api_v2_notification_accounts_url(limit: params[:limit], max_id: notifications.second.id)
  66. )
  67. end
  68. end
  69. end
  70. end