2023-02-22 11:55:31 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-09 05:52:15 +10:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2024-03-13 19:39:26 +11:00
|
|
|
RSpec.describe ActivityPub::ProcessAccountService do
|
2018-04-14 20:41:08 +10:00
|
|
|
subject { described_class.new }
|
|
|
|
|
2024-02-23 08:27:24 +11:00
|
|
|
context 'with property values, an avatar, and a profile header' do
|
2018-04-14 20:41:08 +10:00
|
|
|
let(:payload) do
|
|
|
|
{
|
2018-04-25 22:12:28 +10:00
|
|
|
id: 'https://foo.test',
|
2018-04-14 20:41:08 +10:00
|
|
|
type: 'Actor',
|
2018-04-25 22:12:28 +10:00
|
|
|
inbox: 'https://foo.test/inbox',
|
2018-04-14 20:41:08 +10:00
|
|
|
attachment: [
|
|
|
|
{ type: 'PropertyValue', name: 'Pronouns', value: 'They/them' },
|
|
|
|
{ type: 'PropertyValue', name: 'Occupation', value: 'Unit test' },
|
2023-02-20 16:14:10 +11:00
|
|
|
{ type: 'PropertyValue', name: 'non-string', value: %w(foo bar) },
|
2018-04-14 20:41:08 +10:00
|
|
|
],
|
2024-02-23 08:27:24 +11:00
|
|
|
image: {
|
|
|
|
type: 'Image',
|
|
|
|
mediaType: 'image/png',
|
|
|
|
url: 'https://foo.test/image.png',
|
|
|
|
},
|
|
|
|
icon: {
|
|
|
|
type: 'Image',
|
|
|
|
url: [
|
|
|
|
{
|
|
|
|
mediaType: 'image/png',
|
|
|
|
href: 'https://foo.test/icon.png',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2018-04-14 20:41:08 +10:00
|
|
|
}.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
2024-02-23 08:27:24 +11:00
|
|
|
before do
|
|
|
|
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
|
2018-04-14 20:41:08 +10:00
|
|
|
account = subject.call('alice', 'example.com', payload)
|
2024-02-07 00:04:02 +11:00
|
|
|
|
|
|
|
expect(account.fields)
|
|
|
|
.to be_an(Array)
|
|
|
|
.and have_attributes(size: 2)
|
|
|
|
expect(account.fields.first)
|
|
|
|
.to be_an(Account::Field)
|
|
|
|
.and have_attributes(
|
|
|
|
name: eq('Pronouns'),
|
|
|
|
value: eq('They/them')
|
|
|
|
)
|
|
|
|
expect(account.fields.last)
|
|
|
|
.to be_an(Account::Field)
|
|
|
|
.and have_attributes(
|
|
|
|
name: eq('Occupation'),
|
|
|
|
value: eq('Unit test')
|
|
|
|
)
|
2024-02-23 08:27:24 +11:00
|
|
|
expect(account).to have_attributes(
|
|
|
|
avatar_remote_url: 'https://foo.test/icon.png',
|
|
|
|
header_remote_url: 'https://foo.test/image.png'
|
|
|
|
)
|
2019-03-30 12:12:06 +11:00
|
|
|
end
|
|
|
|
end
|
2020-11-08 10:28:39 +11:00
|
|
|
|
2024-09-10 22:00:40 +10:00
|
|
|
context 'with attribution domains' do
|
|
|
|
let(:payload) do
|
|
|
|
{
|
|
|
|
id: 'https://foo.test',
|
|
|
|
type: 'Actor',
|
|
|
|
inbox: 'https://foo.test/inbox',
|
|
|
|
attributionDomains: [
|
|
|
|
'example.com',
|
|
|
|
],
|
|
|
|
}.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses attribution domains' do
|
|
|
|
account = subject.call('alice', 'example.com', payload)
|
|
|
|
|
|
|
|
expect(account.attribution_domains)
|
|
|
|
.to match_array(%w(example.com))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-08 10:28:39 +11:00
|
|
|
context 'when account is not suspended' do
|
2023-12-21 22:20:12 +11:00
|
|
|
subject { described_class.new.call(account.username, account.domain, payload) }
|
2023-02-20 15:24:14 +11:00
|
|
|
|
2020-11-08 10:28:39 +11:00
|
|
|
let!(:account) { Fabricate(:account, username: 'alice', domain: 'example.com') }
|
|
|
|
|
|
|
|
let(:payload) do
|
|
|
|
{
|
|
|
|
id: 'https://foo.test',
|
|
|
|
type: 'Actor',
|
|
|
|
inbox: 'https://foo.test/inbox',
|
|
|
|
suspended: true,
|
|
|
|
}.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Admin::SuspensionWorker).to receive(:perform_async)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'suspends account remotely' do
|
|
|
|
expect(subject.suspended?).to be true
|
|
|
|
expect(subject.suspension_origin_remote?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'queues suspension worker' do
|
|
|
|
subject
|
|
|
|
expect(Admin::SuspensionWorker).to have_received(:perform_async)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when account is suspended' do
|
2023-02-20 15:24:14 +11:00
|
|
|
subject { described_class.new.call('alice', 'example.com', payload) }
|
|
|
|
|
2020-11-08 10:28:39 +11:00
|
|
|
let!(:account) { Fabricate(:account, username: 'alice', domain: 'example.com', display_name: '') }
|
|
|
|
|
|
|
|
let(:payload) do
|
|
|
|
{
|
|
|
|
id: 'https://foo.test',
|
|
|
|
type: 'Actor',
|
|
|
|
inbox: 'https://foo.test/inbox',
|
|
|
|
suspended: false,
|
|
|
|
name: 'Hoge',
|
|
|
|
}.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Admin::UnsuspensionWorker).to receive(:perform_async)
|
|
|
|
|
|
|
|
account.suspend!(origin: suspension_origin)
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'when locally' do
|
2020-11-08 10:28:39 +11:00
|
|
|
let(:suspension_origin) { :local }
|
|
|
|
|
|
|
|
it 'does not unsuspend it' do
|
|
|
|
expect(subject.suspended?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update any attributes' do
|
|
|
|
expect(subject.display_name).to_not eq 'Hoge'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'when remotely' do
|
2020-11-08 10:28:39 +11:00
|
|
|
let(:suspension_origin) { :remote }
|
|
|
|
|
|
|
|
it 'unsuspends it' do
|
|
|
|
expect(subject.suspended?).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'queues unsuspension worker' do
|
|
|
|
subject
|
|
|
|
expect(Admin::UnsuspensionWorker).to have_received(:perform_async)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates attributes' do
|
|
|
|
expect(subject.display_name).to eq 'Hoge'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-07 10:15:24 +11:00
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'when discovering many subdomains in a short timeframe' do
|
2023-07-12 17:49:33 +10:00
|
|
|
subject do
|
2022-12-07 10:15:24 +11:00
|
|
|
8.times do |i|
|
|
|
|
domain = "test#{i}.testdomain.com"
|
|
|
|
json = {
|
|
|
|
id: "https://#{domain}/users/1",
|
|
|
|
type: 'Actor',
|
|
|
|
inbox: "https://#{domain}/inbox",
|
|
|
|
}.with_indifferent_access
|
|
|
|
described_class.new.call('alice', domain, json)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-12 17:49:33 +10:00
|
|
|
before do
|
|
|
|
stub_const 'ActivityPub::ProcessAccountService::SUBDOMAINS_RATELIMIT', 5
|
|
|
|
end
|
|
|
|
|
2023-12-06 19:44:51 +11:00
|
|
|
it 'creates accounts without exceeding rate limit' do
|
|
|
|
expect { subject }
|
|
|
|
.to create_some_remote_accounts
|
|
|
|
.and create_fewer_than_rate_limit_accounts
|
2022-12-07 10:15:24 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'when Accounts referencing other accounts' do
|
2022-12-07 10:15:24 +11:00
|
|
|
let(:payload) do
|
|
|
|
{
|
|
|
|
'@context': ['https://www.w3.org/ns/activitystreams'],
|
|
|
|
id: 'https://foo.test/users/1',
|
|
|
|
type: 'Person',
|
|
|
|
inbox: 'https://foo.test/inbox',
|
|
|
|
featured: 'https://foo.test/users/1/featured',
|
|
|
|
preferredUsername: 'user1',
|
|
|
|
}.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2023-05-11 18:32:09 +10:00
|
|
|
stub_const 'ActivityPub::ProcessAccountService::DISCOVERIES_PER_REQUEST', 5
|
|
|
|
|
2022-12-07 10:15:24 +11:00
|
|
|
8.times do |i|
|
|
|
|
actor_json = {
|
|
|
|
'@context': ['https://www.w3.org/ns/activitystreams'],
|
|
|
|
id: "https://foo.test/users/#{i}",
|
|
|
|
type: 'Person',
|
|
|
|
inbox: 'https://foo.test/inbox',
|
|
|
|
featured: "https://foo.test/users/#{i}/featured",
|
|
|
|
preferredUsername: "user#{i}",
|
|
|
|
}.with_indifferent_access
|
|
|
|
status_json = {
|
|
|
|
'@context': ['https://www.w3.org/ns/activitystreams'],
|
|
|
|
id: "https://foo.test/users/#{i}/status",
|
|
|
|
attributedTo: "https://foo.test/users/#{i}",
|
|
|
|
type: 'Note',
|
|
|
|
content: "@user#{i + 1} test",
|
|
|
|
tag: [
|
|
|
|
{
|
|
|
|
type: 'Mention',
|
|
|
|
href: "https://foo.test/users/#{i + 1}",
|
2023-02-18 08:56:20 +11:00
|
|
|
name: "@user#{i + 1}",
|
2023-02-19 08:54:30 +11:00
|
|
|
},
|
2022-12-07 10:15:24 +11:00
|
|
|
],
|
2023-02-19 01:33:41 +11:00
|
|
|
to: ['as:Public', "https://foo.test/users/#{i + 1}"],
|
2022-12-07 10:15:24 +11:00
|
|
|
}.with_indifferent_access
|
|
|
|
featured_json = {
|
|
|
|
'@context': ['https://www.w3.org/ns/activitystreams'],
|
|
|
|
id: "https://foo.test/users/#{i}/featured",
|
|
|
|
type: 'OrderedCollection',
|
2023-05-20 01:13:29 +10:00
|
|
|
totalItems: 1,
|
2022-12-07 10:15:24 +11:00
|
|
|
orderedItems: [status_json],
|
|
|
|
}.with_indifferent_access
|
|
|
|
webfinger = {
|
|
|
|
subject: "acct:user#{i}@foo.test",
|
2024-07-24 00:42:31 +10:00
|
|
|
links: [{ rel: 'self', href: "https://foo.test/users/#{i}", type: 'application/activity+json' }],
|
2022-12-07 10:15:24 +11:00
|
|
|
}.with_indifferent_access
|
|
|
|
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' })
|
|
|
|
stub_request(:get, "https://foo.test/.well-known/webfinger?resource=acct:user#{i}@foo.test").to_return(body: webfinger.to_json, headers: { 'Content-Type': 'application/jrd+json' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-09 02:01:08 +10:00
|
|
|
it 'creates accounts without exceeding rate limit', :inline_jobs do
|
2023-12-06 19:44:51 +11:00
|
|
|
expect { subject.call('user1', 'foo.test', payload) }
|
|
|
|
.to create_some_remote_accounts
|
|
|
|
.and create_fewer_than_rate_limit_accounts
|
2022-12-07 10:15:24 +11:00
|
|
|
end
|
2023-12-06 19:44:51 +11:00
|
|
|
end
|
2022-12-07 10:15:24 +11:00
|
|
|
|
2023-12-06 19:44:51 +11:00
|
|
|
private
|
|
|
|
|
|
|
|
def create_some_remote_accounts
|
|
|
|
change(Account.remote, :count).by_at_least(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_fewer_than_rate_limit_accounts
|
|
|
|
change(Account.remote, :count).by_at_most(5)
|
2022-12-07 10:15:24 +11:00
|
|
|
end
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|