home_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Home', :inline_jobs 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 and statuses of followed users' do
  27. subject
  28. expect(response).to have_http_status(200)
  29. expect(response.content_type)
  30. .to start_with('application/json')
  31. expect(response.parsed_body.pluck(:id)).to match_array(home_statuses.map { |status| status.id.to_s })
  32. end
  33. context 'with limit param' do
  34. let(:params) { { limit: 1 } }
  35. it 'returns only the requested number of statuses with pagination headers', :aggregate_failures do
  36. subject
  37. expect(response.parsed_body.size).to eq(params[:limit])
  38. expect(response)
  39. .to include_pagination_headers(
  40. prev: api_v1_timelines_home_url(limit: params[:limit], min_id: ana.statuses.first.id),
  41. next: api_v1_timelines_home_url(limit: params[:limit], max_id: ana.statuses.first.id)
  42. )
  43. expect(response.content_type)
  44. .to start_with('application/json')
  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. expect(response.content_type)
  57. .to start_with('application/json')
  58. end
  59. end
  60. context 'without an authorization header' do
  61. let(:headers) { {} }
  62. it 'returns http unauthorized' do
  63. subject
  64. expect(response).to have_http_status(401)
  65. expect(response.content_type)
  66. .to start_with('application/json')
  67. end
  68. end
  69. context 'without a user context' do
  70. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: nil, scopes: scopes) }
  71. it 'returns http unprocessable entity', :aggregate_failures do
  72. subject
  73. expect(response)
  74. .to have_http_status(422)
  75. .and not_have_http_link_header
  76. expect(response.content_type)
  77. .to start_with('application/json')
  78. end
  79. end
  80. end
  81. end