2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
2013-06-15 05:52:40 -04:00
|
|
|
require 'digest/sha1'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe Upload do
|
|
|
|
|
|
|
|
it { should belong_to :user }
|
2013-04-07 11:52:46 -04:00
|
|
|
|
2013-06-13 17:44:24 -04:00
|
|
|
it { should have_many :post_uploads }
|
|
|
|
it { should have_many :posts }
|
2013-06-12 19:43:50 -04:00
|
|
|
|
2013-06-16 04:39:48 -04:00
|
|
|
it { should have_many :optimized_images }
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it { should validate_presence_of :original_filename }
|
|
|
|
it { should validate_presence_of :filesize }
|
2013-04-07 11:52:46 -04:00
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
let(:upload) { build(:upload) }
|
|
|
|
let(:thumbnail) { build(:optimized_image, upload: upload) }
|
2013-04-07 11:52:46 -04:00
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
let(:user_id) { 1 }
|
|
|
|
let(:url) { "http://domain.com" }
|
2013-04-07 11:52:46 -04:00
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
let(:image) do
|
|
|
|
ActionDispatch::Http::UploadedFile.new({
|
|
|
|
filename: 'logo.png',
|
|
|
|
tempfile: File.new("#{Rails.root}/spec/fixtures/images/logo.png")
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:image_sha1) { Digest::SHA1.file(image.tempfile).hexdigest }
|
|
|
|
|
|
|
|
let(:attachment) do
|
|
|
|
ActionDispatch::Http::UploadedFile.new({
|
|
|
|
filename: File.basename(__FILE__),
|
|
|
|
tempfile: File.new(__FILE__)
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
context ".create_thumbnail!" do
|
|
|
|
|
|
|
|
it "does not create a thumbnail when disabled" do
|
|
|
|
SiteSetting.stubs(:create_thumbnails?).returns(false)
|
|
|
|
SiteSetting.expects(:enable_s3_uploads?).never
|
|
|
|
upload.create_thumbnail!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not create a thumbnail when using S3" do
|
|
|
|
SiteSetting.expects(:create_thumbnails?).returns(true)
|
|
|
|
SiteSetting.expects(:enable_s3_uploads?).returns(true)
|
|
|
|
upload.expects(:has_thumbnail?).never
|
|
|
|
upload.create_thumbnail!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not create another thumbnail" do
|
|
|
|
SiteSetting.expects(:create_thumbnails?).returns(true)
|
|
|
|
SiteSetting.expects(:enable_s3_uploads?).returns(false)
|
|
|
|
upload.expects(:has_thumbnail?).returns(true)
|
|
|
|
OptimizedImage.expects(:create_for).never
|
|
|
|
upload.create_thumbnail!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a thumbnail" do
|
|
|
|
upload = Fabricate(:upload)
|
|
|
|
thumbnail = Fabricate(:optimized_image, upload: upload)
|
|
|
|
SiteSetting.expects(:create_thumbnails?).returns(true)
|
|
|
|
SiteSetting.expects(:enable_s3_uploads?).returns(false)
|
|
|
|
upload.expects(:has_thumbnail?).returns(false)
|
|
|
|
OptimizedImage.expects(:create_for).returns(thumbnail)
|
|
|
|
upload.create_thumbnail!
|
|
|
|
upload.reload
|
|
|
|
upload.optimized_images.count.should == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context ".create_for" do
|
|
|
|
|
|
|
|
it "does not create another upload if it already exists" do
|
|
|
|
Upload.expects(:where).with(sha1: image_sha1).returns([upload])
|
|
|
|
Upload.expects(:create!).never
|
|
|
|
Upload.create_for(user_id, image).should == upload
|
2013-04-07 11:52:46 -04:00
|
|
|
end
|
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
it "computes width & height for images" do
|
|
|
|
SiteSetting.expects(:authorized_image?).returns(true)
|
|
|
|
FastImage.any_instance.expects(:size).returns([100, 200])
|
|
|
|
ImageSizer.expects(:resize)
|
|
|
|
ActionDispatch::Http::UploadedFile.any_instance.expects(:rewind)
|
|
|
|
Upload.create_for(user_id, image)
|
|
|
|
end
|
2013-04-07 11:52:46 -04:00
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
it "does not create an upload when there is an error with FastImage" do
|
|
|
|
SiteSetting.expects(:authorized_image?).returns(true)
|
|
|
|
Upload.expects(:create!).never
|
|
|
|
expect { Upload.create_for(user_id, attachment) }.to raise_error(FastImage::UnknownImageType)
|
|
|
|
end
|
2013-04-07 11:52:46 -04:00
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
it "does not compute width & height for non-image" do
|
|
|
|
SiteSetting.expects(:authorized_image?).returns(false)
|
|
|
|
FastImage.any_instance.expects(:size).never
|
|
|
|
Upload.create_for(user_id, image)
|
2013-05-30 21:13:37 -04:00
|
|
|
end
|
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
it "saves proper information" do
|
|
|
|
Upload.expects(:store_file).returns(url)
|
|
|
|
upload = Upload.create_for(user_id, image)
|
|
|
|
upload.user_id.should == user_id
|
|
|
|
upload.original_filename.should == image.original_filename
|
|
|
|
upload.filesize.should == File.size(image.tempfile)
|
|
|
|
upload.sha1.should == Digest::SHA1.file(image.tempfile).hexdigest
|
|
|
|
upload.width.should == 244
|
|
|
|
upload.height.should == 66
|
|
|
|
upload.url.should == url
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-05-30 21:13:37 -04:00
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
context ".store_file" do
|
2013-05-30 21:13:37 -04:00
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
it "store files on s3 when enabled" do
|
|
|
|
SiteSetting.expects(:enable_s3_uploads?).returns(true)
|
|
|
|
LocalStore.expects(:store_file).never
|
|
|
|
S3.expects(:store_file)
|
|
|
|
Upload.store_file(image, image_sha1, 1)
|
2013-04-07 11:52:46 -04:00
|
|
|
end
|
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
it "store files locally by default" do
|
|
|
|
S3.expects(:store_file).never
|
|
|
|
LocalStore.expects(:store_file)
|
|
|
|
Upload.store_file(image, image_sha1, 1)
|
2013-04-07 11:52:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
context ".remove_file" do
|
|
|
|
|
|
|
|
it "remove files on s3 when enabled" do
|
|
|
|
SiteSetting.expects(:enable_s3_uploads?).returns(true)
|
|
|
|
LocalStore.expects(:remove_file).never
|
|
|
|
S3.expects(:remove_file)
|
|
|
|
Upload.remove_file(upload.url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "remove files locally by default" do
|
|
|
|
S3.expects(:remove_file).never
|
|
|
|
LocalStore.expects(:remove_file)
|
|
|
|
Upload.remove_file(upload.url)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context ".has_been_uploaded?" do
|
2013-06-16 04:21:01 -04:00
|
|
|
|
|
|
|
it "identifies internal or relatives urls" do
|
|
|
|
Discourse.expects(:base_url_no_prefix).returns("http://discuss.site.com")
|
2013-07-10 16:59:07 -04:00
|
|
|
Upload.has_been_uploaded?("http://discuss.site.com/uploads/default/42/0123456789ABCDEF.jpg").should == true
|
2013-06-19 15:51:41 -04:00
|
|
|
Upload.has_been_uploaded?("/upload/42/0123456789ABCDEF.jpg").should == true
|
2013-06-16 04:21:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "identifies internal urls when using a CDN" do
|
|
|
|
ActionController::Base.expects(:asset_host).returns("http://my.cdn.com").twice
|
2013-07-10 16:59:07 -04:00
|
|
|
Upload.has_been_uploaded?("http://my.cdn.com/uploads/default/42/0123456789ABCDEF.jpg").should == true
|
2013-06-16 04:21:01 -04:00
|
|
|
end
|
|
|
|
|
2013-06-22 07:38:42 -04:00
|
|
|
it "identifies S3 uploads" do
|
|
|
|
SiteSetting.stubs(:enable_s3_uploads).returns(true)
|
2013-07-16 17:51:12 -04:00
|
|
|
SiteSetting.stubs(:s3_upload_bucket).returns("Bucket")
|
|
|
|
Upload.has_been_uploaded?("//s3.amazonaws.com/Bucket/1337.png").should == true
|
2013-06-22 07:38:42 -04:00
|
|
|
end
|
|
|
|
|
2013-06-16 04:21:01 -04:00
|
|
|
it "identifies external urls" do
|
2013-07-10 16:59:07 -04:00
|
|
|
Upload.has_been_uploaded?("http://domain.com/uploads/default/42/0123456789ABCDEF.jpg").should == false
|
2013-07-16 17:51:12 -04:00
|
|
|
Upload.has_been_uploaded?("//s3.amazonaws.com/Bucket/1337.png").should == false
|
2013-06-16 04:21:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
context ".get_from_url" do
|
|
|
|
|
|
|
|
it "works only when the file has been uploaded" do
|
|
|
|
Upload.expects(:has_been_uploaded?).returns(false)
|
|
|
|
Upload.expects(:where).never
|
|
|
|
Upload.get_from_url("discourse.org")
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|