2020-02-16 23:21:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe "tasks/uploads" do
|
|
|
|
before do
|
|
|
|
Rake::Task.clear
|
|
|
|
Discourse::Application.load_tasks
|
2020-03-02 18:03:58 -05:00
|
|
|
SiteSetting.authorized_extensions += "|pdf"
|
2020-02-16 23:21:43 -05:00
|
|
|
end
|
|
|
|
|
2020-03-02 18:03:58 -05:00
|
|
|
describe "uploads:secure_upload_analyse_and_update" do
|
2020-02-16 23:21:43 -05:00
|
|
|
let!(:uploads) do
|
|
|
|
[
|
|
|
|
multi_post_upload1,
|
|
|
|
upload1,
|
2020-03-25 17:16:02 -04:00
|
|
|
upload2,
|
|
|
|
upload3
|
2020-02-16 23:21:43 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
let(:multi_post_upload1) { Fabricate(:upload_s3) }
|
|
|
|
let(:upload1) { Fabricate(:upload_s3) }
|
|
|
|
let(:upload2) { Fabricate(:upload_s3) }
|
2020-03-25 17:16:02 -04:00
|
|
|
let(:upload3) { Fabricate(:upload_s3, original_filename: 'test.pdf', extension: 'pdf') }
|
2020-02-16 23:21:43 -05:00
|
|
|
|
|
|
|
let!(:post1) { Fabricate(:post) }
|
|
|
|
let!(:post2) { Fabricate(:post) }
|
|
|
|
let!(:post3) { Fabricate(:post) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
PostUpload.create(post: post1, upload: multi_post_upload1)
|
|
|
|
PostUpload.create(post: post2, upload: multi_post_upload1)
|
|
|
|
PostUpload.create(post: post2, upload: upload1)
|
|
|
|
PostUpload.create(post: post3, upload: upload2)
|
2020-03-02 18:03:58 -05:00
|
|
|
PostUpload.create(post: post3, upload: upload3)
|
2020-02-16 23:21:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def invoke_task
|
2020-02-19 23:47:47 -05:00
|
|
|
capture_stdout do
|
2020-03-02 18:03:58 -05:00
|
|
|
Rake::Task['uploads:secure_upload_analyse_and_update'].invoke
|
2020-02-19 23:47:47 -05:00
|
|
|
end
|
2020-02-16 23:21:43 -05: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 11:18:34 -04:00
|
|
|
expect { invoke_task }.to raise_error(SystemExit)
|
2020-02-16 23:21:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when store is external" do
|
|
|
|
before do
|
|
|
|
enable_s3_uploads(uploads)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when secure media is enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.secure_media = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sets an access_control_post for each post upload, using the first linked post in the case of multiple links" do
|
|
|
|
invoke_task
|
|
|
|
expect(multi_post_upload1.reload.access_control_post).to eq(post1)
|
|
|
|
expect(upload1.reload.access_control_post).to eq(post2)
|
|
|
|
expect(upload2.reload.access_control_post).to eq(post3)
|
2020-03-02 18:03:58 -05:00
|
|
|
expect(upload3.reload.access_control_post).to eq(post3)
|
2020-02-16 23:21:43 -05:00
|
|
|
end
|
|
|
|
|
2020-03-25 17:16:02 -04:00
|
|
|
it "sets the uploads that are media and attachments in the read restricted topic category to secure" do
|
2020-02-16 23:21:43 -05:00
|
|
|
post3.topic.update(category: Fabricate(:private_category, group: Fabricate(:group)))
|
|
|
|
invoke_task
|
|
|
|
expect(upload2.reload.secure).to eq(true)
|
|
|
|
expect(upload1.reload.secure).to eq(false)
|
2020-03-25 17:16:02 -04:00
|
|
|
expect(upload3.reload.secure).to eq(true)
|
2020-02-16 23:21:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the upload in the PM topic to secure" do
|
|
|
|
post3.topic.update(archetype: 'private_message', category: nil)
|
|
|
|
invoke_task
|
|
|
|
expect(upload2.reload.secure).to eq(true)
|
|
|
|
expect(upload1.reload.secure).to eq(false)
|
|
|
|
end
|
|
|
|
|
2020-03-25 23:31:39 -04:00
|
|
|
it "rebakes the posts attached for uploads that change secure status" do
|
|
|
|
post3.topic.update(category: Fabricate(:private_category, group: Fabricate(:group)))
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
post1.update_columns(baked_at: 1.week.ago)
|
|
|
|
post2.update_columns(baked_at: 1.week.ago)
|
|
|
|
post3.update_columns(baked_at: 1.week.ago)
|
2020-02-16 23:21:43 -05:00
|
|
|
|
|
|
|
invoke_task
|
|
|
|
|
2020-03-25 23:31:39 -04:00
|
|
|
expect(post1.reload.baked_at).to eq_time(1.week.ago)
|
|
|
|
expect(post2.reload.baked_at).to eq_time(1.week.ago)
|
|
|
|
expect(post3.reload.baked_at).not_to eq_time(1.week.ago)
|
2020-02-16 23:21:43 -05:00
|
|
|
end
|
2020-03-02 18:03:58 -05:00
|
|
|
|
|
|
|
context "for an upload that is already secure and does not need to change" do
|
|
|
|
before do
|
|
|
|
post3.topic.update(archetype: 'private_message', category: nil)
|
|
|
|
upload2.update(access_control_post: post3)
|
|
|
|
upload2.update_secure_status
|
2020-03-25 23:31:39 -04:00
|
|
|
upload3.update(access_control_post: post3)
|
|
|
|
upload3.update_secure_status
|
2020-03-02 18:03:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not rebake the associated post" do
|
2020-03-25 23:31:39 -04:00
|
|
|
freeze_time
|
|
|
|
|
|
|
|
post3.update_columns(baked_at: 1.week.ago)
|
2020-03-02 18:03:58 -05:00
|
|
|
invoke_task
|
2020-03-25 23:31:39 -04:00
|
|
|
|
|
|
|
expect(post3.reload.baked_at).to eq_time(1.week.ago)
|
2020-03-02 18:03:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not attempt to update the acl" do
|
|
|
|
Discourse.store.expects(:update_upload_ACL).with(upload2).never
|
|
|
|
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)
|
|
|
|
PostUpload.create(post: post_for_upload, upload: upload_to_mark_not_secure)
|
|
|
|
enable_s3_uploads(uploads.concat([upload_to_mark_not_secure]))
|
|
|
|
invoke_task
|
|
|
|
expect(upload_to_mark_not_secure.reload.secure).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "uploads:disable_secure_media" do
|
|
|
|
def invoke_task
|
|
|
|
capture_stdout do
|
|
|
|
Rake::Task['uploads:disable_secure_media'].invoke
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
enable_s3_uploads(uploads)
|
|
|
|
SiteSetting.secure_media = true
|
|
|
|
PostUpload.create(post: post1, upload: upload1)
|
|
|
|
PostUpload.create(post: post1, upload: upload2)
|
|
|
|
PostUpload.create(post: post2, upload: upload3)
|
|
|
|
PostUpload.create(post: post2, upload: upload4)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:uploads) do
|
|
|
|
[
|
|
|
|
upload1, upload2, upload3, upload4, upload5
|
|
|
|
]
|
|
|
|
end
|
|
|
|
let(:post1) { Fabricate(:post) }
|
|
|
|
let(:post2) { Fabricate(:post) }
|
|
|
|
let(:upload1) { Fabricate(:upload_s3, secure: true, access_control_post: post1) }
|
|
|
|
let(:upload2) { Fabricate(:upload_s3, secure: true, access_control_post: post1) }
|
|
|
|
let(:upload3) { Fabricate(:upload_s3, secure: true, access_control_post: post2) }
|
|
|
|
let(:upload4) { Fabricate(:upload_s3, secure: true, access_control_post: post2) }
|
|
|
|
let(:upload5) { Fabricate(:upload_s3, secure: false) }
|
|
|
|
|
|
|
|
it "disables the secure media setting" do
|
|
|
|
invoke_task
|
|
|
|
expect(SiteSetting.secure_media).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates all secure uploads to secure: false" do
|
|
|
|
invoke_task
|
|
|
|
[upload1, upload2, upload3, upload4].each do |upl|
|
|
|
|
expect(upl.reload.secure).to eq(false)
|
2020-02-16 23:21:43 -05:00
|
|
|
end
|
|
|
|
end
|
2020-03-02 18:03:58 -05:00
|
|
|
|
|
|
|
it "rebakes the associated posts" do
|
2020-03-25 23:31:39 -04:00
|
|
|
freeze_time
|
|
|
|
|
|
|
|
post1.update_columns(baked_at: 1.week.ago)
|
|
|
|
post2.update_columns(baked_at: 1.week.ago)
|
2020-03-02 18:03:58 -05:00
|
|
|
invoke_task
|
2020-03-25 23:31:39 -04:00
|
|
|
|
|
|
|
expect(post1.reload.baked_at).not_to eq_time(1.week.ago)
|
|
|
|
expect(post2.reload.baked_at).not_to eq_time(1.week.ago)
|
2020-03-02 18:03:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the affected ACLs" do
|
|
|
|
FileStore::S3Store.any_instance.expects(:update_upload_ACL).times(4)
|
|
|
|
invoke_task
|
|
|
|
end
|
2020-02-16 23:21:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def enable_s3_uploads(uploads)
|
|
|
|
SiteSetting.enable_s3_uploads = true
|
|
|
|
SiteSetting.s3_upload_bucket = "s3-upload-bucket"
|
|
|
|
SiteSetting.s3_access_key_id = "some key"
|
|
|
|
SiteSetting.s3_secret_access_key = "some secrets3_region key"
|
|
|
|
|
|
|
|
stub_request(:head, "https://#{SiteSetting.s3_upload_bucket}.s3.amazonaws.com/")
|
|
|
|
|
|
|
|
uploads.each do |upload|
|
|
|
|
stub_request(
|
|
|
|
:put,
|
|
|
|
"https://#{SiteSetting.s3_upload_bucket}.s3.amazonaws.com/original/1X/#{upload.sha1}.#{upload.extension}?acl"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|