2023-02-21 19:55:31 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-26 10:42:38 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2019-07-10 12:59:28 -04:00
|
|
|
RSpec.describe FetchResourceService, type: :service do
|
2018-02-09 21:31:38 -05:00
|
|
|
describe '#call' do
|
2019-07-10 12:59:28 -04:00
|
|
|
subject { described_class.new.call(url) }
|
2018-02-09 21:31:38 -05:00
|
|
|
|
2023-02-19 23:24:14 -05:00
|
|
|
let(:url) { 'http://example.com' }
|
|
|
|
|
2019-07-11 08:49:55 -04:00
|
|
|
context 'with blank url' do
|
2018-02-09 21:31:38 -05:00
|
|
|
let(:url) { '' }
|
2023-02-18 17:10:19 -05:00
|
|
|
|
2018-02-09 21:31:38 -05:00
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
2018-02-07 18:17:53 -05:00
|
|
|
|
2019-07-11 08:49:55 -04:00
|
|
|
context 'when request fails' do
|
2018-02-09 21:31:38 -05:00
|
|
|
before do
|
2019-07-11 08:49:55 -04:00
|
|
|
stub_request(:get, url).to_return(status: 500, body: '', headers: {})
|
2018-02-07 18:17:53 -05:00
|
|
|
end
|
2018-02-09 21:31:38 -05:00
|
|
|
|
|
|
|
it { is_expected.to be_nil }
|
2018-02-07 18:17:53 -05:00
|
|
|
end
|
|
|
|
|
2019-07-11 08:49:55 -04:00
|
|
|
context 'when OpenSSL::SSL::SSLError is raised' do
|
2018-02-09 21:31:38 -05:00
|
|
|
before do
|
2023-06-22 08:55:22 -04:00
|
|
|
request = instance_double(Request)
|
2020-04-25 16:01:08 -04:00
|
|
|
allow(Request).to receive(:new).and_return(request)
|
|
|
|
allow(request).to receive(:add_headers)
|
|
|
|
allow(request).to receive(:on_behalf_of)
|
|
|
|
allow(request).to receive(:perform).and_raise(OpenSSL::SSL::SSLError)
|
2018-02-09 21:31:38 -05:00
|
|
|
end
|
2018-02-07 18:17:53 -05:00
|
|
|
|
2019-07-11 08:49:55 -04:00
|
|
|
it { is_expected.to be_nil }
|
2018-02-07 18:17:53 -05:00
|
|
|
end
|
2018-02-08 18:12:35 -05:00
|
|
|
|
2019-07-11 08:49:55 -04:00
|
|
|
context 'when HTTP::ConnectionError is raised' do
|
2018-02-08 18:12:35 -05:00
|
|
|
before do
|
2023-06-22 08:55:22 -04:00
|
|
|
request = instance_double(Request)
|
2020-04-25 16:01:08 -04:00
|
|
|
allow(Request).to receive(:new).and_return(request)
|
|
|
|
allow(request).to receive(:add_headers)
|
|
|
|
allow(request).to receive(:on_behalf_of)
|
|
|
|
allow(request).to receive(:perform).and_raise(HTTP::ConnectionError)
|
2018-02-08 18:12:35 -05:00
|
|
|
end
|
|
|
|
|
2019-07-11 08:49:55 -04:00
|
|
|
it { is_expected.to be_nil }
|
2018-02-08 18:12:35 -05:00
|
|
|
end
|
|
|
|
|
2019-07-11 08:49:55 -04:00
|
|
|
context 'when request succeeds' do
|
2018-02-09 21:31:38 -05:00
|
|
|
let(:body) { '' }
|
2019-07-11 08:49:55 -04:00
|
|
|
|
|
|
|
let(:content_type) { 'application/json' }
|
|
|
|
|
|
|
|
let(:headers) do
|
|
|
|
{ 'Content-Type' => content_type }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:json) do
|
|
|
|
{
|
2024-02-01 09:56:46 -05:00
|
|
|
id: 'http://example.com/foo',
|
2018-02-09 21:31:38 -05:00
|
|
|
'@context': ActivityPub::TagManager::CONTEXT,
|
|
|
|
type: 'Note',
|
|
|
|
}.to_json
|
2019-07-11 08:49:55 -04:00
|
|
|
end
|
2018-02-09 21:31:38 -05:00
|
|
|
|
2018-02-08 18:12:35 -05:00
|
|
|
before do
|
2019-07-11 08:49:55 -04:00
|
|
|
stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
|
2023-02-17 21:59:57 -05:00
|
|
|
stub_request(:get, 'http://example.com/foo').to_return(status: 200, body: json, headers: { 'Content-Type' => 'application/activity+json' })
|
2019-07-11 08:49:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'signs request' do
|
|
|
|
subject
|
2022-09-21 16:45:57 -04:00
|
|
|
expect(a_request(:get, url).with(headers: { 'Signature' => /keyId="#{Regexp.escape(ActivityPub::TagManager.instance.key_uri_for(Account.representative))}"/ })).to have_been_made
|
2018-02-08 18:12:35 -05:00
|
|
|
end
|
|
|
|
|
2019-07-11 08:49:55 -04:00
|
|
|
context 'when content type is application/atom+xml' do
|
2018-02-09 21:31:38 -05:00
|
|
|
let(:content_type) { 'application/atom+xml' }
|
|
|
|
|
2023-02-20 00:14:50 -05:00
|
|
|
it { is_expected.to be_nil }
|
2018-02-09 21:31:38 -05:00
|
|
|
end
|
|
|
|
|
2019-07-11 08:49:55 -04:00
|
|
|
context 'when content type is activity+json' do
|
2018-11-22 06:49:07 -05:00
|
|
|
let(:content_type) { 'application/activity+json; charset=utf-8' }
|
|
|
|
let(:body) { json }
|
|
|
|
|
2024-02-01 09:56:46 -05:00
|
|
|
it { is_expected.to eq ['http://example.com/foo', { prefetched_body: body }] }
|
2018-11-22 06:49:07 -05:00
|
|
|
end
|
|
|
|
|
2019-07-11 08:49:55 -04:00
|
|
|
context 'when content type is ld+json with profile' do
|
2018-11-22 06:49:07 -05:00
|
|
|
let(:content_type) { 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' }
|
2018-02-09 21:31:38 -05:00
|
|
|
let(:body) { json }
|
|
|
|
|
2024-02-01 09:56:46 -05:00
|
|
|
it { is_expected.to eq ['http://example.com/foo', { prefetched_body: body }] }
|
2018-02-08 18:12:35 -05:00
|
|
|
end
|
|
|
|
|
2019-07-11 08:49:55 -04:00
|
|
|
context 'when link header is present' do
|
2023-02-18 09:33:41 -05:00
|
|
|
let(:headers) { { 'Link' => '<http://example.com/foo>; rel="alternate"; type="application/activity+json"' } }
|
2018-02-09 21:31:38 -05:00
|
|
|
|
2024-02-01 09:56:46 -05:00
|
|
|
it { is_expected.to eq ['http://example.com/foo', { prefetched_body: json }] }
|
2018-02-09 21:31:38 -05:00
|
|
|
end
|
|
|
|
|
2019-07-11 08:49:55 -04:00
|
|
|
context 'when content type is text/html' do
|
2018-02-09 21:31:38 -05:00
|
|
|
let(:content_type) { 'text/html' }
|
|
|
|
let(:body) { '<html><head><link rel="alternate" href="http://example.com/foo" type="application/activity+json"/></head></html>' }
|
|
|
|
|
2024-02-01 09:56:46 -05:00
|
|
|
it { is_expected.to eq ['http://example.com/foo', { prefetched_body: json }] }
|
2018-02-08 18:12:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-26 10:42:38 -04:00
|
|
|
end
|