2023-02-22 09:55:31 +09:00
# frozen_string_literal: true
2017-04-12 04:40:14 +09:00
require 'rails_helper'
2023-05-04 12:49:53 +09:00
RSpec . describe Settings :: ImportsController do
2017-04-28 22:12:37 +09:00
render_views
2017-04-12 04:40:14 +09:00
2023-05-02 19:08:48 +09:00
let ( :user ) { Fabricate ( :user ) }
2017-04-12 04:40:14 +09:00
before do
2023-05-02 19:08:48 +09:00
sign_in user , scope : :user
2017-04-12 04:40:14 +09:00
end
2023-05-02 19:08:48 +09:00
describe 'GET #index' do
let! ( :import ) { Fabricate ( :bulk_import , account : user . account ) }
let! ( :other_import ) { Fabricate ( :bulk_import ) }
2023-04-19 23:07:29 +09:00
before do
2023-05-02 19:08:48 +09:00
get :index
end
2023-11-11 00:13:42 +09:00
it 'assigns the expected imports' , :aggregate_failures do
2018-04-22 04:35:07 +09:00
expect ( response ) . to have_http_status ( 200 )
2023-11-11 00:13:42 +09:00
expect ( assigns ( :recent_imports ) ) . to eq [ import ]
2023-12-21 18:28:41 +09:00
expect ( assigns ( :recent_imports ) ) . to_not include ( other_import )
2023-04-19 23:07:29 +09:00
expect ( response . headers [ 'Cache-Control' ] ) . to include ( 'private, no-store' )
end
2017-04-12 04:40:14 +09:00
end
2023-05-02 19:08:48 +09:00
describe 'GET #show' do
before do
get :show , params : { id : bulk_import . id }
end
context 'with someone else\'s import' do
let ( :bulk_import ) { Fabricate ( :bulk_import , state : :unconfirmed ) }
it 'returns http not found' do
expect ( response ) . to have_http_status ( 404 )
end
end
context 'with an already-confirmed import' do
let ( :bulk_import ) { Fabricate ( :bulk_import , account : user . account , state : :in_progress ) }
it 'returns http not found' do
expect ( response ) . to have_http_status ( 404 )
end
end
context 'with an unconfirmed import' do
let ( :bulk_import ) { Fabricate ( :bulk_import , account : user . account , state : :unconfirmed ) }
it 'returns http success' do
expect ( response ) . to have_http_status ( 200 )
end
end
end
describe 'POST #confirm' do
subject { post :confirm , params : { id : bulk_import . id } }
before do
allow ( BulkImportWorker ) . to receive ( :perform_async )
end
context 'with someone else\'s import' do
let ( :bulk_import ) { Fabricate ( :bulk_import , state : :unconfirmed ) }
2023-11-11 00:13:42 +09:00
it 'does not change the import\'s state and returns missing' , :aggregate_failures do
2023-05-02 19:08:48 +09:00
expect { subject } . to_not ( change { bulk_import . reload . state } )
expect ( BulkImportWorker ) . to_not have_received ( :perform_async )
expect ( response ) . to have_http_status ( 404 )
end
end
context 'with an already-confirmed import' do
let ( :bulk_import ) { Fabricate ( :bulk_import , account : user . account , state : :in_progress ) }
2023-11-11 00:13:42 +09:00
it 'does not change the import\'s state and returns missing' , :aggregate_failures do
2023-05-02 19:08:48 +09:00
expect { subject } . to_not ( change { bulk_import . reload . state } )
expect ( BulkImportWorker ) . to_not have_received ( :perform_async )
expect ( response ) . to have_http_status ( 404 )
end
end
context 'with an unconfirmed import' do
let ( :bulk_import ) { Fabricate ( :bulk_import , account : user . account , state : :unconfirmed ) }
2023-11-11 00:13:42 +09:00
it 'changes the import\'s state to scheduled and redirects' , :aggregate_failures do
2023-05-02 19:08:48 +09:00
expect { subject } . to change { bulk_import . reload . state . to_sym } . from ( :unconfirmed ) . to ( :scheduled )
expect ( BulkImportWorker ) . to have_received ( :perform_async ) . with ( bulk_import . id )
expect ( response ) . to redirect_to ( settings_imports_path )
end
end
end
describe 'DELETE #destroy' do
subject { delete :destroy , params : { id : bulk_import . id } }
context 'with someone else\'s import' do
let ( :bulk_import ) { Fabricate ( :bulk_import , state : :unconfirmed ) }
2023-11-11 00:13:42 +09:00
it 'does not delete the import and returns missing' , :aggregate_failures do
2023-05-02 19:08:48 +09:00
expect { subject } . to_not ( change { BulkImport . exists? ( bulk_import . id ) } )
expect ( response ) . to have_http_status ( 404 )
end
end
context 'with an already-confirmed import' do
let ( :bulk_import ) { Fabricate ( :bulk_import , account : user . account , state : :in_progress ) }
2023-11-11 00:13:42 +09:00
it 'does not delete the import and returns missing' , :aggregate_failures do
2023-05-02 19:08:48 +09:00
expect { subject } . to_not ( change { BulkImport . exists? ( bulk_import . id ) } )
expect ( response ) . to have_http_status ( 404 )
end
end
context 'with an unconfirmed import' do
let ( :bulk_import ) { Fabricate ( :bulk_import , account : user . account , state : :unconfirmed ) }
2023-11-11 00:13:42 +09:00
it 'deletes the import and redirects' , :aggregate_failures do
2023-05-02 19:08:48 +09:00
expect { subject } . to change { BulkImport . exists? ( bulk_import . id ) } . from ( true ) . to ( false )
expect ( response ) . to redirect_to ( settings_imports_path )
end
end
end
describe 'GET #failures' do
subject { get :failures , params : { id : bulk_import . id } , format : :csv }
shared_examples 'export failed rows' do | expected_contents |
let ( :bulk_import ) { Fabricate ( :bulk_import , account : user . account , type : import_type , state : :finished ) }
before do
2023-12-21 18:28:41 +09:00
rows . each { | data | Fabricate ( :bulk_import_row , bulk_import : bulk_import , data : data ) }
2023-05-02 19:08:48 +09:00
bulk_import . update ( total_items : bulk_import . rows . count , processed_items : bulk_import . rows . count , imported_items : 0 )
end
2023-11-11 00:13:42 +09:00
it 'returns expected contents' , :aggregate_failures do
2023-05-02 19:08:48 +09:00
subject
2023-11-11 00:13:42 +09:00
expect ( response ) . to have_http_status ( 200 )
2023-05-02 19:08:48 +09:00
expect ( response . body ) . to eq expected_contents
end
end
context 'with follows' do
let ( :import_type ) { 'following' }
2023-12-21 18:28:41 +09:00
let ( :rows ) do
2023-05-02 19:08:48 +09:00
[
{ 'acct' = > 'foo@bar' } ,
2023-11-08 22:03:44 +09:00
{ 'acct' = > 'user@bar' , 'show_reblogs' = > false , 'notify' = > true , 'languages' = > %w( fr de ) } ,
2023-12-21 18:28:41 +09:00
]
2023-05-02 19:08:48 +09:00
end
include_examples 'export failed rows' , " Account address,Show boosts,Notify on new posts,Languages \n foo@bar,true,false, \n user@bar,false,true, \" fr, de \" \n "
end
context 'with blocks' do
let ( :import_type ) { 'blocking' }
2023-12-21 18:28:41 +09:00
let ( :rows ) do
2023-05-02 19:08:48 +09:00
[
{ 'acct' = > 'foo@bar' } ,
{ 'acct' = > 'user@bar' } ,
2023-12-21 18:28:41 +09:00
]
2023-05-02 19:08:48 +09:00
end
include_examples 'export failed rows' , " foo@bar \n user@bar \n "
end
context 'with mutes' do
let ( :import_type ) { 'muting' }
2023-12-21 18:28:41 +09:00
let ( :rows ) do
2023-05-02 19:08:48 +09:00
[
{ 'acct' = > 'foo@bar' } ,
{ 'acct' = > 'user@bar' , 'hide_notifications' = > false } ,
2023-12-21 18:28:41 +09:00
]
2023-05-02 19:08:48 +09:00
end
include_examples 'export failed rows' , " Account address,Hide notifications \n foo@bar,true \n user@bar,false \n "
end
context 'with domain blocks' do
let ( :import_type ) { 'domain_blocking' }
2023-12-21 18:28:41 +09:00
let ( :rows ) do
2023-05-02 19:08:48 +09:00
[
{ 'domain' = > 'bad.domain' } ,
{ 'domain' = > 'evil.domain' } ,
2023-12-21 18:28:41 +09:00
]
2023-05-02 19:08:48 +09:00
end
include_examples 'export failed rows' , " bad.domain \n evil.domain \n "
end
context 'with bookmarks' do
let ( :import_type ) { 'bookmarks' }
2023-12-21 18:28:41 +09:00
let ( :rows ) do
2023-05-02 19:08:48 +09:00
[
{ 'uri' = > 'https://foo.com/1' } ,
{ 'uri' = > 'https://foo.com/2' } ,
2023-12-21 18:28:41 +09:00
]
2023-05-02 19:08:48 +09:00
end
include_examples 'export failed rows' , " https://foo.com/1 \n https://foo.com/2 \n "
end
2023-10-17 00:20:28 +09:00
context 'with lists' do
let ( :import_type ) { 'lists' }
2023-12-21 18:28:41 +09:00
let ( :rows ) do
2023-10-17 00:20:28 +09:00
[
{ 'list_name' = > 'Amigos' , 'acct' = > 'user@example.com' } ,
{ 'list_name' = > 'Frenemies' , 'acct' = > 'user@org.org' } ,
2023-12-21 18:28:41 +09:00
]
2023-10-17 00:20:28 +09:00
end
include_examples 'export failed rows' , " Amigos,user@example.com \n Frenemies,user@org.org \n "
end
2023-05-02 19:08:48 +09:00
end
2017-04-12 04:40:14 +09:00
describe 'POST #create' do
2023-05-02 19:08:48 +09:00
subject do
2017-04-12 04:40:14 +09:00
post :create , params : {
2023-05-02 19:08:48 +09:00
form_import : {
type : import_type ,
mode : import_mode ,
data : fixture_file_upload ( import_file ) ,
2023-02-18 23:33:41 +09:00
} ,
2017-04-12 04:40:14 +09:00
}
2023-05-02 19:08:48 +09:00
end
shared_examples 'successful import' do | type , file , mode |
let ( :import_type ) { type }
let ( :import_file ) { file }
let ( :import_mode ) { mode }
2017-04-12 04:40:14 +09:00
2023-11-11 00:13:42 +09:00
it 'creates an unconfirmed bulk_import with expected type and redirects' , :aggregate_failures do
2023-05-02 19:08:48 +09:00
expect { subject } . to change { user . account . bulk_imports . pluck ( :state , :type ) } . from ( [ ] ) . to ( [ [ 'unconfirmed' , import_type ] ] )
expect ( response ) . to redirect_to ( settings_import_path ( user . account . bulk_imports . first ) )
end
2017-04-12 04:40:14 +09:00
end
2023-05-02 19:08:48 +09:00
shared_examples 'unsuccessful import' do | type , file , mode |
let ( :import_type ) { type }
let ( :import_file ) { file }
let ( :import_mode ) { mode }
2023-11-11 00:13:42 +09:00
it 'does not creates an unconfirmed bulk_import' , :aggregate_failures do
2023-05-02 19:08:48 +09:00
expect { subject } . to_not ( change { user . account . bulk_imports . count } )
2017-04-12 04:40:14 +09:00
2023-05-02 19:08:48 +09:00
expect ( assigns ( :import ) . errors ) . to_not be_empty
end
2017-04-12 04:40:14 +09:00
end
2023-05-02 19:08:48 +09:00
it_behaves_like 'successful import' , 'following' , 'imports.txt' , 'merge'
it_behaves_like 'successful import' , 'following' , 'imports.txt' , 'overwrite'
it_behaves_like 'successful import' , 'blocking' , 'imports.txt' , 'merge'
it_behaves_like 'successful import' , 'blocking' , 'imports.txt' , 'overwrite'
it_behaves_like 'successful import' , 'muting' , 'imports.txt' , 'merge'
it_behaves_like 'successful import' , 'muting' , 'imports.txt' , 'overwrite'
it_behaves_like 'successful import' , 'domain_blocking' , 'domain_blocks.csv' , 'merge'
it_behaves_like 'successful import' , 'domain_blocking' , 'domain_blocks.csv' , 'overwrite'
it_behaves_like 'successful import' , 'bookmarks' , 'bookmark-imports.txt' , 'merge'
it_behaves_like 'successful import' , 'bookmarks' , 'bookmark-imports.txt' , 'overwrite'
it_behaves_like 'unsuccessful import' , 'following' , 'domain_blocks.csv' , 'merge'
it_behaves_like 'unsuccessful import' , 'following' , 'domain_blocks.csv' , 'overwrite'
it_behaves_like 'unsuccessful import' , 'blocking' , 'domain_blocks.csv' , 'merge'
it_behaves_like 'unsuccessful import' , 'blocking' , 'domain_blocks.csv' , 'overwrite'
it_behaves_like 'unsuccessful import' , 'muting' , 'domain_blocks.csv' , 'merge'
it_behaves_like 'unsuccessful import' , 'muting' , 'domain_blocks.csv' , 'overwrite'
it_behaves_like 'unsuccessful import' , 'following' , 'empty.csv' , 'merge'
it_behaves_like 'unsuccessful import' , 'following' , 'empty.csv' , 'overwrite'
2017-04-12 04:40:14 +09:00
end
end