public_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Public' 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. shared_examples 'a successful request to the public timeline' do
  9. it 'returns the expected statuses successfully', :aggregate_failures do
  10. subject
  11. expect(response).to have_http_status(200)
  12. expect(response.content_type)
  13. .to start_with('application/json')
  14. expect(response.parsed_body.pluck(:id)).to match_array(expected_statuses.map { |status| status.id.to_s })
  15. end
  16. end
  17. describe 'GET /api/v1/timelines/public' do
  18. subject do
  19. get '/api/v1/timelines/public', headers: headers, params: params
  20. end
  21. let!(:local_status) { Fabricate(:status, account: Fabricate.build(:account, domain: nil)) }
  22. let!(:remote_status) { Fabricate(:status, account: Fabricate.build(:account, domain: 'example.com')) }
  23. let!(:media_status) { Fabricate(:status, media_attachments: [Fabricate.build(:media_attachment)]) }
  24. let(:params) { {} }
  25. before do
  26. Fabricate(:status, visibility: :private)
  27. end
  28. context 'when the instance allows public preview' do
  29. let(:expected_statuses) { [local_status, remote_status, media_status] }
  30. it_behaves_like 'forbidden for wrong scope', 'profile'
  31. context 'with an authorized user' do
  32. it_behaves_like 'a successful request to the public timeline'
  33. end
  34. context 'with an anonymous user' do
  35. let(:headers) { {} }
  36. it_behaves_like 'a successful request to the public timeline'
  37. end
  38. context 'with local param' do
  39. let(:params) { { local: true } }
  40. let(:expected_statuses) { [local_status, media_status] }
  41. it_behaves_like 'a successful request to the public timeline'
  42. end
  43. context 'with remote param' do
  44. let(:params) { { remote: true } }
  45. let(:expected_statuses) { [remote_status] }
  46. it_behaves_like 'a successful request to the public timeline'
  47. end
  48. context 'with local and remote params' do
  49. let(:params) { { local: true, remote: true } }
  50. let(:expected_statuses) { [local_status, remote_status, media_status] }
  51. it_behaves_like 'a successful request to the public timeline'
  52. end
  53. context 'with only_media param' do
  54. let(:params) { { only_media: true } }
  55. let(:expected_statuses) { [media_status] }
  56. it_behaves_like 'a successful request to the public timeline'
  57. end
  58. context 'with limit param' do
  59. let(:params) { { limit: 1 } }
  60. it 'returns only the requested number of statuses and sets pagination headers', :aggregate_failures do
  61. subject
  62. expect(response).to have_http_status(200)
  63. expect(response.content_type)
  64. .to start_with('application/json')
  65. expect(response.parsed_body.size).to eq(params[:limit])
  66. expect(response)
  67. .to include_pagination_headers(
  68. prev: api_v1_timelines_public_url(limit: params[:limit], min_id: media_status.id),
  69. next: api_v1_timelines_public_url(limit: params[:limit], max_id: media_status.id)
  70. )
  71. end
  72. end
  73. end
  74. context 'when the instance does not allow public preview' do
  75. before do
  76. Form::AdminSettings.new(timeline_preview: false).save
  77. end
  78. it_behaves_like 'forbidden for wrong scope', 'profile'
  79. context 'without an authentication token' do
  80. let(:headers) { {} }
  81. it 'returns http unprocessable entity' do
  82. subject
  83. expect(response).to have_http_status(422)
  84. expect(response.content_type)
  85. .to start_with('application/json')
  86. end
  87. end
  88. context 'with an application access token, not bound to a user' do
  89. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: nil, scopes: scopes) }
  90. it 'returns http unprocessable entity' do
  91. subject
  92. expect(response).to have_http_status(422)
  93. expect(response.content_type)
  94. .to start_with('application/json')
  95. end
  96. end
  97. context 'with an authenticated user' do
  98. let(:expected_statuses) { [local_status, remote_status, media_status] }
  99. it_behaves_like 'a successful request to the public timeline'
  100. end
  101. end
  102. end
  103. end