import_service_spec.rb 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ImportService, type: :service do
  4. include RoutingHelper
  5. let!(:account) { Fabricate(:account, locked: false) }
  6. let!(:bob) { Fabricate(:account, username: 'bob', locked: false) }
  7. let!(:eve) { Fabricate(:account, username: 'eve', domain: 'example.com', locked: false, protocol: :activitypub, inbox_url: 'https://example.com/inbox') }
  8. before do
  9. stub_request(:post, 'https://example.com/inbox').to_return(status: 200)
  10. end
  11. context 'when importing old-style list of muted users' do
  12. subject { described_class.new }
  13. let(:csv) { attachment_fixture('mute-imports.txt') }
  14. describe 'when no accounts are muted' do
  15. let(:import) { Import.create(account: account, type: 'muting', data: csv) }
  16. it 'mutes the listed accounts, including notifications' do
  17. subject.call(import)
  18. expect(account.muting.count).to eq 2
  19. expect(Mute.find_by(account: account, target_account: bob).hide_notifications).to be true
  20. end
  21. end
  22. describe 'when some accounts are muted and overwrite is not set' do
  23. let(:import) { Import.create(account: account, type: 'muting', data: csv) }
  24. it 'mutes the listed accounts, including notifications' do
  25. account.mute!(bob, notifications: false)
  26. subject.call(import)
  27. expect(account.muting.count).to eq 2
  28. expect(Mute.find_by(account: account, target_account: bob).hide_notifications).to be true
  29. end
  30. end
  31. describe 'when some accounts are muted and overwrite is set' do
  32. let(:import) { Import.create(account: account, type: 'muting', data: csv, overwrite: true) }
  33. it 'mutes the listed accounts, including notifications' do
  34. account.mute!(bob, notifications: false)
  35. subject.call(import)
  36. expect(account.muting.count).to eq 2
  37. expect(Mute.find_by(account: account, target_account: bob).hide_notifications).to be true
  38. end
  39. end
  40. end
  41. context 'when importing new-style list of muted users' do
  42. subject { described_class.new }
  43. let(:csv) { attachment_fixture('new-mute-imports.txt') }
  44. describe 'when no accounts are muted' do
  45. let(:import) { Import.create(account: account, type: 'muting', data: csv) }
  46. it 'mutes the listed accounts, respecting notifications' do
  47. subject.call(import)
  48. expect(account.muting.count).to eq 2
  49. expect(Mute.find_by(account: account, target_account: bob).hide_notifications).to be true
  50. expect(Mute.find_by(account: account, target_account: eve).hide_notifications).to be false
  51. end
  52. end
  53. describe 'when some accounts are muted and overwrite is not set' do
  54. let(:import) { Import.create(account: account, type: 'muting', data: csv) }
  55. it 'mutes the listed accounts, respecting notifications' do
  56. account.mute!(bob, notifications: true)
  57. subject.call(import)
  58. expect(account.muting.count).to eq 2
  59. expect(Mute.find_by(account: account, target_account: bob).hide_notifications).to be true
  60. expect(Mute.find_by(account: account, target_account: eve).hide_notifications).to be false
  61. end
  62. end
  63. describe 'when some accounts are muted and overwrite is set' do
  64. let(:import) { Import.create(account: account, type: 'muting', data: csv, overwrite: true) }
  65. it 'mutes the listed accounts, respecting notifications' do
  66. account.mute!(bob, notifications: true)
  67. subject.call(import)
  68. expect(account.muting.count).to eq 2
  69. expect(Mute.find_by(account: account, target_account: bob).hide_notifications).to be true
  70. expect(Mute.find_by(account: account, target_account: eve).hide_notifications).to be false
  71. end
  72. end
  73. end
  74. context 'when importing old-style list of followed users' do
  75. subject { described_class.new }
  76. let(:csv) { attachment_fixture('mute-imports.txt') }
  77. describe 'when no accounts are followed' do
  78. let(:import) { Import.create(account: account, type: 'following', data: csv) }
  79. it 'follows the listed accounts, including boosts' do
  80. subject.call(import)
  81. expect(account.following.count).to eq 1
  82. expect(account.follow_requests.count).to eq 1
  83. expect(Follow.find_by(account: account, target_account: bob).show_reblogs).to be true
  84. end
  85. end
  86. describe 'when some accounts are already followed and overwrite is not set' do
  87. let(:import) { Import.create(account: account, type: 'following', data: csv) }
  88. it 'follows the listed accounts, including notifications' do
  89. account.follow!(bob, reblogs: false)
  90. subject.call(import)
  91. expect(account.following.count).to eq 1
  92. expect(account.follow_requests.count).to eq 1
  93. expect(Follow.find_by(account: account, target_account: bob).show_reblogs).to be true
  94. end
  95. end
  96. describe 'when some accounts are already followed and overwrite is set' do
  97. let(:import) { Import.create(account: account, type: 'following', data: csv, overwrite: true) }
  98. it 'mutes the listed accounts, including notifications' do
  99. account.follow!(bob, reblogs: false)
  100. subject.call(import)
  101. expect(account.following.count).to eq 1
  102. expect(account.follow_requests.count).to eq 1
  103. expect(Follow.find_by(account: account, target_account: bob).show_reblogs).to be true
  104. end
  105. end
  106. end
  107. context 'when importing new-style list of followed users' do
  108. subject { described_class.new }
  109. let(:csv) { attachment_fixture('new-following-imports.txt') }
  110. describe 'when no accounts are followed' do
  111. let(:import) { Import.create(account: account, type: 'following', data: csv) }
  112. it 'follows the listed accounts, respecting boosts' do
  113. subject.call(import)
  114. expect(account.following.count).to eq 1
  115. expect(account.follow_requests.count).to eq 1
  116. expect(Follow.find_by(account: account, target_account: bob).show_reblogs).to be true
  117. expect(FollowRequest.find_by(account: account, target_account: eve).show_reblogs).to be false
  118. end
  119. end
  120. describe 'when some accounts are already followed and overwrite is not set' do
  121. let(:import) { Import.create(account: account, type: 'following', data: csv) }
  122. it 'mutes the listed accounts, respecting notifications' do
  123. account.follow!(bob, reblogs: true)
  124. subject.call(import)
  125. expect(account.following.count).to eq 1
  126. expect(account.follow_requests.count).to eq 1
  127. expect(Follow.find_by(account: account, target_account: bob).show_reblogs).to be true
  128. expect(FollowRequest.find_by(account: account, target_account: eve).show_reblogs).to be false
  129. end
  130. end
  131. describe 'when some accounts are already followed and overwrite is set' do
  132. let(:import) { Import.create(account: account, type: 'following', data: csv, overwrite: true) }
  133. it 'mutes the listed accounts, respecting notifications' do
  134. account.follow!(bob, reblogs: true)
  135. subject.call(import)
  136. expect(account.following.count).to eq 1
  137. expect(account.follow_requests.count).to eq 1
  138. expect(Follow.find_by(account: account, target_account: bob).show_reblogs).to be true
  139. expect(FollowRequest.find_by(account: account, target_account: eve).show_reblogs).to be false
  140. end
  141. end
  142. end
  143. # Based on the bug report 20571 where UTF-8 encoded domains were rejecting import of their users
  144. #
  145. # https://github.com/mastodon/mastodon/issues/20571
  146. context 'with a utf-8 encoded domains' do
  147. subject { described_class.new }
  148. let!(:nare) { Fabricate(:account, username: 'nare', domain: 'թութ.հայ', locked: false, protocol: :activitypub, inbox_url: 'https://թութ.հայ/inbox') }
  149. let(:csv) { attachment_fixture('utf8-followers.txt') }
  150. let(:import) { Import.create(account: account, type: 'following', data: csv) }
  151. # Make sure to not actually go to the remote server
  152. before do
  153. stub_request(:post, 'https://թութ.հայ/inbox').to_return(status: 200)
  154. end
  155. it 'follows the listed account' do
  156. expect(account.follow_requests.count).to eq 0
  157. subject.call(import)
  158. expect(account.follow_requests.count).to eq 1
  159. end
  160. end
  161. context 'when importing bookmarks' do
  162. subject { described_class.new }
  163. let(:csv) { attachment_fixture('bookmark-imports.txt') }
  164. let(:local_account) { Fabricate(:account, username: 'foo', domain: '') }
  165. let!(:remote_status) { Fabricate(:status, uri: 'https://example.com/statuses/1312') }
  166. let!(:direct_status) { Fabricate(:status, uri: 'https://example.com/statuses/direct', visibility: :direct) }
  167. around(:each) do |example|
  168. local_before = Rails.configuration.x.local_domain
  169. web_before = Rails.configuration.x.web_domain
  170. Rails.configuration.x.local_domain = 'local.com'
  171. Rails.configuration.x.web_domain = 'local.com'
  172. example.run
  173. Rails.configuration.x.web_domain = web_before
  174. Rails.configuration.x.local_domain = local_before
  175. end
  176. before do
  177. service = instance_double(ActivityPub::FetchRemoteStatusService)
  178. allow(ActivityPub::FetchRemoteStatusService).to receive(:new).and_return(service)
  179. allow(service).to receive(:call).with('https://unknown-remote.com/users/bar/statuses/1') do
  180. Fabricate(:status, uri: 'https://unknown-remote.com/users/bar/statuses/1')
  181. end
  182. end
  183. describe 'when no bookmarks are set' do
  184. let(:import) { Import.create(account: account, type: 'bookmarks', data: csv) }
  185. it 'adds the toots the user has access to to bookmarks' do
  186. local_status = Fabricate(:status, account: local_account, uri: 'https://local.com/users/foo/statuses/42', id: 42, local: true)
  187. subject.call(import)
  188. expect(account.bookmarks.map(&:status).map(&:id)).to include(local_status.id)
  189. expect(account.bookmarks.map(&:status).map(&:id)).to include(remote_status.id)
  190. expect(account.bookmarks.map(&:status).map(&:id)).to_not include(direct_status.id)
  191. expect(account.bookmarks.count).to eq 3
  192. end
  193. end
  194. end
  195. end