bulk_import_row_service_spec.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe BulkImportRowService do
  4. subject { described_class.new }
  5. let(:account) { Fabricate(:account) }
  6. let(:import) { Fabricate(:bulk_import, account: account, type: import_type) }
  7. let(:import_row) { Fabricate(:bulk_import_row, bulk_import: import, data: data) }
  8. describe '#call' do
  9. context 'when importing a follow' do
  10. let(:import_type) { 'following' }
  11. let(:target_account) { Fabricate(:account) }
  12. let(:service_double) { instance_double(FollowService, call: nil) }
  13. let(:data) do
  14. { 'acct' => target_account.acct }
  15. end
  16. before do
  17. allow(FollowService).to receive(:new).and_return(service_double)
  18. end
  19. it 'calls FollowService with the expected arguments and returns true' do
  20. expect(subject.call(import_row)).to be true
  21. expect(service_double).to have_received(:call).with(account, target_account, { reblogs: nil, notify: nil, languages: nil })
  22. end
  23. end
  24. context 'when importing a block' do
  25. let(:import_type) { 'blocking' }
  26. let(:target_account) { Fabricate(:account) }
  27. let(:service_double) { instance_double(BlockService, call: nil) }
  28. let(:data) do
  29. { 'acct' => target_account.acct }
  30. end
  31. before do
  32. allow(BlockService).to receive(:new).and_return(service_double)
  33. end
  34. it 'calls BlockService with the expected arguments and returns true' do
  35. expect(subject.call(import_row)).to be true
  36. expect(service_double).to have_received(:call).with(account, target_account)
  37. end
  38. end
  39. context 'when importing a mute' do
  40. let(:import_type) { 'muting' }
  41. let(:target_account) { Fabricate(:account) }
  42. let(:service_double) { instance_double(MuteService, call: nil) }
  43. let(:data) do
  44. { 'acct' => target_account.acct }
  45. end
  46. before do
  47. allow(MuteService).to receive(:new).and_return(service_double)
  48. end
  49. it 'calls MuteService with the expected arguments and returns true' do
  50. expect(subject.call(import_row)).to be true
  51. expect(service_double).to have_received(:call).with(account, target_account, { notifications: nil })
  52. end
  53. end
  54. context 'when importing a bookmark' do
  55. let(:import_type) { 'bookmarks' }
  56. let(:data) do
  57. { 'uri' => ActivityPub::TagManager.instance.uri_for(target_status) }
  58. end
  59. context 'when the status is public' do
  60. let(:target_status) { Fabricate(:status) }
  61. it 'bookmarks the status and returns true' do
  62. expect(subject.call(import_row)).to be true
  63. expect(account.bookmarked?(target_status)).to be true
  64. end
  65. end
  66. context 'when the status is not accessible to the user' do
  67. let(:target_status) { Fabricate(:status, visibility: :direct) }
  68. it 'does not bookmark the status and returns false' do
  69. expect(subject.call(import_row)).to be false
  70. expect(account.bookmarked?(target_status)).to be false
  71. end
  72. end
  73. end
  74. context 'when importing a list row' do
  75. let(:import_type) { 'lists' }
  76. let(:target_account) { Fabricate(:account) }
  77. let(:list_name) { 'my list' }
  78. let(:data) do
  79. { 'acct' => target_account.acct, 'list_name' => list_name }
  80. end
  81. shared_examples 'common behavior' do
  82. shared_examples 'row import success and list addition' do
  83. it 'returns true and adds the target account to the list' do
  84. result = nil
  85. expect { result = subject.call(import_row) }
  86. .to change { result }.from(nil).to(true)
  87. .and add_target_account_to_list
  88. end
  89. end
  90. context 'when the target account is already followed' do
  91. before do
  92. account.follow!(target_account)
  93. end
  94. include_examples 'row import success and list addition'
  95. end
  96. context 'when the user already requested to follow the target account' do
  97. before do
  98. account.request_follow!(target_account)
  99. end
  100. include_examples 'row import success and list addition'
  101. end
  102. context 'when the target account is neither followed nor requested' do
  103. include_examples 'row import success and list addition'
  104. end
  105. context 'when the target account is the user themself' do
  106. let(:target_account) { account }
  107. include_examples 'row import success and list addition'
  108. end
  109. def add_target_account_to_list
  110. change { target_account_on_list? }
  111. .from(false)
  112. .to(true)
  113. end
  114. def target_account_on_list?
  115. ListAccount
  116. .joins(:list)
  117. .exists?(
  118. account_id: target_account.id,
  119. list: { title: list_name }
  120. )
  121. end
  122. end
  123. context 'when the list does not exist yet' do
  124. include_examples 'common behavior'
  125. end
  126. context 'when the list exists' do
  127. before do
  128. Fabricate(:list, account: account, title: list_name)
  129. end
  130. include_examples 'common behavior'
  131. it 'does not create a new list' do
  132. account.follow!(target_account)
  133. expect { subject.call(import_row) }.to_not(change { List.where(title: list_name).count })
  134. end
  135. end
  136. end
  137. end
  138. end