home_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe 'Home' do
  4. let(:user) { Fabricate(:user) }
  5. let(:scopes) { 'read:statuses' }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'GET /api/v1/timelines/home' do
  9. subject do
  10. get '/api/v1/timelines/home', headers: headers, params: params
  11. end
  12. let(:params) { {} }
  13. it_behaves_like 'forbidden for wrong scope', 'write write:statuses'
  14. context 'when the timeline is available' do
  15. let(:home_statuses) { bob.statuses + ana.statuses }
  16. let!(:bob) { Fabricate(:account) }
  17. let!(:tim) { Fabricate(:account) }
  18. let!(:ana) { Fabricate(:account) }
  19. before do
  20. user.account.follow!(bob)
  21. user.account.follow!(ana)
  22. PostStatusService.new.call(bob, text: 'New toot from bob.')
  23. PostStatusService.new.call(tim, text: 'New toot from tim.')
  24. PostStatusService.new.call(ana, text: 'New toot from ana.')
  25. end
  26. it 'returns http success' do
  27. subject
  28. expect(response).to have_http_status(200)
  29. end
  30. it 'returns the statuses of followed users' do
  31. subject
  32. expect(body_as_json.pluck(:id)).to match_array(home_statuses.map { |status| status.id.to_s })
  33. end
  34. context 'with limit param' do
  35. let(:params) { { limit: 1 } }
  36. it 'returns only the requested number of statuses' do
  37. subject
  38. expect(body_as_json.size).to eq(params[:limit])
  39. end
  40. it 'sets the correct pagination headers', :aggregate_failures do
  41. subject
  42. headers = response.headers['Link']
  43. expect(headers.find_link(%w(rel prev)).href).to eq(api_v1_timelines_home_url(limit: 1, min_id: ana.statuses.first.id.to_s))
  44. expect(headers.find_link(%w(rel next)).href).to eq(api_v1_timelines_home_url(limit: 1, max_id: ana.statuses.first.id.to_s))
  45. end
  46. end
  47. end
  48. context 'when the timeline is regenerating' do
  49. let(:timeline) { instance_double(HomeFeed, regenerating?: true, get: []) }
  50. before do
  51. allow(HomeFeed).to receive(:new).and_return(timeline)
  52. end
  53. it 'returns http partial content' do
  54. subject
  55. expect(response).to have_http_status(206)
  56. end
  57. end
  58. context 'without an authorization header' do
  59. let(:headers) { {} }
  60. it 'returns http unauthorized' do
  61. subject
  62. expect(response).to have_http_status(401)
  63. end
  64. end
  65. context 'without a user context' do
  66. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: nil, scopes: scopes) }
  67. it 'returns http unprocessable entity', :aggregate_failures do
  68. subject
  69. expect(response).to have_http_status(422)
  70. expect(response.headers['Link']).to be_nil
  71. end
  72. end
  73. end
  74. end