bulk_import_row_service_spec.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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(:data) do
  78. { 'acct' => target_account.acct, 'list_name' => 'my list' }
  79. end
  80. shared_examples 'common behavior' do
  81. context 'when the target account is already followed' do
  82. before do
  83. account.follow!(target_account)
  84. end
  85. it 'returns true' do
  86. expect(subject.call(import_row)).to be true
  87. end
  88. it 'adds the target account to the list' do
  89. expect { subject.call(import_row) }.to change { ListAccount.joins(:list).exists?(account_id: target_account.id, list: { title: 'my list' }) }.from(false).to(true)
  90. end
  91. end
  92. context 'when the user already requested to follow the target account' do
  93. before do
  94. account.request_follow!(target_account)
  95. end
  96. it 'returns true' do
  97. expect(subject.call(import_row)).to be true
  98. end
  99. it 'adds the target account to the list' do
  100. expect { subject.call(import_row) }.to change { ListAccount.joins(:list).exists?(account_id: target_account.id, list: { title: 'my list' }) }.from(false).to(true)
  101. end
  102. end
  103. context 'when the target account is neither followed nor requested' do
  104. it 'returns true' do
  105. expect(subject.call(import_row)).to be true
  106. end
  107. it 'adds the target account to the list' do
  108. expect { subject.call(import_row) }.to change { ListAccount.joins(:list).exists?(account_id: target_account.id, list: { title: 'my list' }) }.from(false).to(true)
  109. end
  110. end
  111. context 'when the target account is the user themself' do
  112. let(:target_account) { account }
  113. it 'returns true' do
  114. expect(subject.call(import_row)).to be true
  115. end
  116. it 'adds the target account to the list' do
  117. expect { subject.call(import_row) }.to change { ListAccount.joins(:list).exists?(account_id: target_account.id, list: { title: 'my list' }) }.from(false).to(true)
  118. end
  119. end
  120. end
  121. context 'when the list does not exist yet' do
  122. include_examples 'common behavior'
  123. end
  124. context 'when the list exists' do
  125. before do
  126. Fabricate(:list, account: account, title: 'my list')
  127. end
  128. include_examples 'common behavior'
  129. end
  130. end
  131. end
  132. end