Refactor ActivityPub::ProcessAccountService and ActivityPub::FetchRemoteActorService (#39783)

This commit is contained in:
Claire authored and GitHub committed 2026-07-10 14:16:46 +00:00
1 parent cf41960f64
commit ea3d8a82c2
7 files changed
+199 -124

No files matched your search

@@ -33,20 +33,16 @@ RSpec.describe ActivityPub::FetchRemoteAccountService do
end
context 'when the account does not have a inbox' do
let!(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice', type: 'application/activity+json' }] } }
before do
actor[:inbox] = nil
stub_request(:get, 'https://example.com/alice').to_return(body: actor.to_json, headers: { 'Content-Type': 'application/activity+json' })
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: webfinger.to_json, headers: { 'Content-Type': 'application/jrd+json' })
end
it 'fetches resource and looks up webfinger and returns nil' do
it 'fetches resource but skips webfinger lookup and returns nil' do
expect(account).to be_nil
expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
end
end
@@ -33,20 +33,16 @@ RSpec.describe ActivityPub::FetchRemoteActorService do
end
context 'when the account does not have a inbox' do
let!(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice', type: 'application/activity+json' }] } }
before do
actor[:inbox] = nil
stub_request(:get, 'https://example.com/alice').to_return(body: actor.to_json, headers: { 'Content-Type': 'application/activity+json' })
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: webfinger.to_json, headers: { 'Content-Type': 'application/jrd+json' })
end
it 'fetches resource and looks up webfinger and returns nil' do
it 'fetches resource but skips webfinger lookup and returns nil' do
expect(account).to be_nil
expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
end
end
@@ -5,12 +5,27 @@ require 'rails_helper'
RSpec.describe ActivityPub::ProcessAccountService do
subject { described_class.new }
def stub_webfinger!
webfinger = {
subject: "acct:#{payload['preferredUsername']}@#{Addressable::URI.parse(payload['id']).host}",
links: [
{
rel: 'self',
href: payload['id'],
type: 'application/activity+json',
},
],
}.deep_stringify_keys
stub_request(:get, "#{Addressable::URI.parse(payload['id']).origin}/.well-known/webfinger?resource=#{webfinger['subject']}").to_return(body: webfinger.to_json, headers: { 'Content-Type': 'application/jrd+json' })
end
context 'with property values, an avatar, and a profile header' do
let(:payload) do
{
id: 'https://foo.test',
type: 'Actor',
inbox: 'https://foo.test/inbox',
preferredUsername: 'alice',
attachment: [
{ type: 'PropertyValue', name: 'Pronouns', value: 'They/them' },
{ type: 'PropertyValue', name: 'Occupation', value: 'Unit test' },
@@ -30,16 +45,17 @@ RSpec.describe ActivityPub::ProcessAccountService do
},
],
},
}.with_indifferent_access
}.deep_stringify_keys
end
before do
stub_webfinger!
stub_request(:get, 'https://foo.test/image.png').to_return(request_fixture('avatar.txt'))
stub_request(:get, 'https://foo.test/icon.png').to_return(request_fixture('avatar.txt'))
end
it 'parses property values, avatar and profile header as expected' do
account = subject.call('alice', 'example.com', payload)
account = subject.call(payload)
expect(account.fields)
.to be_an(Array)
@@ -68,6 +84,7 @@ RSpec.describe ActivityPub::ProcessAccountService do
{
'id' => 'https://foo.test',
'type' => 'Actor',
'preferredUsername' => 'alice',
'inbox' => 'https://foo.test/inbox',
'featured' => 'https://foo.test/featured',
'followers' => 'https://foo.test/followers',
@@ -77,12 +94,13 @@ RSpec.describe ActivityPub::ProcessAccountService do
end
before do
stub_webfinger!
stub_request(:get, %r{^https://foo\.test/follow})
.to_return(status: 200, body: '', headers: {})
end
it 'parses and sets the URIs, queues jobs to synchronize' do
account = subject.call('alice', 'example.com', payload)
account = subject.call(payload)
expect(account.featured_collection_url).to eq 'https://foo.test/featured'
expect(account.followers_url).to eq 'https://foo.test/followers'
@@ -108,11 +126,13 @@ RSpec.describe ActivityPub::ProcessAccountService do
owner: 'https://foo.test/actor',
publicKeyPem: public_key,
},
}.with_indifferent_access
}.deep_stringify_keys
end
before { stub_webfinger! }
it 'stores the key' do
account = subject.call('alice', 'example.com', payload)
account = subject.call(payload)
expect(account.public_key).to eq ''
expect(account.keypairs).to contain_exactly(
@@ -125,17 +145,17 @@ RSpec.describe ActivityPub::ProcessAccountService do
end
context 'when the account was known with a legacy key' do
let!(:alice) { Fabricate(:account, uri: 'https://foo.test/actor', domain: 'example.com', username: 'alice', legacy_keypair: true) }
let!(:alice) { Fabricate(:account, uri: 'https://foo.test/actor', domain: 'foo.test', username: 'alice', legacy_keypair: true) }
it 'invalidates the legacy key and stores the new key' do
expect { subject.call('alice', 'example.com', payload) }
expect { subject.call(payload) }
.to change { alice.reload.public_key }.to('')
.and change { alice.reload.keypairs.to_a }.from([]).to(contain_exactly(have_attributes({ uri: 'https://foo.test/actor#key1', type: 'rsa', public_key: })))
end
end
context 'when the account was known with an old key' do
let!(:alice) { Fabricate(:account, uri: 'https://foo.test/actor', domain: 'example.com', username: 'alice') }
let!(:alice) { Fabricate(:account, uri: 'https://foo.test/actor', domain: 'foo.test', username: 'alice') }
before do
alice.keypairs.delete_all
@@ -143,7 +163,7 @@ RSpec.describe ActivityPub::ProcessAccountService do
end
it 'invalidates the old key and stores the new key' do
expect { subject.call('alice', 'example.com', payload) }
expect { subject.call(payload) }
.to change { alice.reload.keypairs.to_a }.from(contain_exactly(have_attributes({ uri: 'https://foo.test/actor#old-key' }))).to(contain_exactly(have_attributes({ uri: 'https://foo.test/actor#key1', type: 'rsa', public_key: })))
expect(alice.reload.public_key)
@@ -175,11 +195,12 @@ RSpec.describe ActivityPub::ProcessAccountService do
end
before do
stub_webfinger!
stub_request(:get, key_id).to_return(status: 200, body: key_document.to_json, headers: { 'Content-Type': 'application/activity+json' })
end
it 'stores the key' do
account = subject.call('alice', 'example.com', payload)
account = subject.call(payload)
expect(account.public_key).to eq ''
expect(account.keypairs).to contain_exactly(
@@ -209,7 +230,7 @@ RSpec.describe ActivityPub::ProcessAccountService do
let(:key_document) { payload }
it 'stores the key' do
account = subject.call('alice', 'example.com', payload)
account = subject.call(payload)
expect(account.public_key).to eq ''
expect(account.keypairs).to contain_exactly(
@@ -223,17 +244,17 @@ RSpec.describe ActivityPub::ProcessAccountService do
end
context 'when the account was known with a legacy key' do
let!(:alice) { Fabricate(:account, uri: 'https://foo.test/actor', domain: 'example.com', username: 'alice', legacy_keypair: true) }
let!(:alice) { Fabricate(:account, uri: 'https://foo.test/actor', domain: 'foo.test', username: 'alice', legacy_keypair: true) }
it 'invalidates the legacy key and stores the new key' do
expect { subject.call('alice', 'example.com', payload) }
expect { subject.call(payload) }
.to change { alice.reload.public_key }.to('')
.and change { alice.reload.keypairs.to_a }.from([]).to(contain_exactly(have_attributes({ uri: key_id, type: 'rsa', public_key: })))
end
end
context 'when the account was known with an old key' do
let!(:alice) { Fabricate(:account, uri: 'https://foo.test/actor', domain: 'example.com', username: 'alice') }
let!(:alice) { Fabricate(:account, uri: 'https://foo.test/actor', domain: 'foo.test', username: 'alice') }
before do
alice.keypairs.delete_all
@@ -241,7 +262,7 @@ RSpec.describe ActivityPub::ProcessAccountService do
end
it 'invalidates the legacy key and stores the new key' do
expect { subject.call('alice', 'example.com', payload) }
expect { subject.call(payload) }
.to change { alice.reload.keypairs.to_a }.from(contain_exactly(have_attributes({ uri: 'https://foo.test/actor#old-key' }))).to(contain_exactly(have_attributes({ uri: key_id, type: 'rsa', public_key: })))
expect(alice.reload.public_key)
@@ -269,11 +290,13 @@ RSpec.describe ActivityPub::ProcessAccountService do
publicKeyPem: 'bar',
},
],
}.with_indifferent_access
}.deep_stringify_keys
end
before { stub_webfinger! }
it 'stores the keys' do
account = subject.call('alice', 'example.com', payload)
account = subject.call(payload)
expect(account.public_key).to eq ''
expect(account.keypairs).to contain_exactly(
@@ -291,10 +314,10 @@ RSpec.describe ActivityPub::ProcessAccountService do
end
context 'when the account was known with a legacy key' do
let!(:alice) { Fabricate(:account, uri: 'https://foo.test/actor', domain: 'example.com', username: 'alice', legacy_keypair: true) }
let!(:alice) { Fabricate(:account, uri: 'https://foo.test/actor', domain: 'foo.test', username: 'alice', legacy_keypair: true) }
it 'invalidates the legacy key and stores the new keys' do
expect { subject.call('alice', 'example.com', payload) }
expect { subject.call(payload) }
.to change { alice.reload.public_key }.to('')
.and change { alice.keypairs.to_a }.from([]).to(
contain_exactly(
@@ -327,11 +350,13 @@ RSpec.describe ActivityPub::ProcessAccountService do
publicKeyMultibase: 'z2MGw4gk84USotaWf4AkJ83DcnrfgGaceF86KQXRYMfQ7xqnUG81FVWa2N5inzNigXsDkm2LxpuyYSajqZr1CwHqnJbVEw1rhN25tbJSFyej6TejRh3k67CK9nTVHdXFoVKgAFxLwgiqJwCyyYWesaQKXAQfwXYqCBxPyaDjFfWkya6xeLaNuKFYGLcVzZZQjL99dnzUpNiENFPkVmJokE1wKPpHttGpLgm9sizHNDFuwHaz2ZZRnnZ6CT95FzdrMmaDXofn1ikbKBTdumuiRWSVwwZXffcXRN6Ti1a8NfhxQDdqhT7CAmM9NjQhnrqs1vss6YdcrHP5GmQN2Mz8GenQZFnyhJZK2iPxETnxq7YJRqTduN8KC8SMfjLVB8LD7rBM5d6s8dopdgJCVBpy2p', # rubocop:disable Layout/LineLength
},
],
}.with_indifferent_access
}.deep_stringify_keys
end
before { stub_webfinger! }
it 'stores the keys' do
account = subject.call('alice', 'example.com', payload)
account = subject.call(payload)
expect(account.public_key).to eq ''
expect(account.keypairs).to contain_exactly(
@@ -347,10 +372,10 @@ RSpec.describe ActivityPub::ProcessAccountService do
end
context 'when the account was known with a legacy key' do
let!(:alice) { Fabricate(:account, uri: 'https://foo.test/actor', domain: 'example.com', username: 'alice', legacy_keypair: true) }
let!(:alice) { Fabricate(:account, uri: 'https://foo.test/actor', domain: 'foo.test', username: 'alice', legacy_keypair: true) }
it 'invalidates the legacy key and stores the new keys' do
expect { subject.call('alice', 'example.com', payload) }
expect { subject.call(payload) }
.to change { alice.reload.public_key }.to('')
.and change { alice.keypairs.to_a }.from([]).to(
contain_exactly(
@@ -368,14 +393,17 @@ RSpec.describe ActivityPub::ProcessAccountService do
id: 'https://foo.test',
type: 'Actor',
inbox: 'https://foo.test/inbox',
preferredUsername: 'alice',
attributionDomains: [
'example.com',
],
}.with_indifferent_access
}.deep_stringify_keys
end
before { stub_webfinger! }
it 'parses attribution domains' do
account = subject.call('alice', 'example.com', payload)
account = subject.call(payload)
expect(account.attribution_domains)
.to match_array(%w(example.com))
@@ -388,14 +416,17 @@ RSpec.describe ActivityPub::ProcessAccountService do
id: 'https://foo.test',
type: 'Actor',
inbox: 'https://foo.test/inbox',
preferredUsername: 'alice',
showMedia: true,
showRepliesInMedia: false,
showFeatured: false,
}.with_indifferent_access
}.deep_stringify_keys
end
before { stub_webfinger! }
it 'sets the profile settings as expected' do
account = subject.call('alice', 'example.com', payload)
account = subject.call(payload)
expect(account)
.to have_attributes(
@@ -412,6 +443,7 @@ RSpec.describe ActivityPub::ProcessAccountService do
id: 'https://foo.test',
type: 'Actor',
inbox: 'https://foo.test/inbox',
preferredUsername: 'alice',
featured: {
type: 'OrderedCollection',
orderedItems: ['https://example.com/statuses/1'],
@@ -419,8 +451,10 @@ RSpec.describe ActivityPub::ProcessAccountService do
}.deep_stringify_keys
end
before { stub_webfinger! }
it 'queues featured collection synchronization', :aggregate_failures do
account = subject.call('alice', 'example.com', payload)
account = subject.call(payload)
expect(account.featured_collection_url).to eq ''
expect(ActivityPub::SynchronizeFeaturedCollectionWorker).to have_enqueued_sidekiq_job(account.id, { 'hashtag' => true, 'request_id' => anything, 'collection' => payload['featured'] })
@@ -428,20 +462,23 @@ RSpec.describe ActivityPub::ProcessAccountService do
end
context 'when account is not suspended' do
subject { described_class.new.call(account.username, account.domain, payload) }
let!(:account) { Fabricate(:account, username: 'alice', domain: 'example.com') }
subject { described_class.new.call(payload) }
let(:payload) do
{
id: 'https://foo.test',
id: 'https://example.com',
preferredUsername: 'alice',
type: 'Actor',
inbox: 'https://foo.test/inbox',
inbox: 'https://example.com/inbox',
suspended: true,
}.with_indifferent_access
}.deep_stringify_keys
end
before do
stub_webfinger!
Fabricate(:account, username: 'alice', domain: 'example.com')
allow(Admin::SuspensionWorker).to receive(:perform_async)
end
@@ -457,21 +494,24 @@ RSpec.describe ActivityPub::ProcessAccountService do
end
context 'when account is suspended' do
subject { described_class.new.call('alice', 'example.com', payload) }
subject { described_class.new.call(payload) }
let!(:account) { Fabricate(:account, username: 'alice', domain: 'example.com', display_name: '') }
let(:payload) do
{
id: 'https://foo.test',
id: 'https://example.com',
preferredUsername: 'alice',
type: 'Actor',
inbox: 'https://foo.test/inbox',
inbox: 'https://example.com/inbox',
suspended: false,
name: 'Hoge',
}.with_indifferent_access
}.deep_stringify_keys
end
before do
stub_webfinger!
allow(Admin::UnsuspensionWorker).to receive(:perform_async)
account.suspend!(origin: suspension_origin)
@@ -513,15 +553,27 @@ RSpec.describe ActivityPub::ProcessAccountService do
domain = "test#{i}.testdomain.com"
json = {
id: "https://#{domain}/users/1",
preferredUsername: 'alice',
type: 'Actor',
inbox: "https://#{domain}/inbox",
}.with_indifferent_access
described_class.new.call('alice', domain, json)
}.deep_stringify_keys
described_class.new.call(json)
end
end
before do
stub_const 'ActivityPub::ProcessAccountService::SUBDOMAINS_RATELIMIT', 5
8.times do |i|
domain = "test#{i}.testdomain.com"
webfinger = {
subject: "acct:alice@#{domain}",
links: [{ rel: 'self', href: "https://#{domain}/users/1", type: 'application/activity+json' }],
}.deep_stringify_keys
stub_request(:get, "https://#{domain}/.well-known/webfinger?resource=acct:alice@#{domain}").to_return(body: webfinger.to_json, headers: { 'Content-Type': 'application/jrd+json' })
end
end
it 'creates accounts without exceeding rate limit' do
@@ -540,7 +592,7 @@ RSpec.describe ActivityPub::ProcessAccountService do
inbox: 'https://foo.test/inbox',
featured: 'https://foo.test/users/1/featured',
preferredUsername: 'user1',
}.with_indifferent_access
}.deep_stringify_keys
end
before do
@@ -554,7 +606,7 @@ RSpec.describe ActivityPub::ProcessAccountService do
inbox: 'https://foo.test/inbox',
featured: "https://foo.test/users/#{i}/featured",
preferredUsername: "user#{i}",
}.with_indifferent_access
}.deep_stringify_keys
status_json = {
'@context': ['https://www.w3.org/ns/activitystreams'],
id: "https://foo.test/users/#{i}/status",
@@ -569,18 +621,18 @@ RSpec.describe ActivityPub::ProcessAccountService do
},
],
to: ['as:Public', "https://foo.test/users/#{i + 1}"],
}.with_indifferent_access
}.deep_stringify_keys
featured_json = {
'@context': ['https://www.w3.org/ns/activitystreams'],
id: "https://foo.test/users/#{i}/featured",
type: 'OrderedCollection',
totalItems: 1,
orderedItems: [status_json],
}.with_indifferent_access
}.deep_stringify_keys
webfinger = {
subject: "acct:user#{i}@foo.test",
links: [{ rel: 'self', href: "https://foo.test/users/#{i}", type: 'application/activity+json' }],
}.with_indifferent_access
}.deep_stringify_keys
stub_request(:get, "https://foo.test/users/#{i}").to_return(status: 200, body: actor_json.to_json, headers: { 'Content-Type': 'application/activity+json' })
stub_request(:get, "https://foo.test/users/#{i}/featured").to_return(status: 200, body: featured_json.to_json, headers: { 'Content-Type': 'application/activity+json' })
stub_request(:get, "https://foo.test/users/#{i}/status").to_return(status: 200, body: status_json.to_json, headers: { 'Content-Type': 'application/activity+json' })
@@ -589,7 +641,7 @@ RSpec.describe ActivityPub::ProcessAccountService do
end
it 'creates accounts without exceeding rate limit', :inline_jobs do
expect { subject.call('user1', 'foo.test', payload) }
expect { subject.call(payload) }
.to create_some_remote_accounts
.and create_fewer_than_rate_limit_accounts
end
@@ -599,6 +651,7 @@ RSpec.describe ActivityPub::ProcessAccountService do
let(:payload) do
{
id: 'https://foo.test',
preferredUsername: 'alice',
type: 'Actor',
inbox: 'https://foo.test/inbox',
followers: 'https://foo.test/followers',
@@ -612,17 +665,19 @@ RSpec.describe ActivityPub::ProcessAccountService do
],
},
},
}.with_indifferent_access
}.deep_stringify_keys
end
before do
stub_webfinger!
stub_request(:get, %r{^https://foo\.test/follow})
.to_return(status: 200, body: '', headers: {})
end
context 'when collections feature is enabled' do
it 'sets the interaction policy to the correct value' do
account = subject.call('user1', 'foo.test', payload)
account = subject.call(payload)
expect(account.feature_approval_policy).to eq 0b100000000000000001100
end