imports_controller_spec.rb 9.5 KB

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