2023-10-13 14:19:12 +02:00
|
|
|
# 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) { {} }
|
2023-11-21 14:05:59 +01:00
|
|
|
let!(:favourites) { Fabricate.times(2, :favourite, account: user.account) }
|
2023-10-13 14:19:12 +02:00
|
|
|
|
|
|
|
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'
|
|
|
|
|
2024-09-19 12:15:21 +02:00
|
|
|
it 'returns http success and includes the favourites' do
|
2023-10-13 14:19:12 +02:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2024-09-06 11:58:46 +02:00
|
|
|
expect(response.parsed_body).to match_array(expected_response)
|
2023-10-13 14:19:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with limit param' do
|
2023-11-21 14:05:59 +01:00
|
|
|
let(:params) { { limit: 1 } }
|
2023-10-13 14:19:12 +02:00
|
|
|
|
2024-09-19 12:15:21 +02:00
|
|
|
it 'returns only the requested number of favourites and sets pagination headers' do
|
2023-10-13 14:19:12 +02:00
|
|
|
subject
|
|
|
|
|
2024-09-06 11:58:46 +02:00
|
|
|
expect(response.parsed_body.size).to eq(params[:limit])
|
2024-03-15 11:17:45 +01:00
|
|
|
expect(response)
|
|
|
|
.to include_pagination_headers(
|
|
|
|
prev: api_v1_favourites_url(limit: params[:limit], min_id: favourites.last.id),
|
|
|
|
next: api_v1_favourites_url(limit: params[:limit], max_id: favourites.second.id)
|
|
|
|
)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-10-13 14:19:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without an authorization header' do
|
|
|
|
let(:headers) { {} }
|
|
|
|
|
|
|
|
it 'returns http unauthorized' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(401)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-10-13 14:19:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|