闭社主体 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.

157 lines
7.5 KiB

  1. require 'rails_helper'
  2. RSpec.describe ProcessFeedService do
  3. subject { ProcessFeedService.new }
  4. describe 'processing a feed' do
  5. let(:body) { File.read(File.join(Rails.root, 'spec', 'fixtures', 'xml', 'mastodon.atom')) }
  6. let(:account) { Fabricate(:account, username: 'localhost', domain: 'kickass.zone') }
  7. before do
  8. stub_request(:post, "https://pubsubhubbub.superfeedr.com/").to_return(:status => 200, :body => "", :headers => {})
  9. stub_request(:get, "http://kickass.zone/system/accounts/avatars/000/000/001/large/eris.png").to_return(request_fixture('avatar.txt'))
  10. stub_request(:get, "http://kickass.zone/system/media_attachments/files/000/000/002/original/morpheus_linux.jpg?1476059910").to_return(request_fixture('attachment1.txt'))
  11. stub_request(:get, "http://kickass.zone/system/media_attachments/files/000/000/003/original/gizmo.jpg?1476060065").to_return(request_fixture('attachment2.txt'))
  12. end
  13. context 'when domain does not reject media' do
  14. before do
  15. subject.call(body, account)
  16. end
  17. it 'updates remote user\'s account information' do
  18. account.reload
  19. expect(account.display_name).to eq '::1'
  20. expect(account).to have_attached_file(:avatar)
  21. expect(account.avatar_file_name).not_to be_nil
  22. end
  23. it 'creates posts' do
  24. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=1:objectType=Status')).to_not be_nil
  25. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Status')).to_not be_nil
  26. end
  27. it 'ignores delete statuses unless they existed before' do
  28. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=3:objectType=Status')).to be_nil
  29. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=12:objectType=Status')).to be_nil
  30. end
  31. it 'does not create statuses for follows' do
  32. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=1:objectType=Follow')).to be_nil
  33. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Follow')).to be_nil
  34. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=4:objectType=Follow')).to be_nil
  35. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=7:objectType=Follow')).to be_nil
  36. end
  37. it 'does not create statuses for favourites' do
  38. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Favourite')).to be_nil
  39. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=3:objectType=Favourite')).to be_nil
  40. end
  41. it 'creates posts with media' do
  42. status = Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=14:objectType=Status')
  43. expect(status).to_not be_nil
  44. expect(status.media_attachments.first).to have_attached_file(:file)
  45. expect(status.media_attachments.first.image?).to be true
  46. expect(status.media_attachments.first.file_file_name).not_to be_nil
  47. end
  48. end
  49. context 'when domain is set to reject media' do
  50. let!(:domain_block) { Fabricate(:domain_block, domain: 'kickass.zone', reject_media: true) }
  51. before do
  52. subject.call(body, account)
  53. end
  54. it 'updates remote user\'s account information' do
  55. account.reload
  56. expect(account.display_name).to eq '::1'
  57. end
  58. it 'rejects remote user\'s avatar' do
  59. account.reload
  60. expect(account.display_name).to eq '::1'
  61. expect(account.avatar_file_name).to be_nil
  62. end
  63. it 'creates posts' do
  64. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=1:objectType=Status')).to_not be_nil
  65. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Status')).to_not be_nil
  66. end
  67. it 'creates posts with remote-only media' do
  68. status = Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=14:objectType=Status')
  69. expect(status).to_not be_nil
  70. expect(status.media_attachments.first.file_file_name).to be_nil
  71. expect(status.media_attachments.first.unknown?).to be true
  72. end
  73. end
  74. end
  75. it 'does not accept tampered reblogs' do
  76. good_actor = Fabricate(:account, username: 'tracer', domain: 'overwatch.com')
  77. real_body = <<XML
  78. <?xml version="1.0"?>
  79. <entry xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:media="http://purl.org/syndication/atommedia" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:mastodon="http://mastodon.social/schema/1.0">
  80. <id>tag:overwatch.com,2017-04-27:objectId=4467137:objectType=Status</id>
  81. <published>2017-04-27T13:49:25Z</published>
  82. <updated>2017-04-27T13:49:25Z</updated>
  83. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  84. <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
  85. <author>
  86. <id>https://overwatch.com/users/tracer</id>
  87. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  88. <uri>https://overwatch.com/users/tracer</uri>
  89. <name>tracer</name>
  90. </author>
  91. <content type="html">Overwatch rocks</content>
  92. </entry>
  93. XML
  94. stub_request(:head, 'https://overwatch.com/users/tracer/updates/1').to_return(status: 200, headers: { 'Content-Type' => 'application/atom+xml' })
  95. stub_request(:get, 'https://overwatch.com/users/tracer/updates/1').to_return(status: 200, body: real_body)
  96. bad_actor = Fabricate(:account, username: 'sombra', domain: 'talon.xyz')
  97. body = <<XML
  98. <?xml version="1.0"?>
  99. <entry xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:media="http://purl.org/syndication/atommedia" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:mastodon="http://mastodon.social/schema/1.0">
  100. <id>tag:talon.xyz,2017-04-27:objectId=4467137:objectType=Status</id>
  101. <published>2017-04-27T13:49:25Z</published>
  102. <updated>2017-04-27T13:49:25Z</updated>
  103. <author>
  104. <id>https://talon.xyz/users/sombra</id>
  105. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  106. <uri>https://talon.xyz/users/sombra</uri>
  107. <name>sombra</name>
  108. </author>
  109. <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
  110. <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
  111. <content type="html">Overwatch SUCKS AHAHA</content>
  112. <activity:object>
  113. <id>tag:overwatch.com,2017-04-27:objectId=4467137:objectType=Status</id>
  114. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  115. <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
  116. <author>
  117. <id>https://overwatch.com/users/tracer</id>
  118. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  119. <uri>https://overwatch.com/users/tracer</uri>
  120. <name>tracer</name>
  121. </author>
  122. <content type="html">Overwatch SUCKS AHAHA</content>
  123. <link rel="alternate" type="text/html" href="https://overwatch.com/users/tracer/updates/1" />
  124. </activity:object>
  125. </entry>
  126. XML
  127. created_statuses = subject.call(body, bad_actor)
  128. expect(created_statuses.first.reblog?).to be true
  129. expect(created_statuses.first.account_id).to eq bad_actor.id
  130. expect(created_statuses.first.reblog.account_id).to eq good_actor.id
  131. expect(created_statuses.first.reblog.text).to eq 'Overwatch rocks'
  132. end
  133. end