resolve_url_service_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. service = double
  9. allow(FetchResourceService).to receive(:new).and_return service
  10. allow(service).to receive(:call).with(url).and_return(nil)
  11. expect(subject.call(url)).to be_nil
  12. end
  13. context 'searching for a remote private status' do
  14. let(:account) { Fabricate(:account) }
  15. let(:poster) { Fabricate(:account, domain: 'example.com') }
  16. let(:url) { 'https://example.com/@foo/42' }
  17. let(:uri) { 'https://example.com/users/foo/statuses/42' }
  18. let!(:status) { Fabricate(:status, url: url, uri: uri, account: poster, visibility: :private) }
  19. before do
  20. stub_request(:get, url).to_return(status: 404) if url.present?
  21. stub_request(:get, uri).to_return(status: 404)
  22. end
  23. context 'when the account follows the poster' do
  24. before do
  25. account.follow!(poster)
  26. end
  27. context 'when the status uses Mastodon-style URLs' do
  28. let(:url) { 'https://example.com/@foo/42' }
  29. let(:uri) { 'https://example.com/users/foo/statuses/42' }
  30. it 'returns status by url' do
  31. expect(subject.call(url, on_behalf_of: account)).to eq(status)
  32. end
  33. it 'returns status by uri' do
  34. expect(subject.call(uri, on_behalf_of: account)).to eq(status)
  35. end
  36. end
  37. context 'when the status uses pleroma-style URLs' do
  38. let(:url) { nil }
  39. let(:uri) { 'https://example.com/objects/0123-456-789-abc-def' }
  40. it 'returns status by uri' do
  41. expect(subject.call(uri, on_behalf_of: account)).to eq(status)
  42. end
  43. end
  44. end
  45. context 'when the account does not follow the poster' do
  46. context 'when the status uses Mastodon-style URLs' do
  47. let(:url) { 'https://example.com/@foo/42' }
  48. let(:uri) { 'https://example.com/users/foo/statuses/42' }
  49. it 'does not return the status by url' do
  50. expect(subject.call(url, on_behalf_of: account)).to be_nil
  51. end
  52. it 'does not return the status by uri' do
  53. expect(subject.call(uri, on_behalf_of: account)).to be_nil
  54. end
  55. end
  56. context 'when the status uses pleroma-style URLs' do
  57. let(:url) { nil }
  58. let(:uri) { 'https://example.com/objects/0123-456-789-abc-def' }
  59. it 'returns status by uri' do
  60. expect(subject.call(uri, on_behalf_of: account)).to be_nil
  61. end
  62. end
  63. end
  64. end
  65. context 'searching for a local private status' do
  66. let(:account) { Fabricate(:account) }
  67. let(:poster) { Fabricate(:account) }
  68. let!(:status) { Fabricate(:status, account: poster, visibility: :private) }
  69. let(:url) { ActivityPub::TagManager.instance.url_for(status) }
  70. let(:uri) { ActivityPub::TagManager.instance.uri_for(status) }
  71. context 'when the account follows the poster' do
  72. before do
  73. account.follow!(poster)
  74. end
  75. it 'returns status by url' do
  76. expect(subject.call(url, on_behalf_of: account)).to eq(status)
  77. end
  78. it 'returns status by uri' do
  79. expect(subject.call(uri, on_behalf_of: account)).to eq(status)
  80. end
  81. end
  82. context 'when the account does not follow the poster' do
  83. it 'does not return the status by url' do
  84. expect(subject.call(url, on_behalf_of: account)).to be_nil
  85. end
  86. it 'does not return the status by uri' do
  87. expect(subject.call(uri, on_behalf_of: account)).to be_nil
  88. end
  89. end
  90. end
  91. end
  92. end