2013-02-05 14:16:51 -05:00
require 'spec_helper'
require 'cooked_post_processor'
describe CookedPostProcessor do
2013-04-10 17:52:38 +10:00
def cpp ( cooked = nil , options = { } )
2013-02-19 17:57:14 +11:00
post = Fabricate . build ( :post_with_youtube )
2013-04-10 17:52:38 +10:00
post . cooked = cooked if cooked
2013-02-19 17:57:14 +11:00
post . id = 123
2013-04-10 17:52:38 +10:00
CookedPostProcessor . new ( post , options )
2013-02-19 17:57:14 +11:00
end
2013-02-05 14:16:51 -05:00
context 'process_onebox' do
before do
2013-04-10 17:52:38 +10:00
@cpp = cpp ( nil , invalidate_oneboxes : true )
Oneboxer . expects ( :onebox ) . with ( " http://www.youtube.com/watch?v=9bZkp7q19f0 " , post_id : 123 , invalidate_oneboxes : true ) . returns ( '<div>GANGNAM STYLE</div>' )
2013-02-05 14:16:51 -05:00
@cpp . post_process_oneboxes
end
it 'should be dirty' do
@cpp . should be_dirty
end
2013-04-10 17:52:38 +10:00
it 'inserts the onebox without wrapping p' do
@cpp . html . should match_html " <div>GANGNAM STYLE</div> "
2013-02-05 14:16:51 -05:00
end
end
2013-04-10 17:52:38 +10:00
2013-02-05 14:16:51 -05:00
context 'process_images' do
it " has no topic image if there isn't one in the post " do
@post = Fabricate ( :post )
@post . topic . image_url . should be_blank
end
context 'with sized images in the post' do
before do
@topic = Fabricate ( :topic )
@post = Fabricate . build ( :post_with_image_url , topic : @topic , user : @topic . user )
2013-03-23 20:32:59 +05:30
@cpp = CookedPostProcessor . new ( @post , image_sizes : { 'http://www.forumwarz.com/images/header/logo.png' = > { 'width' = > 111 , 'height' = > 222 } } )
2013-02-19 17:57:14 +11:00
@cpp . expects ( :get_size ) . returns ( [ 111 , 222 ] )
2013-02-05 14:16:51 -05:00
end
it " doesn't call image_dimensions because it knows the size " do
2013-02-19 17:57:14 +11:00
@cpp . expects ( :image_dimensions ) . never
2013-02-05 14:16:51 -05:00
@cpp . post_process_images
end
it " adds the width from the image sizes provided " do
@cpp . post_process_images
@cpp . html . should =~ / width= \ "111 \ " /
end
end
2013-06-17 22:46:48 +02:00
context 'with uploaded images in the post' do
before do
@topic = Fabricate ( :topic )
@post = Fabricate ( :post_with_uploads , topic : @topic , user : @topic . user )
@cpp = CookedPostProcessor . new ( @post )
@cpp . expects ( :get_upload_from_url ) . returns ( Fabricate ( :upload ) )
@cpp . expects ( :get_size ) . returns ( [ 100 , 200 ] )
end
it " keeps reverse index up to date " do
@cpp . post_process_images
@post . uploads . reload
@post . uploads . count . should == 1
end
end
2013-02-05 14:16:51 -05:00
context 'with unsized images in the post' do
2013-03-18 13:55:34 -04:00
let ( :user ) { Fabricate ( :user ) }
let ( :topic ) { Fabricate ( :topic , user : user ) }
2013-02-05 14:16:51 -05:00
before do
2013-02-19 17:57:14 +11:00
FastImage . stubs ( :size ) . returns ( [ 123 , 456 ] )
2013-03-18 13:55:34 -04:00
creator = PostCreator . new ( user , raw : Fabricate . build ( :post_with_images ) . raw , topic_id : topic . id )
@post = creator . create
2013-02-05 14:16:51 -05:00
end
2013-02-25 19:42:20 +03:00
it " adds a topic image if there's one in the post " do
2013-02-05 14:16:51 -05:00
@post . topic . reload
2013-04-13 16:31:20 +02:00
@post . topic . image_url . should == " http://test.localhost/path/to/img.jpg "
2013-02-05 14:16:51 -05:00
end
it " adds the height and width to images that don't have them " do
@post . reload
@post . cooked . should =~ / width= \ "123 \ " height= \ "456 \ " /
end
end
2013-05-30 17:17:07 +02:00
context 'with an absolute image path without protocol' do
let ( :user ) { Fabricate ( :user ) }
let ( :topic ) { Fabricate ( :topic , user : user ) }
let ( :post ) { Fabricate . build ( :post_with_s3_image_url , topic : topic , user : user ) }
let ( :processor ) { CookedPostProcessor . new ( post ) }
before do
processor . post_process_images
end
it " doesn't change the protocol " do
processor . html . should =~ / src=" \/ \/ bucket \ .s3 \ .amazonaws \ .com \/ uploads \/ 6 \/ 4 \/ 123 \ .png" /
end
end
2013-06-24 02:10:21 +02:00
context 'with a oneboxed image' do
let ( :user ) { Fabricate ( :user ) }
let ( :topic ) { Fabricate ( :topic , user : user ) }
let ( :post ) { Fabricate . build ( :post_with_oneboxed_image , topic : topic , user : user ) }
let ( :processor ) { CookedPostProcessor . new ( post ) }
before do
processor . post_process_images
end
it " doesn't lightbox " do
processor . html . should_not =~ / class="lightbox" /
end
end
2013-06-26 02:44:20 +02:00
context " with a large image " do
let ( :user ) { Fabricate ( :user ) }
let ( :topic ) { Fabricate ( :topic , user : user ) }
let ( :post ) { Fabricate . build ( :post_with_uploads , topic : topic , user : user ) }
let ( :processor ) { CookedPostProcessor . new ( post ) }
before do
FastImage . stubs ( :size ) . returns ( [ 1000 , 1000 ] )
processor . post_process_images
end
it " generates overlay information " do
processor . html . should =~ / class="lightbox" /
processor . html . should =~ / class="meta" /
processor . html . should =~ / class="filename" /
processor . html . should =~ / class="informations" /
processor . html . should =~ / class="expand" /
end
end
2013-02-05 14:16:51 -05:00
end
2013-02-25 19:42:20 +03:00
context 'link convertor' do
before do
2013-02-19 17:57:14 +11:00
SiteSetting . stubs ( :crawl_images? ) . returns ( true )
end
2013-02-25 19:42:20 +03:00
let :post_with_img do
2013-02-19 17:57:14 +11:00
Fabricate . build ( :post , cooked : '<p><img src="http://hello.com/image.png"></p>' )
end
2013-02-25 19:42:20 +03:00
let :cpp_for_post do
2013-02-19 17:57:14 +11:00
CookedPostProcessor . new ( post_with_img )
end
2013-02-25 19:42:20 +03:00
it 'convert img tags to links if they are sized down' do
2013-02-19 17:57:14 +11:00
cpp_for_post . expects ( :get_size ) . returns ( [ 2000 , 2000 ] ) . twice
2013-02-25 19:42:20 +03:00
cpp_for_post . post_process
2013-02-19 17:57:14 +11:00
cpp_for_post . html . should =~ / a href /
end
2013-02-25 19:42:20 +03:00
it 'does not convert img tags to links if they are small' do
2013-02-19 17:57:14 +11:00
cpp_for_post . expects ( :get_size ) . returns ( [ 200 , 200 ] ) . twice
2013-02-25 19:42:20 +03:00
cpp_for_post . post_process
2013-02-19 17:57:14 +11:00
( cpp_for_post . html !~ / a href / ) . should be_true
end
2013-02-25 19:42:20 +03:00
2013-02-19 17:57:14 +11:00
end
2013-02-05 14:16:51 -05:00
context 'image_dimensions' do
it " returns unless called with a http or https url " do
2013-02-19 17:57:14 +11:00
cpp . image_dimensions ( '/tmp/image.jpg' ) . should be_blank
2013-02-05 14:16:51 -05:00
end
context 'with valid url' do
before do
@url = 'http://www.forumwarz.com/images/header/logo.png'
end
it " doesn't call fastimage if image crawling is disabled " do
SiteSetting . expects ( :crawl_images? ) . returns ( false )
FastImage . expects ( :size ) . never
2013-02-19 17:57:14 +11:00
cpp . image_dimensions ( @url )
2013-02-05 14:16:51 -05:00
end
it " calls fastimage if image crawling is enabled " do
SiteSetting . expects ( :crawl_images? ) . returns ( true )
FastImage . expects ( :size ) . with ( @url )
2013-02-19 17:57:14 +11:00
cpp . image_dimensions ( @url )
2013-02-25 19:42:20 +03:00
end
2013-02-05 14:16:51 -05:00
end
end
2013-06-22 13:38:42 +02:00
context 'is_valid_image_uri?' do
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 12:29:20 +02:00
end
2013-06-24 22:56:03 +02:00
it " doesn't throw exception with a bad URI " do
cpp . is_valid_image_uri? ( " http://do<main.com " ) . should == nil
end
2013-06-15 12:29:20 +02:00
end
2013-06-26 21:53:31 +02:00
context 'get_filename' do
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 "
end
it " returns the original filename of the upload when there is an upload " do
upload = Fabricate . build ( :upload , { original_filename : " upload.jpg " } )
cpp . get_filename ( upload , " http://domain.com/image.png " ) . should == " upload.jpg "
end
it " returns a generic name for pasted images " do
2013-07-04 00:39:23 +02:00
upload = Fabricate . build ( :upload , { original_filename : " blob.png " } )
2013-06-26 21:53:31 +02:00
cpp . get_filename ( upload , " http://domain.com/image.png " ) . should == I18n . t ( 'upload.pasted_image_filename' )
end
end
2013-02-05 14:16:51 -05:00
end