2020-02-17 12:21:43 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe "tasks/uploads" do
|
|
|
|
before do
|
2020-03-03 07:03:58 +08:00
|
|
|
SiteSetting.authorized_extensions += "|pdf"
|
2024-10-31 11:33:11 +08:00
|
|
|
STDIN.stubs(:gets).returns("y\n")
|
2020-02-17 12:21:43 +08:00
|
|
|
end
|
|
|
|
|
2020-03-03 07:03:58 +08:00
|
|
|
describe "uploads:secure_upload_analyse_and_update" do
|
2024-10-31 11:33:11 +08:00
|
|
|
let!(:uploads) { [multi_post_upload_1, upload_1, upload_2, upload_3] }
|
|
|
|
let(:multi_post_upload_1) { Fabricate(:upload_s3) }
|
|
|
|
let(:upload_1) { Fabricate(:upload_s3) }
|
|
|
|
let(:upload_2) { Fabricate(:upload_s3) }
|
|
|
|
let(:upload_3) { Fabricate(:upload_s3, original_filename: "test.pdf", extension: "pdf") }
|
2020-02-17 12:21:43 +08:00
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
let!(:post_1) { Fabricate(:post) }
|
|
|
|
let!(:post_2) { Fabricate(:post) }
|
|
|
|
let!(:post_3) { Fabricate(:post) }
|
2020-02-17 12:21:43 +08:00
|
|
|
|
|
|
|
before do
|
2024-10-31 11:33:11 +08:00
|
|
|
UploadReference.create(target: post_1, upload: multi_post_upload_1)
|
|
|
|
UploadReference.create(target: post_2, upload: multi_post_upload_1)
|
|
|
|
UploadReference.create(target: post_2, upload: upload_1)
|
|
|
|
UploadReference.create(target: post_3, upload: upload_2)
|
|
|
|
UploadReference.create(target: post_3, upload: upload_3)
|
2020-02-17 12:21:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def invoke_task
|
2024-11-20 12:17:36 +08:00
|
|
|
capture_stdout { invoke_rake_task("uploads:secure_upload_analyse_and_update") }
|
2020-02-17 12:21:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the store is internal" do
|
|
|
|
it "does nothing; this is for external store only" do
|
|
|
|
Upload.expects(:transaction).never
|
2020-03-20 23:18:34 +08:00
|
|
|
expect { invoke_task }.to raise_error(SystemExit)
|
2020-02-17 12:21:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when store is external" do
|
|
|
|
before do
|
2020-09-14 19:32:25 +08:00
|
|
|
setup_s3
|
|
|
|
uploads.each { |upload| stub_upload(upload) }
|
2020-02-17 12:21:43 +08:00
|
|
|
end
|
|
|
|
|
2022-09-29 07:24:33 +08:00
|
|
|
context "when secure upload is enabled" do
|
|
|
|
before { SiteSetting.secure_uploads = true }
|
2020-02-17 12:21:43 +08:00
|
|
|
|
|
|
|
it "sets an access_control_post for each post upload, using the first linked post in the case of multiple links" do
|
|
|
|
invoke_task
|
2024-10-31 11:33:11 +08:00
|
|
|
expect(multi_post_upload_1.reload.access_control_post).to eq(post_1)
|
|
|
|
expect(upload_1.reload.access_control_post).to eq(post_2)
|
|
|
|
expect(upload_2.reload.access_control_post).to eq(post_3)
|
|
|
|
expect(upload_3.reload.access_control_post).to eq(post_3)
|
2020-02-17 12:21:43 +08:00
|
|
|
end
|
|
|
|
|
2023-09-06 07:39:09 +08:00
|
|
|
context "when login_required" do
|
|
|
|
before { SiteSetting.login_required = true }
|
2022-05-23 11:14:11 +08:00
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
after do
|
|
|
|
if File.exist?("secure_upload_analyse_and_update_posts_for_rebake.json")
|
|
|
|
File.delete("secure_upload_analyse_and_update_posts_for_rebake.json")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sets everything attached to a post as secure" do
|
|
|
|
invoke_task
|
|
|
|
|
|
|
|
expect(upload_2.reload.secure).to eq(true)
|
|
|
|
expect(upload_1.reload.secure).to eq(true)
|
|
|
|
expect(upload_3.reload.secure).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "writes a file with the post IDs to rebake" do
|
|
|
|
invoke_task
|
2022-05-23 11:14:11 +08:00
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
expect(File.exist?("secure_upload_analyse_and_update_posts_for_rebake.json")).to eq(
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
expect(
|
|
|
|
JSON.parse(File.read("secure_upload_analyse_and_update_posts_for_rebake.json")),
|
|
|
|
).to eq({ "post_ids" => [post_1.id, post_2.id, post_3.id] })
|
|
|
|
end
|
2022-05-23 11:14:11 +08:00
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
it "sets the baked_version to NULL for affected posts" do
|
2023-09-06 07:39:09 +08:00
|
|
|
invoke_task
|
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
expect(post_1.reload.baked_version).to eq(nil)
|
|
|
|
expect(post_2.reload.baked_version).to eq(nil)
|
|
|
|
expect(post_3.reload.baked_version).to eq(nil)
|
2023-09-06 07:39:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when secure_uploads_pm_only" do
|
|
|
|
before { SiteSetting.secure_uploads_pm_only = true }
|
|
|
|
|
|
|
|
it "only sets everything attached to a private message post as secure and rebakes all those posts" do
|
2024-10-31 11:33:11 +08:00
|
|
|
post_3.topic.update(archetype: "private_message", category: nil)
|
2023-09-06 07:39:09 +08:00
|
|
|
|
|
|
|
invoke_task
|
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
expect(post_1.reload.baked_version).not_to eq(nil)
|
|
|
|
expect(post_2.reload.baked_version).not_to eq(nil)
|
|
|
|
expect(post_3.reload.baked_version).to eq(nil)
|
|
|
|
expect(upload_1.reload.secure).to eq(false)
|
|
|
|
expect(upload_2.reload.secure).to eq(true)
|
|
|
|
expect(upload_3.reload.secure).to eq(true)
|
2023-09-06 07:39:09 +08:00
|
|
|
end
|
|
|
|
end
|
2022-05-23 11:14:11 +08:00
|
|
|
end
|
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
context "when secure_uploads_pm_only" do
|
|
|
|
before { SiteSetting.secure_uploads_pm_only = true }
|
|
|
|
|
|
|
|
it "sets a non-PM post upload to not secure" do
|
|
|
|
upload_2.update!(secure: true)
|
|
|
|
invoke_task
|
|
|
|
expect(upload_2.reload.secure).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-26 05:16:02 +08:00
|
|
|
it "sets the uploads that are media and attachments in the read restricted topic category to secure" do
|
2024-10-31 11:33:11 +08:00
|
|
|
post_3.topic.update(category: Fabricate(:private_category, group: Fabricate(:group)))
|
2020-02-17 12:21:43 +08:00
|
|
|
invoke_task
|
2024-10-31 11:33:11 +08:00
|
|
|
expect(upload_2.reload.secure).to eq(true)
|
|
|
|
expect(upload_1.reload.secure).to eq(false)
|
|
|
|
expect(upload_3.reload.secure).to eq(true)
|
2020-02-17 12:21:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the upload in the PM topic to secure" do
|
2024-10-31 11:33:11 +08:00
|
|
|
post_3.topic.update(archetype: "private_message", category: nil)
|
2020-02-17 12:21:43 +08:00
|
|
|
invoke_task
|
2024-10-31 11:33:11 +08:00
|
|
|
expect(upload_2.reload.secure).to eq(true)
|
|
|
|
expect(upload_1.reload.secure).to eq(false)
|
2020-02-17 12:21:43 +08:00
|
|
|
end
|
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
it "sets the baked_version version to NULL for the posts attached for uploads that change secure status" do
|
|
|
|
post_3.topic.update(category: Fabricate(:private_category, group: Fabricate(:group)))
|
2020-02-17 12:21:43 +08:00
|
|
|
|
|
|
|
invoke_task
|
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
expect(post_1.reload.baked_version).not_to eq(nil)
|
|
|
|
expect(post_2.reload.baked_version).not_to eq(nil)
|
|
|
|
expect(post_3.reload.baked_version).to eq(nil)
|
2020-02-17 12:21:43 +08:00
|
|
|
end
|
2020-03-03 07:03:58 +08:00
|
|
|
|
|
|
|
context "for an upload that is already secure and does not need to change" do
|
|
|
|
before do
|
2024-10-31 11:33:11 +08:00
|
|
|
post_3.topic.update(archetype: "private_message", category: nil)
|
|
|
|
upload_2.update(access_control_post: post_3)
|
|
|
|
upload_2.update_secure_status
|
|
|
|
upload_3.update(access_control_post: post_3)
|
|
|
|
upload_3.update_secure_status
|
2020-03-03 07:03:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not rebake the associated post" do
|
2020-03-26 11:31:39 +08:00
|
|
|
freeze_time
|
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
post_3.update_columns(baked_at: 1.week.ago)
|
2020-03-03 07:03:58 +08:00
|
|
|
invoke_task
|
2020-03-26 11:31:39 +08:00
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
expect(post_3.reload.baked_at).to eq_time(1.week.ago)
|
2020-03-03 07:03:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not attempt to update the acl" do
|
2024-10-31 11:33:11 +08:00
|
|
|
FileStore::S3Store.any_instance.expects(:update_upload_ACL).with(upload_2).never
|
2020-03-03 07:03:58 +08:00
|
|
|
invoke_task
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for an upload that is already secure and is changing to not secure" do
|
|
|
|
it "changes the upload to not secure and updates the ACL" do
|
|
|
|
upload_to_mark_not_secure = Fabricate(:upload_s3, secure: true)
|
|
|
|
post_for_upload = Fabricate(:post)
|
2022-06-09 07:24:30 +08:00
|
|
|
UploadReference.create(target: post_for_upload, upload: upload_to_mark_not_secure)
|
2020-09-14 19:32:25 +08:00
|
|
|
|
|
|
|
setup_s3
|
|
|
|
uploads.each { |upload| stub_upload(upload) }
|
|
|
|
stub_upload(upload_to_mark_not_secure)
|
|
|
|
|
2020-03-03 07:03:58 +08:00
|
|
|
invoke_task
|
|
|
|
expect(upload_to_mark_not_secure.reload.secure).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-29 07:24:33 +08:00
|
|
|
describe "uploads:disable_secure_uploads" do
|
2020-03-03 07:03:58 +08:00
|
|
|
def invoke_task
|
2024-11-20 12:17:36 +08:00
|
|
|
capture_stdout { invoke_rake_task("uploads:disable_secure_uploads") }
|
2020-03-03 07:03:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2020-09-14 19:32:25 +08:00
|
|
|
setup_s3
|
|
|
|
uploads.each { |upload| stub_upload(upload) }
|
|
|
|
|
2022-09-29 07:24:33 +08:00
|
|
|
SiteSetting.secure_uploads = true
|
2024-10-31 11:33:11 +08:00
|
|
|
UploadReference.create(target: post_1, upload: upload_1)
|
|
|
|
UploadReference.create(target: post_1, upload: upload_2)
|
|
|
|
UploadReference.create(target: post_2, upload: upload_3)
|
|
|
|
UploadReference.create(target: post_2, upload: upload4)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
if File.exist?("secure_upload_analyse_and_update_posts_for_rebake.json")
|
|
|
|
File.delete("secure_upload_analyse_and_update_posts_for_rebake.json")
|
|
|
|
end
|
2020-03-03 07:03:58 +08:00
|
|
|
end
|
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
let!(:uploads) { [upload_1, upload_2, upload_3, upload4, upload5] }
|
|
|
|
let(:post_1) { Fabricate(:post) }
|
|
|
|
let(:post_2) { Fabricate(:post) }
|
|
|
|
let(:upload_1) { Fabricate(:upload_s3, secure: true, access_control_post: post_1) }
|
|
|
|
let(:upload_2) { Fabricate(:upload_s3, secure: true, access_control_post: post_1) }
|
|
|
|
let(:upload_3) { Fabricate(:upload_s3, secure: true, access_control_post: post_2) }
|
|
|
|
let(:upload4) { Fabricate(:upload_s3, secure: true, access_control_post: post_2) }
|
2020-03-03 07:03:58 +08:00
|
|
|
let(:upload5) { Fabricate(:upload_s3, secure: false) }
|
|
|
|
|
2022-09-29 07:24:33 +08:00
|
|
|
it "disables the secure upload setting" do
|
2020-03-03 07:03:58 +08:00
|
|
|
invoke_task
|
2022-09-29 07:24:33 +08:00
|
|
|
expect(SiteSetting.secure_uploads).to eq(false)
|
2020-03-03 07:03:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates all secure uploads to secure: false" do
|
|
|
|
invoke_task
|
2024-10-31 11:33:11 +08:00
|
|
|
[upload_1, upload_2, upload_3, upload4].each { |upl| expect(upl.reload.secure).to eq(false) }
|
2020-02-17 12:21:43 +08:00
|
|
|
end
|
2020-03-03 07:03:58 +08:00
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
it "sets the baked_version to NULL for affected posts" do
|
|
|
|
invoke_task
|
|
|
|
|
|
|
|
expect(post_1.reload.baked_version).to eq(nil)
|
|
|
|
expect(post_2.reload.baked_version).to eq(nil)
|
|
|
|
end
|
2020-03-26 11:31:39 +08:00
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
it "writes a file with the post IDs to rebake" do
|
2020-03-03 07:03:58 +08:00
|
|
|
invoke_task
|
2020-03-26 11:31:39 +08:00
|
|
|
|
2024-10-31 11:33:11 +08:00
|
|
|
expect(File.exist?("secure_upload_analyse_and_update_posts_for_rebake.json")).to eq(true)
|
|
|
|
expect(JSON.parse(File.read("secure_upload_analyse_and_update_posts_for_rebake.json"))).to eq(
|
|
|
|
{ "post_ids" => [post_1.id, post_2.id] },
|
|
|
|
)
|
2020-03-03 07:03:58 +08:00
|
|
|
end
|
|
|
|
|
2022-10-25 07:01:15 +08:00
|
|
|
it "updates the affected ACLs via the SyncAclsForUploads job" do
|
|
|
|
invoke_task
|
|
|
|
expect(Jobs::SyncAclsForUploads.jobs.last["args"][0]["upload_ids"]).to match_array(
|
2024-10-31 11:33:11 +08:00
|
|
|
[upload_1.id, upload_2.id, upload_3.id, upload4.id],
|
2022-10-25 07:01:15 +08:00
|
|
|
)
|
2020-03-03 07:03:58 +08:00
|
|
|
end
|
2020-02-17 12:21:43 +08:00
|
|
|
end
|
2022-11-11 20:00:44 +08:00
|
|
|
|
|
|
|
describe "uploads:downsize" do
|
|
|
|
def invoke_task
|
2024-11-20 12:17:36 +08:00
|
|
|
capture_stdout { invoke_rake_task("uploads:downsize") }
|
2022-11-11 20:00:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
before { STDIN.stubs(:beep) }
|
|
|
|
|
|
|
|
fab!(:upload) { Fabricate(:image_upload, width: 200, height: 200) }
|
|
|
|
|
|
|
|
it "corrects upload attributes" do
|
|
|
|
upload.update!(thumbnail_height: 0)
|
|
|
|
|
|
|
|
expect { invoke_task }.to change { upload.reload.thumbnail_height }.to(200)
|
|
|
|
end
|
2022-11-12 00:56:11 +08:00
|
|
|
|
|
|
|
it "updates attributes of uploads that are over the size limit" do
|
|
|
|
upload.update!(thumbnail_height: 0)
|
2023-12-14 07:22:48 +08:00
|
|
|
SiteSetting.max_image_size_kb = 1
|
2022-11-12 00:56:11 +08:00
|
|
|
|
|
|
|
expect { invoke_task }.to change { upload.reload.thumbnail_height }.to(200)
|
|
|
|
end
|
2022-11-11 20:00:44 +08:00
|
|
|
end
|
2020-02-17 12:21:43 +08:00
|
|
|
end
|