Migrate to request specs in /api/v1/blocks (#25517)

This commit is contained in:
Daniel M Brasil 2023-10-16 11:03:35 -03:00 committed by GitHub
parent d9caa6ed24
commit 8e6116503d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 68 deletions

View file

@ -1,68 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Api::V1::BlocksController do
render_views
let(:user) { Fabricate(:user) }
let(:scopes) { 'read:blocks' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
before { allow(controller).to receive(:doorkeeper_token) { token } }
describe 'GET #index' do
it 'limits according to limit parameter', :aggregate_failures do
Array.new(2) { Fabricate(:block, account: user.account) }
get :index, params: { limit: 1 }
expect(response).to have_http_status(200)
expect(body_as_json.size).to eq 1
end
it 'queries blocks in range according to max_id', :aggregate_failures do
blocks = Array.new(2) { Fabricate(:block, account: user.account) }
get :index, params: { max_id: blocks[1] }
expect(response).to have_http_status(200)
expect(body_as_json.size).to eq 1
expect(body_as_json[0][:id]).to eq blocks[0].target_account_id.to_s
end
it 'queries blocks in range according to since_id', :aggregate_failures do
blocks = Array.new(2) { Fabricate(:block, account: user.account) }
get :index, params: { since_id: blocks[0] }
expect(response).to have_http_status(200)
expect(body_as_json.size).to eq 1
expect(body_as_json[0][:id]).to eq blocks[1].target_account_id.to_s
end
it 'sets pagination header for next path', :aggregate_failures do
blocks = Array.new(2) { Fabricate(:block, account: user.account) }
get :index, params: { limit: 1, since_id: blocks[0] }
expect(response).to have_http_status(200)
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq api_v1_blocks_url(limit: 1, max_id: blocks[1])
end
it 'sets pagination header for previous path', :aggregate_failures do
block = Fabricate(:block, account: user.account)
get :index
expect(response).to have_http_status(200)
expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq api_v1_blocks_url(since_id: block)
end
context 'with wrong scopes' do
let(:scopes) { 'write:blocks' }
it 'returns http forbidden' do
get :index
expect(response).to have_http_status(403)
end
end
end
end

View file

@ -0,0 +1,80 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Blocks' do
let(:user) { Fabricate(:user) }
let(:scopes) { 'read:blocks' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'GET /api/v1/blocks' do
subject do
get '/api/v1/blocks', headers: headers, params: params
end
let!(:blocks) { Fabricate.times(3, :block, account: user.account) }
let(:params) { {} }
let(:expected_response) do
blocks.map { |block| a_hash_including(id: block.target_account.id.to_s, username: block.target_account.username) }
end
it_behaves_like 'forbidden for wrong scope', 'write write:blocks'
it 'returns the blocked accounts', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json).to match_array(expected_response)
end
context 'with limit param' do
let(:params) { { limit: 2 } }
it 'returns only the requested number of blocked accounts' do
subject
expect(body_as_json.size).to eq(params[:limit])
end
it 'sets the correct pagination header for the prev path' do
subject
expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_blocks_url(limit: params[:limit], since_id: blocks.last.id))
end
it 'sets the correct pagination header for the next path' do
subject
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_blocks_url(limit: params[:limit], max_id: blocks[1].id))
end
end
context 'with max_id param' do
let(:params) { { max_id: blocks[1].id } }
it 'queries the blocks in range according to max_id', :aggregate_failures do
subject
response_body = body_as_json
expect(response_body.size).to be 1
expect(response_body[0][:id]).to eq(blocks[0].target_account.id.to_s)
end
end
context 'with since_id param' do
let(:params) { { since_id: blocks[1].id } }
it 'queries the blocks in range according to since_id', :aggregate_failures do
subject
response_body = body_as_json
expect(response_body.size).to be 1
expect(response_body[0][:id]).to eq(blocks[2].target_account.id.to_s)
end
end
end
end