imports_controller_spec.rb 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Settings::ImportsController do
  4. render_views
  5. let(:user) { Fabricate(:user) }
  6. before do
  7. sign_in user, scope: :user
  8. end
  9. describe 'GET #index' do
  10. let!(:import) { Fabricate(:bulk_import, account: user.account) }
  11. let!(:other_import) { Fabricate(:bulk_import) }
  12. before do
  13. get :index
  14. end
  15. it 'assigns the expected imports', :aggregate_failures do
  16. expect(response).to have_http_status(200)
  17. expect(assigns(:recent_imports)).to eq [import]
  18. expect(response.headers['Cache-Control']).to include('private, no-store')
  19. end
  20. end
  21. describe 'GET #show' do
  22. before do
  23. get :show, params: { id: bulk_import.id }
  24. end
  25. context 'with someone else\'s import' do
  26. let(:bulk_import) { Fabricate(:bulk_import, state: :unconfirmed) }
  27. it 'returns http not found' do
  28. expect(response).to have_http_status(404)
  29. end
  30. end
  31. context 'with an already-confirmed import' do
  32. let(:bulk_import) { Fabricate(:bulk_import, account: user.account, state: :in_progress) }
  33. it 'returns http not found' do
  34. expect(response).to have_http_status(404)
  35. end
  36. end
  37. context 'with an unconfirmed import' do
  38. let(:bulk_import) { Fabricate(:bulk_import, account: user.account, state: :unconfirmed) }
  39. it 'returns http success' do
  40. expect(response).to have_http_status(200)
  41. end
  42. end
  43. end
  44. describe 'POST #confirm' do
  45. subject { post :confirm, params: { id: bulk_import.id } }
  46. before do
  47. allow(BulkImportWorker).to receive(:perform_async)
  48. end
  49. context 'with someone else\'s import' do
  50. let(:bulk_import) { Fabricate(:bulk_import, state: :unconfirmed) }
  51. it 'does not change the import\'s state and returns missing', :aggregate_failures do
  52. expect { subject }.to_not(change { bulk_import.reload.state })
  53. expect(BulkImportWorker).to_not have_received(:perform_async)
  54. expect(response).to have_http_status(404)
  55. end
  56. end
  57. context 'with an already-confirmed import' do
  58. let(:bulk_import) { Fabricate(:bulk_import, account: user.account, state: :in_progress) }
  59. it 'does not change the import\'s state and returns missing', :aggregate_failures do
  60. expect { subject }.to_not(change { bulk_import.reload.state })
  61. expect(BulkImportWorker).to_not have_received(:perform_async)
  62. expect(response).to have_http_status(404)
  63. end
  64. end
  65. context 'with an unconfirmed import' do
  66. let(:bulk_import) { Fabricate(:bulk_import, account: user.account, state: :unconfirmed) }
  67. it 'changes the import\'s state to scheduled and redirects', :aggregate_failures do
  68. expect { subject }.to change { bulk_import.reload.state.to_sym }.from(:unconfirmed).to(:scheduled)
  69. expect(BulkImportWorker).to have_received(:perform_async).with(bulk_import.id)
  70. expect(response).to redirect_to(settings_imports_path)
  71. end
  72. end
  73. end
  74. describe 'DELETE #destroy' do
  75. subject { delete :destroy, params: { id: bulk_import.id } }
  76. context 'with someone else\'s import' do
  77. let(:bulk_import) { Fabricate(:bulk_import, state: :unconfirmed) }
  78. it 'does not delete the import and returns missing', :aggregate_failures do
  79. expect { subject }.to_not(change { BulkImport.exists?(bulk_import.id) })
  80. expect(response).to have_http_status(404)
  81. end
  82. end
  83. context 'with an already-confirmed import' do
  84. let(:bulk_import) { Fabricate(:bulk_import, account: user.account, state: :in_progress) }
  85. it 'does not delete the import and returns missing', :aggregate_failures do
  86. expect { subject }.to_not(change { BulkImport.exists?(bulk_import.id) })
  87. expect(response).to have_http_status(404)
  88. end
  89. end
  90. context 'with an unconfirmed import' do
  91. let(:bulk_import) { Fabricate(:bulk_import, account: user.account, state: :unconfirmed) }
  92. it 'deletes the import and redirects', :aggregate_failures do
  93. expect { subject }.to change { BulkImport.exists?(bulk_import.id) }.from(true).to(false)
  94. expect(response).to redirect_to(settings_imports_path)
  95. end
  96. end
  97. end
  98. describe 'GET #failures' do
  99. subject { get :failures, params: { id: bulk_import.id }, format: :csv }
  100. shared_examples 'export failed rows' do |expected_contents|
  101. let(:bulk_import) { Fabricate(:bulk_import, account: user.account, type: import_type, state: :finished) }
  102. before do
  103. bulk_import.update(total_items: bulk_import.rows.count, processed_items: bulk_import.rows.count, imported_items: 0)
  104. end
  105. it 'returns expected contents', :aggregate_failures do
  106. subject
  107. expect(response).to have_http_status(200)
  108. expect(response.body).to eq expected_contents
  109. end
  110. end
  111. context 'with follows' do
  112. let(:import_type) { 'following' }
  113. let!(:rows) do
  114. [
  115. { 'acct' => 'foo@bar' },
  116. { 'acct' => 'user@bar', 'show_reblogs' => false, 'notify' => true, 'languages' => %w(fr de) },
  117. ].map { |data| Fabricate(:bulk_import_row, bulk_import: bulk_import, data: data) }
  118. end
  119. include_examples 'export failed rows', "Account address,Show boosts,Notify on new posts,Languages\nfoo@bar,true,false,\nuser@bar,false,true,\"fr, de\"\n"
  120. end
  121. context 'with blocks' do
  122. let(:import_type) { 'blocking' }
  123. let!(:rows) do
  124. [
  125. { 'acct' => 'foo@bar' },
  126. { 'acct' => 'user@bar' },
  127. ].map { |data| Fabricate(:bulk_import_row, bulk_import: bulk_import, data: data) }
  128. end
  129. include_examples 'export failed rows', "foo@bar\nuser@bar\n"
  130. end
  131. context 'with mutes' do
  132. let(:import_type) { 'muting' }
  133. let!(:rows) do
  134. [
  135. { 'acct' => 'foo@bar' },
  136. { 'acct' => 'user@bar', 'hide_notifications' => false },
  137. ].map { |data| Fabricate(:bulk_import_row, bulk_import: bulk_import, data: data) }
  138. end
  139. include_examples 'export failed rows', "Account address,Hide notifications\nfoo@bar,true\nuser@bar,false\n"
  140. end
  141. context 'with domain blocks' do
  142. let(:import_type) { 'domain_blocking' }
  143. let!(:rows) do
  144. [
  145. { 'domain' => 'bad.domain' },
  146. { 'domain' => 'evil.domain' },
  147. ].map { |data| Fabricate(:bulk_import_row, bulk_import: bulk_import, data: data) }
  148. end
  149. include_examples 'export failed rows', "bad.domain\nevil.domain\n"
  150. end
  151. context 'with bookmarks' do
  152. let(:import_type) { 'bookmarks' }
  153. let!(:rows) do
  154. [
  155. { 'uri' => 'https://foo.com/1' },
  156. { 'uri' => 'https://foo.com/2' },
  157. ].map { |data| Fabricate(:bulk_import_row, bulk_import: bulk_import, data: data) }
  158. end
  159. include_examples 'export failed rows', "https://foo.com/1\nhttps://foo.com/2\n"
  160. end
  161. context 'with lists' do
  162. let(:import_type) { 'lists' }
  163. let!(:rows) do
  164. [
  165. { 'list_name' => 'Amigos', 'acct' => 'user@example.com' },
  166. { 'list_name' => 'Frenemies', 'acct' => 'user@org.org' },
  167. ].map { |data| Fabricate(:bulk_import_row, bulk_import: bulk_import, data: data) }
  168. end
  169. include_examples 'export failed rows', "Amigos,user@example.com\nFrenemies,user@org.org\n"
  170. end
  171. end
  172. describe 'POST #create' do
  173. subject do
  174. post :create, params: {
  175. form_import: {
  176. type: import_type,
  177. mode: import_mode,
  178. data: fixture_file_upload(import_file),
  179. },
  180. }
  181. end
  182. shared_examples 'successful import' do |type, file, mode|
  183. let(:import_type) { type }
  184. let(:import_file) { file }
  185. let(:import_mode) { mode }
  186. it 'creates an unconfirmed bulk_import with expected type and redirects', :aggregate_failures do
  187. expect { subject }.to change { user.account.bulk_imports.pluck(:state, :type) }.from([]).to([['unconfirmed', import_type]])
  188. expect(response).to redirect_to(settings_import_path(user.account.bulk_imports.first))
  189. end
  190. end
  191. shared_examples 'unsuccessful import' do |type, file, mode|
  192. let(:import_type) { type }
  193. let(:import_file) { file }
  194. let(:import_mode) { mode }
  195. it 'does not creates an unconfirmed bulk_import', :aggregate_failures do
  196. expect { subject }.to_not(change { user.account.bulk_imports.count })
  197. expect(assigns(:import).errors).to_not be_empty
  198. end
  199. end
  200. it_behaves_like 'successful import', 'following', 'imports.txt', 'merge'
  201. it_behaves_like 'successful import', 'following', 'imports.txt', 'overwrite'
  202. it_behaves_like 'successful import', 'blocking', 'imports.txt', 'merge'
  203. it_behaves_like 'successful import', 'blocking', 'imports.txt', 'overwrite'
  204. it_behaves_like 'successful import', 'muting', 'imports.txt', 'merge'
  205. it_behaves_like 'successful import', 'muting', 'imports.txt', 'overwrite'
  206. it_behaves_like 'successful import', 'domain_blocking', 'domain_blocks.csv', 'merge'
  207. it_behaves_like 'successful import', 'domain_blocking', 'domain_blocks.csv', 'overwrite'
  208. it_behaves_like 'successful import', 'bookmarks', 'bookmark-imports.txt', 'merge'
  209. it_behaves_like 'successful import', 'bookmarks', 'bookmark-imports.txt', 'overwrite'
  210. it_behaves_like 'unsuccessful import', 'following', 'domain_blocks.csv', 'merge'
  211. it_behaves_like 'unsuccessful import', 'following', 'domain_blocks.csv', 'overwrite'
  212. it_behaves_like 'unsuccessful import', 'blocking', 'domain_blocks.csv', 'merge'
  213. it_behaves_like 'unsuccessful import', 'blocking', 'domain_blocks.csv', 'overwrite'
  214. it_behaves_like 'unsuccessful import', 'muting', 'domain_blocks.csv', 'merge'
  215. it_behaves_like 'unsuccessful import', 'muting', 'domain_blocks.csv', 'overwrite'
  216. it_behaves_like 'unsuccessful import', 'following', 'empty.csv', 'merge'
  217. it_behaves_like 'unsuccessful import', 'following', 'empty.csv', 'overwrite'
  218. end
  219. end