2013-06-16 03:39:48 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe OptimizedImage do
|
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
let(:upload) { build(:upload) }
|
|
|
|
before { upload.id = 42 }
|
2013-06-16 19:46:42 -05:00
|
|
|
|
|
|
|
describe ".create_for" do
|
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
context "when using an internal store" do
|
|
|
|
|
|
|
|
let(:store) { FakeInternalStore.new }
|
|
|
|
before { Discourse.stubs(:store).returns(store) }
|
|
|
|
|
2014-05-26 04:46:43 -05:00
|
|
|
context "when an error happened while generating the thumbnail" do
|
2013-11-05 12:04:47 -06:00
|
|
|
|
|
|
|
it "returns nil" do
|
2014-05-26 04:46:43 -05:00
|
|
|
OptimizedImage.expects(:resize).returns(false)
|
2014-09-25 10:44:48 -05:00
|
|
|
OptimizedImage.create_for(upload, 100, 200).should == nil
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the thumbnail is properly generated" do
|
|
|
|
|
2014-05-26 04:46:43 -05:00
|
|
|
before do
|
|
|
|
OptimizedImage.expects(:resize).returns(true)
|
|
|
|
end
|
2013-11-05 12:04:47 -06:00
|
|
|
|
|
|
|
it "does not download a copy of the original image" do
|
|
|
|
store.expects(:download).never
|
|
|
|
OptimizedImage.create_for(upload, 100, 200)
|
|
|
|
end
|
2013-07-31 16:26:34 -05:00
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
it "closes and removes the tempfile" do
|
|
|
|
Tempfile.any_instance.expects(:close!)
|
|
|
|
OptimizedImage.create_for(upload, 100, 200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works" do
|
|
|
|
oi = OptimizedImage.create_for(upload, 100, 200)
|
|
|
|
oi.sha1.should == "da39a3ee5e6b4b0d3255bfef95601890afd80709"
|
2014-04-14 15:55:57 -05:00
|
|
|
oi.extension.should == ".png"
|
2013-11-05 12:04:47 -06:00
|
|
|
oi.width.should == 100
|
|
|
|
oi.height.should == 200
|
2014-04-14 15:55:57 -05:00
|
|
|
oi.url.should == "/internally/stored/optimized/image.png"
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
2013-07-31 16:26:34 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-16 19:46:42 -05:00
|
|
|
end
|
|
|
|
|
2013-07-31 16:26:34 -05:00
|
|
|
describe "external store" do
|
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
let(:store) { FakeExternalStore.new }
|
|
|
|
before { Discourse.stubs(:store).returns(store) }
|
|
|
|
|
|
|
|
context "when an error happened while generatign the thumbnail" do
|
|
|
|
|
|
|
|
it "returns nil" do
|
2014-05-26 04:46:43 -05:00
|
|
|
OptimizedImage.expects(:resize).returns(false)
|
2014-09-25 10:44:48 -05:00
|
|
|
OptimizedImage.create_for(upload, 100, 200).should == nil
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
2013-07-31 16:26:34 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
context "when the thumbnail is properly generated" do
|
|
|
|
|
2014-05-26 04:46:43 -05:00
|
|
|
before do
|
|
|
|
OptimizedImage.expects(:resize).returns(true)
|
|
|
|
end
|
2013-11-05 12:04:47 -06:00
|
|
|
|
|
|
|
it "downloads a copy of the original image" do
|
|
|
|
Tempfile.any_instance.expects(:close!).twice
|
2014-04-14 15:55:57 -05:00
|
|
|
store.expects(:download).with(upload).returns(Tempfile.new(["discourse-external", ".png"]))
|
2013-11-05 12:04:47 -06:00
|
|
|
OptimizedImage.create_for(upload, 100, 200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works" do
|
|
|
|
oi = OptimizedImage.create_for(upload, 100, 200)
|
|
|
|
oi.sha1.should == "da39a3ee5e6b4b0d3255bfef95601890afd80709"
|
2014-04-14 15:55:57 -05:00
|
|
|
oi.extension.should == ".png"
|
2013-11-05 12:04:47 -06:00
|
|
|
oi.width.should == 100
|
|
|
|
oi.height.should == 200
|
2014-04-14 15:55:57 -05:00
|
|
|
oi.url.should == "/externally/stored/optimized/image.png"
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
2013-07-31 16:26:34 -05:00
|
|
|
end
|
|
|
|
|
2013-06-16 19:46:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-16 03:39:48 -05:00
|
|
|
end
|
2013-11-05 12:04:47 -06:00
|
|
|
|
|
|
|
class FakeInternalStore
|
|
|
|
|
|
|
|
def internal?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def external?
|
|
|
|
!internal?
|
|
|
|
end
|
|
|
|
|
|
|
|
def path_for(upload)
|
|
|
|
upload.url
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_optimized_image(file, optimized_image)
|
|
|
|
"/internally/stored/optimized/image#{optimized_image.extension}"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class FakeExternalStore
|
|
|
|
|
|
|
|
def external?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal?
|
|
|
|
!external?
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_optimized_image(file, optimized_image)
|
|
|
|
"/externally/stored/optimized/image#{optimized_image.extension}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def download(upload)
|
|
|
|
extension = File.extname(upload.original_filename)
|
|
|
|
Tempfile.new(["discourse-s3", extension])
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|