import_service_spec.rb 6.8 KB

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