about summary refs log tree commit diff
path: root/spec
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2022-05-04 12:00:22 -0500
committerStarfall <us@starfall.systems>2022-05-04 12:00:22 -0500
commite9b2e11520056d0ec822ac0862923d00c6a1289c (patch)
tree53b22a4fecda9846fc6c77cf4067c071a6287df0 /spec
parent9ec0ecda66745c892961a917b18354f42dfc1f19 (diff)
parent58ac5ae643dc57ffd20017c54e7be523deaef156 (diff)
Merge remote-tracking branch 'glitch/main'
Diffstat (limited to 'spec')
-rw-r--r--spec/services/activitypub/fetch_featured_collection_service_spec.rb123
-rw-r--r--spec/services/resolve_account_service_spec.rb2
-rw-r--r--spec/validators/email_mx_validator_spec.rb60
3 files changed, 125 insertions, 60 deletions
diff --git a/spec/services/activitypub/fetch_featured_collection_service_spec.rb b/spec/services/activitypub/fetch_featured_collection_service_spec.rb
new file mode 100644
index 000000000..f552b9dc0
--- /dev/null
+++ b/spec/services/activitypub/fetch_featured_collection_service_spec.rb
@@ -0,0 +1,123 @@
+require 'rails_helper'
+
+RSpec.describe ActivityPub::FetchFeaturedCollectionService, type: :service do
+  let(:actor) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/account', featured_collection_url: 'https://example.com/account/pinned') }
+
+  let!(:known_status) { Fabricate(:status, account: actor, uri: 'https://example.com/account/pinned/1') }
+
+  let(:status_json_1) do
+    {
+      '@context': 'https://www.w3.org/ns/activitystreams',
+      type: 'Note',
+      id: 'https://example.com/account/pinned/1',
+      content: 'foo',
+      attributedTo: actor.uri,
+      to: 'https://www.w3.org/ns/activitystreams#Public',
+    }
+  end
+
+  let(:status_json_2) do
+    {
+      '@context': 'https://www.w3.org/ns/activitystreams',
+      type: 'Note',
+      id: 'https://example.com/account/pinned/2',
+      content: 'foo',
+      attributedTo: actor.uri,
+      to: 'https://www.w3.org/ns/activitystreams#Public',
+    }
+  end
+
+  let(:status_json_4) do
+    {
+      '@context': 'https://www.w3.org/ns/activitystreams',
+      type: 'Note',
+      id: 'https://example.com/account/pinned/4',
+      content: 'foo',
+      attributedTo: actor.uri,
+      to: 'https://www.w3.org/ns/activitystreams#Public',
+    }
+  end
+
+  let(:items) do
+    [
+      'https://example.com/account/pinned/1', # known
+      status_json_2, # unknown inlined
+      'https://example.com/account/pinned/3', # unknown unreachable
+      'https://example.com/account/pinned/4', # unknown reachable
+    ]
+  end
+
+  let(:payload) do
+    {
+      '@context': 'https://www.w3.org/ns/activitystreams',
+      type: 'Collection',
+      id: actor.featured_collection_url,
+      items: items,
+    }.with_indifferent_access
+  end
+
+  subject { described_class.new }
+
+  shared_examples 'sets pinned posts' do
+    before do
+      stub_request(:get, 'https://example.com/account/pinned/1').to_return(status: 200, body: Oj.dump(status_json_1))
+      stub_request(:get, 'https://example.com/account/pinned/2').to_return(status: 200, body: Oj.dump(status_json_2))
+      stub_request(:get, 'https://example.com/account/pinned/3').to_return(status: 404)
+      stub_request(:get, 'https://example.com/account/pinned/4').to_return(status: 200, body: Oj.dump(status_json_4))
+
+      subject.call(actor)
+    end
+
+    it 'sets expected posts as pinned posts' do
+      expect(actor.pinned_statuses.pluck(:uri)).to match_array ['https://example.com/account/pinned/1', 'https://example.com/account/pinned/2', 'https://example.com/account/pinned/4']
+    end
+  end
+
+  describe '#call' do
+    context 'when the endpoint is a Collection' do
+      before do
+        stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: Oj.dump(payload))
+      end
+
+      it_behaves_like 'sets pinned posts'
+    end
+
+    context 'when the endpoint is an OrderedCollection' do
+      let(:payload) do
+        {
+          '@context': 'https://www.w3.org/ns/activitystreams',
+          type: 'OrderedCollection',
+          id: actor.featured_collection_url,
+          orderedItems: items,
+        }.with_indifferent_access
+      end
+
+      before do
+        stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: Oj.dump(payload))
+      end
+
+      it_behaves_like 'sets pinned posts'
+    end
+
+    context 'when the endpoint is a paginated Collection' do
+      let(:payload) do
+        {
+          '@context': 'https://www.w3.org/ns/activitystreams',
+          type: 'Collection',
+          id: actor.featured_collection_url,
+          first: {
+            type: 'CollectionPage',
+            partOf: actor.featured_collection_url,
+            items: items,
+          }
+        }.with_indifferent_access
+      end
+
+      before do
+        stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: Oj.dump(payload))
+      end
+
+      it_behaves_like 'sets pinned posts'
+    end
+  end
+end
diff --git a/spec/services/resolve_account_service_spec.rb b/spec/services/resolve_account_service_spec.rb
index 7b1e8885c..8c302e1d8 100644
--- a/spec/services/resolve_account_service_spec.rb
+++ b/spec/services/resolve_account_service_spec.rb
@@ -220,6 +220,8 @@ RSpec.describe ResolveAccountService, type: :service do
           return_values << described_class.new.call('foo@ap.example.com')
         rescue ActiveRecord::RecordNotUnique
           fail_occurred = true
+        ensure
+          RedisConfiguration.pool.checkin if Thread.current[:redis]
         end
       end
     end
diff --git a/spec/validators/email_mx_validator_spec.rb b/spec/validators/email_mx_validator_spec.rb
index af0eb98f5..4feedd0c7 100644
--- a/spec/validators/email_mx_validator_spec.rb
+++ b/spec/validators/email_mx_validator_spec.rb
@@ -56,66 +56,6 @@ describe EmailMxValidator do
       expect(user.errors).to have_received(:add)
     end
 
-    it 'adds an error if the A record is blacklisted' do
-      EmailDomainBlock.create!(domain: 'alternate-example.com', ips: ['1.2.3.4'])
-      resolver = double
-
-      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([])
-      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '1.2.3.4')])
-      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
-      allow(resolver).to receive(:timeouts=).and_return(nil)
-      allow(Resolv::DNS).to receive(:open).and_yield(resolver)
-
-      subject.validate(user)
-      expect(user.errors).to have_received(:add)
-    end
-
-    it 'adds an error if the AAAA record is blacklisted' do
-      EmailDomainBlock.create!(domain: 'alternate-example.com', ips: ['fd00::1'])
-      resolver = double
-
-      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([])
-      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
-      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([double(address: 'fd00::1')])
-      allow(resolver).to receive(:timeouts=).and_return(nil)
-      allow(Resolv::DNS).to receive(:open).and_yield(resolver)
-
-      subject.validate(user)
-      expect(user.errors).to have_received(:add)
-    end
-
-    it 'adds an error if the A record of the MX record is blacklisted' do
-      EmailDomainBlock.create!(domain: 'mail.other-domain.com', ips: ['2.3.4.5'])
-      resolver = double
-
-      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
-      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
-      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
-      allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '2.3.4.5')])
-      allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
-      allow(resolver).to receive(:timeouts=).and_return(nil)
-      allow(Resolv::DNS).to receive(:open).and_yield(resolver)
-
-      subject.validate(user)
-      expect(user.errors).to have_received(:add)
-    end
-
-    it 'adds an error if the AAAA record of the MX record is blacklisted' do
-      EmailDomainBlock.create!(domain: 'mail.other-domain.com', ips: ['fd00::2'])
-      resolver = double
-
-      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
-      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
-      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
-      allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([])
-      allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([double(address: 'fd00::2')])
-      allow(resolver).to receive(:timeouts=).and_return(nil)
-      allow(Resolv::DNS).to receive(:open).and_yield(resolver)
-
-      subject.validate(user)
-      expect(user.errors).to have_received(:add)
-    end
-
     it 'adds an error if the MX record is blacklisted' do
       EmailDomainBlock.create!(domain: 'mail.example.com')
       resolver = double