bookmarks_spec.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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(3, :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' do
  21. subject
  22. expect(response).to have_http_status(200)
  23. end
  24. it 'returns the bookmarked statuses' 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: 2 } }
  30. it 'paginates correctly', :aggregate_failures do
  31. subject
  32. expect(body_as_json.size).to eq(params[:limit])
  33. expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_bookmarks_url(limit: params[:limit], min_id: bookmarks.last.id))
  34. expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_bookmarks_url(limit: params[:limit], max_id: bookmarks[1].id))
  35. end
  36. end
  37. context 'without the authorization header' do
  38. let(:headers) { {} }
  39. it 'returns http unauthorized' do
  40. subject
  41. expect(response).to have_http_status(401)
  42. end
  43. end
  44. end
  45. end