You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
7.3 KiB

  1. require 'rails_helper'
  2. RSpec.describe ImportService, type: :service do
  3. let!(:account) { Fabricate(:account, locked: false) }
  4. let!(:bob) { Fabricate(:account, username: 'bob', locked: false) }
  5. let!(:eve) { Fabricate(:account, username: 'eve', domain: 'example.com', locked: false, protocol: :activitypub, inbox_url: 'https://example.com/inbox') }
  6. before do
  7. stub_request(:post, "https://example.com/inbox").to_return(status: 200)
  8. end
  9. context 'import old-style list of muted users' do
  10. subject { ImportService.new }
  11. let(:csv) { attachment_fixture('mute-imports.txt') }
  12. describe 'when no accounts are muted' do
  13. let(:import) { Import.create(account: account, type: 'muting', data: csv) }
  14. it 'mutes the listed accounts, including notifications' do
  15. subject.call(import)
  16. expect(account.muting.count).to eq 2
  17. expect(Mute.find_by(account: account, target_account: bob).hide_notifications).to be true
  18. end
  19. end
  20. describe 'when some accounts are muted and overwrite is not set' do
  21. let(:import) { Import.create(account: account, type: 'muting', data: csv) }
  22. it 'mutes the listed accounts, including notifications' do
  23. account.mute!(bob, notifications: false)
  24. subject.call(import)
  25. expect(account.muting.count).to eq 2
  26. expect(Mute.find_by(account: account, target_account: bob).hide_notifications).to be true
  27. end
  28. end
  29. describe 'when some accounts are muted and overwrite is set' do
  30. let(:import) { Import.create(account: account, type: 'muting', data: csv, overwrite: true) }
  31. it 'mutes the listed accounts, including notifications' do
  32. account.mute!(bob, notifications: false)
  33. subject.call(import)
  34. expect(account.muting.count).to eq 2
  35. expect(Mute.find_by(account: account, target_account: bob).hide_notifications).to be true
  36. end
  37. end
  38. end
  39. context 'import new-style list of muted users' do
  40. subject { ImportService.new }
  41. let(:csv) { attachment_fixture('new-mute-imports.txt') }
  42. describe 'when no accounts are muted' do
  43. let(:import) { Import.create(account: account, type: 'muting', data: csv) }
  44. it 'mutes the listed accounts, respecting notifications' do
  45. subject.call(import)
  46. expect(account.muting.count).to eq 2
  47. expect(Mute.find_by(account: account, target_account: bob).hide_notifications).to be true
  48. expect(Mute.find_by(account: account, target_account: eve).hide_notifications).to be false
  49. end
  50. end
  51. describe 'when some accounts are muted and overwrite is not set' do
  52. let(:import) { Import.create(account: account, type: 'muting', data: csv) }
  53. it 'mutes the listed accounts, respecting notifications' do
  54. account.mute!(bob, notifications: true)
  55. subject.call(import)
  56. expect(account.muting.count).to eq 2
  57. expect(Mute.find_by(account: account, target_account: bob).hide_notifications).to be true
  58. expect(Mute.find_by(account: account, target_account: eve).hide_notifications).to be false
  59. end
  60. end
  61. describe 'when some accounts are muted and overwrite is set' do
  62. let(:import) { Import.create(account: account, type: 'muting', data: csv, overwrite: true) }
  63. it 'mutes the listed accounts, respecting notifications' do
  64. account.mute!(bob, notifications: true)
  65. subject.call(import)
  66. expect(account.muting.count).to eq 2
  67. expect(Mute.find_by(account: account, target_account: bob).hide_notifications).to be true
  68. expect(Mute.find_by(account: account, target_account: eve).hide_notifications).to be false
  69. end
  70. end
  71. end
  72. context 'import old-style list of followed users' do
  73. subject { ImportService.new }
  74. let(:csv) { attachment_fixture('mute-imports.txt') }
  75. before do
  76. allow(NotificationWorker).to receive(:perform_async)
  77. end
  78. describe 'when no accounts are followed' do
  79. let(:import) { Import.create(account: account, type: 'following', data: csv) }
  80. it 'follows the listed accounts, including boosts' do
  81. subject.call(import)
  82. expect(account.following.count).to eq 1
  83. expect(account.follow_requests.count).to eq 1
  84. expect(Follow.find_by(account: account, target_account: bob).show_reblogs).to be true
  85. end
  86. end
  87. describe 'when some accounts are already followed and overwrite is not set' do
  88. let(:import) { Import.create(account: account, type: 'following', data: csv) }
  89. it 'follows the listed accounts, including notifications' do
  90. account.follow!(bob, reblogs: false)
  91. subject.call(import)
  92. expect(account.following.count).to eq 1
  93. expect(account.follow_requests.count).to eq 1
  94. expect(Follow.find_by(account: account, target_account: bob).show_reblogs).to be true
  95. end
  96. end
  97. describe 'when some accounts are already followed and overwrite is set' do
  98. let(:import) { Import.create(account: account, type: 'following', data: csv, overwrite: true) }
  99. it 'mutes the listed accounts, including notifications' do
  100. account.follow!(bob, reblogs: false)
  101. subject.call(import)
  102. expect(account.following.count).to eq 1
  103. expect(account.follow_requests.count).to eq 1
  104. expect(Follow.find_by(account: account, target_account: bob).show_reblogs).to be true
  105. end
  106. end
  107. end
  108. context 'import new-style list of followed users' do
  109. subject { ImportService.new }
  110. let(:csv) { attachment_fixture('new-following-imports.txt') }
  111. before do
  112. allow(NotificationWorker).to receive(:perform_async)
  113. end
  114. describe 'when no accounts are followed' do
  115. let(:import) { Import.create(account: account, type: 'following', data: csv) }
  116. it 'follows the listed accounts, respecting boosts' do
  117. subject.call(import)
  118. expect(account.following.count).to eq 1
  119. expect(account.follow_requests.count).to eq 1
  120. expect(Follow.find_by(account: account, target_account: bob).show_reblogs).to be true
  121. expect(FollowRequest.find_by(account: account, target_account: eve).show_reblogs).to be false
  122. end
  123. end
  124. describe 'when some accounts are already followed and overwrite is not set' do
  125. let(:import) { Import.create(account: account, type: 'following', data: csv) }
  126. it 'mutes the listed accounts, respecting notifications' do
  127. account.follow!(bob, reblogs: true)
  128. subject.call(import)
  129. expect(account.following.count).to eq 1
  130. expect(account.follow_requests.count).to eq 1
  131. expect(Follow.find_by(account: account, target_account: bob).show_reblogs).to be true
  132. expect(FollowRequest.find_by(account: account, target_account: eve).show_reblogs).to be false
  133. end
  134. end
  135. describe 'when some accounts are already followed and overwrite is set' do
  136. let(:import) { Import.create(account: account, type: 'following', data: csv, overwrite: true) }
  137. it 'mutes the listed accounts, respecting notifications' do
  138. account.follow!(bob, reblogs: true)
  139. subject.call(import)
  140. expect(account.following.count).to eq 1
  141. expect(account.follow_requests.count).to eq 1
  142. expect(Follow.find_by(account: account, target_account: bob).show_reblogs).to be true
  143. expect(FollowRequest.find_by(account: account, target_account: eve).show_reblogs).to be false
  144. end
  145. end
  146. end
  147. end