imports_controller_spec.rb 10.0 KB

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