fetch_remote_account_service_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. require 'rails_helper'
  2. RSpec.describe ActivityPub::FetchRemoteAccountService do
  3. subject { ActivityPub::FetchRemoteAccountService.new }
  4. let!(:actor) do
  5. {
  6. '@context': 'https://www.w3.org/ns/activitystreams',
  7. id: 'https://example.com/alice',
  8. type: 'Person',
  9. preferredUsername: 'alice',
  10. name: 'Alice',
  11. summary: 'Foo bar',
  12. }
  13. end
  14. describe '#call' do
  15. let(:account) { subject.call('https://example.com/alice') }
  16. shared_examples 'sets profile data' do
  17. it 'returns an account' do
  18. expect(account).to be_an Account
  19. end
  20. it 'sets display name' do
  21. expect(account.display_name).to eq 'Alice'
  22. end
  23. it 'sets note' do
  24. expect(account.note).to eq 'Foo bar'
  25. end
  26. it 'sets URL' do
  27. expect(account.url).to eq 'https://example.com/alice'
  28. end
  29. end
  30. context 'when URI and WebFinger share the same host' do
  31. let!(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice' }] } }
  32. before do
  33. stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor))
  34. 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' })
  35. end
  36. it 'fetches resource' do
  37. account
  38. expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
  39. end
  40. it 'looks up webfinger' do
  41. account
  42. expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
  43. end
  44. it 'sets username and domain from webfinger' do
  45. expect(account.username).to eq 'alice'
  46. expect(account.domain).to eq 'example.com'
  47. end
  48. include_examples 'sets profile data'
  49. end
  50. context 'when WebFinger presents different domain than URI' do
  51. let!(:webfinger) { { subject: 'acct:alice@iscool.af', links: [{ rel: 'self', href: 'https://example.com/alice' }] } }
  52. before do
  53. stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor))
  54. 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' })
  55. 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' })
  56. end
  57. it 'fetches resource' do
  58. account
  59. expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
  60. end
  61. it 'looks up webfinger' do
  62. account
  63. expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
  64. end
  65. it 'looks up "redirected" webfinger' do
  66. account
  67. expect(a_request(:get, 'https://iscool.af/.well-known/webfinger?resource=acct:alice@iscool.af')).to have_been_made.once
  68. end
  69. it 'sets username and domain from final webfinger' do
  70. expect(account.username).to eq 'alice'
  71. expect(account.domain).to eq 'iscool.af'
  72. end
  73. include_examples 'sets profile data'
  74. end
  75. end
  76. end