2023-02-22 00:55:31 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-09-21 20:45:57 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2024-03-13 08:39:26 +00:00
|
|
|
RSpec.describe ActivityPub::FetchRemoteActorService do
|
2023-06-06 11:58:33 +00:00
|
|
|
subject { described_class.new }
|
2022-09-21 20:45:57 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
describe '#call' do
|
2024-02-01 14:56:46 +00:00
|
|
|
let(:account) { subject.call('https://example.com/alice') }
|
2022-09-21 20:45:57 +00:00
|
|
|
|
|
|
|
shared_examples 'sets profile data' do
|
2024-01-26 17:44:12 +00:00
|
|
|
it 'returns an account and sets attributes' do
|
|
|
|
expect(account)
|
|
|
|
.to be_an(Account)
|
|
|
|
.and have_attributes(
|
|
|
|
display_name: eq('Alice'),
|
|
|
|
note: eq('Foo bar'),
|
|
|
|
url: eq('https://example.com/alice')
|
|
|
|
)
|
2022-09-21 20:45:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account does not have a inbox' do
|
2024-07-23 14:42:31 +00:00
|
|
|
let!(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice', type: 'application/activity+json' }] } }
|
2022-09-21 20:45:57 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
actor[:inbox] = nil
|
|
|
|
|
2024-02-16 10:56:12 +00:00
|
|
|
stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor), headers: { 'Content-Type': 'application/activity+json' })
|
2022-09-21 20:45:57 +00:00
|
|
|
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
|
|
|
|
|
2024-01-26 17:44:12 +00:00
|
|
|
it 'fetches resource and looks up webfinger and returns nil' do
|
|
|
|
expect(account).to be_nil
|
2022-09-21 20:45:57 +00:00
|
|
|
|
2024-01-26 17:44:12 +00:00
|
|
|
expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
|
2022-09-21 20:45:57 +00:00
|
|
|
expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when URI and WebFinger share the same host' do
|
2024-07-23 14:42:31 +00:00
|
|
|
let!(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice', type: 'application/activity+json' }] } }
|
2022-09-21 20:45:57 +00:00
|
|
|
|
|
|
|
before do
|
2024-02-16 10:56:12 +00:00
|
|
|
stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor), headers: { 'Content-Type': 'application/activity+json' })
|
2022-09-21 20:45:57 +00:00
|
|
|
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
|
|
|
|
|
2024-01-26 17:44:12 +00:00
|
|
|
it 'fetches resource and looks up webfinger and sets values' do
|
2022-09-21 20:45:57 +00:00
|
|
|
account
|
|
|
|
|
2024-01-26 17:44:12 +00:00
|
|
|
expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
|
2022-09-21 20:45:57 +00:00
|
|
|
expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
|
|
|
|
|
|
|
|
expect(account.username).to eq 'alice'
|
|
|
|
expect(account.domain).to eq 'example.com'
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'sets profile data'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when WebFinger presents different domain than URI' do
|
2024-07-23 14:42:31 +00:00
|
|
|
let!(:webfinger) { { subject: 'acct:alice@iscool.af', links: [{ rel: 'self', href: 'https://example.com/alice', type: 'application/activity+json' }] } }
|
2022-09-21 20:45:57 +00:00
|
|
|
|
|
|
|
before do
|
2024-02-16 10:56:12 +00:00
|
|
|
stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor), headers: { 'Content-Type': 'application/activity+json' })
|
2022-09-21 20:45:57 +00:00
|
|
|
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' })
|
|
|
|
stub_request(:get, 'https://iscool.af/.well-known/webfinger?resource=acct:alice@iscool.af').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
|
|
|
|
end
|
|
|
|
|
2024-01-26 17:44:12 +00:00
|
|
|
it 'fetches resource and looks up webfinger and follows redirect and sets values' do
|
2022-09-21 20:45:57 +00:00
|
|
|
account
|
|
|
|
|
2024-01-26 17:44:12 +00:00
|
|
|
expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
|
2022-09-21 20:45:57 +00:00
|
|
|
expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
|
|
|
|
expect(a_request(:get, 'https://iscool.af/.well-known/webfinger?resource=acct:alice@iscool.af')).to have_been_made.once
|
|
|
|
|
|
|
|
expect(account.username).to eq 'alice'
|
|
|
|
expect(account.domain).to eq 'iscool.af'
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'sets profile data'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when WebFinger returns a different URI' do
|
2024-07-23 14:42:31 +00:00
|
|
|
let!(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/bob', type: 'application/activity+json' }] } }
|
2022-09-21 20:45:57 +00:00
|
|
|
|
|
|
|
before do
|
2024-02-16 10:56:12 +00:00
|
|
|
stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor), headers: { 'Content-Type': 'application/activity+json' })
|
2022-09-21 20:45:57 +00:00
|
|
|
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
|
|
|
|
|
2024-01-26 17:44:12 +00:00
|
|
|
it 'fetches resource and looks up webfinger and does not create account' do
|
|
|
|
expect(account).to be_nil
|
2022-09-21 20:45:57 +00:00
|
|
|
|
2024-01-26 17:44:12 +00:00
|
|
|
expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
|
2022-09-21 20:45:57 +00:00
|
|
|
expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when WebFinger returns a different URI after a redirection' do
|
2024-07-23 14:42:31 +00:00
|
|
|
let!(:webfinger) { { subject: 'acct:alice@iscool.af', links: [{ rel: 'self', href: 'https://example.com/bob', type: 'application/activity+json' }] } }
|
2022-09-21 20:45:57 +00:00
|
|
|
|
|
|
|
before do
|
2024-02-16 10:56:12 +00:00
|
|
|
stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor), headers: { 'Content-Type': 'application/activity+json' })
|
2022-09-21 20:45:57 +00:00
|
|
|
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' })
|
|
|
|
stub_request(:get, 'https://iscool.af/.well-known/webfinger?resource=acct:alice@iscool.af').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
|
|
|
|
end
|
|
|
|
|
2024-01-26 17:44:12 +00:00
|
|
|
it 'fetches resource and looks up webfinger and follows redirect and does not create account' do
|
|
|
|
expect(account).to be_nil
|
2022-09-21 20:45:57 +00:00
|
|
|
|
2024-01-26 17:44:12 +00:00
|
|
|
expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
|
2022-09-21 20:45:57 +00:00
|
|
|
expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
|
|
|
|
expect(a_request(:get, 'https://iscool.af/.well-known/webfinger?resource=acct:alice@iscool.af')).to have_been_made.once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with wrong id' do
|
|
|
|
it 'does not create account' do
|
|
|
|
expect(subject.call('https://fake.address/@foo', prefetched_body: Oj.dump(actor))).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|