favourites_controller_spec.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. require 'rails_helper'
  2. RSpec.describe Api::V1::FavouritesController, type: :controller do
  3. render_views
  4. let(:user) { Fabricate(:user) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read') }
  6. describe 'GET #index' do
  7. context 'without token' do
  8. it 'returns http unauthorized' do
  9. get :index
  10. expect(response).to have_http_status :unauthorized
  11. end
  12. end
  13. context 'with token' do
  14. context 'without read scope' do
  15. before do
  16. allow(controller).to receive(:doorkeeper_token) do
  17. Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: '')
  18. end
  19. end
  20. it 'returns http forbidden' do
  21. get :index
  22. expect(response).to have_http_status :forbidden
  23. end
  24. end
  25. context 'without valid resource owner' do
  26. before do
  27. token = Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read')
  28. user.destroy!
  29. allow(controller).to receive(:doorkeeper_token) { token }
  30. end
  31. it 'returns http unprocessable entity' do
  32. get :index
  33. expect(response).to have_http_status :unprocessable_entity
  34. end
  35. end
  36. context 'with read scope and valid resource owner' do
  37. before do
  38. allow(controller).to receive(:doorkeeper_token) do
  39. Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:favourites')
  40. end
  41. end
  42. it 'shows favourites owned by the user' do
  43. favourite_by_user = Fabricate(:favourite, account: user.account)
  44. favourite_by_others = Fabricate(:favourite)
  45. get :index
  46. expect(assigns(:statuses)).to match_array [favourite_by_user.status]
  47. end
  48. it 'adds pagination headers if necessary' do
  49. favourite = Fabricate(:favourite, account: user.account)
  50. get :index, params: { limit: 1 }
  51. expect(response.headers['Link'].find_link(['rel', 'next']).href).to eq "http://test.host/api/v1/favourites?limit=1&max_id=#{favourite.id}"
  52. expect(response.headers['Link'].find_link(['rel', 'prev']).href).to eq "http://test.host/api/v1/favourites?limit=1&min_id=#{favourite.id}"
  53. end
  54. it 'does not add pagination headers if not necessary' do
  55. get :index
  56. expect(response.headers['Link']).to eq nil
  57. end
  58. end
  59. end
  60. end
  61. end