discourse/spec/components/file_store/local_store_spec.rb

121 lines
3.5 KiB
Ruby
Raw Normal View History

require 'rails_helper'
require 'file_store/local_store'
2013-11-05 13:04:47 -05:00
describe FileStore::LocalStore do
2013-11-05 13:04:47 -05:00
let(:store) { FileStore::LocalStore.new }
let(:upload) { Fabricate(:upload) }
let(:uploaded_file) { file_from_fixtures("logo.png") }
let(:optimized_image) { Fabricate(:optimized_image) }
2013-11-05 13:04:47 -05:00
describe ".store_upload" do
it "returns a relative url" do
store.expects(:copy_file)
expect(store.store_upload(uploaded_file, upload)).to match(/\/uploads\/default\/original\/.+#{upload.sha1}\.png/)
end
end
2013-11-05 13:04:47 -05:00
describe ".store_optimized_image" do
it "returns a relative url" do
store.expects(:copy_file)
expect(store.store_optimized_image({}, optimized_image)).to match(/\/uploads\/default\/optimized\/.+#{optimized_image.upload.sha1}_#{OptimizedImage::VERSION}_100x200\.png/)
end
end
2013-11-05 13:04:47 -05:00
describe ".remove_upload" do
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)
end
2013-11-27 16:01:41 -05:00
it "moves the file to the tombstone" do
FileUtils.expects(:mkdir_p)
FileUtils.expects(:move)
2016-05-22 23:22:25 -04:00
File.expects(:exists?).returns(true)
2013-08-13 16:08:29 -04:00
store.remove_upload(upload)
end
end
2013-11-05 13:04:47 -05:00
describe ".remove_optimized_image" do
let(:optimized_image) { Fabricate(:optimized_image, url: "/uploads/default/_optimized/42/253dc8edf9d4ada1.png") }
2013-08-13 16:08:29 -04:00
2013-11-27 16:01:41 -05:00
it "moves the file to the tombstone" do
FileUtils.expects(:mkdir_p)
FileUtils.expects(:move)
2016-05-22 23:22:25 -04:00
File.expects(:exists?).returns(true)
store.remove_optimized_image(optimized_image)
2013-11-05 13:04:47 -05: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-08-13 16:08:29 -04:00
2013-11-05 13:04:47 -05:00
it "identifies relatives urls" do
2015-01-09 11:34:37 -05:00
expect(store.has_been_uploaded?("/uploads/default/42/0123456789ABCDEF.jpg")).to eq(true)
2013-11-05 13:04:47 -05:00
end
2013-11-05 13:04:47 -05:00
it "identifies local urls" do
Discourse.stubs(:base_url_no_prefix).returns("http://discuss.site.com")
2015-01-09 11:34:37 -05:00
expect(store.has_been_uploaded?("http://discuss.site.com/uploads/default/42/0123456789ABCDEF.jpg")).to eq(true)
expect(store.has_been_uploaded?("//discuss.site.com/uploads/default/42/0123456789ABCDEF.jpg")).to eq(true)
end
it "identifies local urls when using a CDN" do
Rails.configuration.action_controller.stubs(:asset_host).returns("http://my.cdn.com")
2015-01-09 11:34:37 -05:00
expect(store.has_been_uploaded?("http://my.cdn.com/uploads/default/42/0123456789ABCDEF.jpg")).to eq(true)
expect(store.has_been_uploaded?("//my.cdn.com/uploads/default/42/0123456789ABCDEF.jpg")).to eq(true)
end
it "does not match dummy urls" do
2015-01-09 11:34:37 -05:00
expect(store.has_been_uploaded?("http://domain.com/uploads/default/42/0123456789ABCDEF.jpg")).to eq(false)
expect(store.has_been_uploaded?("//domain.com/uploads/default/42/0123456789ABCDEF.jpg")).to eq(false)
2013-11-05 13:04:47 -05:00
end
end
def stub_for_subfolder
GlobalSetting.stubs(:relative_url_root).returns('/forum')
Discourse.stubs(:base_uri).returns("/forum")
end
2013-11-05 13:04:47 -05:00
describe ".absolute_base_url" do
it "is present" do
2015-01-09 11:34:37 -05:00
expect(store.absolute_base_url).to eq("http://test.localhost/uploads/default")
2013-11-05 13:04:47 -05:00
end
it "supports subfolder" do
stub_for_subfolder
expect(store.absolute_base_url).to eq("http://test.localhost/forum/uploads/default")
end
2013-11-05 13:04:47 -05:00
end
describe ".relative_base_url" do
it "is present" do
2015-01-09 11:34:37 -05:00
expect(store.relative_base_url).to eq("/uploads/default")
2013-11-05 13:04:47 -05:00
end
it "supports subfolder" do
stub_for_subfolder
expect(store.relative_base_url).to eq("/forum/uploads/default")
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
end