2014-01-01 03:37:43 +08:00
require 'spec_helper'
describe TopicEmbed do
2014-12-31 22:55:03 +08:00
it { is_expected . to belong_to :topic }
it { is_expected . to belong_to :post }
it { is_expected . to validate_presence_of :embed_url }
2014-01-01 03:37:43 +08:00
context '.import' do
let ( :user ) { Fabricate ( :user ) }
let ( :title ) { " How to turn a fish from good to evil in 30 seconds " }
let ( :url ) { 'http://eviltrout.com/123' }
let ( :contents ) { " hello world new post <a href='/hello'>hello</a> <img src='/images/wat.jpg'> " }
2015-08-19 05:15:46 +08:00
let! ( :embeddable_host ) { Fabricate ( :embeddable_host ) }
2014-01-01 03:37:43 +08:00
it " returns nil when the URL is malformed " do
2014-12-31 22:55:03 +08:00
expect ( TopicEmbed . import ( user , " invalid url " , title , contents ) ) . to eq ( nil )
expect ( TopicEmbed . count ) . to eq ( 0 )
2014-01-01 03:37:43 +08:00
end
context 'creation of a post' do
let! ( :post ) { TopicEmbed . import ( user , url , title , contents ) }
it " works as expected with a new URL " do
2014-12-31 22:55:03 +08:00
expect ( post ) . to be_present
2014-01-01 03:37:43 +08:00
# It uses raw_html rendering
2014-12-31 22:55:03 +08:00
expect ( post . cook_method ) . to eq ( Post . cook_methods [ :raw_html ] )
expect ( post . cooked ) . to eq ( post . raw )
2014-01-01 03:37:43 +08:00
# It converts relative URLs to absolute
2014-12-31 22:55:03 +08:00
expect ( post . cooked . start_with? ( " hello world new post <a href= \" http://eviltrout.com/hello \" >hello</a> <img src= \" http://eviltrout.com/images/wat.jpg \" > " ) ) . to eq ( true )
2014-01-01 03:37:43 +08:00
2014-12-31 22:55:03 +08:00
expect ( post . topic . has_topic_embed? ) . to eq ( true )
expect ( TopicEmbed . where ( topic_id : post . topic_id ) ) . to be_present
2015-08-19 05:15:46 +08:00
expect ( post . topic . category ) . to eq ( embeddable_host . category )
2014-01-01 03:37:43 +08:00
end
it " Supports updating the post " do
post = TopicEmbed . import ( user , url , title , " muhahaha new contents! " )
2014-12-31 22:55:03 +08:00
expect ( post . cooked ) . to match ( / new contents / )
2014-01-01 03:37:43 +08:00
end
2014-03-27 11:24:57 +08:00
it " Should leave uppercase Feed Entry URL untouched in content " do
cased_url = 'http://eviltrout.com/ABCD'
post = TopicEmbed . import ( user , cased_url , title , " some random content " )
2014-12-31 22:55:03 +08:00
expect ( post . cooked ) . to match ( / #{ cased_url } / )
2014-03-27 11:24:57 +08:00
end
it " Should leave lowercase Feed Entry URL untouched in content " do
cased_url = 'http://eviltrout.com/abcd'
post = TopicEmbed . import ( user , cased_url , title , " some random content " )
2014-12-31 22:55:03 +08:00
expect ( post . cooked ) . to match ( / #{ cased_url } / )
2014-03-27 11:24:57 +08:00
end
2014-01-01 03:37:43 +08:00
end
end
end