闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.8 KiB

  1. require 'rails_helper'
  2. RSpec.describe FetchRemoteAccountService, type: :service do
  3. let(:url) { 'https://example.com/alice' }
  4. let(:prefetched_body) { nil }
  5. let(:protocol) { :ostatus }
  6. subject { FetchRemoteAccountService.new.call(url, prefetched_body, protocol) }
  7. let(:actor) do
  8. {
  9. '@context': 'https://www.w3.org/ns/activitystreams',
  10. id: 'https://example.com/alice',
  11. type: 'Person',
  12. preferredUsername: 'alice',
  13. name: 'Alice',
  14. summary: 'Foo bar',
  15. inbox: 'http://example.com/alice/inbox',
  16. }
  17. end
  18. let(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice' }] } }
  19. let(:xml) { File.read(File.join(Rails.root, 'spec', 'fixtures', 'xml', 'mastodon.atom')) }
  20. shared_examples 'return Account' do
  21. it { is_expected.to be_an Account }
  22. end
  23. context 'protocol is :activitypub' do
  24. let(:prefetched_body) { Oj.dump(actor) }
  25. let(:protocol) { :activitypub }
  26. before do
  27. 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' })
  28. end
  29. include_examples 'return Account'
  30. end
  31. context 'protocol is :ostatus' do
  32. let(:prefetched_body) { xml }
  33. let(:protocol) { :ostatus }
  34. before do
  35. stub_request(:get, "https://kickass.zone/.well-known/webfinger?resource=acct:localhost@kickass.zone").to_return(request_fixture('webfinger-hacker3.txt'))
  36. stub_request(:get, "https://kickass.zone/api/statuses/user_timeline/7477.atom").to_return(request_fixture('feed.txt'))
  37. end
  38. include_examples 'return Account'
  39. it 'does not update account information if XML comes from an unverified domain' do
  40. feed_xml = <<-XML.squish
  41. <?xml version="1.0" encoding="UTF-8"?>
  42. <feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/">
  43. <author>
  44. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  45. <uri>http://kickass.zone/users/localhost</uri>
  46. <name>localhost</name>
  47. <poco:preferredUsername>localhost</poco:preferredUsername>
  48. <poco:displayName>Villain!!!</poco:displayName>
  49. </author>
  50. </feed>
  51. XML
  52. returned_account = described_class.new.call('https://real-fake-domains.com/alice', feed_xml, :ostatus)
  53. expect(returned_account.display_name).to_not eq 'Villain!!!'
  54. end
  55. end
  56. context 'when prefetched_body is nil' do
  57. context 'protocol is :activitypub' do
  58. before do
  59. stub_request(:get, url).to_return(status: 200, body: Oj.dump(actor), headers: { 'Content-Type' => 'application/activity+json' })
  60. 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' })
  61. end
  62. include_examples 'return Account'
  63. end
  64. context 'protocol is :ostatus' do
  65. before do
  66. stub_request(:get, url).to_return(status: 200, body: xml, headers: { 'Content-Type' => 'application/atom+xml' })
  67. stub_request(:get, "https://kickass.zone/.well-known/webfinger?resource=acct:localhost@kickass.zone").to_return(request_fixture('webfinger-hacker3.txt'))
  68. stub_request(:get, "https://kickass.zone/api/statuses/user_timeline/7477.atom").to_return(request_fixture('feed.txt'))
  69. end
  70. include_examples 'return Account'
  71. end
  72. end
  73. end