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
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
let(:s3_helper) { stub }
|
|
|
|
let(:store) { FileStore::S3Store.new(s3_helper) }
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2015-05-27 19:03:24 -04:00
|
|
|
let(: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
|
|
|
|
2015-05-27 19:03:24 -04:00
|
|
|
let(: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
|
|
|
|
SiteSetting.stubs(:s3_upload_bucket).returns("S3_Upload_Bucket")
|
|
|
|
SiteSetting.stubs(:s3_access_key_id).returns("s3_access_key_id")
|
|
|
|
SiteSetting.stubs(:s3_secret_access_key).returns("s3_secret_access_key")
|
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
describe ".store_upload" do
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
it "returns an absolute schemaless url" do
|
2014-09-24 16:52:09 -04:00
|
|
|
s3_helper.expects(:upload)
|
2016-07-01 03:22:30 -04:00
|
|
|
expect(store.store_upload(uploaded_file, upload)).to match(/\/\/s3_upload_bucket\.s3\.amazonaws\.com\/original\/.+#{upload.sha1}\.png/)
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
describe ".store_optimized_image" do
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
it "returns an absolute schemaless url" do
|
2014-09-24 16:52:09 -04:00
|
|
|
s3_helper.expects(:upload)
|
2016-07-01 03:22:30 -04:00
|
|
|
expect(store.store_optimized_image(optimized_image_file, optimized_image)).to match(/\/\/s3_upload_bucket\.s3\.amazonaws\.com\/optimized\/.+#{optimized_image.upload.sha1}_#{OptimizedImage::VERSION}_100x200\.png/)
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
describe ".remove_upload" do
|
|
|
|
|
|
|
|
it "calls remove_file with the url" do
|
|
|
|
store.expects(:remove_file).with(upload.url)
|
2013-08-13 16:08:29 -04:00
|
|
|
store.remove_upload(upload)
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
describe ".remove_optimized_image" do
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
it "calls remove_file with the url" do
|
|
|
|
store.expects(:remove_file).with(optimized_image.url)
|
|
|
|
store.remove_optimized_image(optimized_image)
|
|
|
|
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
|
2015-05-27 11:50:49 -04:00
|
|
|
expect(store.has_been_uploaded?("//s3_upload_bucket.s3.amazonaws.com/1337.png")).to eq(true)
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not match other s3 urls" do
|
2015-05-27 11:50:49 -04:00
|
|
|
expect(store.has_been_uploaded?("//s3_upload_bucket.s3-us-east-1.amazonaws.com/1337.png")).to eq(false)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(store.has_been_uploaded?("//s3.amazonaws.com/s3_upload_bucket/1337.png")).to eq(false)
|
|
|
|
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
|
2015-05-27 11:50:49 -04:00
|
|
|
expect(store.absolute_base_url).to eq("//s3_upload_bucket.s3.amazonaws.com")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the proper endpoint" do
|
|
|
|
SiteSetting.stubs(:s3_region).returns("us-east-1")
|
|
|
|
expect(FileStore::S3Store.new(s3_helper).absolute_base_url).to eq("//s3_upload_bucket.s3.amazonaws.com")
|
|
|
|
|
|
|
|
SiteSetting.stubs(:s3_region).returns("us-east-2")
|
|
|
|
expect(FileStore::S3Store.new(s3_helper).absolute_base_url).to eq("//s3_upload_bucket.s3-us-east-2.amazonaws.com")
|
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
|
|
|
|
|
|
|
|
def assert_path(path, expected)
|
|
|
|
upload = Upload.new(url: path)
|
|
|
|
|
|
|
|
path = store.path_for(upload)
|
|
|
|
expected = FileStore::LocalStore.new.path_for(upload) if expected
|
|
|
|
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|