about summary refs log tree commit diff
path: root/spec/services
diff options
context:
space:
mode:
Diffstat (limited to 'spec/services')
-rw-r--r--spec/services/account_search_service_spec.rb4
-rw-r--r--spec/services/fetch_atom_service_spec.rb85
-rw-r--r--spec/services/fetch_link_card_service_spec.rb10
-rw-r--r--spec/services/fetch_remote_account_service_spec.rb67
-rw-r--r--spec/services/fetch_remote_status_service_spec.rb31
-rw-r--r--spec/services/precompute_feed_service_spec.rb7
-rw-r--r--spec/services/report_service_spec.rb25
-rw-r--r--spec/services/resolve_account_service_spec.rb (renamed from spec/services/resolve_remote_account_service_spec.rb)2
-rw-r--r--spec/services/resolve_url_service_spec.rb (renamed from spec/services/fetch_remote_resource_service_spec.rb)2
-rw-r--r--spec/services/search_service_spec.rb6
10 files changed, 227 insertions, 12 deletions
diff --git a/spec/services/account_search_service_spec.rb b/spec/services/account_search_service_spec.rb
index 4685e619f..9bb27edad 100644
--- a/spec/services/account_search_service_spec.rb
+++ b/spec/services/account_search_service_spec.rb
@@ -123,7 +123,7 @@ describe AccountSearchService do
     describe 'when there is a domain but no exact match' do
       it 'follows the remote account when resolve is true' do
         service = double(call: nil)
-        allow(ResolveRemoteAccountService).to receive(:new).and_return(service)
+        allow(ResolveAccountService).to receive(:new).and_return(service)
 
         results = subject.call('newuser@remote.com', 10, nil, resolve: true)
         expect(service).to have_received(:call).with('newuser@remote.com')
@@ -131,7 +131,7 @@ describe AccountSearchService do
 
       it 'does not follow the remote account when resolve is false' do
         service = double(call: nil)
-        allow(ResolveRemoteAccountService).to receive(:new).and_return(service)
+        allow(ResolveAccountService).to receive(:new).and_return(service)
 
         results = subject.call('newuser@remote.com', 10, nil, resolve: false)
         expect(service).not_to have_received(:call)
diff --git a/spec/services/fetch_atom_service_spec.rb b/spec/services/fetch_atom_service_spec.rb
index 5491fd027..2bd127e92 100644
--- a/spec/services/fetch_atom_service_spec.rb
+++ b/spec/services/fetch_atom_service_spec.rb
@@ -1,4 +1,89 @@
 require 'rails_helper'
 
 RSpec.describe FetchAtomService do
+  describe '#call' do
+    let(:url) { 'http://example.com' }
+    subject { FetchAtomService.new.call(url) }
+
+    context 'url is blank' do
+      let(:url) { '' }
+      it { is_expected.to be_nil }
+    end
+
+    context 'request failed' do
+      before do
+        WebMock.stub_request(:get, url).to_return(status: 500, body: '', headers: {})
+      end
+
+      it { is_expected.to be_nil }
+    end
+
+    context 'raise OpenSSL::SSL::SSLError' do
+      before do
+        allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(OpenSSL::SSL::SSLError)
+      end
+
+      it 'output log and return nil' do
+        expect_any_instance_of(ActiveSupport::Logger).to receive(:debug).with('SSL error: OpenSSL::SSL::SSLError')
+        is_expected.to be_nil
+      end
+    end
+
+    context 'raise HTTP::ConnectionError' do
+      before do
+        allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(HTTP::ConnectionError)
+      end
+
+      it 'output log and return nil' do
+        expect_any_instance_of(ActiveSupport::Logger).to receive(:debug).with('HTTP ConnectionError: HTTP::ConnectionError')
+        is_expected.to be_nil
+      end
+    end
+
+    context 'response success' do
+      let(:body) { '' }
+      let(:headers) { { 'Content-Type' => content_type } }
+      let(:json) {
+        { id: 1,
+          '@context': ActivityPub::TagManager::CONTEXT,
+          type: 'Note',
+        }.to_json
+      }
+
+      before do
+        WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
+      end
+
+      context 'content type is application/atom+xml' do
+        let(:content_type) { 'application/atom+xml' }
+
+        it { is_expected.to eq [url, {:prefetched_body=>""}, :ostatus] }
+      end
+
+      context 'content_type is json' do
+        let(:content_type) { 'application/activity+json' }
+        let(:body) { json }
+
+        it { is_expected.to eq [1, { prefetched_body: body, id: true }, :activitypub] }
+      end
+
+      before do
+        WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
+        WebMock.stub_request(:get, 'http://example.com/foo').to_return(status: 200, body: json, headers: { 'Content-Type' => 'application/activity+json' })
+      end
+
+      context 'has link header' do
+        let(:headers) { { 'Link' => '<http://example.com/foo>; rel="alternate"; type="application/activity+json"', } }
+
+        it { is_expected.to eq [1, { prefetched_body: json, id: true }, :activitypub] }
+      end
+
+      context 'content type is text/html' do
+        let(:content_type) { 'text/html' }
+        let(:body) { '<html><head><link rel="alternate" href="http://example.com/foo" type="application/activity+json"/></head></html>' }
+
+        it { is_expected.to eq [1, { prefetched_body: json, id: true }, :activitypub] }
+      end
+    end
+  end
 end
diff --git a/spec/services/fetch_link_card_service_spec.rb b/spec/services/fetch_link_card_service_spec.rb
index ba61d22c3..edacc4425 100644
--- a/spec/services/fetch_link_card_service_spec.rb
+++ b/spec/services/fetch_link_card_service_spec.rb
@@ -15,6 +15,8 @@ RSpec.describe FetchLinkCardService do
     stub_request(:head, 'http://example.com/日本語').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
     stub_request(:get, 'http://example.com/日本語').to_return(request_fixture('sjis.txt'))
     stub_request(:head, 'https://github.com/qbi/WannaCry').to_return(status: 404)
+    stub_request(:head, 'http://example.com/test-').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
+    stub_request(:get, 'http://example.com/test-').to_return(request_fixture('idn.txt'))
 
     subject.call(status)
   end
@@ -63,6 +65,14 @@ RSpec.describe FetchLinkCardService do
         expect(status.preview_cards.first.title).to eq("SJISのページ")
       end
     end
+
+    context do
+      let(:status) { Fabricate(:status, text: 'test http://example.com/test-') }
+
+      it 'works with a URL ending with a hyphen' do
+        expect(a_request(:get, 'http://example.com/test-')).to have_been_made.at_least_once
+      end
+    end
   end
 
   context 'in a remote status' do
diff --git a/spec/services/fetch_remote_account_service_spec.rb b/spec/services/fetch_remote_account_service_spec.rb
index bb1877c7a..4388d4cf4 100644
--- a/spec/services/fetch_remote_account_service_spec.rb
+++ b/spec/services/fetch_remote_account_service_spec.rb
@@ -1,4 +1,71 @@
 require 'rails_helper'
 
 RSpec.describe FetchRemoteAccountService do
+  let(:url) { 'https://example.com' }
+  let(:prefetched_body) { nil }
+  let(:protocol) { :ostatus }
+  subject { FetchRemoteAccountService.new.call(url, prefetched_body, protocol) }
+
+  let(:actor) do
+    {
+      '@context': 'https://www.w3.org/ns/activitystreams',
+      id: 'https://example.com/alice',
+      type: 'Person',
+      preferredUsername: 'alice',
+      name: 'Alice',
+      summary: 'Foo bar',
+      inbox: 'http://example.com/alice/inbox',
+    }
+  end
+
+  let(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice' }] } }
+  let(:xml) { File.read(File.join(Rails.root, 'spec', 'fixtures', 'xml', 'mastodon.atom')) }
+
+  shared_examples 'return Account' do
+    it { is_expected.to be_an Account }
+  end
+
+  context 'protocol is :activitypub' do
+    let(:prefetched_body) { Oj.dump(actor) }
+    let(:protocol) { :activitypub }
+
+    before do
+      stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
+    end
+
+    include_examples 'return Account'
+  end
+
+  context 'protocol is :ostatus' do
+    let(:prefetched_body) { xml }
+    let(:protocol) { :ostatus }
+
+    before do
+      stub_request(:get, "https://kickass.zone/.well-known/webfinger?resource=acct:localhost@kickass.zone").to_return(request_fixture('webfinger-hacker3.txt'))
+      stub_request(:get, "https://kickass.zone/api/statuses/user_timeline/7477.atom").to_return(request_fixture('feed.txt'))
+    end
+
+    include_examples 'return Account'
+  end
+
+  context 'when prefetched_body is nil' do
+    context 'protocol is :activitypub' do
+      before do
+        stub_request(:get, url).to_return(status: 200, body: Oj.dump(actor), headers: { 'Content-Type' => 'application/activity+json' })
+        stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
+      end
+
+      include_examples 'return Account'
+    end
+
+    context 'protocol is :ostatus' do
+      before do
+        stub_request(:get, url).to_return(status: 200, body: xml, headers: { 'Content-Type' => 'application/atom+xml' })
+        stub_request(:get, "https://kickass.zone/.well-known/webfinger?resource=acct:localhost@kickass.zone").to_return(request_fixture('webfinger-hacker3.txt'))
+        stub_request(:get, "https://kickass.zone/api/statuses/user_timeline/7477.atom").to_return(request_fixture('feed.txt'))
+      end
+
+      include_examples 'return Account'
+    end
+  end
 end
diff --git a/spec/services/fetch_remote_status_service_spec.rb b/spec/services/fetch_remote_status_service_spec.rb
index cbdecbf25..fa5782b94 100644
--- a/spec/services/fetch_remote_status_service_spec.rb
+++ b/spec/services/fetch_remote_status_service_spec.rb
@@ -1,4 +1,35 @@
 require 'rails_helper'
 
 RSpec.describe FetchRemoteStatusService do
+  let(:account) { Fabricate(:account) }
+  let(:prefetched_body) { nil }
+  let(:valid_domain) { Rails.configuration.x.local_domain }
+
+  let(:note) do
+    {
+      '@context': 'https://www.w3.org/ns/activitystreams',
+      id: "https://#{valid_domain}/@foo/1234",
+      type: 'Note',
+      content: 'Lorem ipsum',
+      attributedTo: ActivityPub::TagManager.instance.uri_for(account),
+    }
+  end
+
+  context 'protocol is :activitypub' do
+    subject { described_class.new.call(note[:id], prefetched_body, protocol) }
+    let(:prefetched_body) { Oj.dump(note) }
+    let(:protocol) { :activitypub }
+
+    before do
+      account.update(uri: ActivityPub::TagManager.instance.uri_for(account))
+      subject
+    end
+
+    it 'creates status' do
+      status = account.statuses.first
+
+      expect(status).to_not be_nil
+      expect(status.text).to eq 'Lorem ipsum'
+    end
+  end
 end
diff --git a/spec/services/precompute_feed_service_spec.rb b/spec/services/precompute_feed_service_spec.rb
index 396a3c3fb..43340bffc 100644
--- a/spec/services/precompute_feed_service_spec.rb
+++ b/spec/services/precompute_feed_service_spec.rb
@@ -9,14 +9,11 @@ RSpec.describe PrecomputeFeedService do
     let(:account) { Fabricate(:account) }
     it 'fills a user timeline with statuses' do
       account = Fabricate(:account)
-      followed_account = Fabricate(:account)
-      Fabricate(:follow, account: account, target_account: followed_account)
-      reblog = Fabricate(:status, account: followed_account)
-      status = Fabricate(:status, account: account, reblog: reblog)
+      status = Fabricate(:status, account: account)
 
       subject.call(account)
 
-      expect(Redis.current.zscore(FeedManager.instance.key(:home, account.id), reblog.id)).to be_within(0.1).of(status.id.to_f)
+      expect(Redis.current.zscore(FeedManager.instance.key(:home, account.id), status.id)).to be_within(0.1).of(status.id.to_f)
     end
 
     it 'does not raise an error even if it could not find any status' do
diff --git a/spec/services/report_service_spec.rb b/spec/services/report_service_spec.rb
new file mode 100644
index 000000000..2f926ef00
--- /dev/null
+++ b/spec/services/report_service_spec.rb
@@ -0,0 +1,25 @@
+require 'rails_helper'
+
+RSpec.describe ReportService do
+  subject { described_class.new }
+
+  let(:source_account) { Fabricate(:account) }
+
+  context 'for a remote account' do
+    let(:remote_account) { Fabricate(:account, domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox') }
+
+    before do
+      stub_request(:post, 'http://example.com/inbox').to_return(status: 200)
+    end
+
+    it 'sends ActivityPub payload when forward is true' do
+      subject.call(source_account, remote_account, forward: true)
+      expect(a_request(:post, 'http://example.com/inbox')).to have_been_made
+    end
+
+    it 'does not send anything when forward is false' do
+      subject.call(source_account, remote_account, forward: false)
+      expect(a_request(:post, 'http://example.com/inbox')).to_not have_been_made
+    end
+  end
+end
diff --git a/spec/services/resolve_remote_account_service_spec.rb b/spec/services/resolve_account_service_spec.rb
index d0bb6a137..5f1b4467b 100644
--- a/spec/services/resolve_remote_account_service_spec.rb
+++ b/spec/services/resolve_account_service_spec.rb
@@ -1,6 +1,6 @@
 require 'rails_helper'
 
-RSpec.describe ResolveRemoteAccountService do
+RSpec.describe ResolveAccountService do
   subject { described_class.new }
 
   before do
diff --git a/spec/services/fetch_remote_resource_service_spec.rb b/spec/services/resolve_url_service_spec.rb
index b80fb2475..1e9be4c07 100644
--- a/spec/services/fetch_remote_resource_service_spec.rb
+++ b/spec/services/resolve_url_service_spec.rb
@@ -2,7 +2,7 @@
 
 require 'rails_helper'
 
-describe FetchRemoteResourceService do
+describe ResolveURLService do
   subject { described_class.new }
 
   describe '#call' do
diff --git a/spec/services/search_service_spec.rb b/spec/services/search_service_spec.rb
index 3ffcc389b..957b60c7d 100644
--- a/spec/services/search_service_spec.rb
+++ b/spec/services/search_service_spec.rb
@@ -26,7 +26,7 @@ describe SearchService do
       context 'that does not find anything' do
         it 'returns the empty results' do
           service = double(call: nil)
-          allow(FetchRemoteResourceService).to receive(:new).and_return(service)
+          allow(ResolveURLService).to receive(:new).and_return(service)
           results = subject.call(@query, 10)
 
           expect(service).to have_received(:call).with(@query)
@@ -38,7 +38,7 @@ describe SearchService do
         it 'includes the account in the results' do
           account = Account.new
           service = double(call: account)
-          allow(FetchRemoteResourceService).to receive(:new).and_return(service)
+          allow(ResolveURLService).to receive(:new).and_return(service)
 
           results = subject.call(@query, 10)
           expect(service).to have_received(:call).with(@query)
@@ -50,7 +50,7 @@ describe SearchService do
         it 'includes the status in the results' do
           status = Status.new
           service = double(call: status)
-          allow(FetchRemoteResourceService).to receive(:new).and_return(service)
+          allow(ResolveURLService).to receive(:new).and_return(service)
 
           results = subject.call(@query, 10)
           expect(service).to have_received(:call).with(@query)