discourse/spec/components/cooked_post_processor_spec.rb

314 lines
9.6 KiB
Ruby
Raw Normal View History

2013-11-06 02:04:47 +08:00
require "spec_helper"
require "cooked_post_processor"
2013-02-06 03:16:51 +08:00
describe CookedPostProcessor do
2013-11-06 02:04:47 +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
2013-11-06 02:04:47 +08:00
cpp.expects(:keep_reverse_index_up_to_date).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)
2013-11-06 02:04:47 +08:00
cpp.expects(:optimize_urls).in_sequence(post_process)
cpp.expects(:pull_hotlinked_images).in_sequence(post_process)
2013-07-08 07:39:08 +08:00
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-11-06 02:04:47 +08:00
context ".keep_reverse_index_up_to_date" do
2013-10-14 20:27:41 +08:00
2013-11-06 02:04:47 +08:00
let(:post) { build(:post_with_uploads, id: 123) }
2013-10-14 20:27:41 +08:00
let(:cpp) { CookedPostProcessor.new(post) }
2013-11-06 02:04:47 +08:00
it "finds all the uploads in the post" do
Upload.expects(:get_from_url).with("/uploads/default/2/2345678901234567.jpg")
Upload.expects(:get_from_url).with("/uploads/default/1/1234567890123456.jpg")
cpp.keep_reverse_index_up_to_date
2013-10-14 20:27:41 +08:00
end
2013-11-06 02:04:47 +08:00
it "cleans the reverse index up for the current post" do
PostUpload.expects(:delete_all).with(post_id: post.id)
cpp.keep_reverse_index_up_to_date
2013-07-11 04:59:07 +08:00
end
end
2013-11-06 02:04:47 +08:00
context ".post_process_images" do
2013-07-08 07:39:08 +08:00
2013-11-06 02:04:47 +08:00
context "with image_sizes" do
2013-07-08 07:39:08 +08:00
let(:post) { build(:post_with_image_url) }
2013-11-06 02:04:47 +08:00
let(:cpp) { CookedPostProcessor.new(post, image_sizes: {"http://foo.bar/image.png" => {"width" => 111, "height" => 222}}) }
2013-07-08 07:39:08 +08:00
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-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)
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-11-06 02:04:47 +08:00
cpp.html.should match_html '<div><a href="/uploads/default/1/1234567890123456.jpg" class="lightbox"><img src="/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-07-08 07:39:08 +08:00
context "topic image" do
2013-07-08 07:39:08 +08:00
let(:topic) { build(:topic, id: 1) }
let(:post) { Fabricate(:post_with_uploaded_image, topic: topic) }
2013-07-08 07:39:08 +08:00
let(:cpp) { CookedPostProcessor.new(post) }
2013-07-08 07:39:08 +08:00
it "adds a topic image if there's one in the post" do
2013-11-06 02:04:47 +08:00
FastImage.stubs(:size)
post.topic.image_url.should be_nil
2013-07-08 07:39:08 +08:00
cpp.post_process_images
post.topic.reload
2013-11-06 02:04:47 +08:00
post.topic.image_url.should be_present
end
2013-07-08 07:39:08 +08:00
end
2013-07-08 07:39:08 +08:00
end
2013-06-24 08:10:21 +08:00
2013-11-06 02:04:47 +08:00
context ".extract_images" do
2013-06-24 08:10:21 +08:00
2013-11-06 02:04:47 +08:00
let(:post) { build(:post_with_images_in_quote_and_onebox) }
let(:cpp) { CookedPostProcessor.new(post) }
2013-07-08 07:39:08 +08:00
2013-11-06 02:04:47 +08:00
it "does not extract images inside oneboxes or quotes" do
cpp.extract_images.length.should == 0
2013-06-24 08:10:21 +08:00
end
2013-11-06 02:04:47 +08:00
end
context ".get_size_from_image_sizes" do
let(:post) { build(:post) }
let(:cpp) { CookedPostProcessor.new(post) }
it "returns the size" do
image_sizes = { "http://my.discourse.org/image.png" => { "width" => 111, "height" => 222 } }
cpp.get_size_from_image_sizes("/image.png", image_sizes).should == [111, 222]
2013-07-08 07:39:08 +08:00
end
2013-06-26 08:44:20 +08:00
2013-11-06 02:04:47 +08:00
end
context ".get_size" do
let(:post) { build(:post) }
let(:cpp) { CookedPostProcessor.new(post) }
it "ensures urls are absolute" do
cpp.expects(:is_valid_image_url?).with("http://test.localhost/relative/url/image.png")
cpp.get_size("/relative/url/image.png")
end
it "ensures urls have a default scheme" do
cpp.expects(:is_valid_image_url?).with("http://schemaless.url/image.jpg")
cpp.get_size("//schemaless.url/image.jpg")
end
it "caches the results" do
SiteSetting.stubs(:crawl_images?).returns(true)
FastImage.expects(:size).returns([200, 400])
cpp.get_size("http://foo.bar/image3.png")
cpp.get_size("http://foo.bar/image3.png").should == [200, 400]
end
context "when crawl_images is disabled" do
before { SiteSetting.stubs(:crawl_images?).returns(false) }
it "doesn't call FastImage" do
FastImage.expects(:size).never
cpp.get_size("http://foo.bar/image1.png").should == nil
end
it "is always allowed to crawl our own images" do
store = stub
store.expects(:has_been_uploaded?).returns(true)
Discourse.expects(:store).returns(store)
FastImage.expects(:size).returns([100, 200])
cpp.get_size("http://foo.bar/image2.png").should == [100, 200]
end
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
end
2013-06-26 08:44:20 +08:00
2013-11-06 02:04:47 +08:00
context ".is_valid_image_url?" do
let(:post) { build(:post) }
let(:cpp) { CookedPostProcessor.new(post) }
it "validates HTTP(s) urls" do
cpp.is_valid_image_url?("http://domain.com").should == true
cpp.is_valid_image_url?("https://domain.com").should == true
end
it "doesn't validate other urls" do
cpp.is_valid_image_url?("ftp://domain.com").should == false
cpp.is_valid_image_url?("ftps://domain.com").should == false
cpp.is_valid_image_url?("/tmp/image.png").should == false
cpp.is_valid_image_url?("//domain.com").should == false
end
it "doesn't throw an exception with a bad URI" do
cpp.is_valid_image_url?("http://do<main.com").should == nil
end
end
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
2013-11-06 02:04:47 +08:00
context ".post_process_oneboxes" do
2013-07-08 07:39:08 +08:00
2013-11-06 02:04:47 +08:00
let(:post) { build(:post_with_youtube, id: 123) }
let(:cpp) { CookedPostProcessor.new(post, invalidate_oneboxes: true) }
2013-11-06 02:04:47 +08:00
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-02-06 03:16:51 +08:00
end
2013-11-06 02:04:47 +08:00
it "is dirty" do
cpp.should be_dirty
end
2013-02-06 03:16:51 +08:00
2013-11-06 02:04:47 +08:00
it "inserts the onebox without wrapping p" do
cpp.html.should match_html "<div>GANGNAM STYLE</div>"
end
2013-11-06 02:04:47 +08:00
end
2013-02-06 03:16:51 +08:00
2013-11-06 02:04:47 +08:00
context ".optimize_urls" do
2013-07-08 07:39:08 +08:00
2013-11-06 02:04:47 +08:00
let(:post) { build(:post_with_uploads_and_links) }
let(:cpp) { CookedPostProcessor.new(post) }
it "uses schemaless url for uploads" do
cpp.optimize_urls
cpp.html.should match_html '<a href="//test.localhost/uploads/default/2/2345678901234567.jpg">Link</a>
<img src="//test.localhost/uploads/default/1/1234567890123456.jpg"><a href="http://www.google.com">Google</a>
<img src="http://foo.bar/image.png">'
2013-07-08 07:39:08 +08:00
end
2013-11-06 02:04:47 +08:00
context "when CDN is enabled" do
it "uses schemaless CDN url for uploads" do
Rails.configuration.action_controller.stubs(:asset_host).returns("http://my.cdn.com")
cpp.optimize_urls
cpp.html.should match_html '<a href="//my.cdn.com/uploads/default/2/2345678901234567.jpg">Link</a>
<img src="//my.cdn.com/uploads/default/1/1234567890123456.jpg"><a href="http://www.google.com">Google</a>
<img src="http://foo.bar/image.png">'
end
2013-02-06 03:16:51 +08:00
end
2013-02-06 03:16:51 +08:00
end
2013-11-06 02:04:47 +08:00
context ".pull_hotlinked_images" do
2013-07-08 07:39:08 +08:00
let(:post) { build(:post) }
let(:cpp) { CookedPostProcessor.new(post) }
2013-11-06 02:04:47 +08:00
it "does not run when crawl images is disabled" do
SiteSetting.stubs(:crawl_images).returns(false)
Jobs.expects(:cancel_scheduled_job).never
cpp.pull_hotlinked_images
end
2013-11-06 02:04:47 +08:00
context "when crawl_images? is enabled" do
before { SiteSetting.stubs(:crawl_images).returns(true) }
it "runs only when a user updated the post" do
post.updated_by = Discourse.system_user
Jobs.expects(:cancel_scheduled_job).never
cpp.pull_hotlinked_images
end
context "and the post has been updated by a user" do
before { post.id = 42 }
it "ensures only one job is scheduled right after the ninja_edit_window" do
Jobs.expects(:cancel_scheduled_job).with(:pull_hotlinked_images, post_id: post.id).once
delay = SiteSetting.ninja_edit_window + 1
Jobs.expects(:enqueue_in).with(delay.minutes, :pull_hotlinked_images, post_id: post.id).once
cpp.pull_hotlinked_images
end
end
2013-06-25 04:56:03 +08:00
end
end
2013-02-06 03:16:51 +08:00
end