2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'cooked_post_processor'
|
|
|
|
|
|
|
|
describe CookedPostProcessor do
|
2013-04-10 15:52:38 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
context "post_process" do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
let(:post) { build(:post) }
|
|
|
|
let(:cpp) { CookedPostProcessor.new(post) }
|
|
|
|
let(:post_process) { sequence("post_process") }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-11 04:59:07 +08:00
|
|
|
it "post process in sequence" do
|
|
|
|
cpp.expects(:post_process_attachments).in_sequence(post_process)
|
2013-07-08 07:39:08 +08:00
|
|
|
cpp.expects(:post_process_images).in_sequence(post_process)
|
|
|
|
cpp.expects(:post_process_oneboxes).in_sequence(post_process)
|
|
|
|
cpp.post_process
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-11 04:59:07 +08:00
|
|
|
context "post_process_attachments" do
|
|
|
|
|
|
|
|
context "with attachment" do
|
|
|
|
|
|
|
|
let(:upload) { Fabricate(:upload) }
|
|
|
|
let(:post) { Fabricate(:post_with_an_attachment) }
|
|
|
|
let(:cpp) { CookedPostProcessor.new(post) }
|
|
|
|
|
|
|
|
# all in one test to speed things up
|
|
|
|
it "works" do
|
|
|
|
Upload.expects(:get_from_url).returns(upload)
|
|
|
|
cpp.post_process_attachments
|
|
|
|
# ensures absolute urls on attachment
|
2013-08-01 05:26:34 +08:00
|
|
|
cpp.html.should =~ /#{Discourse.store.absolute_base_url}/
|
2013-07-11 04:59:07 +08:00
|
|
|
# keeps the reverse index up to date
|
|
|
|
post.uploads.reload
|
|
|
|
post.uploads.count.should == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
context "post_process_images" do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
context "with images in quotes and oneboxes" do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
let(:post) { build(:post_with_images_in_quote_and_onebox) }
|
|
|
|
let(:cpp) { CookedPostProcessor.new(post) }
|
|
|
|
before { cpp.post_process_images }
|
2013-04-10 15:52:38 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
it "does not process them" do
|
|
|
|
cpp.html.should match_html post.cooked
|
|
|
|
cpp.should_not be_dirty
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has no topic image if there isn't one in the post" do
|
|
|
|
post.topic.image_url.should be_blank
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-07-17 04:16:33 +08:00
|
|
|
context "with locally uploaded images" do
|
2013-07-08 07:39:08 +08:00
|
|
|
|
|
|
|
let(:upload) { Fabricate(:upload) }
|
2013-07-22 06:37:23 +08:00
|
|
|
let(:post) { Fabricate(:post_with_uploaded_image) }
|
2013-07-08 07:39:08 +08:00
|
|
|
let(:cpp) { CookedPostProcessor.new(post) }
|
2013-07-22 06:37:23 +08:00
|
|
|
before { FastImage.stubs(:size).returns([200, 400]) }
|
2013-07-08 07:39:08 +08:00
|
|
|
|
|
|
|
# all in one test to speed things up
|
|
|
|
it "works" do
|
2013-07-22 06:37:23 +08:00
|
|
|
Upload.expects(:get_from_url).returns(upload)
|
2013-07-08 07:39:08 +08:00
|
|
|
cpp.post_process_images
|
|
|
|
# ensures absolute urls on uploaded images
|
2013-08-01 05:26:34 +08:00
|
|
|
cpp.html.should =~ /#{LocalStore.new.absolute_base_url}/
|
2013-07-15 06:18:17 +08:00
|
|
|
# dirty
|
|
|
|
cpp.should be_dirty
|
2013-07-08 07:39:08 +08:00
|
|
|
# keeps the reverse index up to date
|
|
|
|
post.uploads.reload
|
|
|
|
post.uploads.count.should == 1
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
end
|
|
|
|
|
2013-07-11 04:59:07 +08:00
|
|
|
context "with sized images" do
|
2013-07-08 07:39:08 +08:00
|
|
|
|
|
|
|
let(:post) { build(:post_with_image_url) }
|
|
|
|
let(:cpp) { CookedPostProcessor.new(post, image_sizes: {'http://foo.bar/image.png' => {'width' => 111, 'height' => 222}}) }
|
|
|
|
|
|
|
|
before { FastImage.stubs(:size).returns([150, 250]) }
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it "adds the width from the image sizes provided" do
|
2013-07-08 07:39:08 +08:00
|
|
|
cpp.post_process_images
|
|
|
|
cpp.html.should =~ /width=\"111\"/
|
|
|
|
cpp.should be_dirty
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
context "with unsized images" do
|
|
|
|
|
|
|
|
let(:post) { build(:post_with_unsized_images) }
|
|
|
|
let(:cpp) { CookedPostProcessor.new(post) }
|
2013-06-18 04:46:48 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
it "adds the width and height to images that don't have them" do
|
|
|
|
FastImage.expects(:size).returns([123, 456])
|
|
|
|
cpp.post_process_images
|
|
|
|
cpp.html.should =~ /width="123" height="456"/
|
|
|
|
cpp.should be_dirty
|
2013-06-18 04:46:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
context "with large images" do
|
2013-03-19 01:55:34 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
let(:upload) { Fabricate(:upload) }
|
|
|
|
let(:post) { build(:post_with_large_image) }
|
|
|
|
let(:cpp) { CookedPostProcessor.new(post) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
before do
|
2013-08-26 06:24:24 +08:00
|
|
|
SiteSetting.stubs(:max_image_height).returns(2000)
|
2013-07-08 07:39:08 +08:00
|
|
|
SiteSetting.stubs(:create_thumbnails?).returns(true)
|
|
|
|
Upload.expects(:get_from_url).returns(upload)
|
|
|
|
cpp.stubs(:associate_to_post)
|
|
|
|
FastImage.stubs(:size).returns([1000, 2000])
|
|
|
|
# optimized_image
|
|
|
|
FileUtils.stubs(:mkdir_p)
|
|
|
|
File.stubs(:open)
|
|
|
|
ImageSorcery.any_instance.expects(:convert).returns(true)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
it "generates overlay information" do
|
|
|
|
cpp.post_process_images
|
2013-09-27 16:55:50 +08:00
|
|
|
cpp.html.should match_html '<div><a href="http://test.localhost/uploads/default/1/1234567890123456.jpg" class="lightbox"><img src="http://test.localhost/uploads/default/_optimized/da3/9a3/ee5e6b4b0d_690x1380.jpg" width="690" height="1380"><div class="meta">
|
2013-07-24 15:24:28 +08:00
|
|
|
<span class="filename">uploaded.jpg</span><span class="informations">1000x2000 1.21 KB</span><span class="expand"></span>
|
2013-07-08 07:39:08 +08:00
|
|
|
</div></a></div>'
|
|
|
|
cpp.should be_dirty
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-05-30 23:17:07 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
context "topic image" do
|
2013-05-30 23:17:07 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
let(:topic) { build(:topic, id: 1) }
|
2013-07-22 06:37:23 +08:00
|
|
|
let(:post) { Fabricate(:post_with_uploaded_image, topic: topic) }
|
2013-07-08 07:39:08 +08:00
|
|
|
let(:cpp) { CookedPostProcessor.new(post) }
|
2013-05-30 23:17:07 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
it "adds a topic image if there's one in the post" do
|
|
|
|
FastImage.stubs(:size).returns([100, 100])
|
|
|
|
cpp.post_process_images
|
|
|
|
post.topic.reload
|
|
|
|
post.topic.image_url.should == "http://test.localhost/uploads/default/2/3456789012345678.png"
|
2013-05-30 23:17:07 +08:00
|
|
|
end
|
2013-07-08 07:39:08 +08:00
|
|
|
|
2013-05-30 23:17:07 +08:00
|
|
|
end
|
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
end
|
2013-06-24 08:10:21 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
context "post_process_oneboxes" do
|
2013-06-24 08:10:21 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
let(:post) { build(:post_with_youtube, id: 123) }
|
|
|
|
let(:cpp) { CookedPostProcessor.new(post, invalidate_oneboxes: true) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Oneboxer.expects(:onebox).with("http://www.youtube.com/watch?v=9bZkp7q19f0", post_id: 123, invalidate_oneboxes: true).returns('<div>GANGNAM STYLE</div>')
|
|
|
|
cpp.post_process_oneboxes
|
2013-06-24 08:10:21 +08:00
|
|
|
end
|
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
it "should be dirty" do
|
|
|
|
cpp.should be_dirty
|
|
|
|
end
|
2013-06-26 08:44:20 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
it "inserts the onebox without wrapping p" do
|
|
|
|
cpp.html.should match_html "<div>GANGNAM STYLE</div>"
|
|
|
|
end
|
2013-06-26 08:44:20 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
end
|
2013-06-26 08:44:20 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
context "get_filename" do
|
2013-06-26 08:44:20 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
let(:post) { build(:post) }
|
|
|
|
let(:cpp) { CookedPostProcessor.new(post) }
|
|
|
|
|
|
|
|
it "returns the filename of the src when there is no upload" do
|
|
|
|
cpp.get_filename(nil, "http://domain.com/image.png").should == "image.png"
|
2013-06-26 08:44:20 +08:00
|
|
|
end
|
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
it "returns the original filename of the upload when there is an upload" do
|
|
|
|
upload = build(:upload, { original_filename: "upload.jpg" })
|
|
|
|
cpp.get_filename(upload, "http://domain.com/image.png").should == "upload.jpg"
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
it "returns a generic name for pasted images" do
|
|
|
|
upload = build(:upload, { original_filename: "blob.png" })
|
|
|
|
cpp.get_filename(upload, "http://domain.com/image.png").should == I18n.t('upload.pasted_image_filename')
|
2013-02-19 14:57:14 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "get_size" do
|
|
|
|
|
|
|
|
let(:post) { build(:post) }
|
|
|
|
let(:cpp) { CookedPostProcessor.new(post) }
|
2013-07-07 01:10:53 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
it "ensures s3 urls have a default scheme" do
|
|
|
|
FastImage.stubs(:size)
|
|
|
|
cpp.expects(:is_valid_image_uri?).with("http://bucket.s3.aws.amazon.com/image.jpg")
|
|
|
|
cpp.get_size("//bucket.s3.aws.amazon.com/image.jpg")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
context "crawl_images is disabled" do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
before { SiteSetting.stubs(:crawl_images?).returns(false) }
|
2013-07-07 01:10:53 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
it "doesn't call FastImage" do
|
2013-02-06 03:16:51 +08:00
|
|
|
FastImage.expects(:size).never
|
2013-08-01 05:26:34 +08:00
|
|
|
cpp.get_size("http://foo.bar/image1.png").should == nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-08-01 05:26:34 +08:00
|
|
|
it "is always allowed to crawl our own images" do
|
|
|
|
store = {}
|
|
|
|
Discourse.expects(:store).returns(store)
|
|
|
|
store.expects(:has_been_uploaded?).returns(true)
|
2013-07-08 07:39:08 +08:00
|
|
|
FastImage.expects(:size).returns([100, 200])
|
2013-08-01 05:26:34 +08:00
|
|
|
cpp.get_size("http://foo.bar/image2.png").should == [100, 200]
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-07-08 07:39:08 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it "caches the results" do
|
|
|
|
SiteSetting.stubs(:crawl_images?).returns(true)
|
|
|
|
FastImage.expects(:size).returns([200, 400])
|
2013-08-01 05:26:34 +08:00
|
|
|
cpp.get_size("http://foo.bar/image3.png")
|
|
|
|
cpp.get_size("http://foo.bar/image3.png").should == [200, 400]
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-07-07 01:10:53 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-07-07 01:10:53 +08:00
|
|
|
context "is_valid_image_uri?" do
|
2013-06-22 19:38:42 +08:00
|
|
|
|
2013-07-08 07:39:08 +08:00
|
|
|
let(:post) { build(:post) }
|
|
|
|
let(:cpp) { CookedPostProcessor.new(post) }
|
|
|
|
|
2013-06-22 19:38:42 +08:00
|
|
|
it "needs the scheme to be either http or https" do
|
|
|
|
cpp.is_valid_image_uri?("http://domain.com").should == true
|
|
|
|
cpp.is_valid_image_uri?("https://domain.com").should == true
|
|
|
|
cpp.is_valid_image_uri?("ftp://domain.com").should == false
|
|
|
|
cpp.is_valid_image_uri?("ftps://domain.com").should == false
|
|
|
|
cpp.is_valid_image_uri?("//domain.com").should == false
|
|
|
|
cpp.is_valid_image_uri?("/tmp/image.png").should == false
|
2013-06-15 18:29:20 +08:00
|
|
|
end
|
|
|
|
|
2013-07-07 01:10:53 +08:00
|
|
|
it "doesn't throw an exception with a bad URI" do
|
2013-06-25 04:56:03 +08:00
|
|
|
cpp.is_valid_image_uri?("http://do<main.com").should == nil
|
|
|
|
end
|
|
|
|
|
2013-06-15 18:29:20 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|