Migrate to request specs in /api/v1/favourites
(#25518)
This commit is contained in:
parent
7664e3b692
commit
ecdb31d479
2 changed files with 71 additions and 80 deletions
|
@ -1,80 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require 'rails_helper'
|
|
||||||
|
|
||||||
RSpec.describe Api::V1::FavouritesController do
|
|
||||||
render_views
|
|
||||||
|
|
||||||
let(:user) { Fabricate(:user) }
|
|
||||||
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read') }
|
|
||||||
|
|
||||||
describe 'GET #index' do
|
|
||||||
context 'without token' do
|
|
||||||
it 'returns http unauthorized' do
|
|
||||||
get :index
|
|
||||||
expect(response).to have_http_status 401
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with token' do
|
|
||||||
context 'without read scope' do
|
|
||||||
before do
|
|
||||||
allow(controller).to receive(:doorkeeper_token) do
|
|
||||||
Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: '')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns http forbidden' do
|
|
||||||
get :index
|
|
||||||
expect(response).to have_http_status 403
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'without valid resource owner' do
|
|
||||||
before do
|
|
||||||
token = Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read')
|
|
||||||
user.destroy!
|
|
||||||
|
|
||||||
allow(controller).to receive(:doorkeeper_token) { token }
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns http unprocessable entity' do
|
|
||||||
get :index
|
|
||||||
expect(response).to have_http_status 422
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with read scope and valid resource owner' do
|
|
||||||
before do
|
|
||||||
allow(controller).to receive(:doorkeeper_token) do
|
|
||||||
Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:favourites')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'shows favourites owned by the user' do
|
|
||||||
favourite_by_user = Fabricate(:favourite, account: user.account)
|
|
||||||
favourite_by_others = Fabricate(:favourite)
|
|
||||||
|
|
||||||
get :index
|
|
||||||
|
|
||||||
expect(assigns(:statuses)).to contain_exactly(favourite_by_user.status)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'adds pagination headers if necessary' do
|
|
||||||
favourite = Fabricate(:favourite, account: user.account)
|
|
||||||
|
|
||||||
get :index, params: { limit: 1 }
|
|
||||||
|
|
||||||
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq "http://test.host/api/v1/favourites?limit=1&max_id=#{favourite.id}"
|
|
||||||
expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq "http://test.host/api/v1/favourites?limit=1&min_id=#{favourite.id}"
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'does not add pagination headers if not necessary' do
|
|
||||||
get :index
|
|
||||||
|
|
||||||
expect(response.headers['Link']).to be_nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
71
spec/requests/api/v1/favourites_spec.rb
Normal file
71
spec/requests/api/v1/favourites_spec.rb
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe 'Favourites' do
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
|
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
|
||||||
|
let(:scopes) { 'read:favourites' }
|
||||||
|
let(:headers) { { Authorization: "Bearer #{token.token}" } }
|
||||||
|
|
||||||
|
describe 'GET /api/v1/favourites' do
|
||||||
|
subject do
|
||||||
|
get '/api/v1/favourites', headers: headers, params: params
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:params) { {} }
|
||||||
|
let!(:favourites) { Fabricate.times(3, :favourite, account: user.account) }
|
||||||
|
|
||||||
|
let(:expected_response) do
|
||||||
|
favourites.map do |favourite|
|
||||||
|
a_hash_including(id: favourite.status.id.to_s, account: a_hash_including(id: favourite.status.account.id.to_s))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it_behaves_like 'forbidden for wrong scope', 'write'
|
||||||
|
|
||||||
|
it 'returns http success' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(200)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns the favourites' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
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 favourites' 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_favourites_url(limit: params[:limit], min_id: favourites.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_favourites_url(limit: params[:limit], max_id: favourites[1].id))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'without an authorization header' do
|
||||||
|
let(:headers) { {} }
|
||||||
|
|
||||||
|
it 'returns http unauthorized' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(401)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue