2017-05-05 16:26:04 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2018-05-02 17:58:48 +01:00
|
|
|
describe ResolveURLService, type: :service do
|
2017-05-05 16:26:04 +01:00
|
|
|
subject { described_class.new }
|
|
|
|
|
|
|
|
describe '#call' do
|
2019-07-10 17:59:28 +01:00
|
|
|
it 'returns nil when there is no resource url' do
|
2023-10-19 15:55:06 +01:00
|
|
|
url = 'http://example.com/missing-resource'
|
|
|
|
Fabricate(:account, uri: url, domain: 'example.com')
|
2023-06-22 13:55:22 +01:00
|
|
|
service = instance_double(FetchResourceService)
|
2017-05-05 16:26:04 +01:00
|
|
|
|
2019-07-10 17:59:28 +01:00
|
|
|
allow(FetchResourceService).to receive(:new).and_return service
|
2022-04-28 19:19:10 +01:00
|
|
|
allow(service).to receive(:response_code).and_return(404)
|
2019-07-10 17:59:28 +01:00
|
|
|
allow(service).to receive(:call).with(url).and_return(nil)
|
2017-05-05 16:26:04 +01:00
|
|
|
|
2019-07-10 17:59:28 +01:00
|
|
|
expect(subject.call(url)).to be_nil
|
2017-05-05 16:26:04 +01:00
|
|
|
end
|
2020-12-17 05:51:49 +00:00
|
|
|
|
2022-04-28 19:19:10 +01:00
|
|
|
it 'returns known account on temporary error' do
|
|
|
|
url = 'http://example.com/missing-resource'
|
2023-07-20 17:23:48 +01:00
|
|
|
known_account = Fabricate(:account, uri: url, domain: 'example.com')
|
2023-06-22 13:55:22 +01:00
|
|
|
service = instance_double(FetchResourceService)
|
2022-04-28 19:19:10 +01:00
|
|
|
|
|
|
|
allow(FetchResourceService).to receive(:new).and_return service
|
|
|
|
allow(service).to receive(:response_code).and_return(500)
|
|
|
|
allow(service).to receive(:call).with(url).and_return(nil)
|
|
|
|
|
|
|
|
expect(subject.call(url)).to eq known_account
|
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when searching for a remote private status' do
|
2020-12-17 05:51:49 +00:00
|
|
|
let(:account) { Fabricate(:account) }
|
|
|
|
let(:poster) { Fabricate(:account, domain: 'example.com') }
|
|
|
|
let(:url) { 'https://example.com/@foo/42' }
|
|
|
|
let(:uri) { 'https://example.com/users/foo/statuses/42' }
|
|
|
|
let!(:status) { Fabricate(:status, url: url, uri: uri, account: poster, visibility: :private) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(:get, url).to_return(status: 404) if url.present?
|
|
|
|
stub_request(:get, uri).to_return(status: 404)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account follows the poster' do
|
|
|
|
before do
|
|
|
|
account.follow!(poster)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the status uses Mastodon-style URLs' do
|
|
|
|
let(:url) { 'https://example.com/@foo/42' }
|
|
|
|
let(:uri) { 'https://example.com/users/foo/statuses/42' }
|
|
|
|
|
|
|
|
it 'returns status by url' do
|
|
|
|
expect(subject.call(url, on_behalf_of: account)).to eq(status)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns status by uri' do
|
|
|
|
expect(subject.call(uri, on_behalf_of: account)).to eq(status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the status uses pleroma-style URLs' do
|
|
|
|
let(:url) { nil }
|
|
|
|
let(:uri) { 'https://example.com/objects/0123-456-789-abc-def' }
|
|
|
|
|
|
|
|
it 'returns status by uri' do
|
|
|
|
expect(subject.call(uri, on_behalf_of: account)).to eq(status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account does not follow the poster' do
|
|
|
|
context 'when the status uses Mastodon-style URLs' do
|
|
|
|
let(:url) { 'https://example.com/@foo/42' }
|
|
|
|
let(:uri) { 'https://example.com/users/foo/statuses/42' }
|
|
|
|
|
|
|
|
it 'does not return the status by url' do
|
|
|
|
expect(subject.call(url, on_behalf_of: account)).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return the status by uri' do
|
|
|
|
expect(subject.call(uri, on_behalf_of: account)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the status uses pleroma-style URLs' do
|
|
|
|
let(:url) { nil }
|
|
|
|
let(:uri) { 'https://example.com/objects/0123-456-789-abc-def' }
|
|
|
|
|
|
|
|
it 'returns status by uri' do
|
|
|
|
expect(subject.call(uri, on_behalf_of: account)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when searching for a local private status' do
|
2020-12-17 05:51:49 +00:00
|
|
|
let(:account) { Fabricate(:account) }
|
|
|
|
let(:poster) { Fabricate(:account) }
|
|
|
|
let!(:status) { Fabricate(:status, account: poster, visibility: :private) }
|
|
|
|
let(:url) { ActivityPub::TagManager.instance.url_for(status) }
|
|
|
|
let(:uri) { ActivityPub::TagManager.instance.uri_for(status) }
|
|
|
|
|
|
|
|
context 'when the account follows the poster' do
|
|
|
|
before do
|
|
|
|
account.follow!(poster)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns status by url' do
|
|
|
|
expect(subject.call(url, on_behalf_of: account)).to eq(status)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns status by uri' do
|
|
|
|
expect(subject.call(uri, on_behalf_of: account)).to eq(status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account does not follow the poster' do
|
|
|
|
it 'does not return the status by url' do
|
|
|
|
expect(subject.call(url, on_behalf_of: account)).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return the status by uri' do
|
|
|
|
expect(subject.call(uri, on_behalf_of: account)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-05-17 13:52:26 +01:00
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when searching for a link that redirects to a local public status' do
|
2022-05-17 13:52:26 +01:00
|
|
|
let(:account) { Fabricate(:account) }
|
|
|
|
let(:poster) { Fabricate(:account) }
|
|
|
|
let!(:status) { Fabricate(:status, account: poster, visibility: :public) }
|
|
|
|
let(:url) { 'https://link.to/foobar' }
|
|
|
|
let(:status_url) { ActivityPub::TagManager.instance.url_for(status) }
|
2023-02-17 21:56:20 +00:00
|
|
|
let(:uri) { ActivityPub::TagManager.instance.uri_for(status) }
|
2022-05-17 13:52:26 +01:00
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(:get, url).to_return(status: 302, headers: { 'Location' => status_url })
|
|
|
|
body = ActiveModelSerializers::SerializableResource.new(status, serializer: ActivityPub::NoteSerializer, adapter: ActivityPub::Adapter).to_json
|
|
|
|
stub_request(:get, status_url).to_return(body: body, headers: { 'Content-Type' => 'application/activity+json' })
|
2024-02-01 14:56:46 +00:00
|
|
|
stub_request(:get, uri).to_return(body: body, headers: { 'Content-Type' => 'application/activity+json' })
|
2022-05-17 13:52:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns status by url' do
|
|
|
|
expect(subject.call(url, on_behalf_of: account)).to eq(status)
|
|
|
|
end
|
|
|
|
end
|
2023-06-29 13:48:54 +01:00
|
|
|
|
|
|
|
context 'when searching for a local link of a remote private status' do
|
|
|
|
let(:account) { Fabricate(:account) }
|
|
|
|
let(:poster) { Fabricate(:account, username: 'foo', domain: 'example.com') }
|
|
|
|
let(:url) { 'https://example.com/@foo/42' }
|
|
|
|
let(:uri) { 'https://example.com/users/foo/statuses/42' }
|
|
|
|
let!(:status) { Fabricate(:status, url: url, uri: uri, account: poster, visibility: :private) }
|
|
|
|
let(:search_url) { "https://#{Rails.configuration.x.local_domain}/@foo@example.com/#{status.id}" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(:get, url).to_return(status: 404) if url.present?
|
|
|
|
stub_request(:get, uri).to_return(status: 404)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account follows the poster' do
|
|
|
|
before do
|
|
|
|
account.follow!(poster)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the status' do
|
|
|
|
expect(subject.call(search_url, on_behalf_of: account)).to eq(status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account does not follow the poster' do
|
|
|
|
it 'does not return the status' do
|
|
|
|
expect(subject.call(search_url, on_behalf_of: account)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-05 16:26:04 +01:00
|
|
|
end
|
|
|
|
end
|