2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-07-31 17:26:34 -04:00
|
|
|
require 'file_store/s3_store'
|
2015-05-25 23:08:31 -04:00
|
|
|
require 'file_store/local_store'
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
describe FileStore::S3Store do
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2016-08-19 02:08:04 -04:00
|
|
|
let(:store) { FileStore::S3Store.new }
|
|
|
|
let(:s3_helper) { store.instance_variable_get(:@s3_helper) }
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:upload) { Fabricate(:upload) }
|
2014-07-14 11:34:23 -04:00
|
|
|
let(:uploaded_file) { file_from_fixtures("logo.png") }
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:optimized_image) { Fabricate(:optimized_image) }
|
2014-07-14 11:34:23 -04:00
|
|
|
let(:optimized_image_file) { file_from_fixtures("logo.png") }
|
2013-07-31 17:26:34 -04:00
|
|
|
|
|
|
|
before(:each) do
|
2016-08-14 23:21:24 -04:00
|
|
|
SiteSetting.s3_upload_bucket = "s3-upload-bucket"
|
|
|
|
SiteSetting.s3_access_key_id = "s3-access-key-id"
|
|
|
|
SiteSetting.s3_secret_access_key = "s3-secret-access-key"
|
2017-10-06 01:20:01 -04:00
|
|
|
SiteSetting.enable_s3_uploads = true
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
shared_context 's3 helpers' do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:upload) do
|
2016-08-15 04:06:29 -04:00
|
|
|
Fabricate(:upload, sha1: Digest::SHA1.hexdigest('secreet image string'))
|
|
|
|
end
|
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
let(:store) { FileStore::S3Store.new }
|
|
|
|
let(:client) { Aws::S3::Client.new(stub_responses: true) }
|
|
|
|
let(:resource) { Aws::S3::Resource.new(client: client) }
|
|
|
|
let(:s3_bucket) { resource.bucket("s3-upload-bucket") }
|
|
|
|
let(:s3_helper) { store.instance_variable_get(:@s3_helper) }
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
before do
|
2017-07-27 21:20:09 -04:00
|
|
|
SiteSetting.s3_region = 'us-west-1'
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
context 'uploading to s3' do
|
|
|
|
include_context "s3 helpers"
|
|
|
|
|
2019-02-08 04:01:19 -05:00
|
|
|
let(:s3_object) { stub }
|
2019-01-04 01:16:22 -05:00
|
|
|
let(:etag) { "etag" }
|
|
|
|
|
2019-02-08 04:01:19 -05:00
|
|
|
before do
|
|
|
|
s3_object.stubs(:put).returns(Aws::S3::Types::PutObjectOutput.new(etag: "\"#{etag}\""))
|
|
|
|
end
|
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
describe "#store_upload" do
|
|
|
|
it "returns an absolute schemaless url" do
|
2016-08-15 04:06:29 -04:00
|
|
|
store.expects(:get_depth_for).with(upload.id).returns(0)
|
2018-08-07 23:26:05 -04:00
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
|
2016-08-12 05:18:19 -04:00
|
|
|
|
|
|
|
s3_bucket.expects(:object).with("original/1X/#{upload.sha1}.png").returns(s3_object)
|
2019-01-04 01:16:22 -05:00
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
expect(store.store_upload(uploaded_file, upload)).to eq(
|
2018-08-26 21:22:46 -04:00
|
|
|
"//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/original/1X/#{upload.sha1}.png"
|
2016-08-12 05:18:19 -04:00
|
|
|
)
|
2019-01-04 01:16:22 -05:00
|
|
|
expect(upload.etag).to eq(etag)
|
2016-08-12 05:18:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "when s3_upload_bucket includes folders path" do
|
|
|
|
before do
|
|
|
|
SiteSetting.s3_upload_bucket = "s3-upload-bucket/discourse-uploads"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an absolute schemaless url" do
|
2016-08-15 04:06:29 -04:00
|
|
|
store.expects(:get_depth_for).with(upload.id).returns(0)
|
2016-08-12 05:18:19 -04:00
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket)
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
s3_bucket.expects(:object).with("discourse-uploads/original/1X/#{upload.sha1}.png").returns(s3_object)
|
2019-01-04 01:16:22 -05:00
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
expect(store.store_upload(uploaded_file, upload)).to eq(
|
2018-08-26 21:22:46 -04:00
|
|
|
"//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/discourse-uploads/original/1X/#{upload.sha1}.png"
|
2016-08-12 05:18:19 -04:00
|
|
|
)
|
2019-01-04 01:16:22 -05:00
|
|
|
expect(upload.etag).to eq(etag)
|
2016-08-12 05:18:19 -04:00
|
|
|
end
|
|
|
|
end
|
2019-06-05 23:27:24 -04:00
|
|
|
|
|
|
|
describe "when private uploads are enabled" do
|
|
|
|
it "returns signed URL for eligible private upload" do
|
|
|
|
SiteSetting.prevent_anons_from_downloading_files = true
|
|
|
|
SiteSetting.authorized_extensions = "pdf|png|jpg|gif"
|
|
|
|
upload.update!(original_filename: "small.pdf", extension: "pdf")
|
|
|
|
|
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
|
|
|
|
s3_bucket.expects(:object).with("original/1X/#{upload.sha1}.pdf").returns(s3_object).at_least_once
|
|
|
|
s3_object.expects(:presigned_url).with(:get, expires_in: S3Helper::DOWNLOAD_URL_EXPIRES_AFTER_SECONDS)
|
|
|
|
|
|
|
|
expect(store.store_upload(uploaded_file, upload)).to eq(
|
|
|
|
"//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/original/1X/#{upload.sha1}.pdf"
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(store.url_for(upload)).not_to eq(upload.url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns regular URL for ineligible private upload" do
|
|
|
|
SiteSetting.prevent_anons_from_downloading_files = true
|
|
|
|
|
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
|
|
|
|
s3_bucket.expects(:object).with("original/1X/#{upload.sha1}.png").returns(s3_object).at_least_once
|
|
|
|
|
|
|
|
expect(store.store_upload(uploaded_file, upload)).to eq(
|
|
|
|
"//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/original/1X/#{upload.sha1}.png"
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(store.url_for(upload)).to eq(upload.url)
|
|
|
|
end
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
describe "#store_optimized_image" do
|
|
|
|
it "returns an absolute schemaless url" do
|
2016-08-15 04:06:29 -04:00
|
|
|
store.expects(:get_depth_for).with(optimized_image.upload.id).returns(0)
|
2016-08-12 05:18:19 -04:00
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket)
|
|
|
|
path = "optimized/1X/#{optimized_image.upload.sha1}_#{OptimizedImage::VERSION}_100x200.png"
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
s3_bucket.expects(:object).with(path).returns(s3_object)
|
2019-01-04 01:16:22 -05:00
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
expect(store.store_optimized_image(optimized_image_file, optimized_image)).to eq(
|
2018-08-26 21:22:46 -04:00
|
|
|
"//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/#{path}"
|
2016-08-12 05:18:19 -04:00
|
|
|
)
|
2019-01-04 01:16:22 -05:00
|
|
|
expect(optimized_image.etag).to eq(etag)
|
2016-08-12 05:18:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "when s3_upload_bucket includes folders path" do
|
|
|
|
before do
|
|
|
|
SiteSetting.s3_upload_bucket = "s3-upload-bucket/discourse-uploads"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an absolute schemaless url" do
|
2016-08-15 04:06:29 -04:00
|
|
|
store.expects(:get_depth_for).with(optimized_image.upload.id).returns(0)
|
2016-08-12 05:18:19 -04:00
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket)
|
|
|
|
path = "discourse-uploads/optimized/1X/#{optimized_image.upload.sha1}_#{OptimizedImage::VERSION}_100x200.png"
|
|
|
|
|
|
|
|
s3_bucket.expects(:object).with(path).returns(s3_object)
|
2019-01-04 01:16:22 -05:00
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
expect(store.store_optimized_image(optimized_image_file, optimized_image)).to eq(
|
2018-08-26 21:22:46 -04:00
|
|
|
"//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/#{path}"
|
2016-08-12 05:18:19 -04:00
|
|
|
)
|
2019-01-04 01:16:22 -05:00
|
|
|
expect(optimized_image.etag).to eq(etag)
|
2016-08-12 05:18:19 -04:00
|
|
|
end
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
2016-08-12 05:18:19 -04:00
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2018-08-07 23:26:05 -04:00
|
|
|
context 'copying files in S3' do
|
|
|
|
include_context "s3 helpers"
|
|
|
|
|
|
|
|
describe '#copy_file' do
|
|
|
|
it "copies the from in S3 with the right paths" do
|
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket)
|
|
|
|
|
|
|
|
upload.update!(
|
2018-08-26 21:22:46 -04:00
|
|
|
url: "//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/original/1X/#{upload.sha1}.png"
|
2018-08-07 23:26:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
source = Discourse.store.get_path_for_upload(upload)
|
|
|
|
destination = Discourse.store.get_path_for_upload(upload).sub('.png', '.jpg')
|
|
|
|
|
|
|
|
s3_object = stub
|
|
|
|
|
2018-08-08 03:57:58 -04:00
|
|
|
s3_bucket.expects(:object).with(destination).returns(s3_object)
|
2018-08-07 23:26:05 -04:00
|
|
|
|
|
|
|
s3_object.expects(:copy_from).with(
|
2018-08-08 03:57:58 -04:00
|
|
|
copy_source: "s3-upload-bucket/#{source}"
|
2018-08-07 23:26:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
store.copy_file(upload.url, source, destination)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
context 'removal from s3' do
|
|
|
|
include_context "s3 helpers"
|
|
|
|
|
|
|
|
describe "#remove_upload" do
|
2016-08-14 23:21:24 -04:00
|
|
|
it "removes the file from s3 with the right paths" do
|
2016-08-15 04:06:29 -04:00
|
|
|
store.expects(:get_depth_for).with(upload.id).returns(0)
|
2018-08-07 23:26:05 -04:00
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
|
2019-04-29 03:32:25 -04:00
|
|
|
upload.update!(url: "//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/original/1X/#{upload.sha1}.png")
|
2016-08-14 23:21:24 -04:00
|
|
|
s3_object = stub
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2016-08-14 23:21:24 -04:00
|
|
|
s3_bucket.expects(:object).with("tombstone/original/1X/#{upload.sha1}.png").returns(s3_object)
|
|
|
|
s3_object.expects(:copy_from).with(copy_source: "s3-upload-bucket/original/1X/#{upload.sha1}.png")
|
|
|
|
s3_bucket.expects(:object).with("original/1X/#{upload.sha1}.png").returns(s3_object)
|
|
|
|
s3_object.expects(:delete)
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2016-08-14 23:21:24 -04:00
|
|
|
store.remove_upload(upload)
|
|
|
|
end
|
2016-08-12 05:18:19 -04:00
|
|
|
|
2019-01-11 08:05:38 -05:00
|
|
|
it "removes the optimized image from s3 with the right paths" do
|
|
|
|
optimized = Fabricate(:optimized_image, version: 1)
|
|
|
|
upload = optimized.upload
|
|
|
|
path = "optimized/1X/#{upload.sha1}_#{optimized.version}_#{optimized.width}x#{optimized.height}.png"
|
|
|
|
|
|
|
|
store.expects(:get_depth_for).with(upload.id).returns(0)
|
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
|
2019-04-29 03:32:25 -04:00
|
|
|
optimized.update!(url: "//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/#{path}")
|
2019-01-11 08:05:38 -05:00
|
|
|
s3_object = stub
|
|
|
|
|
|
|
|
s3_bucket.expects(:object).with("tombstone/#{path}").returns(s3_object)
|
|
|
|
s3_object.expects(:copy_from).with(copy_source: "s3-upload-bucket/#{path}")
|
|
|
|
s3_bucket.expects(:object).with(path).returns(s3_object)
|
|
|
|
s3_object.expects(:delete)
|
|
|
|
|
|
|
|
store.remove_optimized_image(optimized)
|
|
|
|
end
|
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
describe "when s3_upload_bucket includes folders path" do
|
|
|
|
before do
|
|
|
|
SiteSetting.s3_upload_bucket = "s3-upload-bucket/discourse-uploads"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes the file from s3 with the right paths" do
|
2016-08-15 04:06:29 -04:00
|
|
|
store.expects(:get_depth_for).with(upload.id).returns(0)
|
2018-08-07 23:26:05 -04:00
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
|
2019-04-29 03:32:25 -04:00
|
|
|
upload.update!(url: "//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/discourse-uploads/original/1X/#{upload.sha1}.png")
|
2016-08-12 05:18:19 -04:00
|
|
|
s3_object = stub
|
|
|
|
|
|
|
|
s3_bucket.expects(:object).with("discourse-uploads/tombstone/original/1X/#{upload.sha1}.png").returns(s3_object)
|
|
|
|
s3_object.expects(:copy_from).with(copy_source: "s3-upload-bucket/discourse-uploads/original/1X/#{upload.sha1}.png")
|
|
|
|
s3_bucket.expects(:object).with("discourse-uploads/original/1X/#{upload.sha1}.png").returns(s3_object)
|
|
|
|
s3_object.expects(:delete)
|
|
|
|
|
|
|
|
store.remove_upload(upload)
|
|
|
|
end
|
|
|
|
end
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
describe "#remove_optimized_image" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:optimized_image) { Fabricate(:optimized_image, upload: upload) }
|
2019-01-03 02:34:10 -05:00
|
|
|
|
|
|
|
let(:image_path) do
|
|
|
|
FileStore::BaseStore.new.get_path_for_optimized_image(optimized_image)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
optimized_image.update!(
|
|
|
|
url: "//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com#{image_path}"
|
2016-08-14 23:21:24 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes the file from s3 with the right paths" do
|
2018-08-07 23:26:05 -04:00
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
|
2016-08-14 23:21:24 -04:00
|
|
|
s3_object = stub
|
|
|
|
|
2019-01-03 02:34:10 -05:00
|
|
|
s3_bucket.expects(:object).with("tombstone/#{image_path}").returns(s3_object)
|
|
|
|
s3_object.expects(:copy_from).with(copy_source: "s3-upload-bucket/#{image_path}")
|
|
|
|
s3_bucket.expects(:object).with("#{image_path}").returns(s3_object)
|
2016-08-14 23:21:24 -04:00
|
|
|
s3_object.expects(:delete)
|
|
|
|
|
|
|
|
store.remove_optimized_image(optimized_image)
|
|
|
|
end
|
2016-08-12 05:18:19 -04:00
|
|
|
|
|
|
|
describe "when s3_upload_bucket includes folders path" do
|
|
|
|
before do
|
|
|
|
SiteSetting.s3_upload_bucket = "s3-upload-bucket/discourse-uploads"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes the file from s3 with the right paths" do
|
2018-08-07 23:26:05 -04:00
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
|
2016-08-12 05:18:19 -04:00
|
|
|
s3_object = stub
|
|
|
|
|
2019-01-03 02:34:10 -05:00
|
|
|
s3_bucket.expects(:object)
|
|
|
|
.with("discourse-uploads/tombstone/#{image_path}")
|
|
|
|
.returns(s3_object)
|
|
|
|
|
|
|
|
s3_object.expects(:copy_from).with(
|
|
|
|
copy_source: "s3-upload-bucket/discourse-uploads/#{image_path}"
|
|
|
|
)
|
|
|
|
|
|
|
|
s3_bucket.expects(:object).with(
|
|
|
|
"discourse-uploads/#{image_path}"
|
|
|
|
).returns(s3_object)
|
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
s3_object.expects(:delete)
|
|
|
|
|
|
|
|
store.remove_optimized_image(optimized_image)
|
|
|
|
end
|
|
|
|
end
|
2016-08-14 23:21:24 -04:00
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
describe ".has_been_uploaded?" do
|
2013-07-31 17:26:34 -04:00
|
|
|
|
|
|
|
it "identifies S3 uploads" do
|
2018-08-26 21:22:46 -04:00
|
|
|
expect(store.has_been_uploaded?("//s3-upload-bucket.s3.dualstack.us-east-1.amazonaws.com/1337.png")).to eq(true)
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not match other s3 urls" do
|
2018-08-26 21:22:46 -04:00
|
|
|
expect(store.has_been_uploaded?("//s3-upload-bucket.s3.amazonaws.com/1337.png")).to eq(false)
|
2016-08-14 23:21:24 -04:00
|
|
|
expect(store.has_been_uploaded?("//s3-upload-bucket.s3-us-east-1.amazonaws.com/1337.png")).to eq(false)
|
|
|
|
expect(store.has_been_uploaded?("//s3.amazonaws.com/s3-upload-bucket/1337.png")).to eq(false)
|
2018-07-06 18:15:28 -04:00
|
|
|
expect(store.has_been_uploaded?("//s4_upload_bucket.s3.amazonaws.com/1337.png")).to eq(false)
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".absolute_base_url" do
|
|
|
|
it "returns a lowercase schemaless absolute url" do
|
2018-08-26 21:22:46 -04:00
|
|
|
expect(store.absolute_base_url).to eq("//s3-upload-bucket.s3.dualstack.us-east-1.amazonaws.com")
|
2015-05-27 11:50:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the proper endpoint" do
|
2018-07-06 18:15:28 -04:00
|
|
|
SiteSetting.s3_region = "us-east-1"
|
2018-08-26 21:22:46 -04:00
|
|
|
expect(FileStore::S3Store.new(s3_helper).absolute_base_url).to eq("//s3-upload-bucket.s3.dualstack.us-east-1.amazonaws.com")
|
2018-07-06 18:15:28 -04:00
|
|
|
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.s3_region = "us-west-2"
|
2018-08-26 21:22:46 -04:00
|
|
|
expect(FileStore::S3Store.new(s3_helper).absolute_base_url).to eq("//s3-upload-bucket.s3.dualstack.us-west-2.amazonaws.com")
|
2016-07-14 10:56:09 -04:00
|
|
|
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.s3_region = "cn-north-1"
|
2016-08-14 23:21:24 -04:00
|
|
|
expect(FileStore::S3Store.new(s3_helper).absolute_base_url).to eq("//s3-upload-bucket.s3.cn-north-1.amazonaws.com.cn")
|
2016-07-14 10:56:09 -04:00
|
|
|
|
2018-08-26 21:22:46 -04:00
|
|
|
SiteSetting.s3_region = "cn-northwest-1"
|
|
|
|
expect(FileStore::S3Store.new(s3_helper).absolute_base_url).to eq("//s3-upload-bucket.s3.cn-northwest-1.amazonaws.com.cn")
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is external" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(store.external?).to eq(true)
|
|
|
|
expect(store.internal?).to eq(false)
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
describe ".purge_tombstone" do
|
|
|
|
|
|
|
|
it "updates tombstone lifecycle" do
|
|
|
|
s3_helper.expects(:update_tombstone_lifecycle)
|
|
|
|
store.purge_tombstone(1.day)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-05-25 23:08:31 -04:00
|
|
|
describe ".path_for" do
|
2019-05-29 06:53:31 -04:00
|
|
|
def assert_path(path, expected)
|
|
|
|
upload = Upload.new(url: path)
|
|
|
|
|
|
|
|
path = store.path_for(upload)
|
|
|
|
expected = FileStore::LocalStore.new.path_for(upload) if expected
|
2019-05-28 21:00:25 -04:00
|
|
|
|
2019-05-29 06:53:31 -04:00
|
|
|
expect(path).to eq(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly falls back to local" do
|
|
|
|
assert_path("/hello", "/hello")
|
|
|
|
assert_path("//hello", nil)
|
|
|
|
assert_path("http://hello", nil)
|
|
|
|
assert_path("https://hello", nil)
|
2015-05-25 23:08:31 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-21 21:31:33 -04:00
|
|
|
|
2019-06-05 23:27:24 -04:00
|
|
|
context 'update ACL' do
|
|
|
|
include_context "s3 helpers"
|
|
|
|
let(:s3_object) { stub }
|
|
|
|
|
|
|
|
describe ".update_upload_ACL" do
|
|
|
|
it "sets acl to private when private uploads are enabled" do
|
|
|
|
SiteSetting.prevent_anons_from_downloading_files = true
|
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket)
|
|
|
|
s3_bucket.expects(:object).with("original/1X/#{upload.sha1}.png").returns(s3_object)
|
|
|
|
s3_object.expects(:acl).returns(s3_object)
|
|
|
|
s3_object.expects(:put).with(acl: "private").returns(s3_object)
|
|
|
|
|
|
|
|
expect(store.update_upload_ACL(upload)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sets acl to public when private uploads are disabled" do
|
|
|
|
SiteSetting.prevent_anons_from_downloading_files = false
|
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket)
|
|
|
|
s3_bucket.expects(:object).with("original/1X/#{upload.sha1}.png").returns(s3_object)
|
|
|
|
s3_object.expects(:acl).returns(s3_object)
|
|
|
|
s3_object.expects(:put).with(acl: "public-read").returns(s3_object)
|
|
|
|
|
|
|
|
expect(store.update_upload_ACL(upload)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-21 21:31:33 -04:00
|
|
|
describe '.cdn_url' do
|
|
|
|
|
|
|
|
it 'supports subfolder' do
|
2018-08-21 22:31:13 -04:00
|
|
|
SiteSetting.s3_upload_bucket = 's3-upload-bucket/livechat'
|
|
|
|
SiteSetting.s3_cdn_url = 'https://rainbow.com'
|
|
|
|
|
|
|
|
# none of this should matter at all
|
|
|
|
# subfolder should not leak into uploads
|
|
|
|
global_setting :relative_url_root, '/community'
|
2018-08-21 21:31:33 -04:00
|
|
|
Discourse.stubs(:base_uri).returns("/community")
|
2018-08-21 22:31:13 -04:00
|
|
|
|
2018-08-26 21:22:46 -04:00
|
|
|
url = "//s3-upload-bucket.s3.dualstack.us-east-1.amazonaws.com/livechat/original/gif.png"
|
2018-08-21 22:31:13 -04:00
|
|
|
|
|
|
|
expect(store.cdn_url(url)).to eq("https://rainbow.com/original/gif.png")
|
2018-08-21 21:31:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-04 11:32:51 -04:00
|
|
|
describe ".download_url" do
|
|
|
|
include_context "s3 helpers"
|
|
|
|
let(:s3_object) { stub }
|
|
|
|
|
|
|
|
it "returns correct short URL with dl=1 param" do
|
|
|
|
expect(store.download_url(upload)).to eq("#{upload.short_path}?dl=1")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".url_for" do
|
|
|
|
include_context "s3 helpers"
|
|
|
|
let(:s3_object) { stub }
|
|
|
|
|
|
|
|
it "returns signed URL with content disposition when requesting to download image" do
|
|
|
|
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
|
|
|
|
s3_bucket.expects(:object).with("original/1X/#{upload.sha1}.png").returns(s3_object)
|
|
|
|
opts = {
|
|
|
|
expires_in: S3Helper::DOWNLOAD_URL_EXPIRES_AFTER_SECONDS,
|
2019-08-07 11:00:43 -04:00
|
|
|
response_content_disposition: %Q|attachment; filename="#{upload.original_filename}"; filename*=UTF-8''#{upload.original_filename}|
|
2019-07-04 11:32:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
s3_object.expects(:presigned_url).with(:get, opts)
|
|
|
|
|
|
|
|
expect(store.url_for(upload, force_download: true)).not_to eq(upload.url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|