Clean up Webfinger lib spec (#38259)

This commit is contained in:
Matt Jankowski
2026-03-18 09:23:06 -04:00
committed by GitHub
parent bd9b24f1ef
commit 815c2cf8e9
2 changed files with 45 additions and 18 deletions

View File

@@ -3,45 +3,72 @@
require 'rails_helper'
RSpec.describe Webfinger do
describe 'self link' do
describe '#initialize' do
subject { described_class.new(uri) }
context 'when called with local account' do
let(:uri) { 'acct:alice' }
it 'handles value and raises error' do
expect { subject }.to raise_error(ArgumentError, /for local account/)
end
end
context 'when called with remote account' do
let(:uri) { 'acct:alice@host.example' }
it 'handles value and sets attributes' do
expect { subject }.to_not raise_error
end
end
end
describe '#perform' do
subject { described_class.new('acct:alice@example.com').perform }
context 'when self link is specified with type application/activity+json' do
let!(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice', type: 'application/activity+json' }] } }
let(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice', type: 'application/activity+json' }] } }
before do
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 'correctly parses the response' do
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: JSON.generate(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
expect(subject.self_link_href).to eq 'https://example.com/alice'
end
end
context 'when self link is specified with type application/ld+json' do
let!(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice', type: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' }] } }
let(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice', type: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' }] } }
before do
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 'correctly parses the response' do
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: JSON.generate(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
expect(subject.self_link_href).to eq 'https://example.com/alice'
end
end
context 'when self link is specified with incorrect type' do
let!(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice', type: 'application/json"' }] } }
let(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice', type: 'application/json"' }] } }
before do
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 'raises an error' do
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: JSON.generate(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
expect { subject }
.to raise_error(Webfinger::Error)
end
end
context 'when response body is not parsable' do
it 'raises an error' do
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')
.to_return(body: 'XXX', headers: { 'Content-Type': 'application/jrd+json' })
before do
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: 'XXX', headers: { 'Content-Type': 'application/jrd+json' })
end
it 'raises an error' do
expect { subject }
.to raise_error(Webfinger::Error)
end
@@ -63,7 +90,7 @@ RSpec.describe Webfinger do
before do
stub_request(:get, 'https://example.com/.well-known/host-meta').to_return(body: host_meta, headers: { 'Content-Type': 'application/jrd+json' })
stub_request(:get, 'https://example.com/.well-known/nonStandardWebfinger?resource=acct:alice@example.com').to_return(body: JSON.generate(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
stub_request(:get, 'https://example.com/.well-known/nonStandardWebfinger?resource=acct:alice@example.com').to_return(body: webfinger.to_json, headers: { 'Content-Type': 'application/jrd+json' })
end
it 'uses host meta details' do