imports_controller_spec.rb 9.5 KB

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