From 8acd9acb08944c92f34e264f7a744c160e1f3bf2 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Tue, 11 Oct 2011 16:00:10 -0700 Subject: [PATCH] tests moved and passing --- app/helpers/o_embed_helper.rb | 9 +- spec/helpers/markdownify_helper_spec.rb | 129 ------------------------ spec/helpers/o_embed_helper_spec.rb | 128 +++++++++++++++++++++++ spec/models/post_spec.rb | 11 -- spec/models/status_message_spec.rb | 8 ++ 5 files changed, 141 insertions(+), 144 deletions(-) create mode 100644 spec/helpers/o_embed_helper_spec.rb diff --git a/app/helpers/o_embed_helper.rb b/app/helpers/o_embed_helper.rb index aa6559faa..70d489e62 100644 --- a/app/helpers/o_embed_helper.rb +++ b/app/helpers/o_embed_helper.rb @@ -1,9 +1,10 @@ module OEmbedHelper def o_embed_html(cache) data = cache.data - title = data.fetch('title', 'an awesome post') - html ||= link_to(title, cache.url, :target => '_blank') - return nil unless data.has_key?('type') + data = {} if data.blank? + title = data.fetch('title', cache.url) + html = link_to(title, cache.url, :target => '_blank') + return html unless data.has_key?('type') case data['type'] when 'video', 'rich' if cache.is_trusted_and_has_html? @@ -27,6 +28,6 @@ module OEmbedHelper end def oembed_image_tag(cache, prefix) - image_tag(cache.data[prefix + 'url'], cache.image_options_hash(prefix)) + image_tag(cache.data[prefix + 'url'], cache.options_hash(prefix)) end end diff --git a/spec/helpers/markdownify_helper_spec.rb b/spec/helpers/markdownify_helper_spec.rb index ca1641b96..ed73c7d1b 100644 --- a/spec/helpers/markdownify_helper_spec.rb +++ b/spec/helpers/markdownify_helper_spec.rb @@ -58,135 +58,6 @@ describe MarkdownifyHelper do formatted = markdownify(message) formatted.should =~ /hovercard/ end - - context 'when posting a link with oEmbed support' do - scenarios = { - "photo" => { - "oembed_data" => { - "trusted_endpoint_url" => "__!SPOOFED!__", - "version" => "1.0", - "type" => "photo", - "title" => "ZB8T0193", - "width" => "240", - "height" => "160", - "url" => "http://farm4.static.flickr.com/3123/2341623661_7c99f48bbf_m.jpg" - }, - "link_url" => 'http://www.flickr.com/photos/bees/2341623661', - "oembed_get_request" => "http://www.flickr.com/services/oembed/?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://www.flickr.com/photos/bees/2341623661", - }, - - "unsupported" => { - "oembed_data" => "", - "oembed_get_request" => 'http://www.we-do-not-support-oembed.com/index.html', - "link_url" => 'http://www.we-do-not-support-oembed.com/index.html' - }, - - "secure_video" => { - "oembed_data" => { - "version" => "1.0", - "type" => "video", - "width" => 425, - "height" => 344, - "title" => "Amazing Nintendo Facts", - "html" => " - - - - - ", - }, - "link_url" => "http://youtube.com/watch?v=M3r2XDceM6A&format=json", - "oembed_get_request" => "http://www.youtube.com/oembed?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://youtube.com/watch?v=M3r2XDceM6A", - }, - - "unsecure_video" => { - "oembed_data" => { - "version" => "1.0", - "type" => "video", - "title" => "This is a video from an unsecure source", - "html" => " - - - - - ", - }, - "link_url" => "http://mytube.com/watch?v=M3r2XDceM6A&format=json", - "discovery_data" => '', - "oembed_get_request" => "http://www.mytube.com/oembed?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://mytube.com/watch?v=M3r2XDceM6A", - }, - - "secure_rich" => { - "oembed_data" => { - "version" => "1.0", - "type" => "rich", - "width" => 425, - "height" => 344, - "title" => "Amazing Nintendo Facts", - "html" => " - - - - - ", - }, - "link_url" => "http://youtube.com/watch?v=M3r2XDceM6A&format=json", - "oembed_get_request" => "http://www.youtube.com/oembed?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://youtube.com/watch?v=M3r2XDceM6A", - }, - - "unsecure_rich" => { - "oembed_data" => { - "version" => "1.0", - "type" => "rich", - "title" => "This is a video from an unsecure source", - "html" => " - - - - - ", - }, - "link_url" => "http://mytube.com/watch?v=M3r2XDceM6A&format=json", - "discovery_data" => '', - "oembed_get_request" => "http://www.mytube.com/oembed?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://mytube.com/watch?v=M3r2XDceM6A", - }, - } - - scenarios.each do |type, data| - specify 'for type "'+type+'"' do - url = - stub_request(:get, data['link_url']).to_return(:status => 200, :body => data['discovery_data']) if data.has_key?('discovery_data') - stub_request(:get, data['oembed_get_request']).to_return(:status => 200, :body => data['oembed_data'].to_json.to_s) - - message = "Look at this! "+data['link_url'] - Jobs::GatherOEmbedData.perform(message) - OEmbedCache.find_by_url(data['link_url']).should_not be_nil unless type == 'unsupported' - - formatted = markdownify(message, :oembed => true) - case type - when 'photo' - formatted.should =~ /#{data['oembed_data']['url']}/ - when 'unsupported' - formatted.should =~ /#{data['link_url']}/ - when 'secure_video', 'secure_rich' - formatted.should =~ /#{data['oembed_data']['html']}/ - when 'unsecure_video', 'unsecure_rich' - formatted.should_not =~ /#{data['oembed_data']['html']}/ - formatted.should =~ /#{data['oembed_data']['title']}/ - formatted.should =~ /#{data['oembed_data']['url']}/ - end - end - end - - end end end end diff --git a/spec/helpers/o_embed_helper_spec.rb b/spec/helpers/o_embed_helper_spec.rb new file mode 100644 index 000000000..b984e8d51 --- /dev/null +++ b/spec/helpers/o_embed_helper_spec.rb @@ -0,0 +1,128 @@ +require 'spec_helper' + +describe OEmbedHelper do + describe 'o_embed_html' do + scenarios = { + "photo" => { + "oembed_data" => { + "trusted_endpoint_url" => "__!SPOOFED!__", + "version" => "1.0", + "type" => "photo", + "title" => "ZB8T0193", + "width" => "240", + "height" => "160", + "url" => "http://farm4.static.flickr.com/3123/2341623661_7c99f48bbf_m.jpg" + }, + "link_url" => 'http://www.flickr.com/photos/bees/2341623661', + "oembed_get_request" => "http://www.flickr.com/services/oembed/?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://www.flickr.com/photos/bees/2341623661", + }, + + "unsupported" => { + "oembed_data" => "", + "oembed_get_request" => 'http://www.we-do-not-support-oembed.com/index.html', + "link_url" => 'http://www.we-do-not-support-oembed.com/index.html' + }, + + "secure_video" => { + "oembed_data" => { + "version" => "1.0", + "type" => "video", + "width" => 425, + "height" => 344, + 'trusted_endpoint_url' => ::OEmbed::Providers::Youtube.endpoint, + "title" => "Amazing Nintendo Facts", + "html" => " + + + + + ", + }, + "link_url" => "http://youtube.com/watch?v=M3r2XDceM6A&format=json", + "oembed_get_request" => "http://www.youtube.com/oembed?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://youtube.com/watch?v=M3r2XDceM6A", + }, + + "unsecure_video" => { + "oembed_data" => { + "version" => "1.0", + "type" => "video", + "title" => "This is a video from an unsecure source", + "html" => " + + + + + ", + }, + "link_url" => "http://mytube.com/watch?v=M3r2XDceM6A&format=json", + "discovery_data" => '', + "oembed_get_request" => "http://www.mytube.com/oembed?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://mytube.com/watch?v=M3r2XDceM6A", + }, + + "secure_rich" => { + "oembed_data" => { + "version" => "1.0", + "type" => "rich", + "width" => 425, + "height" => 344, + 'trusted_endpoint_url' => ::OEmbed::Providers::Youtube.endpoint, + "title" => "Amazing Nintendo Facts", + "html" => " + + + + + ", + }, + "link_url" => "http://youtube.com/watch?v=M3r2XDceM6A&format=json", + "oembed_get_request" => "http://www.youtube.com/oembed?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://youtube.com/watch?v=M3r2XDceM6A", + }, + + "unsecure_rich" => { + "oembed_data" => { + "version" => "1.0", + "type" => "rich", + "title" => "This is a video from an unsecure source", + "html" => " + + + + + ", + }, + "link_url" => "http://mytube.com/watch?v=M3r2XDceM6A&format=json", + "discovery_data" => '', + "oembed_get_request" => "http://www.mytube.com/oembed?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://mytube.com/watch?v=M3r2XDceM6A", + }, + } + + scenarios.each do |type, data| + specify 'for type "'+type+'"' do + real_data = data['oembed_data'] + cache = OEmbedCache.new(:url => data['link_url']) + cache.data = real_data + formatted = o_embed_html(cache) + case type + when 'photo' + formatted.should =~ /#{data['oembed_data']['url']}/ + when 'unsupported' + formatted.should =~ /#{data['link_url']}/ + when 'secure_video', 'secure_rich' + formatted.should =~ /#{data['oembed_data']['html']}/ + when 'unsecure_video', 'unsecure_rich' + formatted.should_not =~ /#{data['oembed_data']['html']}/ + formatted.should =~ /#{data['oembed_data']['title']}/ + formatted.should =~ /#{data['oembed_data']['url']}/ + end + end + end + end +end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 10a769bd8..fade3ab7a 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -68,17 +68,6 @@ describe Post do end end - describe 'oEmbed' do - it 'should gather oEmbed data' do - stub_request(:get, "http://gdata.youtube.com/feeds/api/videos/M3r2XDceM6A?v=2").to_return(:status => 200, :body => "") - - text = 'http://youtube.com/watch?v=M3r2XDceM6A&format=json' - Resque.should_receive(:enqueue).with(Jobs::GatherOEmbedData, text) - post = Factory.create(:status_message, :author => @user.person, :text => text) - post.save! - end - end - describe 'serialization' do it 'should serialize the handle and not the sender' do post = @user.post :status_message, :text => "hello", :to => @aspect.id diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index 5f1e1a4ac..005e0afd0 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -320,4 +320,12 @@ STR sm.oembed_url.should == 'http://youtube.com' end end + + describe 'oembed' do + it 'should queue a GatherOembedData if it includes a link' do + sm = Factory.build(:status_message, :text => 'http://youtube.com is so cool. so is https://joindiaspora.com') + Resque.should_receive(:enqueue).with(Jobs::GatherOEmbedData, instance_of(Fixnum), instance_of(String)) + sm.save + end + end end