2014-12-22 21:47:04 +05:30
require " spec_helper "
describe ExportCsvController do
2015-01-02 12:29:05 +05:30
let ( :export_filename ) { " user-archive-999.csv " }
2014-12-22 21:47:04 +05:30
context " while logged in as normal user " do
before { @user = log_in ( :user ) }
describe " .export_entity " do
it " enqueues export job " do
Jobs . expects ( :enqueue ) . with ( :export_csv_file , has_entries ( entity : " user_archive " , user_id : @user . id ) )
xhr :post , :export_entity , entity : " user_archive " , entity_type : " user "
response . should be_success
end
2014-12-30 18:07:05 +05:30
it " should not enqueue export job if rate limit is reached " do
Jobs :: ExportCsvFile . any_instance . expects ( :execute ) . never
UserExport . create ( export_type : " user " , user_id : @user . id )
xhr :post , :export_entity , entity : " user_archive " , entity_type : " user "
response . should_not be_success
end
2014-12-22 21:47:04 +05:30
it " returns 404 when normal user tries to export admin entity " do
xhr :post , :export_entity , entity : " staff_action " , entity_type : " admin "
response . should_not be_success
end
end
describe " .download " do
it " uses send_file to transmit the export file " do
2014-12-28 21:43:49 +05:30
file = UserExport . create ( export_type : " user " , user_id : @user . id )
2015-01-02 12:29:05 +05:30
file_name = " user-archive- #{ file . id } .csv "
2014-12-22 21:47:04 +05:30
controller . stubs ( :render )
2014-12-28 21:43:49 +05:30
export = UserExport . new ( )
UserExport . expects ( :get_download_path ) . with ( file_name ) . returns ( export )
2014-12-22 21:47:04 +05:30
subject . expects ( :send_file ) . with ( export )
2014-12-24 14:41:41 +05:30
get :show , id : file_name
2014-12-22 21:47:04 +05:30
response . should be_success
end
2014-12-24 14:41:41 +05:30
it " returns 404 when the user tries to export another user's csv file " do
get :show , id : export_filename
response . should be_not_found
2014-12-22 21:47:04 +05:30
end
it " returns 404 when the export file does not exist " do
2014-12-28 21:43:49 +05:30
UserExport . expects ( :get_download_path ) . returns ( nil )
2014-12-24 14:41:41 +05:30
get :show , id : export_filename
2014-12-22 21:47:04 +05:30
response . should be_not_found
end
end
end
context " while logged in as an admin " do
before { @admin = log_in ( :admin ) }
describe " .export_entity " do
it " enqueues export job " do
Jobs . expects ( :enqueue ) . with ( :export_csv_file , has_entries ( entity : " staff_action " , user_id : @admin . id ) )
xhr :post , :export_entity , entity : " staff_action " , entity_type : " admin "
response . should be_success
end
2014-12-30 18:07:05 +05:30
it " should not rate limit export for staff " do
Jobs . expects ( :enqueue ) . with ( :export_csv_file , has_entries ( entity : " staff_action " , user_id : @admin . id ) )
UserExport . create ( export_type : " admin " , user_id : @admin . id )
xhr :post , :export_entity , entity : " staff_action " , entity_type : " admin "
response . should be_success
end
2014-12-22 21:47:04 +05:30
end
describe " .download " do
it " uses send_file to transmit the export file " do
2014-12-28 21:43:49 +05:30
file = UserExport . create ( export_type : " admin " , user_id : @admin . id )
2015-01-02 12:29:05 +05:30
file_name = " screened-email- #{ file . id } .csv "
2014-12-22 21:47:04 +05:30
controller . stubs ( :render )
2014-12-28 21:43:49 +05:30
export = UserExport . new ( )
UserExport . expects ( :get_download_path ) . with ( file_name ) . returns ( export )
2014-12-22 21:47:04 +05:30
subject . expects ( :send_file ) . with ( export )
2014-12-24 14:41:41 +05:30
get :show , id : file_name
2014-12-22 21:47:04 +05:30
response . should be_success
end
it " returns 404 when the export file does not exist " do
2014-12-28 21:43:49 +05:30
UserExport . expects ( :get_download_path ) . returns ( nil )
2014-12-24 14:41:41 +05:30
get :show , id : export_filename
2014-12-22 21:47:04 +05:30
response . should be_not_found
end
end
end
end