1
0

blocks_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Blocks' do
  4. let(:user) { Fabricate(:user) }
  5. let(:scopes) { 'read:blocks' }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'GET /api/v1/blocks' do
  9. subject do
  10. get '/api/v1/blocks', headers: headers, params: params
  11. end
  12. let!(:blocks) { Fabricate.times(3, :block, account: user.account) }
  13. let(:params) { {} }
  14. let(:expected_response) do
  15. blocks.map { |block| a_hash_including(id: block.target_account.id.to_s, username: block.target_account.username) }
  16. end
  17. it_behaves_like 'forbidden for wrong scope', 'write write:blocks'
  18. it 'returns the blocked accounts', :aggregate_failures do
  19. subject
  20. expect(response).to have_http_status(200)
  21. expect(response.content_type)
  22. .to start_with('application/json')
  23. expect(response.parsed_body).to match_array(expected_response)
  24. end
  25. context 'with limit param' do
  26. let(:params) { { limit: 2 } }
  27. it 'returns only the requested number of blocked accounts and sets link header pagination' do
  28. subject
  29. expect(response.parsed_body.size).to eq(params[:limit])
  30. expect(response.content_type)
  31. .to start_with('application/json')
  32. expect(response)
  33. .to include_pagination_headers(
  34. prev: api_v1_blocks_url(limit: params[:limit], since_id: blocks.last.id),
  35. next: api_v1_blocks_url(limit: params[:limit], max_id: blocks.second.id)
  36. )
  37. end
  38. end
  39. context 'with max_id param' do
  40. let(:params) { { max_id: blocks[1].id } }
  41. it 'queries the blocks in range according to max_id', :aggregate_failures do
  42. subject
  43. expect(response.parsed_body)
  44. .to contain_exactly(include(id: blocks.first.target_account.id.to_s))
  45. end
  46. end
  47. context 'with since_id param' do
  48. let(:params) { { since_id: blocks[1].id } }
  49. it 'queries the blocks in range according to since_id', :aggregate_failures do
  50. subject
  51. expect(response.parsed_body)
  52. .to contain_exactly(include(id: blocks[2].target_account.id.to_s))
  53. end
  54. end
  55. end
  56. end