discourse/spec/components/file_store/local_store_spec.rb

129 lines
3.6 KiB
Ruby
Raw Normal View History

require 'spec_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) { build(:upload) }
2014-04-14 16:55:57 -04:00
let(:uploaded_file) { File.new("#{Rails.root}/spec/fixtures/images/logo.png") }
let(:optimized_image) { build(:optimized_image) }
2013-11-05 13:04:47 -05:00
let(:avatar) { build(:upload) }
2013-11-05 13:04:47 -05:00
describe ".store_upload" do
it "returns a relative url" do
Time.stubs(:now).returns(Time.utc(2013, 2, 17, 12, 0, 0, 0))
upload.stubs(:id).returns(42)
store.expects(:copy_file)
store.store_upload(uploaded_file, upload).should == "/uploads/default/42/253dc8edf9d4ada1.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)
2013-08-13 16:08:29 -04:00
store.store_optimized_image({}, optimized_image).should == "/uploads/default/_optimized/86f/7e4/37faa5a7fc_100x200.png"
end
end
2013-11-05 13:04:47 -05:00
describe ".store_avatar" do
it "returns a relative url" do
store.expects(:copy_file)
2014-04-14 16:55:57 -04:00
store.store_avatar({}, upload, 100).should == "/uploads/default/avatars/e9d/71f/5ee7c92d6d/100.png"
2013-11-05 13:04:47 -05:00
end
end
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
upload = Upload.new
upload.stubs(:url).returns("/path/to/file")
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)
2013-08-13 16:08:29 -04:00
upload = Upload.new
upload.stubs(:url).returns("/uploads/default/42/253dc8edf9d4ada1.png")
store.remove_upload(upload)
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-27 16:01:41 -05:00
it "moves the file to the tombstone" do
FileUtils.expects(:mkdir_p)
FileUtils.expects(:move)
2013-11-05 13:04:47 -05:00
oi = OptimizedImage.new
oi.stubs(:url).returns("/uploads/default/_optimized/42/253dc8edf9d4ada1.png")
2013-11-27 16:01:41 -05:00
store.remove_optimized_image(upload)
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
store.has_been_uploaded?("/uploads/default/42/0123456789ABCDEF.jpg").should == true
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")
store.has_been_uploaded?("http://discuss.site.com/uploads/default/42/0123456789ABCDEF.jpg").should == true
2013-11-05 13:04:47 -05:00
store.has_been_uploaded?("//discuss.site.com/uploads/default/42/0123456789ABCDEF.jpg").should == true
end
it "identifies local urls when using a CDN" do
Rails.configuration.action_controller.stubs(:asset_host).returns("http://my.cdn.com")
store.has_been_uploaded?("http://my.cdn.com/uploads/default/42/0123456789ABCDEF.jpg").should == true
2013-11-05 13:04:47 -05:00
store.has_been_uploaded?("//my.cdn.com/uploads/default/42/0123456789ABCDEF.jpg").should == true
end
it "does not match dummy urls" do
store.has_been_uploaded?("http://domain.com/uploads/default/42/0123456789ABCDEF.jpg").should == false
2013-11-05 13:04:47 -05:00
store.has_been_uploaded?("//domain.com/uploads/default/42/0123456789ABCDEF.jpg").should == false
end
end
describe ".absolute_base_url" do
it "is present" do
store.absolute_base_url.should == "http://test.localhost/uploads/default"
end
end
describe ".relative_base_url" do
it "is present" do
store.relative_base_url.should == "/uploads/default"
end
end
it "is internal" do
store.internal?.should == true
store.external?.should == false
end
describe ".avatar_template" do
2013-11-05 13:04:47 -05:00
it "is present" do
2014-04-14 16:55:57 -04:00
store.avatar_template(avatar).should == "/uploads/default/avatars/e9d/71f/5ee7c92d6d/{size}.png"
end
end
end