blocks_controller_spec.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Api::V1::BlocksController do
  4. render_views
  5. let(:user) { Fabricate(:user) }
  6. let(:scopes) { 'read:blocks' }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  8. before { allow(controller).to receive(:doorkeeper_token) { token } }
  9. describe 'GET #index' do
  10. it 'limits according to limit parameter' do
  11. Array.new(2) { Fabricate(:block, account: user.account) }
  12. get :index, params: { limit: 1 }
  13. expect(body_as_json.size).to eq 1
  14. end
  15. it 'queries blocks in range according to max_id' do
  16. blocks = Array.new(2) { Fabricate(:block, account: user.account) }
  17. get :index, params: { max_id: blocks[1] }
  18. expect(body_as_json.size).to eq 1
  19. expect(body_as_json[0][:id]).to eq blocks[0].target_account_id.to_s
  20. end
  21. it 'queries blocks in range according to since_id' do
  22. blocks = Array.new(2) { Fabricate(:block, account: user.account) }
  23. get :index, params: { since_id: blocks[0] }
  24. expect(body_as_json.size).to eq 1
  25. expect(body_as_json[0][:id]).to eq blocks[1].target_account_id.to_s
  26. end
  27. it 'sets pagination header for next path' do
  28. blocks = Array.new(2) { Fabricate(:block, account: user.account) }
  29. get :index, params: { limit: 1, since_id: blocks[0] }
  30. expect(response.headers['Link'].find_link(%w(rel next)).href).to eq api_v1_blocks_url(limit: 1, max_id: blocks[1])
  31. end
  32. it 'sets pagination header for previous path' do
  33. block = Fabricate(:block, account: user.account)
  34. get :index
  35. expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq api_v1_blocks_url(since_id: block)
  36. end
  37. it 'returns http success' do
  38. get :index
  39. expect(response).to have_http_status(200)
  40. end
  41. context 'with wrong scopes' do
  42. let(:scopes) { 'write:blocks' }
  43. it 'returns http forbidden' do
  44. get :index
  45. expect(response).to have_http_status(403)
  46. end
  47. end
  48. end
  49. end