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-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
|
|
|
|
2014-07-14 11:34:23 -04:00
|
|
|
let(:image_filename) { "logo.png" }
|
2014-11-03 13:54:10 -05:00
|
|
|
let(:image) { file_from_fixtures(image_filename) }
|
2014-07-14 11:34:23 -04:00
|
|
|
let(:image_filesize) { File.size(image) }
|
2014-04-14 16:55:57 -04:00
|
|
|
let(:image_sha1) { Digest::SHA1.file(image).hexdigest }
|
2013-07-13 17:42:19 -04:00
|
|
|
|
2014-11-03 13:54:10 -05:00
|
|
|
let(:image_svg_filename) { "image.svg" }
|
|
|
|
let(:image_svg) { file_from_fixtures(image_svg_filename) }
|
|
|
|
let(:image_svg_filesize) { File.size(image_svg) }
|
|
|
|
|
2014-04-14 16:55:57 -04:00
|
|
|
let(:attachment_path) { __FILE__ }
|
|
|
|
let(:attachment) { File.new(attachment_path) }
|
|
|
|
let(:attachment_filename) { File.basename(attachment_path) }
|
|
|
|
let(:attachment_filesize) { File.size(attachment_path) }
|
2013-07-23 18:54:18 -04:00
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
context ".create_thumbnail!" do
|
|
|
|
|
|
|
|
it "does not create a thumbnail when disabled" do
|
|
|
|
SiteSetting.stubs(:create_thumbnails?).returns(false)
|
2013-07-31 17:26:34 -04:00
|
|
|
OptimizedImage.expects(:create_for).never
|
2013-09-27 04:55:50 -04:00
|
|
|
upload.create_thumbnail!(100, 100)
|
2013-07-13 17:42:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a thumbnail" do
|
|
|
|
upload = Fabricate(:upload)
|
|
|
|
thumbnail = Fabricate(:optimized_image, upload: upload)
|
|
|
|
SiteSetting.expects(:create_thumbnails?).returns(true)
|
|
|
|
OptimizedImage.expects(:create_for).returns(thumbnail)
|
2013-09-27 04:55:50 -04:00
|
|
|
upload.create_thumbnail!(100, 100)
|
2013-07-13 17:42:19 -04:00
|
|
|
upload.reload
|
|
|
|
upload.optimized_images.count.should == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
context "#create_for" do
|
2013-07-13 17:42:19 -04:00
|
|
|
|
2014-07-09 17:59:57 -04:00
|
|
|
before { Upload.stubs(:fix_image_orientation) }
|
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
it "does not create another upload if it already exists" do
|
2014-05-06 09:41:59 -04:00
|
|
|
Upload.expects(:find_by).with(sha1: image_sha1).returns(upload)
|
2014-04-14 16:55:57 -04:00
|
|
|
Upload.expects(:save).never
|
|
|
|
Upload.create_for(user_id, image, image_filename, image_filesize).should == upload
|
2013-04-07 11:52:46 -04:00
|
|
|
end
|
|
|
|
|
2014-07-09 17:59:57 -04:00
|
|
|
it "fix image orientation" do
|
|
|
|
Upload.expects(:fix_image_orientation).with(image.path)
|
|
|
|
Upload.create_for(user_id, image, image_filename, image_filesize)
|
|
|
|
end
|
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
it "computes width & height for images" do
|
|
|
|
FastImage.any_instance.expects(:size).returns([100, 200])
|
|
|
|
ImageSizer.expects(:resize)
|
2014-04-14 16:55:57 -04:00
|
|
|
image.expects(:rewind).twice
|
|
|
|
Upload.create_for(user_id, image, image_filename, image_filesize)
|
2013-07-13 17:42:19 -04:00
|
|
|
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
|
2014-04-14 16:55:57 -04:00
|
|
|
FileHelper.expects(:is_image?).returns(true)
|
|
|
|
Upload.expects(:save).never
|
|
|
|
upload = Upload.create_for(user_id, attachment, attachment_filename, attachment_filesize)
|
|
|
|
upload.errors.size.should > 0
|
2013-07-13 17:42:19 -04:00
|
|
|
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
|
|
|
|
FastImage.any_instance.expects(:size).never
|
2014-04-14 16:55:57 -04:00
|
|
|
upload = Upload.create_for(user_id, attachment, attachment_filename, attachment_filesize)
|
|
|
|
upload.errors.size.should > 0
|
2013-05-30 21:13:37 -04:00
|
|
|
end
|
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
it "saves proper information" do
|
2013-07-31 17:26:34 -04:00
|
|
|
store = {}
|
|
|
|
Discourse.expects(:store).returns(store)
|
|
|
|
store.expects(:store_upload).returns(url)
|
2014-04-14 16:55:57 -04:00
|
|
|
|
|
|
|
upload = Upload.create_for(user_id, image, image_filename, image_filesize)
|
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
upload.user_id.should == user_id
|
2014-04-14 16:55:57 -04:00
|
|
|
upload.original_filename.should == image_filename
|
|
|
|
upload.filesize.should == image_filesize
|
|
|
|
upload.sha1.should == image_sha1
|
2013-07-13 17:42:19 -04:00
|
|
|
upload.width.should == 244
|
|
|
|
upload.height.should == 66
|
|
|
|
upload.url.should == url
|
|
|
|
end
|
|
|
|
|
2014-11-03 13:54:10 -05:00
|
|
|
context "when svg is authorized" do
|
|
|
|
|
|
|
|
before { SiteSetting.stubs(:authorized_extensions).returns("svg") }
|
|
|
|
|
|
|
|
it "consider SVG as an image" do
|
|
|
|
store = {}
|
|
|
|
Discourse.expects(:store).returns(store)
|
|
|
|
store.expects(:store_upload).returns(url)
|
|
|
|
|
|
|
|
upload = Upload.create_for(user_id, image_svg, image_svg_filename, image_svg_filesize)
|
|
|
|
|
|
|
|
upload.user_id.should == user_id
|
|
|
|
upload.original_filename.should == image_svg_filename
|
|
|
|
upload.filesize.should == image_svg_filesize
|
|
|
|
upload.width.should == 100
|
|
|
|
upload.height.should == 50
|
|
|
|
upload.url.should == url
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
end
|
2013-05-30 21:13:37 -04:00
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
context ".get_from_url" do
|
|
|
|
|
2013-07-21 18:37:23 -04:00
|
|
|
it "works when the file has been uploaded" do
|
2014-05-06 09:41:59 -04:00
|
|
|
Upload.expects(:find_by).returns(nil).once
|
2013-07-21 18:37:23 -04:00
|
|
|
Upload.get_from_url("/uploads/default/1/10387531.jpg")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works when using a cdn" do
|
|
|
|
Rails.configuration.action_controller.stubs(:asset_host).returns("http://my.cdn.com")
|
2014-05-06 09:41:59 -04:00
|
|
|
Upload.expects(:find_by).with(url: "/uploads/default/1/02395732905.jpg").returns(nil).once
|
2013-07-21 18:37:23 -04:00
|
|
|
Upload.get_from_url("http://my.cdn.com/uploads/default/1/02395732905.jpg")
|
|
|
|
end
|
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
it "works only when the file has been uploaded" do
|
2014-05-06 09:41:59 -04:00
|
|
|
Upload.expects(:find_by).never
|
2013-07-21 18:37:23 -04:00
|
|
|
Upload.get_from_url("http://domain.com/my/file.txt")
|
2013-07-13 17:42:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|