resolve_url_service_spec.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe ResolveURLService, type: :service 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. known_account = Fabricate(:account, uri: url)
  9. service = double
  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)
  18. service = double
  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 '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' do
  42. expect(subject.call(url, on_behalf_of: account)).to eq(status)
  43. end
  44. it 'returns status by uri' do
  45. expect(subject.call(uri, on_behalf_of: account)).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' do
  61. expect(subject.call(url, on_behalf_of: account)).to be_nil
  62. end
  63. it 'does not return the status by uri' do
  64. expect(subject.call(uri, on_behalf_of: account)).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 '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' do
  87. expect(subject.call(url, on_behalf_of: account)).to eq(status)
  88. end
  89. it 'returns status by uri' do
  90. expect(subject.call(uri, on_behalf_of: account)).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' do
  95. expect(subject.call(url, on_behalf_of: account)).to be_nil
  96. end
  97. it 'does not return the status by uri' do
  98. expect(subject.call(uri, on_behalf_of: account)).to be_nil
  99. end
  100. end
  101. end
  102. context '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. end
  114. it 'returns status by url' do
  115. expect(subject.call(url, on_behalf_of: account)).to eq(status)
  116. end
  117. end
  118. context 'when searching for a local link of a remote private status' do
  119. let(:account) { Fabricate(:account) }
  120. let(:poster) { Fabricate(:account, username: 'foo', domain: 'example.com') }
  121. let(:url) { 'https://example.com/@foo/42' }
  122. let(:uri) { 'https://example.com/users/foo/statuses/42' }
  123. let!(:status) { Fabricate(:status, url: url, uri: uri, account: poster, visibility: :private) }
  124. let(:search_url) { "https://#{Rails.configuration.x.local_domain}/@foo@example.com/#{status.id}" }
  125. before do
  126. stub_request(:get, url).to_return(status: 404) if url.present?
  127. stub_request(:get, uri).to_return(status: 404)
  128. end
  129. context 'when the account follows the poster' do
  130. before do
  131. account.follow!(poster)
  132. end
  133. it 'returns the status' do
  134. expect(subject.call(search_url, on_behalf_of: account)).to eq(status)
  135. end
  136. end
  137. context 'when the account does not follow the poster' do
  138. it 'does not return the status' do
  139. expect(subject.call(search_url, on_behalf_of: account)).to be_nil
  140. end
  141. end
  142. end
  143. end
  144. end