2016-04-01 05:33:25 +08:00
|
|
|
require 'rails_helper'
|
2013-06-15 17:52:40 +08:00
|
|
|
require 'digest/sha1'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe Upload do
|
2013-06-16 16:39:48 +08:00
|
|
|
|
2013-07-14 05:42:19 +08:00
|
|
|
let(:upload) { build(:upload) }
|
|
|
|
let(:thumbnail) { build(:optimized_image, upload: upload) }
|
2013-04-07 23:52:46 +08:00
|
|
|
|
2013-07-14 05:42:19 +08:00
|
|
|
let(:user_id) { 1 }
|
|
|
|
let(:url) { "http://domain.com" }
|
2013-04-07 23:52:46 +08:00
|
|
|
|
2014-07-14 23:34:23 +08:00
|
|
|
let(:image_filename) { "logo.png" }
|
2014-11-04 02:54:10 +08:00
|
|
|
let(:image) { file_from_fixtures(image_filename) }
|
2014-07-14 23:34:23 +08:00
|
|
|
let(:image_filesize) { File.size(image) }
|
2014-04-15 04:55:57 +08:00
|
|
|
let(:image_sha1) { Digest::SHA1.file(image).hexdigest }
|
2013-07-14 05:42:19 +08:00
|
|
|
|
2014-11-04 02:54:10 +08: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-15 04:55:57 +08: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-24 06:54:18 +08:00
|
|
|
|
2013-07-14 05:42:19 +08:00
|
|
|
context ".create_thumbnail!" do
|
|
|
|
|
|
|
|
it "does not create a thumbnail when disabled" do
|
|
|
|
SiteSetting.stubs(:create_thumbnails?).returns(false)
|
2013-08-01 05:26:34 +08:00
|
|
|
OptimizedImage.expects(:create_for).never
|
2013-09-27 16:55:50 +08:00
|
|
|
upload.create_thumbnail!(100, 100)
|
2013-07-14 05:42:19 +08: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 16:55:50 +08:00
|
|
|
upload.create_thumbnail!(100, 100)
|
2013-07-14 05:42:19 +08:00
|
|
|
upload.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(upload.optimized_images.count).to eq(1)
|
2013-07-14 05:42:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
context "#create_for" do
|
2013-07-14 05:42:19 +08:00
|
|
|
|
2015-07-15 23:15:43 +08:00
|
|
|
before do
|
|
|
|
Upload.stubs(:fix_image_orientation)
|
|
|
|
ImageOptim.any_instance.stubs(:optimize_image!)
|
|
|
|
end
|
2014-07-10 05:59:57 +08:00
|
|
|
|
2013-07-14 05:42:19 +08:00
|
|
|
it "does not create another upload if it already exists" do
|
2014-05-06 21:41:59 +08:00
|
|
|
Upload.expects(:find_by).with(sha1: image_sha1).returns(upload)
|
2014-04-15 04:55:57 +08:00
|
|
|
Upload.expects(:save).never
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Upload.create_for(user_id, image, image_filename, image_filesize)).to eq(upload)
|
2013-04-07 23:52:46 +08:00
|
|
|
end
|
|
|
|
|
2014-07-10 05:59:57 +08: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-14 05:42:19 +08:00
|
|
|
it "computes width & height for images" do
|
|
|
|
ImageSizer.expects(:resize)
|
2014-04-15 04:55:57 +08:00
|
|
|
image.expects(:rewind).twice
|
|
|
|
Upload.create_for(user_id, image, image_filename, image_filesize)
|
2013-07-14 05:42:19 +08:00
|
|
|
end
|
2013-04-07 23:52:46 +08:00
|
|
|
|
2013-07-14 05:42:19 +08:00
|
|
|
it "does not compute width & height for non-image" do
|
|
|
|
FastImage.any_instance.expects(:size).never
|
2014-04-15 04:55:57 +08:00
|
|
|
upload = Upload.create_for(user_id, attachment, attachment_filename, attachment_filesize)
|
2015-01-29 02:33:11 +08:00
|
|
|
expect(upload.errors.size).to be > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "generates an error when the image is too large" do
|
|
|
|
SiteSetting.stubs(:max_image_size_kb).returns(1)
|
|
|
|
upload = Upload.create_for(user_id, image, image_filename, image_filesize)
|
|
|
|
expect(upload.errors.size).to be > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "generates an error when the attachment is too large" do
|
|
|
|
SiteSetting.stubs(:max_attachment_size_kb).returns(1)
|
|
|
|
upload = Upload.create_for(user_id, attachment, attachment_filename, attachment_filesize)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(upload.errors.size).to be > 0
|
2013-05-31 09:13:37 +08:00
|
|
|
end
|
|
|
|
|
2013-07-14 05:42:19 +08:00
|
|
|
it "saves proper information" do
|
2013-08-01 05:26:34 +08:00
|
|
|
store = {}
|
|
|
|
Discourse.expects(:store).returns(store)
|
|
|
|
store.expects(:store_upload).returns(url)
|
2014-04-15 04:55:57 +08:00
|
|
|
|
|
|
|
upload = Upload.create_for(user_id, image, image_filename, image_filesize)
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(upload.user_id).to eq(user_id)
|
|
|
|
expect(upload.original_filename).to eq(image_filename)
|
|
|
|
expect(upload.filesize).to eq(image_filesize)
|
|
|
|
expect(upload.width).to eq(244)
|
|
|
|
expect(upload.height).to eq(66)
|
|
|
|
expect(upload.url).to eq(url)
|
2013-07-14 05:42:19 +08:00
|
|
|
end
|
|
|
|
|
2014-11-04 02:54:10 +08: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)
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(upload.user_id).to eq(user_id)
|
|
|
|
expect(upload.original_filename).to eq(image_svg_filename)
|
|
|
|
expect(upload.filesize).to eq(image_svg_filesize)
|
|
|
|
expect(upload.width).to eq(100)
|
|
|
|
expect(upload.height).to eq(50)
|
|
|
|
expect(upload.url).to eq(url)
|
2014-11-04 02:54:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-07-14 05:42:19 +08:00
|
|
|
end
|
2013-05-31 09:13:37 +08:00
|
|
|
|
2013-07-14 05:42:19 +08:00
|
|
|
context ".get_from_url" do
|
|
|
|
|
2013-07-22 06:37:23 +08:00
|
|
|
it "works when the file has been uploaded" do
|
2014-05-06 21:41:59 +08:00
|
|
|
Upload.expects(:find_by).returns(nil).once
|
2013-07-22 06:37:23 +08: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 21:41:59 +08:00
|
|
|
Upload.expects(:find_by).with(url: "/uploads/default/1/02395732905.jpg").returns(nil).once
|
2013-07-22 06:37:23 +08:00
|
|
|
Upload.get_from_url("http://my.cdn.com/uploads/default/1/02395732905.jpg")
|
|
|
|
end
|
|
|
|
|
2013-07-14 05:42:19 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|