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/local_store'
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
describe FileStore::LocalStore do
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
let(:store) { FileStore::LocalStore.new }
|
2013-07-31 17:26:34 -04:00
|
|
|
|
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") }
|
2019-12-18 00:51:57 -05:00
|
|
|
let(:upload_path) { Discourse.store.upload_path }
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:optimized_image) { Fabricate(:optimized_image) }
|
2014-09-29 12:31:53 -04:00
|
|
|
|
2018-09-17 22:48:29 -04:00
|
|
|
describe "#store_upload" do
|
2013-07-31 17:26:34 -04:00
|
|
|
|
|
|
|
it "returns a relative url" do
|
|
|
|
store.expects(:copy_file)
|
2019-12-18 00:51:57 -05:00
|
|
|
expect(store.store_upload(uploaded_file, upload)).to match(/\/#{upload_path}\/original\/.+#{upload.sha1}\.png/)
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-09-17 22:48:29 -04:00
|
|
|
describe "#store_optimized_image" do
|
2013-07-31 17:26:34 -04:00
|
|
|
|
|
|
|
it "returns a relative url" do
|
|
|
|
store.expects(:copy_file)
|
2019-12-18 00:51:57 -05:00
|
|
|
expect(store.store_optimized_image({}, optimized_image)).to match(/\/#{upload_path}\/optimized\/.+#{optimized_image.upload.sha1}_#{OptimizedImage::VERSION}_100x200\.png/)
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-09-17 22:48:29 -04:00
|
|
|
describe "#remove_upload" do
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-08-13 16:08:29 -04:00
|
|
|
it "does not delete non uploaded" do
|
2013-11-27 16:01:41 -05:00
|
|
|
FileUtils.expects(:mkdir_p).never
|
2013-08-13 16:08:29 -04:00
|
|
|
store.remove_upload(upload)
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
2013-11-27 16:01:41 -05:00
|
|
|
it "moves the file to the tombstone" do
|
2018-09-17 22:48:29 -04:00
|
|
|
begin
|
|
|
|
upload = UploadCreator.new(
|
|
|
|
file_from_fixtures("smallest.png"),
|
|
|
|
"smallest.png"
|
|
|
|
).create_for(Fabricate(:user).id)
|
|
|
|
|
|
|
|
path = store.path_for(upload)
|
|
|
|
mtime = File.mtime(path)
|
|
|
|
|
|
|
|
sleep 0.01 # Delay a little for mtime to be updated
|
|
|
|
store.remove_upload(upload)
|
|
|
|
tombstone_path = path.sub("/uploads/", "/uploads/tombstone/")
|
|
|
|
|
|
|
|
expect(File.exist?(tombstone_path)).to eq(true)
|
|
|
|
expect(File.mtime(tombstone_path)).to_not eq(mtime)
|
|
|
|
ensure
|
|
|
|
[path, tombstone_path].each do |file_path|
|
|
|
|
File.delete(file_path) if File.exist?(file_path)
|
|
|
|
end
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-09-17 22:48:29 -04:00
|
|
|
describe "#remove_optimized_image" do
|
2013-11-27 16:01:41 -05:00
|
|
|
it "moves the file to the tombstone" do
|
2018-09-17 22:48:29 -04:00
|
|
|
begin
|
|
|
|
upload = UploadCreator.new(
|
|
|
|
file_from_fixtures("smallest.png"),
|
|
|
|
"smallest.png"
|
|
|
|
).create_for(Fabricate(:user).id)
|
|
|
|
|
|
|
|
upload.create_thumbnail!(1, 1)
|
|
|
|
upload.reload
|
|
|
|
|
|
|
|
optimized_image = upload.thumbnail(1, 1)
|
|
|
|
path = store.path_for(optimized_image)
|
|
|
|
|
|
|
|
store.remove_optimized_image(optimized_image)
|
|
|
|
tombstone_path = path.sub("/uploads/", "/uploads/tombstone/")
|
|
|
|
|
|
|
|
expect(File.exist?(tombstone_path)).to eq(true)
|
|
|
|
ensure
|
|
|
|
[path, tombstone_path].each do |file_path|
|
|
|
|
File.delete(file_path) if File.exist?(file_path)
|
|
|
|
end
|
|
|
|
end
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-09-17 22:48:29 -04:00
|
|
|
describe "#has_been_uploaded?" do
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
it "identifies relatives urls" do
|
2019-12-18 00:51:57 -05:00
|
|
|
expect(store.has_been_uploaded?("/#{upload_path}/42/0123456789ABCDEF.jpg")).to eq(true)
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
it "identifies local urls" do
|
|
|
|
Discourse.stubs(:base_url_no_prefix).returns("http://discuss.site.com")
|
2019-12-18 00:51:57 -05:00
|
|
|
expect(store.has_been_uploaded?("http://discuss.site.com/#{upload_path}/42/0123456789ABCDEF.jpg")).to eq(true)
|
|
|
|
expect(store.has_been_uploaded?("//discuss.site.com/#{upload_path}/42/0123456789ABCDEF.jpg")).to eq(true)
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "identifies local urls when using a CDN" do
|
|
|
|
Rails.configuration.action_controller.stubs(:asset_host).returns("http://my.cdn.com")
|
2019-12-18 00:51:57 -05:00
|
|
|
expect(store.has_been_uploaded?("http://my.cdn.com/#{upload_path}/42/0123456789ABCDEF.jpg")).to eq(true)
|
|
|
|
expect(store.has_been_uploaded?("//my.cdn.com/#{upload_path}/42/0123456789ABCDEF.jpg")).to eq(true)
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not match dummy urls" do
|
2019-12-18 00:51:57 -05:00
|
|
|
expect(store.has_been_uploaded?("http://domain.com/#{upload_path}/42/0123456789ABCDEF.jpg")).to eq(false)
|
|
|
|
expect(store.has_been_uploaded?("//domain.com/#{upload_path}/42/0123456789ABCDEF.jpg")).to eq(false)
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-09-17 22:48:29 -04:00
|
|
|
describe "#absolute_base_url" do
|
2013-11-05 13:04:47 -05:00
|
|
|
|
|
|
|
it "is present" do
|
2019-12-18 00:51:57 -05:00
|
|
|
expect(store.absolute_base_url).to eq("http://test.localhost/#{upload_path}")
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
2016-04-20 15:42:13 -04:00
|
|
|
it "supports subfolder" do
|
2019-11-15 00:48:24 -05:00
|
|
|
set_subfolder "/forum"
|
2019-12-18 00:51:57 -05:00
|
|
|
expect(store.absolute_base_url).to eq("http://test.localhost/forum/#{upload_path}")
|
2016-04-20 15:42:13 -04:00
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
2018-09-17 22:48:29 -04:00
|
|
|
describe "#relative_base_url" do
|
2013-11-05 13:04:47 -05:00
|
|
|
|
|
|
|
it "is present" do
|
2019-12-18 00:51:57 -05:00
|
|
|
expect(store.relative_base_url).to eq("/#{upload_path}")
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
2016-04-20 15:42:13 -04:00
|
|
|
it "supports subfolder" do
|
2019-11-15 00:48:24 -05:00
|
|
|
set_subfolder "/forum"
|
2019-12-18 00:51:57 -05:00
|
|
|
expect(store.relative_base_url).to eq("/forum/#{upload_path}")
|
2016-04-20 15:42:13 -04:00
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is internal" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(store.internal?).to eq(true)
|
|
|
|
expect(store.external?).to eq(false)
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
2021-05-27 11:42:25 -04:00
|
|
|
describe "#get_path_for" do
|
|
|
|
it "returns the correct path" do
|
|
|
|
expect(store.get_path_for("original", upload.id, upload.sha1, ".#{upload.extension}"))
|
|
|
|
.to match(%r|/#{upload_path}/original/.+#{upload.sha1}\.png|)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#get_path_for_upload" do
|
|
|
|
it "returns the correct path" do
|
|
|
|
expect(store.get_path_for_upload(upload))
|
|
|
|
.to match(%r|/#{upload_path}/original/.+#{upload.sha1}\.png|)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#get_path_for_optimized_image" do
|
|
|
|
it "returns the correct path" do
|
|
|
|
expect(store.get_path_for_optimized_image(optimized_image))
|
|
|
|
.to match(%r|/#{upload_path}/optimized/.+#{optimized_image.upload.sha1}_#{OptimizedImage::VERSION}_100x200\.png|)
|
|
|
|
end
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|