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.

213 lines
10 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(:head, "http://kickass.zone/media/2").to_return(:status => 404)
  10. stub_request(:head, "http://kickass.zone/media/3").to_return(:status => 404)
  11. stub_request(:get, "http://kickass.zone/system/accounts/avatars/000/000/001/large/eris.png").to_return(request_fixture('avatar.txt'))
  12. stub_request(:get, "http://kickass.zone/system/media_attachments/files/000/000/002/original/morpheus_linux.jpg?1476059910").to_return(request_fixture('attachment1.txt'))
  13. stub_request(:get, "http://kickass.zone/system/media_attachments/files/000/000/003/original/gizmo.jpg?1476060065").to_return(request_fixture('attachment2.txt'))
  14. end
  15. context 'when domain does not reject media' do
  16. before do
  17. subject.call(body, account)
  18. end
  19. it 'updates remote user\'s account information' do
  20. account.reload
  21. expect(account.display_name).to eq '::1'
  22. expect(account).to have_attached_file(:avatar)
  23. expect(account.avatar_file_name).not_to be_nil
  24. end
  25. it 'creates posts' do
  26. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=1:objectType=Status')).to_not be_nil
  27. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Status')).to_not be_nil
  28. end
  29. it 'marks replies as replies' do
  30. status = Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Status')
  31. expect(status.reply?).to be true
  32. end
  33. it 'sets account being replied to when possible' do
  34. status = Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Status')
  35. expect(status.in_reply_to_account_id).to eq status.account_id
  36. end
  37. it 'ignores delete statuses unless they existed before' do
  38. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=3:objectType=Status')).to be_nil
  39. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=12:objectType=Status')).to be_nil
  40. end
  41. it 'does not create statuses for follows' do
  42. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=1:objectType=Follow')).to be_nil
  43. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Follow')).to be_nil
  44. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=4:objectType=Follow')).to be_nil
  45. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=7:objectType=Follow')).to be_nil
  46. end
  47. it 'does not create statuses for favourites' do
  48. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Favourite')).to be_nil
  49. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=3:objectType=Favourite')).to be_nil
  50. end
  51. it 'creates posts with media' do
  52. status = Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=14:objectType=Status')
  53. expect(status).to_not be_nil
  54. expect(status.media_attachments.first).to have_attached_file(:file)
  55. expect(status.media_attachments.first.image?).to be true
  56. expect(status.media_attachments.first.file_file_name).not_to be_nil
  57. end
  58. end
  59. context 'when domain is set to reject media' do
  60. let!(:domain_block) { Fabricate(:domain_block, domain: 'kickass.zone', reject_media: true) }
  61. before do
  62. subject.call(body, account)
  63. end
  64. it 'updates remote user\'s account information' do
  65. account.reload
  66. expect(account.display_name).to eq '::1'
  67. end
  68. it 'rejects remote user\'s avatar' do
  69. account.reload
  70. expect(account.display_name).to eq '::1'
  71. expect(account.avatar_file_name).to be_nil
  72. end
  73. it 'creates posts' do
  74. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=1:objectType=Status')).to_not be_nil
  75. expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Status')).to_not be_nil
  76. end
  77. it 'creates posts with remote-only media' do
  78. status = Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=14:objectType=Status')
  79. expect(status).to_not be_nil
  80. expect(status.media_attachments.first.file_file_name).to be_nil
  81. expect(status.media_attachments.first.unknown?).to be true
  82. end
  83. end
  84. end
  85. it 'does not accept tampered reblogs' do
  86. good_actor = Fabricate(:account, username: 'tracer', domain: 'overwatch.com')
  87. real_body = <<XML
  88. <?xml version="1.0"?>
  89. <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">
  90. <id>tag:overwatch.com,2017-04-27:objectId=4467137:objectType=Status</id>
  91. <published>2017-04-27T13:49:25Z</published>
  92. <updated>2017-04-27T13:49:25Z</updated>
  93. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  94. <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
  95. <author>
  96. <id>https://overwatch.com/users/tracer</id>
  97. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  98. <uri>https://overwatch.com/users/tracer</uri>
  99. <name>tracer</name>
  100. </author>
  101. <content type="html">Overwatch rocks</content>
  102. </entry>
  103. XML
  104. stub_request(:head, 'https://overwatch.com/users/tracer/updates/1').to_return(status: 200, headers: { 'Content-Type' => 'application/atom+xml' })
  105. stub_request(:get, 'https://overwatch.com/users/tracer/updates/1').to_return(status: 200, body: real_body)
  106. bad_actor = Fabricate(:account, username: 'sombra', domain: 'talon.xyz')
  107. body = <<XML
  108. <?xml version="1.0"?>
  109. <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">
  110. <id>tag:talon.xyz,2017-04-27:objectId=4467137:objectType=Status</id>
  111. <published>2017-04-27T13:49:25Z</published>
  112. <updated>2017-04-27T13:49:25Z</updated>
  113. <author>
  114. <id>https://talon.xyz/users/sombra</id>
  115. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  116. <uri>https://talon.xyz/users/sombra</uri>
  117. <name>sombra</name>
  118. </author>
  119. <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
  120. <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
  121. <content type="html">Overwatch SUCKS AHAHA</content>
  122. <activity:object>
  123. <id>tag:overwatch.com,2017-04-27:objectId=4467137:objectType=Status</id>
  124. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  125. <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
  126. <author>
  127. <id>https://overwatch.com/users/tracer</id>
  128. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  129. <uri>https://overwatch.com/users/tracer</uri>
  130. <name>tracer</name>
  131. </author>
  132. <content type="html">Overwatch SUCKS AHAHA</content>
  133. <link rel="alternate" type="text/html" href="https://overwatch.com/users/tracer/updates/1" />
  134. </activity:object>
  135. </entry>
  136. XML
  137. created_statuses = subject.call(body, bad_actor)
  138. expect(created_statuses.first.reblog?).to be true
  139. expect(created_statuses.first.account_id).to eq bad_actor.id
  140. expect(created_statuses.first.reblog.account_id).to eq good_actor.id
  141. expect(created_statuses.first.reblog.text).to eq 'Overwatch rocks'
  142. end
  143. it 'ignores statuses with an out-of-order delete' do
  144. sender = Fabricate(:account, username: 'tracer', domain: 'overwatch.com')
  145. delete_body = <<XML
  146. <?xml version="1.0"?>
  147. <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">
  148. <id>tag:overwatch.com,2017-04-27:objectId=4487555:objectType=Status</id>
  149. <published>2017-04-27T13:49:25Z</published>
  150. <updated>2017-04-27T13:49:25Z</updated>
  151. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  152. <activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
  153. <author>
  154. <id>https://overwatch.com/users/tracer</id>
  155. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  156. <uri>https://overwatch.com/users/tracer</uri>
  157. <name>tracer</name>
  158. </author>
  159. </entry>
  160. XML
  161. status_body = <<XML
  162. <?xml version="1.0"?>
  163. <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">
  164. <id>tag:overwatch.com,2017-04-27:objectId=4487555:objectType=Status</id>
  165. <published>2017-04-27T13:49:25Z</published>
  166. <updated>2017-04-27T13:49:25Z</updated>
  167. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  168. <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
  169. <author>
  170. <id>https://overwatch.com/users/tracer</id>
  171. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  172. <uri>https://overwatch.com/users/tracer</uri>
  173. <name>tracer</name>
  174. </author>
  175. <content type="html">Overwatch rocks</content>
  176. </entry>
  177. XML
  178. subject.call(delete_body, sender)
  179. created_statuses = subject.call(status_body, sender)
  180. expect(created_statuses).to be_empty
  181. end
  182. end