2019-04-30 10:27:42 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 05:27:38 +03:00
|
|
|
RSpec.describe ExportCsvController do
|
2018-06-06 06:46:04 +03:00
|
|
|
context "while logged in as normal user" do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:user)
|
2025-01-24 08:13:25 +11:00
|
|
|
fab!(:user2) { Fabricate(:user) }
|
2018-06-06 06:46:04 +03:00
|
|
|
before { sign_in(user) }
|
|
|
|
|
2018-09-21 09:07:13 +08:00
|
|
|
describe "#export_entity" do
|
2020-08-27 15:54:25 -07:00
|
|
|
it "enqueues user archive job" do
|
2018-06-06 06:46:04 +03:00
|
|
|
post "/export_csv/export_entity.json", params: { entity: "user_archive" }
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2020-08-27 15:54:25 -07:00
|
|
|
expect(Jobs::ExportUserArchive.jobs.size).to eq(1)
|
2018-06-06 06:46:04 +03:00
|
|
|
|
2020-08-27 15:54:25 -07:00
|
|
|
job_data = Jobs::ExportUserArchive.jobs.first["args"].first
|
2018-06-06 06:46:04 +03: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 19:27:25 +05:30
|
|
|
expect(response.status).to eq(422)
|
2020-08-27 15:54:25 -07:00
|
|
|
expect(Jobs::ExportUserArchive.jobs.size).to eq(0)
|
2018-06-06 06:46:04 +03: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 19:27:25 +05:30
|
|
|
expect(response.status).to eq(422)
|
2018-06-06 06:46:04 +03:00
|
|
|
expect(Jobs::ExportCsvFile.jobs.size).to eq(0)
|
|
|
|
end
|
2018-09-19 03:16:45 +05:30
|
|
|
|
2025-01-24 08:13:25 +11:00
|
|
|
it "does not allow a normal user to export another user's archive" do
|
|
|
|
post "/export_csv/export_entity.json",
|
|
|
|
params: {
|
|
|
|
entity: "user_archive",
|
|
|
|
args: {
|
|
|
|
export_user_id: user2.id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(Jobs::ExportUserArchive.jobs.size).to eq(0)
|
|
|
|
end
|
|
|
|
|
2018-09-19 03:16:45 +05:30
|
|
|
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-06 06:46:04 +03:00
|
|
|
end
|
2025-01-24 08:13:25 +11:00
|
|
|
|
|
|
|
describe "#latest_user_archive" do
|
|
|
|
it "returns the latest user archive" do
|
|
|
|
export = generate_exports(user)
|
|
|
|
|
|
|
|
get "/export_csv/latest_user_archive/#{user.id}.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["user_export"]["id"]).to eq(export.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nothing when the user has no archives" do
|
|
|
|
get "/export_csv/latest_user_archive/#{user.id}.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow a normal user to view another user's archive" do
|
|
|
|
generate_exports(user2)
|
|
|
|
get "/export_csv/latest_user_archive/#{user2.id}.json"
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
2018-06-06 06:46:04 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
context "while logged in as an admin" do
|
2025-01-24 08:13:25 +11:00
|
|
|
fab!(:user)
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:admin)
|
2018-06-06 06:46:04 +03:00
|
|
|
before { sign_in(admin) }
|
|
|
|
|
2018-09-21 09:07:13 +08:00
|
|
|
describe "#export_entity" do
|
2018-06-06 06:46:04 +03:00
|
|
|
it "enqueues export job" do
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "staff_action" }
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2018-06-06 06:46:04 +03: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 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2018-06-06 06:46:04 +03: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-19 03:16:45 +05:30
|
|
|
|
2025-01-24 08:13:25 +11:00
|
|
|
it "allows user archives for other users" do
|
|
|
|
post "/export_csv/export_entity.json",
|
|
|
|
params: {
|
|
|
|
entity: "user_archive",
|
|
|
|
args: {
|
|
|
|
export_user_id: user.id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(Jobs::ExportUserArchive.jobs.size).to eq(1)
|
|
|
|
|
|
|
|
job_data = Jobs::ExportUserArchive.jobs.first["args"].first
|
|
|
|
expect(job_data["user_id"]).to eq(user.id)
|
|
|
|
end
|
|
|
|
|
2018-09-19 03:16:45 +05:30
|
|
|
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
|
2024-02-22 13:47:15 -06:00
|
|
|
|
|
|
|
it "fails requests where the entity is too long" do
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "x" * 200 }
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "fails requests where the name arg is too long" do
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "foo", args: { name: "x" * 200 } }
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
2018-06-06 06:46:04 +03:00
|
|
|
end
|
2025-01-24 08:13:25 +11:00
|
|
|
|
|
|
|
describe "#latest_user_archive" do
|
|
|
|
it "allows an admin to view another user's archive" do
|
|
|
|
export = generate_exports(user)
|
|
|
|
get "/export_csv/latest_user_archive/#{user.id}.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["user_export"]["id"]).to eq(export.id)
|
|
|
|
end
|
|
|
|
end
|
2018-06-06 06:46:04 +03:00
|
|
|
end
|
2018-09-21 09:07:13 +08:00
|
|
|
|
|
|
|
context "while logged in as a moderator" do
|
2025-01-24 08:13:25 +11:00
|
|
|
fab!(:user)
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:moderator)
|
2018-09-21 09:07:13 +08:00
|
|
|
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
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 19:27:25 +05:30
|
|
|
expect(response.status).to eq(422)
|
2018-09-21 09:07:13 +08:00
|
|
|
end
|
|
|
|
|
2024-11-13 14:04:20 +11:00
|
|
|
it "does not allow moderators to export screened_email if they has no permission to view emails" do
|
|
|
|
SiteSetting.moderators_view_emails = false
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "screened_email" }
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows moderator to export screened_email if they has permission to view emails" do
|
|
|
|
SiteSetting.moderators_view_emails = true
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "screened_email" }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["success"]).to eq("OK")
|
|
|
|
|
|
|
|
job_data = Jobs::ExportCsvFile.jobs.first["args"].first
|
|
|
|
expect(job_data["entity"]).to eq("screened_email")
|
|
|
|
expect(job_data["user_id"]).to eq(moderator.id)
|
|
|
|
end
|
|
|
|
|
2025-01-24 08:13:25 +11:00
|
|
|
it "does not allow moderators to export another user's archive" do
|
|
|
|
post "/export_csv/export_entity.json",
|
|
|
|
params: {
|
|
|
|
entity: "user_archive",
|
|
|
|
args: {
|
|
|
|
export_user_id: user.id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(Jobs::ExportUserArchive.jobs.size).to eq(0)
|
|
|
|
end
|
|
|
|
|
2018-09-21 09:07:13 +08:00
|
|
|
it "allows moderator to export other entities" do
|
|
|
|
post "/export_csv/export_entity.json", params: { entity: "staff_action" }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
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(moderator.id)
|
|
|
|
end
|
|
|
|
end
|
2025-01-24 08:13:25 +11:00
|
|
|
|
|
|
|
describe "#latest_user_archive" do
|
|
|
|
it "does not allow a moderator to view another user's archive" do
|
|
|
|
generate_exports(user)
|
|
|
|
get "/export_csv/latest_user_archive/#{user.id}.json"
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_exports(user)
|
|
|
|
csv_file_1 = Fabricate(:upload, created_at: 1.day.ago)
|
|
|
|
topic_1 = Fabricate(:topic, created_at: 1.day.ago)
|
|
|
|
Fabricate(:post, topic: topic_1)
|
|
|
|
UserExport.create!(
|
|
|
|
file_name: "test",
|
|
|
|
user: user,
|
|
|
|
upload_id: csv_file_1.id,
|
|
|
|
topic_id: topic_1.id,
|
|
|
|
created_at: 1.day.ago,
|
|
|
|
)
|
|
|
|
|
|
|
|
csv_file_2 = Fabricate(:upload, created_at: 12.hours.ago)
|
|
|
|
topic_2 = Fabricate(:topic, created_at: 12.hours.ago)
|
|
|
|
UserExport.create!(
|
|
|
|
file_name: "test2",
|
|
|
|
user: user,
|
|
|
|
upload_id: csv_file_2.id,
|
|
|
|
topic_id: topic_2.id,
|
|
|
|
created_at: 12.hours.ago,
|
|
|
|
)
|
2018-09-21 09:07:13 +08:00
|
|
|
end
|
2018-06-06 06:46:04 +03:00
|
|
|
end
|