1
0

favourites_spec.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 and includes the favourites' do
  21. subject
  22. expect(response).to have_http_status(200)
  23. expect(response.content_type)
  24. .to start_with('application/json')
  25. expect(response.parsed_body).to match_array(expected_response)
  26. end
  27. context 'with limit param' do
  28. let(:params) { { limit: 1 } }
  29. it 'returns only the requested number of favourites and sets pagination headers' do
  30. subject
  31. expect(response.parsed_body.size).to eq(params[:limit])
  32. expect(response)
  33. .to include_pagination_headers(
  34. prev: api_v1_favourites_url(limit: params[:limit], min_id: favourites.last.id),
  35. next: api_v1_favourites_url(limit: params[:limit], max_id: favourites.second.id)
  36. )
  37. expect(response.content_type)
  38. .to start_with('application/json')
  39. end
  40. end
  41. context 'without an authorization header' do
  42. let(:headers) { {} }
  43. it 'returns http unauthorized' do
  44. subject
  45. expect(response).to have_http_status(401)
  46. expect(response.content_type)
  47. .to start_with('application/json')
  48. end
  49. end
  50. end
  51. end