import_service_spec.rb 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. context 'import bookmarks' do
  143. subject { ImportService.new }
  144. let(:csv) { attachment_fixture('bookmark-imports.txt') }
  145. around(:each) do |example|
  146. local_before = Rails.configuration.x.local_domain
  147. web_before = Rails.configuration.x.web_domain
  148. Rails.configuration.x.local_domain = 'local.com'
  149. Rails.configuration.x.web_domain = 'local.com'
  150. example.run
  151. Rails.configuration.x.web_domain = web_before
  152. Rails.configuration.x.local_domain = local_before
  153. end
  154. let(:local_account) { Fabricate(:account, username: 'foo', domain: '') }
  155. let!(:remote_status) { Fabricate(:status, uri: 'https://example.com/statuses/1312') }
  156. let!(:direct_status) { Fabricate(:status, uri: 'https://example.com/statuses/direct', visibility: :direct) }
  157. before do
  158. service = double
  159. allow(ActivityPub::FetchRemoteStatusService).to receive(:new).and_return(service)
  160. allow(service).to receive(:call).with('https://unknown-remote.com/users/bar/statuses/1') do
  161. Fabricate(:status, uri: 'https://unknown-remote.com/users/bar/statuses/1')
  162. end
  163. end
  164. describe 'when no bookmarks are set' do
  165. let(:import) { Import.create(account: account, type: 'bookmarks', data: csv) }
  166. it 'adds the toots the user has access to to bookmarks' do
  167. local_status = Fabricate(:status, account: local_account, uri: 'https://local.com/users/foo/statuses/42', id: 42, local: true)
  168. subject.call(import)
  169. expect(account.bookmarks.map(&:status).map(&:id)).to include(local_status.id)
  170. expect(account.bookmarks.map(&:status).map(&:id)).to include(remote_status.id)
  171. expect(account.bookmarks.map(&:status).map(&:id)).not_to include(direct_status.id)
  172. expect(account.bookmarks.count).to eq 3
  173. end
  174. end
  175. end
  176. end