blocks_spec.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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(body_as_json).to match_array(expected_response)
  22. end
  23. context 'with limit param' do
  24. let(:params) { { limit: 2 } }
  25. it 'returns only the requested number of blocked accounts' do
  26. subject
  27. expect(body_as_json.size).to eq(params[:limit])
  28. end
  29. it 'sets correct link header pagination' do
  30. subject
  31. expect(response)
  32. .to include_pagination_headers(
  33. prev: api_v1_blocks_url(limit: params[:limit], since_id: blocks.last.id),
  34. next: api_v1_blocks_url(limit: params[:limit], max_id: blocks.second.id)
  35. )
  36. end
  37. end
  38. context 'with max_id param' do
  39. let(:params) { { max_id: blocks[1].id } }
  40. it 'queries the blocks in range according to max_id', :aggregate_failures do
  41. subject
  42. response_body = body_as_json
  43. expect(response_body.size).to be 1
  44. expect(response_body[0][:id]).to eq(blocks[0].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. response_body = body_as_json
  52. expect(response_body.size).to be 1
  53. expect(response_body[0][:id]).to eq(blocks[2].target_account.id.to_s)
  54. end
  55. end
  56. end
  57. end