2015-10-11 17:41:23 +08:00
|
|
|
require "rails_helper"
|
2014-12-23 00:17:04 +08:00
|
|
|
|
|
|
|
describe ExportCsvController do
|
2015-01-16 01:30:59 +08:00
|
|
|
let(:export_filename) { "user-archive-codinghorror-150115-234817-999.csv.gz" }
|
2014-12-23 00:17:04 +08:00
|
|
|
|
2015-06-08 13:39:38 +08:00
|
|
|
context "while not logged in" do
|
|
|
|
describe ".download" do
|
|
|
|
it "returns 404 when the unauthorized user tries to export csv file" do
|
|
|
|
get :show, id: export_filename
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-12-23 00:17:04 +08:00
|
|
|
|
|
|
|
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))
|
2017-05-20 03:59:37 +08:00
|
|
|
xhr :post, :export_entity, entity: "user_archive"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-12-23 00:17:04 +08:00
|
|
|
end
|
|
|
|
|
2014-12-30 20:37:05 +08:00
|
|
|
it "should not enqueue export job if rate limit is reached" do
|
|
|
|
Jobs::ExportCsvFile.any_instance.expects(:execute).never
|
2015-01-16 01:30:59 +08:00
|
|
|
UserExport.create(file_name: "user-archive-codinghorror-150116-003249", user_id: @user.id)
|
2017-05-20 03:59:37 +08:00
|
|
|
xhr :post, :export_entity, entity: "user_archive"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2014-12-30 20:37:05 +08:00
|
|
|
end
|
|
|
|
|
2014-12-23 00:17:04 +08:00
|
|
|
it "returns 404 when normal user tries to export admin entity" do
|
2017-05-20 03:59:37 +08:00
|
|
|
xhr :post, :export_entity, entity: "staff_action"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2014-12-23 00:17:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".download" do
|
|
|
|
it "uses send_file to transmit the export file" do
|
2015-01-16 01:30:59 +08:00
|
|
|
file = UserExport.create(file_name: "user-archive-codinghorror-150116-003249", user_id: @user.id)
|
|
|
|
file_name = "user-archive-codinghorror-150116-003249-#{file.id}.csv.gz"
|
2014-12-23 00:17:04 +08:00
|
|
|
controller.stubs(:render)
|
2014-12-29 00:13:49 +08:00
|
|
|
export = UserExport.new()
|
|
|
|
UserExport.expects(:get_download_path).with(file_name).returns(export)
|
2014-12-23 00:17:04 +08:00
|
|
|
subject.expects(:send_file).with(export)
|
2014-12-24 17:11:41 +08:00
|
|
|
get :show, id: file_name
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-12-23 00:17:04 +08:00
|
|
|
end
|
|
|
|
|
2014-12-24 17:11:41 +08:00
|
|
|
it "returns 404 when the user tries to export another user's csv file" do
|
|
|
|
get :show, id: export_filename
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_not_found
|
2014-12-23 00:17:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 when the export file does not exist" do
|
2014-12-29 00:13:49 +08:00
|
|
|
UserExport.expects(:get_download_path).returns(nil)
|
2014-12-24 17:11:41 +08:00
|
|
|
get :show, id: export_filename
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_not_found
|
2014-12-23 00:17:04 +08:00
|
|
|
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))
|
2017-05-20 03:59:37 +08:00
|
|
|
xhr :post, :export_entity, entity: "staff_action"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-12-23 00:17:04 +08:00
|
|
|
end
|
2014-12-30 20:37:05 +08:00
|
|
|
|
|
|
|
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))
|
2015-01-16 01:30:59 +08:00
|
|
|
UserExport.create(file_name: "screened-email-150116-010145", user_id: @admin.id)
|
2017-05-20 03:59:37 +08:00
|
|
|
xhr :post, :export_entity, entity: "staff_action"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-12-30 20:37:05 +08:00
|
|
|
end
|
2014-12-23 00:17:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".download" do
|
|
|
|
it "uses send_file to transmit the export file" do
|
2015-01-16 01:30:59 +08:00
|
|
|
file = UserExport.create(file_name: "screened-email-150116-010145", user_id: @admin.id)
|
|
|
|
file_name = "screened-email-150116-010145-#{file.id}.csv.gz"
|
2014-12-23 00:17:04 +08:00
|
|
|
controller.stubs(:render)
|
2014-12-29 00:13:49 +08:00
|
|
|
export = UserExport.new()
|
|
|
|
UserExport.expects(:get_download_path).with(file_name).returns(export)
|
2014-12-23 00:17:04 +08:00
|
|
|
subject.expects(:send_file).with(export)
|
2014-12-24 17:11:41 +08:00
|
|
|
get :show, id: file_name
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-12-23 00:17:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 when the export file does not exist" do
|
2014-12-29 00:13:49 +08:00
|
|
|
UserExport.expects(:get_download_path).returns(nil)
|
2014-12-24 17:11:41 +08:00
|
|
|
get :show, id: export_filename
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_not_found
|
2014-12-23 00:17:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|