fetch_remote_status_service_spec.rb 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::FetchRemoteStatusService, type: :service do
  4. include ActionView::Helpers::TextHelper
  5. subject { described_class.new }
  6. let!(:sender) { Fabricate(:account, domain: 'foo.bar', uri: 'https://foo.bar') }
  7. let(:existing_status) { nil }
  8. let(:note) do
  9. {
  10. '@context': 'https://www.w3.org/ns/activitystreams',
  11. id: 'https://foo.bar/@foo/1234',
  12. type: 'Note',
  13. content: 'Lorem ipsum',
  14. attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
  15. }
  16. end
  17. before do
  18. stub_request(:get, 'https://foo.bar/watch?v=12345').to_return(status: 404, body: '')
  19. stub_request(:get, object[:id]).to_return(body: Oj.dump(object))
  20. end
  21. describe '#call' do
  22. before do
  23. existing_status
  24. subject.call(object[:id], prefetched_body: Oj.dump(object))
  25. end
  26. context 'with Note object' do
  27. let(:object) { note }
  28. it 'creates status' do
  29. status = sender.statuses.first
  30. expect(status).to_not be_nil
  31. expect(status.text).to eq 'Lorem ipsum'
  32. end
  33. end
  34. context 'with Video object' do
  35. let(:object) do
  36. {
  37. '@context': 'https://www.w3.org/ns/activitystreams',
  38. id: 'https://foo.bar/@foo/1234',
  39. type: 'Video',
  40. name: 'Nyan Cat 10 hours remix',
  41. attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
  42. url: [
  43. {
  44. type: 'Link',
  45. mimeType: 'application/x-bittorrent',
  46. href: 'https://foo.bar/12345.torrent',
  47. },
  48. {
  49. type: 'Link',
  50. mimeType: 'text/html',
  51. href: 'https://foo.bar/watch?v=12345',
  52. },
  53. ],
  54. }
  55. end
  56. it 'creates status' do
  57. status = sender.statuses.first
  58. expect(status).to_not be_nil
  59. expect(status.url).to eq 'https://foo.bar/watch?v=12345'
  60. expect(strip_tags(status.text)).to eq 'Nyan Cat 10 hours remixhttps://foo.bar/watch?v=12345'
  61. end
  62. end
  63. context 'with Audio object' do
  64. let(:object) do
  65. {
  66. '@context': 'https://www.w3.org/ns/activitystreams',
  67. id: 'https://foo.bar/@foo/1234',
  68. type: 'Audio',
  69. name: 'Nyan Cat 10 hours remix',
  70. attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
  71. url: [
  72. {
  73. type: 'Link',
  74. mimeType: 'application/x-bittorrent',
  75. href: 'https://foo.bar/12345.torrent',
  76. },
  77. {
  78. type: 'Link',
  79. mimeType: 'text/html',
  80. href: 'https://foo.bar/watch?v=12345',
  81. },
  82. ],
  83. }
  84. end
  85. it 'creates status' do
  86. status = sender.statuses.first
  87. expect(status).to_not be_nil
  88. expect(status.url).to eq 'https://foo.bar/watch?v=12345'
  89. expect(strip_tags(status.text)).to eq 'Nyan Cat 10 hours remixhttps://foo.bar/watch?v=12345'
  90. end
  91. end
  92. context 'with Event object' do
  93. let(:object) do
  94. {
  95. '@context': 'https://www.w3.org/ns/activitystreams',
  96. id: 'https://foo.bar/@foo/1234',
  97. type: 'Event',
  98. name: "Let's change the world",
  99. attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
  100. }
  101. end
  102. it 'creates status' do
  103. status = sender.statuses.first
  104. expect(status).to_not be_nil
  105. expect(status.url).to eq 'https://foo.bar/@foo/1234'
  106. expect(strip_tags(status.text)).to eq "Let's change the worldhttps://foo.bar/@foo/1234"
  107. end
  108. end
  109. context 'with wrong id' do
  110. let(:note) do
  111. {
  112. '@context': 'https://www.w3.org/ns/activitystreams',
  113. id: 'https://real.address/@foo/1234',
  114. type: 'Note',
  115. content: 'Lorem ipsum',
  116. attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
  117. }
  118. end
  119. let(:object) do
  120. temp = note.dup
  121. temp[:id] = 'https://fake.address/@foo/5678'
  122. temp
  123. end
  124. it 'does not create status' do
  125. expect(sender.statuses.first).to be_nil
  126. end
  127. end
  128. context 'with a valid Create activity' do
  129. let(:object) do
  130. {
  131. '@context': 'https://www.w3.org/ns/activitystreams',
  132. id: 'https://foo.bar/@foo/1234/create',
  133. type: 'Create',
  134. actor: ActivityPub::TagManager.instance.uri_for(sender),
  135. object: note,
  136. }
  137. end
  138. it 'creates status' do
  139. status = sender.statuses.first
  140. expect(status).to_not be_nil
  141. expect(status.uri).to eq note[:id]
  142. expect(status.text).to eq note[:content]
  143. end
  144. end
  145. context 'with a Create activity with a mismatching id' do
  146. let(:object) do
  147. {
  148. '@context': 'https://www.w3.org/ns/activitystreams',
  149. id: 'https://foo.bar/@foo/1234/create',
  150. type: 'Create',
  151. actor: ActivityPub::TagManager.instance.uri_for(sender),
  152. object: {
  153. id: 'https://real.address/@foo/1234',
  154. type: 'Note',
  155. content: 'Lorem ipsum',
  156. attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
  157. },
  158. }
  159. end
  160. it 'does not create status' do
  161. expect(sender.statuses.first).to be_nil
  162. end
  163. end
  164. context 'when status already exists' do
  165. let(:existing_status) { Fabricate(:status, account: sender, text: 'Foo', uri: note[:id]) }
  166. context 'with a Note object' do
  167. let(:object) { note.merge(updated: '2021-09-08T22:39:25Z') }
  168. it 'updates status' do
  169. existing_status.reload
  170. expect(existing_status.text).to eq 'Lorem ipsum'
  171. expect(existing_status.edits).to_not be_empty
  172. end
  173. end
  174. context 'with a Create activity' do
  175. let(:object) do
  176. {
  177. '@context': 'https://www.w3.org/ns/activitystreams',
  178. id: 'https://foo.bar/@foo/1234/create',
  179. type: 'Create',
  180. actor: ActivityPub::TagManager.instance.uri_for(sender),
  181. object: note.merge(updated: '2021-09-08T22:39:25Z'),
  182. }
  183. end
  184. it 'updates status' do
  185. existing_status.reload
  186. expect(existing_status.text).to eq 'Lorem ipsum'
  187. expect(existing_status.edits).to_not be_empty
  188. end
  189. end
  190. end
  191. end
  192. context 'with statuses referencing other statuses', :sidekiq_inline do
  193. before do
  194. stub_const 'ActivityPub::FetchRemoteStatusService::DISCOVERIES_PER_REQUEST', 5
  195. end
  196. context 'when using inReplyTo' do
  197. let(:object) do
  198. {
  199. '@context': 'https://www.w3.org/ns/activitystreams',
  200. id: 'https://foo.bar/@foo/1',
  201. type: 'Note',
  202. content: 'Lorem ipsum',
  203. inReplyTo: 'https://foo.bar/@foo/2',
  204. attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
  205. }
  206. end
  207. before do
  208. 8.times do |i|
  209. status_json = {
  210. '@context': 'https://www.w3.org/ns/activitystreams',
  211. id: "https://foo.bar/@foo/#{i}",
  212. type: 'Note',
  213. content: 'Lorem ipsum',
  214. inReplyTo: "https://foo.bar/@foo/#{i + 1}",
  215. attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
  216. to: 'as:Public',
  217. }.with_indifferent_access
  218. stub_request(:get, "https://foo.bar/@foo/#{i}").to_return(status: 200, body: status_json.to_json, headers: { 'Content-Type': 'application/activity+json' })
  219. end
  220. end
  221. it 'creates at least some statuses' do
  222. expect { subject.call(object[:id], prefetched_body: Oj.dump(object)) }.to change { sender.statuses.count }.by_at_least(2)
  223. end
  224. it 'creates no more account than the limit allows' do
  225. expect { subject.call(object[:id], prefetched_body: Oj.dump(object)) }.to change { sender.statuses.count }.by_at_most(5)
  226. end
  227. end
  228. context 'when using replies' do
  229. let(:object) do
  230. {
  231. '@context': 'https://www.w3.org/ns/activitystreams',
  232. id: 'https://foo.bar/@foo/1',
  233. type: 'Note',
  234. content: 'Lorem ipsum',
  235. replies: {
  236. type: 'Collection',
  237. id: 'https://foo.bar/@foo/1/replies',
  238. first: {
  239. type: 'CollectionPage',
  240. partOf: 'https://foo.bar/@foo/1/replies',
  241. items: ['https://foo.bar/@foo/2'],
  242. },
  243. },
  244. attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
  245. }
  246. end
  247. before do
  248. 8.times do |i|
  249. status_json = {
  250. '@context': 'https://www.w3.org/ns/activitystreams',
  251. id: "https://foo.bar/@foo/#{i}",
  252. type: 'Note',
  253. content: 'Lorem ipsum',
  254. replies: {
  255. type: 'Collection',
  256. id: "https://foo.bar/@foo/#{i}/replies",
  257. first: {
  258. type: 'CollectionPage',
  259. partOf: "https://foo.bar/@foo/#{i}/replies",
  260. items: ["https://foo.bar/@foo/#{i + 1}"],
  261. },
  262. },
  263. attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
  264. to: 'as:Public',
  265. }.with_indifferent_access
  266. stub_request(:get, "https://foo.bar/@foo/#{i}").to_return(status: 200, body: status_json.to_json, headers: { 'Content-Type': 'application/activity+json' })
  267. end
  268. end
  269. it 'creates at least some statuses' do
  270. expect { subject.call(object[:id], prefetched_body: Oj.dump(object)) }.to change { sender.statuses.count }.by_at_least(2)
  271. end
  272. it 'creates no more account than the limit allows' do
  273. expect { subject.call(object[:id], prefetched_body: Oj.dump(object)) }.to change { sender.statuses.count }.by_at_most(5)
  274. end
  275. end
  276. end
  277. end