1
0

resolve_url_service_spec.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ResolveURLService do
  4. subject { described_class.new }
  5. describe '#call' do
  6. it 'returns nil when there is no resource url' do
  7. url = 'http://example.com/missing-resource'
  8. Fabricate(:account, uri: url, domain: 'example.com')
  9. service = instance_double(FetchResourceService)
  10. allow(FetchResourceService).to receive(:new).and_return service
  11. allow(service).to receive(:response_code).and_return(404)
  12. allow(service).to receive(:call).with(url).and_return(nil)
  13. expect(subject.call(url)).to be_nil
  14. end
  15. it 'returns known account on temporary error' do
  16. url = 'http://example.com/missing-resource'
  17. known_account = Fabricate(:account, uri: url, domain: 'example.com')
  18. service = instance_double(FetchResourceService)
  19. allow(FetchResourceService).to receive(:new).and_return service
  20. allow(service).to receive(:response_code).and_return(500)
  21. allow(service).to receive(:call).with(url).and_return(nil)
  22. expect(subject.call(url)).to eq known_account
  23. end
  24. context 'when searching for a remote private status' do
  25. let(:account) { Fabricate(:account) }
  26. let(:poster) { Fabricate(:account, domain: 'example.com') }
  27. let(:url) { 'https://example.com/@foo/42' }
  28. let(:uri) { 'https://example.com/users/foo/statuses/42' }
  29. let!(:status) { Fabricate(:status, url: url, uri: uri, account: poster, visibility: :private) }
  30. before do
  31. stub_request(:get, url).to_return(status: 404) if url.present?
  32. stub_request(:get, uri).to_return(status: 404)
  33. end
  34. context 'when the account follows the poster' do
  35. before do
  36. account.follow!(poster)
  37. end
  38. context 'when the status uses Mastodon-style URLs' do
  39. let(:url) { 'https://example.com/@foo/42' }
  40. let(:uri) { 'https://example.com/users/foo/statuses/42' }
  41. it 'returns status by URL or URI' do
  42. expect(subject.call(url, on_behalf_of: account))
  43. .to eq(status)
  44. expect(subject.call(uri, on_behalf_of: account))
  45. .to eq(status)
  46. end
  47. end
  48. context 'when the status uses pleroma-style URLs' do
  49. let(:url) { nil }
  50. let(:uri) { 'https://example.com/objects/0123-456-789-abc-def' }
  51. it 'returns status by uri' do
  52. expect(subject.call(uri, on_behalf_of: account)).to eq(status)
  53. end
  54. end
  55. end
  56. context 'when the account does not follow the poster' do
  57. context 'when the status uses Mastodon-style URLs' do
  58. let(:url) { 'https://example.com/@foo/42' }
  59. let(:uri) { 'https://example.com/users/foo/statuses/42' }
  60. it 'does not return the status by URL or URI' do
  61. expect(subject.call(url, on_behalf_of: account))
  62. .to be_nil
  63. expect(subject.call(uri, on_behalf_of: account))
  64. .to be_nil
  65. end
  66. end
  67. context 'when the status uses pleroma-style URLs' do
  68. let(:url) { nil }
  69. let(:uri) { 'https://example.com/objects/0123-456-789-abc-def' }
  70. it 'returns status by uri' do
  71. expect(subject.call(uri, on_behalf_of: account)).to be_nil
  72. end
  73. end
  74. end
  75. end
  76. context 'when searching for a local private status' do
  77. let(:account) { Fabricate(:account) }
  78. let(:poster) { Fabricate(:account) }
  79. let!(:status) { Fabricate(:status, account: poster, visibility: :private) }
  80. let(:url) { ActivityPub::TagManager.instance.url_for(status) }
  81. let(:uri) { ActivityPub::TagManager.instance.uri_for(status) }
  82. context 'when the account follows the poster' do
  83. before do
  84. account.follow!(poster)
  85. end
  86. it 'returns status by URL or URI' do
  87. expect(subject.call(url, on_behalf_of: account))
  88. .to eq(status)
  89. expect(subject.call(uri, on_behalf_of: account))
  90. .to eq(status)
  91. end
  92. end
  93. context 'when the account does not follow the poster' do
  94. it 'does not return the status by URL or URI' do
  95. expect(subject.call(url, on_behalf_of: account))
  96. .to be_nil
  97. expect(subject.call(uri, on_behalf_of: account))
  98. .to be_nil
  99. end
  100. end
  101. end
  102. context 'when searching for a link that redirects to a local public status' do
  103. let(:account) { Fabricate(:account) }
  104. let(:poster) { Fabricate(:account) }
  105. let!(:status) { Fabricate(:status, account: poster, visibility: :public) }
  106. let(:url) { 'https://link.to/foobar' }
  107. let(:status_url) { ActivityPub::TagManager.instance.url_for(status) }
  108. let(:uri) { ActivityPub::TagManager.instance.uri_for(status) }
  109. before do
  110. stub_request(:get, url).to_return(status: 302, headers: { 'Location' => status_url })
  111. body = ActiveModelSerializers::SerializableResource.new(status, serializer: ActivityPub::NoteSerializer, adapter: ActivityPub::Adapter).to_json
  112. stub_request(:get, status_url).to_return(body: body, headers: { 'Content-Type' => 'application/activity+json' })
  113. stub_request(:get, uri).to_return(body: body, headers: { 'Content-Type' => 'application/activity+json' })
  114. end
  115. it 'returns status by url' do
  116. expect(subject.call(url, on_behalf_of: account)).to eq(status)
  117. end
  118. end
  119. context 'when searching for a local link of a remote private status' do
  120. let(:account) { Fabricate(:account) }
  121. let(:poster) { Fabricate(:account, username: 'foo', domain: 'example.com') }
  122. let(:url) { 'https://example.com/@foo/42' }
  123. let(:uri) { 'https://example.com/users/foo/statuses/42' }
  124. let!(:status) { Fabricate(:status, url: url, uri: uri, account: poster, visibility: :private) }
  125. let(:search_url) { "https://#{Rails.configuration.x.local_domain}/@foo@example.com/#{status.id}" }
  126. before do
  127. stub_request(:get, url).to_return(status: 404) if url.present?
  128. stub_request(:get, uri).to_return(status: 404)
  129. end
  130. context 'when the account follows the poster' do
  131. before do
  132. account.follow!(poster)
  133. end
  134. it 'returns the status' do
  135. expect(subject.call(search_url, on_behalf_of: account)).to eq(status)
  136. end
  137. end
  138. context 'when the account does not follow the poster' do
  139. it 'does not return the status' do
  140. expect(subject.call(search_url, on_behalf_of: account)).to be_nil
  141. end
  142. end
  143. end
  144. end
  145. end