discourse/spec/models/optimized_image_spec.rb

147 lines
3.5 KiB
Ruby
Raw Normal View History

2013-06-16 16:39:48 +08:00
require 'spec_helper'
describe OptimizedImage do
2013-11-06 02:04:47 +08:00
let(:upload) { build(:upload) }
before { upload.id = 42 }
describe ".local?" do
def local(url)
OptimizedImage.new(url: url).local?
end
it "correctly detects local vs remote" do
expect(local("//hello")).to eq(false)
expect(local("http://hello")).to eq(false)
expect(local("https://hello")).to eq(false)
expect(local("https://hello")).to eq(false)
expect(local("/hello")).to eq(true)
end
end
describe ".create_for" do
2013-11-06 02:04:47 +08:00
context "when using an internal store" do
let(:store) { FakeInternalStore.new }
before { Discourse.stubs(:store).returns(store) }
context "when an error happened while generating the thumbnail" do
2013-11-06 02:04:47 +08:00
it "returns nil" do
OptimizedImage.expects(:resize).returns(false)
2015-01-06 00:04:23 +08:00
expect(OptimizedImage.create_for(upload, 100, 200)).to eq(nil)
2013-11-06 02:04:47 +08:00
end
end
context "when the thumbnail is properly generated" do
before do
OptimizedImage.expects(:resize).returns(true)
end
2013-11-06 02:04:47 +08:00
it "does not download a copy of the original image" do
store.expects(:download).never
OptimizedImage.create_for(upload, 100, 200)
end
2013-11-06 02:04:47 +08: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)
2015-01-06 00:04:23 +08:00
expect(oi.sha1).to eq("da39a3ee5e6b4b0d3255bfef95601890afd80709")
expect(oi.extension).to eq(".png")
expect(oi.width).to eq(100)
expect(oi.height).to eq(200)
expect(oi.url).to eq("/internally/stored/optimized/image.png")
2013-11-06 02:04:47 +08:00
end
end
end
describe "external store" do
2013-11-06 02:04:47 +08: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
OptimizedImage.expects(:resize).returns(false)
2015-01-06 00:04:23 +08:00
expect(OptimizedImage.create_for(upload, 100, 200)).to eq(nil)
2013-11-06 02:04:47 +08:00
end
end
2013-11-06 02:04:47 +08:00
context "when the thumbnail is properly generated" do
before do
OptimizedImage.expects(:resize).returns(true)
end
2013-11-06 02:04:47 +08:00
it "downloads a copy of the original image" do
2015-02-10 00:13:22 +08:00
Tempfile.any_instance.expects(:close!)
2014-04-15 04:55:57 +08:00
store.expects(:download).with(upload).returns(Tempfile.new(["discourse-external", ".png"]))
2013-11-06 02:04:47 +08:00
OptimizedImage.create_for(upload, 100, 200)
end
it "works" do
oi = OptimizedImage.create_for(upload, 100, 200)
2015-01-06 00:04:23 +08:00
expect(oi.sha1).to eq("da39a3ee5e6b4b0d3255bfef95601890afd80709")
expect(oi.extension).to eq(".png")
expect(oi.width).to eq(100)
expect(oi.height).to eq(200)
expect(oi.url).to eq("/externally/stored/optimized/image.png")
2013-11-06 02:04:47 +08:00
end
end
end
end
2013-06-16 16:39:48 +08:00
end
2013-11-06 02:04:47 +08:00
class FakeInternalStore
def external?
2015-05-30 00:39:47 +08:00
false
2013-11-06 02:04:47 +08:00
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 path_for(upload)
nil
end
2013-11-06 02:04:47 +08:00
def external?
true
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