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

252 lines
12 KiB

  1. require 'rails_helper'
  2. RSpec.describe ProcessFeedService, type: :service 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(:get, 'https://overwatch.com/users/tracer/updates/1').to_return(status: 200, body: real_body, headers: { 'Content-Type' => 'application/atom+xml' })
  105. bad_actor = Fabricate(:account, username: 'sombra', domain: 'talon.xyz')
  106. body = <<XML
  107. <?xml version="1.0"?>
  108. <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">
  109. <id>tag:talon.xyz,2017-04-27:objectId=4467137:objectType=Status</id>
  110. <published>2017-04-27T13:49:25Z</published>
  111. <updated>2017-04-27T13:49:25Z</updated>
  112. <author>
  113. <id>https://talon.xyz/users/sombra</id>
  114. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  115. <uri>https://talon.xyz/users/sombra</uri>
  116. <name>sombra</name>
  117. </author>
  118. <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
  119. <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
  120. <content type="html">Overwatch SUCKS AHAHA</content>
  121. <activity:object>
  122. <id>tag:overwatch.com,2017-04-27:objectId=4467137:objectType=Status</id>
  123. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  124. <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
  125. <author>
  126. <id>https://overwatch.com/users/tracer</id>
  127. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  128. <uri>https://overwatch.com/users/tracer</uri>
  129. <name>tracer</name>
  130. </author>
  131. <content type="html">Overwatch SUCKS AHAHA</content>
  132. <link rel="alternate" type="text/html" href="https://overwatch.com/users/tracer/updates/1" />
  133. </activity:object>
  134. </entry>
  135. XML
  136. created_statuses = subject.call(body, bad_actor)
  137. expect(created_statuses.first.reblog?).to be true
  138. expect(created_statuses.first.account_id).to eq bad_actor.id
  139. expect(created_statuses.first.reblog.account_id).to eq good_actor.id
  140. expect(created_statuses.first.reblog.text).to eq 'Overwatch rocks'
  141. end
  142. it 'ignores reblogs if it failed to retreive reblogged statuses' do
  143. stub_request(:get, 'https://overwatch.com/users/tracer/updates/1').to_return(status: 404)
  144. actor = Fabricate(:account, username: 'tracer', domain: 'overwatch.com')
  145. 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=4467137:objectType=Status</id>
  149. <published>2017-04-27T13:49:25Z</published>
  150. <updated>2017-04-27T13:49:25Z</updated>
  151. <author>
  152. <id>https://overwatch.com/users/tracer</id>
  153. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  154. <uri>https://overwatch.com/users/tracer</uri>
  155. <name>tracer</name>
  156. </author>
  157. <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
  158. <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
  159. <content type="html">Overwatch rocks</content>
  160. <activity:object>
  161. <id>tag:overwatch.com,2017-04-27:objectId=4467137:objectType=Status</id>
  162. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  163. <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
  164. <author>
  165. <id>https://overwatch.com/users/tracer</id>
  166. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  167. <uri>https://overwatch.com/users/tracer</uri>
  168. <name>tracer</name>
  169. </author>
  170. <content type="html">Overwatch rocks</content>
  171. <link rel="alternate" type="text/html" href="https://overwatch.com/users/tracer/updates/1" />
  172. </activity:object>
  173. XML
  174. created_statuses = subject.call(body, actor)
  175. expect(created_statuses).to eq []
  176. end
  177. it 'ignores statuses with an out-of-order delete' do
  178. sender = Fabricate(:account, username: 'tracer', domain: 'overwatch.com')
  179. delete_body = <<XML
  180. <?xml version="1.0"?>
  181. <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">
  182. <id>tag:overwatch.com,2017-04-27:objectId=4487555:objectType=Status</id>
  183. <published>2017-04-27T13:49:25Z</published>
  184. <updated>2017-04-27T13:49:25Z</updated>
  185. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  186. <activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
  187. <author>
  188. <id>https://overwatch.com/users/tracer</id>
  189. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  190. <uri>https://overwatch.com/users/tracer</uri>
  191. <name>tracer</name>
  192. </author>
  193. </entry>
  194. XML
  195. status_body = <<XML
  196. <?xml version="1.0"?>
  197. <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">
  198. <id>tag:overwatch.com,2017-04-27:objectId=4487555:objectType=Status</id>
  199. <published>2017-04-27T13:49:25Z</published>
  200. <updated>2017-04-27T13:49:25Z</updated>
  201. <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  202. <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
  203. <author>
  204. <id>https://overwatch.com/users/tracer</id>
  205. <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  206. <uri>https://overwatch.com/users/tracer</uri>
  207. <name>tracer</name>
  208. </author>
  209. <content type="html">Overwatch rocks</content>
  210. </entry>
  211. XML
  212. subject.call(delete_body, sender)
  213. created_statuses = subject.call(status_body, sender)
  214. expect(created_statuses).to be_empty
  215. end
  216. end