2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe ExportCsvController do
|
2018-06-05 22:46:04 -05:00
|
|
|
context "while logged in as normal user" do
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2018-06-05 22:46:04 -05:00
|
|
|
before { sign_in(user) }
|
|
|
|
|
2018-09-20 20:07:13 -05:00
|
|
|
describe "#export_entity" do
|
2020-08-27 17:54:25 -05:00
|
|
|
it "enqueues user archive job" do
|
2018-06-05 22:46:04 -05:00
|
|
|
post "/export_csv/export_entity.json", params: { entity: "user_archive" }
|
2018-06-07 03:11:09 -05:00
|
|
|
expect(response.status).to eq(200)
|
2020-08-27 17:54:25 -05:00
|
|
|
expect(Jobs::ExportUserArchive.jobs.size).to eq(1)
|
2018-06-05 22:46:04 -05:00
|
|
|
|
2020-08-27 17:54:25 -05:00
|
|
|
job_data = Jobs::ExportUserArchive.jobs.first["args"].first
|
2018-06-05 22:46:04 -05:00
|
|
|
expect(job_data["user_id"]).to eq(user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not enqueue export job if rate limit is reached" do
|
|
|
|
UserExport.create(file_name: "user-archive-codinghorror-150116-003249", user_id: user.id)
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "user_archive" }
|
2019-12-24 07:57:25 -06:00
|
|
|
expect(response.status).to eq(422)
|
2020-08-27 17:54:25 -05:00
|
|
|
expect(Jobs::ExportUserArchive.jobs.size).to eq(0)
|
2018-06-05 22:46:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 when normal user tries to export admin entity" do
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "staff_action" }
|
2019-12-24 07:57:25 -06:00
|
|
|
expect(response.status).to eq(422)
|
2018-06-05 22:46:04 -05:00
|
|
|
expect(Jobs::ExportCsvFile.jobs.size).to eq(0)
|
|
|
|
end
|
2018-09-18 16:46:45 -05:00
|
|
|
|
|
|
|
it "correctly logs the entity export" do
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "user_archive" }
|
|
|
|
|
|
|
|
log_entry = UserHistory.last
|
|
|
|
expect(log_entry.action).to eq(UserHistory.actions[:entity_export])
|
|
|
|
expect(log_entry.acting_user_id).to eq(user.id)
|
|
|
|
expect(log_entry.subject).to eq("user_archive")
|
|
|
|
end
|
2018-06-05 22:46:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "while logged in as an admin" do
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2018-06-05 22:46:04 -05:00
|
|
|
before { sign_in(admin) }
|
|
|
|
|
2018-09-20 20:07:13 -05:00
|
|
|
describe "#export_entity" do
|
2018-06-05 22:46:04 -05:00
|
|
|
it "enqueues export job" do
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "staff_action" }
|
2018-06-07 03:11:09 -05:00
|
|
|
expect(response.status).to eq(200)
|
2018-06-05 22:46:04 -05:00
|
|
|
expect(Jobs::ExportCsvFile.jobs.size).to eq(1)
|
|
|
|
|
|
|
|
job_data = Jobs::ExportCsvFile.jobs.first["args"].first
|
|
|
|
expect(job_data["entity"]).to eq("staff_action")
|
|
|
|
expect(job_data["user_id"]).to eq(admin.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not rate limit export for staff" do
|
|
|
|
UserExport.create(file_name: "screened-email-150116-010145", user_id: admin.id)
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "staff_action" }
|
2018-06-07 03:11:09 -05:00
|
|
|
expect(response.status).to eq(200)
|
2018-06-05 22:46:04 -05:00
|
|
|
expect(Jobs::ExportCsvFile.jobs.size).to eq(1)
|
|
|
|
|
|
|
|
job_data = Jobs::ExportCsvFile.jobs.first["args"].first
|
|
|
|
expect(job_data["entity"]).to eq("staff_action")
|
|
|
|
expect(job_data["user_id"]).to eq(admin.id)
|
|
|
|
end
|
2018-09-18 16:46:45 -05:00
|
|
|
|
|
|
|
it "correctly logs the entity export" do
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "user_list" }
|
|
|
|
|
|
|
|
log_entry = UserHistory.last
|
|
|
|
expect(log_entry.action).to eq(UserHistory.actions[:entity_export])
|
|
|
|
expect(log_entry.acting_user_id).to eq(admin.id)
|
|
|
|
expect(log_entry.subject).to eq("user_list")
|
|
|
|
end
|
2018-06-05 22:46:04 -05:00
|
|
|
end
|
|
|
|
end
|
2018-09-20 20:07:13 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
context "while logged in as a moderator" do
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
2018-09-20 20:07:13 -05:00
|
|
|
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "#export_entity" do
|
|
|
|
it "does not allow moderators to export user_list" do
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "user_list" }
|
2019-12-24 07:57:25 -06:00
|
|
|
expect(response.status).to eq(422)
|
2018-09-20 20:07:13 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "allows moderator to export other entities" do
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "staff_action" }
|
2018-09-20 20:07:13 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(Jobs::ExportCsvFile.jobs.size).to eq(1)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
job_data = Jobs::ExportCsvFile.jobs.first["args"].first
|
|
|
|
expect(job_data["entity"]).to eq("staff_action")
|
|
|
|
expect(job_data["user_id"]).to eq(moderator.id)
|
2018-09-20 20:07:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-05 22:46:04 -05:00
|
|
|
end
|