favourites_controller_spec.rb 2.4 KB

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