link_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Link' 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 link 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/link' do
  18. subject do
  19. get '/api/v1/timelines/link', headers: headers, params: params
  20. end
  21. let(:url) { 'https://example.com/' }
  22. let(:private_status) { Fabricate(:status, visibility: :private) }
  23. let(:undiscoverable_status) { Fabricate(:status, account: Fabricate.build(:account, domain: nil, discoverable: false)) }
  24. let(:local_status) { Fabricate(:status, account: Fabricate.build(:account, domain: nil, discoverable: true)) }
  25. let(:remote_status) { Fabricate(:status, account: Fabricate.build(:account, domain: 'example.com', discoverable: true)) }
  26. let(:params) { { url: url } }
  27. let(:expected_statuses) { [local_status, remote_status] }
  28. let(:preview_card) { Fabricate(:preview_card, url: url) }
  29. before do
  30. if preview_card.present?
  31. preview_card.create_trend!(allowed: true)
  32. [private_status, undiscoverable_status, remote_status, local_status].each do |status|
  33. PreviewCardsStatus.create(status: status, preview_card: preview_card, url: url)
  34. end
  35. end
  36. end
  37. it_behaves_like 'forbidden for wrong scope', 'profile'
  38. context 'when there is no preview card' do
  39. let(:preview_card) { nil }
  40. it 'returns http not found' do
  41. subject
  42. expect(response).to have_http_status(404)
  43. expect(response.content_type)
  44. .to start_with('application/json')
  45. end
  46. end
  47. context 'when preview card is not trending' do
  48. before do
  49. preview_card.trend.destroy!
  50. end
  51. it 'returns http not found' do
  52. subject
  53. expect(response).to have_http_status(404)
  54. expect(response.content_type)
  55. .to start_with('application/json')
  56. end
  57. end
  58. context 'when preview card is trending but not approved' do
  59. before do
  60. preview_card.trend.update(allowed: false)
  61. end
  62. it 'returns http not found' do
  63. subject
  64. expect(response).to have_http_status(404)
  65. expect(response.content_type)
  66. .to start_with('application/json')
  67. end
  68. end
  69. context 'when the instance does not allow public preview' do
  70. before do
  71. Form::AdminSettings.new(timeline_preview: false).save
  72. end
  73. it_behaves_like 'forbidden for wrong scope', 'profile'
  74. context 'without an authentication token' do
  75. let(:headers) { {} }
  76. it 'returns http unprocessable entity' do
  77. subject
  78. expect(response).to have_http_status(422)
  79. expect(response.content_type)
  80. .to start_with('application/json')
  81. end
  82. end
  83. context 'with an application access token, not bound to a user' do
  84. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: nil, scopes: scopes) }
  85. it 'returns http unprocessable entity' do
  86. subject
  87. expect(response).to have_http_status(422)
  88. expect(response.content_type)
  89. .to start_with('application/json')
  90. end
  91. end
  92. context 'when the user is authenticated' do
  93. it_behaves_like 'a successful request to the link timeline'
  94. end
  95. end
  96. context 'when the instance allows public preview' do
  97. context 'with an authorized user' do
  98. it_behaves_like 'a successful request to the link timeline'
  99. end
  100. context 'with an anonymous user' do
  101. let(:headers) { {} }
  102. it_behaves_like 'a successful request to the link timeline'
  103. end
  104. context 'with limit param' do
  105. let(:params) { { limit: 1, url: url } }
  106. it 'returns only the requested number of statuses with pagination headers', :aggregate_failures do
  107. subject
  108. expect(response).to have_http_status(200)
  109. expect(response.content_type)
  110. .to start_with('application/json')
  111. expect(response.parsed_body.size).to eq(params[:limit])
  112. expect(response)
  113. .to include_pagination_headers(
  114. prev: api_v1_timelines_link_url(limit: params[:limit], url: url, min_id: local_status.id),
  115. next: api_v1_timelines_link_url(limit: params[:limit], url: url, max_id: local_status.id)
  116. )
  117. end
  118. end
  119. end
  120. end
  121. end