import_service_spec.rb 9.8 KB

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