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

253 lines
12 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 reblogs if it failed to retreive reblogged statuses' do
  144. stub_request(:head, 'https://overwatch.com/users/tracer/updates/1').to_return(status: 404)
  145. actor = Fabricate(:account, username: 'tracer', domain: 'overwatch.com')
  146. body = <<XML
  147. <?xml version="1.0"?>
  148. <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">
  149. <id>tag:overwatch.com,2017-04-27:objectId=4467137:objectType=Status</id>
  150. <published>2017-04-27T13:49:25Z</published>
  151. <updated>2017-04-27T13:49:25Z</updated>
  152. <author>
  153. <id>https://overwatch.com/users/tracer</id>
  154. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  155. <uri>https://overwatch.com/users/tracer</uri>
  156. <name>tracer</name>
  157. </author>
  158. <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
  159. <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
  160. <content type="html">Overwatch rocks</content>
  161. <activity:object>
  162. <id>tag:overwatch.com,2017-04-27:objectId=4467137:objectType=Status</id>
  163. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  164. <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
  165. <author>
  166. <id>https://overwatch.com/users/tracer</id>
  167. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  168. <uri>https://overwatch.com/users/tracer</uri>
  169. <name>tracer</name>
  170. </author>
  171. <content type="html">Overwatch rocks</content>
  172. <link rel="alternate" type="text/html" href="https://overwatch.com/users/tracer/updates/1" />
  173. </activity:object>
  174. XML
  175. created_statuses = subject.call(body, actor)
  176. expect(created_statuses).to eq []
  177. end
  178. it 'ignores statuses with an out-of-order delete' do
  179. sender = Fabricate(:account, username: 'tracer', domain: 'overwatch.com')
  180. delete_body = <<XML
  181. <?xml version="1.0"?>
  182. <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">
  183. <id>tag:overwatch.com,2017-04-27:objectId=4487555:objectType=Status</id>
  184. <published>2017-04-27T13:49:25Z</published>
  185. <updated>2017-04-27T13:49:25Z</updated>
  186. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  187. <activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
  188. <author>
  189. <id>https://overwatch.com/users/tracer</id>
  190. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  191. <uri>https://overwatch.com/users/tracer</uri>
  192. <name>tracer</name>
  193. </author>
  194. </entry>
  195. XML
  196. status_body = <<XML
  197. <?xml version="1.0"?>
  198. <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">
  199. <id>tag:overwatch.com,2017-04-27:objectId=4487555:objectType=Status</id>
  200. <published>2017-04-27T13:49:25Z</published>
  201. <updated>2017-04-27T13:49:25Z</updated>
  202. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  203. <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
  204. <author>
  205. <id>https://overwatch.com/users/tracer</id>
  206. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  207. <uri>https://overwatch.com/users/tracer</uri>
  208. <name>tracer</name>
  209. </author>
  210. <content type="html">Overwatch rocks</content>
  211. </entry>
  212. XML
  213. subject.call(delete_body, sender)
  214. created_statuses = subject.call(status_body, sender)
  215. expect(created_statuses).to be_empty
  216. end
  217. end