bookmarks_spec.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Bookmarks' do
  4. let(:user) { Fabricate(:user) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:scopes) { 'read:bookmarks' }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'GET /api/v1/bookmarks' do
  9. subject do
  10. get '/api/v1/bookmarks', headers: headers, params: params
  11. end
  12. let(:params) { {} }
  13. let!(:bookmarks) { Fabricate.times(2, :bookmark, account: user.account) }
  14. let(:expected_response) do
  15. bookmarks.map do |bookmark|
  16. a_hash_including(id: bookmark.status.id.to_s, account: a_hash_including(id: bookmark.status.account.id.to_s))
  17. end
  18. end
  19. it_behaves_like 'forbidden for wrong scope', 'write'
  20. it 'returns http success and the bookmarked statuses' 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 'paginates correctly', :aggregate_failures do
  30. subject
  31. expect(response.parsed_body.size)
  32. .to eq(params[:limit])
  33. expect(response.content_type)
  34. .to start_with('application/json')
  35. expect(response)
  36. .to include_pagination_headers(
  37. prev: api_v1_bookmarks_url(limit: params[:limit], min_id: bookmarks.last.id),
  38. next: api_v1_bookmarks_url(limit: params[:limit], max_id: bookmarks.second.id)
  39. )
  40. end
  41. end
  42. context 'without the authorization header' do
  43. let(:headers) { {} }
  44. it 'returns http unauthorized' do
  45. subject
  46. expect(response).to have_http_status(401)
  47. expect(response.content_type)
  48. .to start_with('application/json')
  49. end
  50. end
  51. end
  52. end