favourites_spec.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Favourites' do
  4. let(:user) { Fabricate(:user) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:scopes) { 'read:favourites' }
  7. let(:headers) { { Authorization: "Bearer #{token.token}" } }
  8. describe 'GET /api/v1/favourites' do
  9. subject do
  10. get '/api/v1/favourites', headers: headers, params: params
  11. end
  12. let(:params) { {} }
  13. let!(:favourites) { Fabricate.times(2, :favourite, account: user.account) }
  14. let(:expected_response) do
  15. favourites.map do |favourite|
  16. a_hash_including(id: favourite.status.id.to_s, account: a_hash_including(id: favourite.status.account.id.to_s))
  17. end
  18. end
  19. it_behaves_like 'forbidden for wrong scope', 'write'
  20. it 'returns http success' do
  21. subject
  22. expect(response).to have_http_status(200)
  23. end
  24. it 'returns the favourites' do
  25. subject
  26. expect(body_as_json).to match_array(expected_response)
  27. end
  28. context 'with limit param' do
  29. let(:params) { { limit: 1 } }
  30. it 'returns only the requested number of favourites' do
  31. subject
  32. expect(body_as_json.size).to eq(params[:limit])
  33. end
  34. it 'sets the correct pagination headers' do
  35. subject
  36. expect(response)
  37. .to include_pagination_headers(
  38. prev: api_v1_favourites_url(limit: params[:limit], min_id: favourites.last.id),
  39. next: api_v1_favourites_url(limit: params[:limit], max_id: favourites.second.id)
  40. )
  41. end
  42. end
  43. context 'without an authorization header' do
  44. let(:headers) { {} }
  45. it 'returns http unauthorized' do
  46. subject
  47. expect(response).to have_http_status(401)
  48. end
  49. end
  50. end
  51. end