tests moved and passing
This commit is contained in:
parent
cfb28db00f
commit
8acd9acb08
5 changed files with 141 additions and 144 deletions
|
|
@ -1,9 +1,10 @@
|
||||||
module OEmbedHelper
|
module OEmbedHelper
|
||||||
def o_embed_html(cache)
|
def o_embed_html(cache)
|
||||||
data = cache.data
|
data = cache.data
|
||||||
title = data.fetch('title', 'an awesome post')
|
data = {} if data.blank?
|
||||||
html ||= link_to(title, cache.url, :target => '_blank')
|
title = data.fetch('title', cache.url)
|
||||||
return nil unless data.has_key?('type')
|
html = link_to(title, cache.url, :target => '_blank')
|
||||||
|
return html unless data.has_key?('type')
|
||||||
case data['type']
|
case data['type']
|
||||||
when 'video', 'rich'
|
when 'video', 'rich'
|
||||||
if cache.is_trusted_and_has_html?
|
if cache.is_trusted_and_has_html?
|
||||||
|
|
@ -27,6 +28,6 @@ module OEmbedHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def oembed_image_tag(cache, prefix)
|
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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -58,135 +58,6 @@ describe MarkdownifyHelper do
|
||||||
formatted = markdownify(message)
|
formatted = markdownify(message)
|
||||||
formatted.should =~ /hovercard/
|
formatted.should =~ /hovercard/
|
||||||
end
|
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" => "<object width=\"425\" height=\"344\">
|
|
||||||
<param name=\"movie\" value=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"></param>
|
|
||||||
<param name=\"allowFullScreen\" value=\"true\"></param>
|
|
||||||
<param name=\"allowscriptaccess\" value=\"always\"></param>
|
|
||||||
<embed src=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"
|
|
||||||
type=\"application/x-shockwave-flash\" width=\"425\" height=\"344\"
|
|
||||||
allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed>
|
|
||||||
</object>",
|
|
||||||
},
|
|
||||||
"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" => "<object width=\"425\" height=\"344\">
|
|
||||||
<param name=\"movie\" value=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"></param>
|
|
||||||
<param name=\"allowFullScreen\" value=\"true\"></param>
|
|
||||||
<param name=\"allowscriptaccess\" value=\"always\"></param>
|
|
||||||
<embed src=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"
|
|
||||||
type=\"application/x-shockwave-flash\" width=\"425\" height=\"344\"
|
|
||||||
allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed>
|
|
||||||
</object>",
|
|
||||||
},
|
|
||||||
"link_url" => "http://mytube.com/watch?v=M3r2XDceM6A&format=json",
|
|
||||||
"discovery_data" => '<link rel="alternate" type="application/json+oembed" href="http://www.mytube.com/oembed?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://mytube.com/watch?v=M3r2XDceM6A" />',
|
|
||||||
"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" => "<object width=\"425\" height=\"344\">
|
|
||||||
<param name=\"movie\" value=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"></param>
|
|
||||||
<param name=\"allowFullScreen\" value=\"true\"></param>
|
|
||||||
<param name=\"allowscriptaccess\" value=\"always\"></param>
|
|
||||||
<embed src=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"
|
|
||||||
type=\"application/x-shockwave-flash\" width=\"425\" height=\"344\"
|
|
||||||
allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed>
|
|
||||||
</object>",
|
|
||||||
},
|
|
||||||
"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" => "<object width=\"425\" height=\"344\">
|
|
||||||
<param name=\"movie\" value=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"></param>
|
|
||||||
<param name=\"allowFullScreen\" value=\"true\"></param>
|
|
||||||
<param name=\"allowscriptaccess\" value=\"always\"></param>
|
|
||||||
<embed src=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"
|
|
||||||
type=\"application/x-shockwave-flash\" width=\"425\" height=\"344\"
|
|
||||||
allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed>
|
|
||||||
</object>",
|
|
||||||
},
|
|
||||||
"link_url" => "http://mytube.com/watch?v=M3r2XDceM6A&format=json",
|
|
||||||
"discovery_data" => '<link rel="alternate" type="application/json+oembed" href="http://www.mytube.com/oembed?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://mytube.com/watch?v=M3r2XDceM6A" />',
|
|
||||||
"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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
128
spec/helpers/o_embed_helper_spec.rb
Normal file
128
spec/helpers/o_embed_helper_spec.rb
Normal file
|
|
@ -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" => "<object width=\"425\" height=\"344\">
|
||||||
|
<param name=\"movie\" value=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"></param>
|
||||||
|
<param name=\"allowFullScreen\" value=\"true\"></param>
|
||||||
|
<param name=\"allowscriptaccess\" value=\"always\"></param>
|
||||||
|
<embed src=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"
|
||||||
|
type=\"application/x-shockwave-flash\" width=\"425\" height=\"344\"
|
||||||
|
allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed>
|
||||||
|
</object>",
|
||||||
|
},
|
||||||
|
"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" => "<object width=\"425\" height=\"344\">
|
||||||
|
<param name=\"movie\" value=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"></param>
|
||||||
|
<param name=\"allowFullScreen\" value=\"true\"></param>
|
||||||
|
<param name=\"allowscriptaccess\" value=\"always\"></param>
|
||||||
|
<embed src=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"
|
||||||
|
type=\"application/x-shockwave-flash\" width=\"425\" height=\"344\"
|
||||||
|
allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed>
|
||||||
|
</object>",
|
||||||
|
},
|
||||||
|
"link_url" => "http://mytube.com/watch?v=M3r2XDceM6A&format=json",
|
||||||
|
"discovery_data" => '<link rel="alternate" type="application/json+oembed" href="http://www.mytube.com/oembed?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://mytube.com/watch?v=M3r2XDceM6A" />',
|
||||||
|
"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" => "<object width=\"425\" height=\"344\">
|
||||||
|
<param name=\"movie\" value=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"></param>
|
||||||
|
<param name=\"allowFullScreen\" value=\"true\"></param>
|
||||||
|
<param name=\"allowscriptaccess\" value=\"always\"></param>
|
||||||
|
<embed src=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"
|
||||||
|
type=\"application/x-shockwave-flash\" width=\"425\" height=\"344\"
|
||||||
|
allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed>
|
||||||
|
</object>",
|
||||||
|
},
|
||||||
|
"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" => "<object width=\"425\" height=\"344\">
|
||||||
|
<param name=\"movie\" value=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"></param>
|
||||||
|
<param name=\"allowFullScreen\" value=\"true\"></param>
|
||||||
|
<param name=\"allowscriptaccess\" value=\"always\"></param>
|
||||||
|
<embed src=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"
|
||||||
|
type=\"application/x-shockwave-flash\" width=\"425\" height=\"344\"
|
||||||
|
allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed>
|
||||||
|
</object>",
|
||||||
|
},
|
||||||
|
"link_url" => "http://mytube.com/watch?v=M3r2XDceM6A&format=json",
|
||||||
|
"discovery_data" => '<link rel="alternate" type="application/json+oembed" href="http://www.mytube.com/oembed?format=json&frame=1&iframe=1&maxheight=420&maxwidth=420&url=http://mytube.com/watch?v=M3r2XDceM6A" />',
|
||||||
|
"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
|
||||||
|
|
@ -68,17 +68,6 @@ describe Post do
|
||||||
end
|
end
|
||||||
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
|
describe 'serialization' do
|
||||||
it 'should serialize the handle and not the sender' do
|
it 'should serialize the handle and not the sender' do
|
||||||
post = @user.post :status_message, :text => "hello", :to => @aspect.id
|
post = @user.post :status_message, :text => "hello", :to => @aspect.id
|
||||||
|
|
|
||||||
|
|
@ -320,4 +320,12 @@ STR
|
||||||
sm.oembed_url.should == 'http://youtube.com'
|
sm.oembed_url.should == 'http://youtube.com'
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue