fetch_remote_status_service_spec.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. require 'rails_helper'
  2. RSpec.describe ActivityPub::FetchRemoteStatusService, type: :service do
  3. include ActionView::Helpers::TextHelper
  4. let!(:sender) { Fabricate(:account, domain: 'foo.bar', uri: 'https://foo.bar') }
  5. let!(:recipient) { Fabricate(:account) }
  6. let(:existing_status) { nil }
  7. let(:note) do
  8. {
  9. '@context': 'https://www.w3.org/ns/activitystreams',
  10. id: "https://foo.bar/@foo/1234",
  11. type: 'Note',
  12. content: 'Lorem ipsum',
  13. attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
  14. }
  15. end
  16. subject { described_class.new }
  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. end