replies_controller_spec.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::RepliesController do
  4. let(:status) { Fabricate(:status, visibility: parent_visibility) }
  5. let(:remote_account) { Fabricate(:account, domain: 'foobar.com') }
  6. let(:remote_reply_id) { 'https://foobar.com/statuses/1234' }
  7. let(:remote_querier) { nil }
  8. shared_examples 'common behavior' do
  9. context 'when status is private' do
  10. let(:parent_visibility) { :private }
  11. it 'returns http not found' do
  12. expect(response).to have_http_status(404)
  13. end
  14. end
  15. context 'when status is direct' do
  16. let(:parent_visibility) { :direct }
  17. it 'returns http not found' do
  18. expect(response).to have_http_status(404)
  19. end
  20. end
  21. end
  22. shared_examples 'disallowed access' do
  23. context 'when status is public' do
  24. let(:parent_visibility) { :public }
  25. it 'returns http not found' do
  26. expect(response).to have_http_status(404)
  27. end
  28. end
  29. it_behaves_like 'common behavior'
  30. end
  31. shared_examples 'allowed access' do
  32. context 'when account is permanently suspended' do
  33. let(:parent_visibility) { :public }
  34. before do
  35. status.account.suspend!
  36. status.account.deletion_request.destroy
  37. end
  38. it 'returns http gone' do
  39. expect(response).to have_http_status(410)
  40. end
  41. end
  42. context 'when account is temporarily suspended' do
  43. let(:parent_visibility) { :public }
  44. before do
  45. status.account.suspend!
  46. end
  47. it 'returns http forbidden' do
  48. expect(response).to have_http_status(403)
  49. end
  50. end
  51. context 'when status is public' do
  52. let(:parent_visibility) { :public }
  53. let(:json) { body_as_json }
  54. let(:page_json) { json[:first] }
  55. it 'returns http success' do
  56. expect(response).to have_http_status(200)
  57. end
  58. it 'returns application/activity+json' do
  59. expect(response.media_type).to eq 'application/activity+json'
  60. end
  61. it_behaves_like 'cacheable response'
  62. context 'without only_other_accounts' do
  63. it "returns items with thread author's replies" do
  64. expect(page_json).to be_a Hash
  65. expect(page_json[:items]).to be_an Array
  66. expect(page_json[:items].size).to eq 1
  67. expect(page_json[:items].all? { |item| targets_public_collection?(item) }).to be true
  68. end
  69. context 'when there are few self-replies' do
  70. it 'points next to replies from other people' do
  71. expect(page_json).to be_a Hash
  72. expect(parsed_uri_query_values(page_json[:next])).to include('only_other_accounts=true', 'page=true')
  73. end
  74. end
  75. context 'when there are many self-replies' do
  76. before do
  77. 10.times { Fabricate(:status, account: status.account, thread: status, visibility: :public) }
  78. end
  79. it 'points next to other self-replies' do
  80. expect(page_json).to be_a Hash
  81. expect(parsed_uri_query_values(page_json[:next])).to include('only_other_accounts=false', 'page=true')
  82. end
  83. end
  84. end
  85. context 'with only_other_accounts' do
  86. let(:only_other_accounts) { 'true' }
  87. it 'returns items with other public or unlisted replies' do
  88. expect(page_json).to be_a Hash
  89. expect(page_json[:items]).to be_an Array
  90. expect(page_json[:items].size).to eq 3
  91. end
  92. it 'only inlines items that are local and public or unlisted replies' do
  93. inlined_replies = page_json[:items].select { |x| x.is_a?(Hash) }
  94. expect(inlined_replies.all? { |item| targets_public_collection?(item) }).to be true
  95. expect(inlined_replies.all? { |item| ActivityPub::TagManager.instance.local_uri?(item[:id]) }).to be true
  96. end
  97. it 'uses ids for remote toots' do
  98. remote_replies = page_json[:items].reject { |x| x.is_a?(Hash) }
  99. expect(remote_replies.all? { |item| item.is_a?(String) && !ActivityPub::TagManager.instance.local_uri?(item) }).to be true
  100. end
  101. context 'when there are few replies' do
  102. it 'does not have a next page' do
  103. expect(page_json).to be_a Hash
  104. expect(page_json[:next]).to be_nil
  105. end
  106. end
  107. context 'when there are many replies' do
  108. before do
  109. 10.times { Fabricate(:status, thread: status, visibility: :public) }
  110. end
  111. it 'points next to other replies' do
  112. expect(page_json).to be_a Hash
  113. expect(parsed_uri_query_values(page_json[:next])).to include('only_other_accounts=true', 'page=true')
  114. end
  115. end
  116. end
  117. end
  118. it_behaves_like 'common behavior'
  119. end
  120. before do
  121. stub_const 'ActivityPub::RepliesController::DESCENDANTS_LIMIT', 5
  122. allow(controller).to receive(:signed_request_actor).and_return(remote_querier)
  123. Fabricate(:status, thread: status, visibility: :public)
  124. Fabricate(:status, thread: status, visibility: :public)
  125. Fabricate(:status, thread: status, visibility: :private)
  126. Fabricate(:status, account: status.account, thread: status, visibility: :public)
  127. Fabricate(:status, account: status.account, thread: status, visibility: :private)
  128. Fabricate(:status, account: remote_account, thread: status, visibility: :public, uri: remote_reply_id)
  129. end
  130. describe 'GET #index' do
  131. subject(:response) { get :index, params: { account_username: status.account.username, status_id: status.id, only_other_accounts: only_other_accounts } }
  132. let(:only_other_accounts) { nil }
  133. context 'with no signature' do
  134. it_behaves_like 'allowed access'
  135. end
  136. context 'with signature' do
  137. let(:remote_querier) { Fabricate(:account, domain: 'example.com') }
  138. it_behaves_like 'allowed access'
  139. context 'when signed request account is blocked' do
  140. before do
  141. status.account.block!(remote_querier)
  142. end
  143. it_behaves_like 'disallowed access'
  144. end
  145. context 'when signed request account is domain blocked' do
  146. before do
  147. status.account.block_domain!(remote_querier.domain)
  148. end
  149. it_behaves_like 'disallowed access'
  150. end
  151. end
  152. end
  153. private
  154. def parsed_uri_query_values(uri)
  155. Addressable::URI
  156. .parse(uri)
  157. .query
  158. .split('&')
  159. end
  160. def ap_public_collection
  161. ActivityPub::TagManager::COLLECTIONS[:public]
  162. end
  163. def targets_public_collection?(item)
  164. item[:to].include?(ap_public_collection) || item[:cc].include?(ap_public_collection)
  165. end
  166. end