alert('XSS is evil')
\n" - end - - it 'strips onClick handlers from links' do - omghax = '[XSS](http://joindiaspora.com/" onClick="$\(\'a\'\).remove\(\))' - markdownify(omghax).should_not match(/ onClick/i) - end - end - - it 'does not barf if message is nil' do - markdownify(nil).should == '' - end - - it 'autolinks standard url links' do - markdownified = markdownify("http://joindiaspora.com/") - - doc = Nokogiri.parse(markdownified) - - link = doc.css("a") - - link.attr("href").value.should == "http://joindiaspora.com/" - end - - context 'when formatting status messages' do - it "should leave tags intact" do - message = FactoryGirl.create(:status_message, - :author => alice.person, - :text => "I love #markdown") - formatted = markdownify(message) - formatted.should =~ %r{#markdown} - end - - it 'should leave multi-underscore tags intact' do - message = FactoryGirl.create( - :status_message, - :author => alice.person, - :text => "Here is a #multi_word tag" - ) - formatted = markdownify(message) - formatted.should =~ %r{Here is a #multi_word tag} - - message = FactoryGirl.create( - :status_message, - :author => alice.person, - :text => "Here is a #multi_word_tag yo" - ) - formatted = markdownify(message) - formatted.should =~ %r{Here is a #multi_word_tag yo} - end - - it "should leave mentions intact" do - message = FactoryGirl.create(:status_message, - :author => alice.person, - :text => "Hey @{Bob; #{bob.diaspora_handle}}!") - formatted = markdownify(message) - formatted.should =~ /hovercard/ - end - - it "should leave mentions intact for real diaspora handles" do - new_person = FactoryGirl.create(:person, :diaspora_handle => 'maxwell@joindiaspora.com') - message = FactoryGirl.create(:status_message, - :author => alice.person, - :text => "Hey @{maxwell@joindiaspora.com; #{new_person.diaspora_handle}}!") - formatted = markdownify(message) - formatted.should =~ /hovercard/ - end - - it 'should process text with both a hashtag and a link' do - message = FactoryGirl.create(:status_message, - :author => alice.person, - :text => "Test #tag?\nhttps://joindiaspora.com\n") - formatted = markdownify(message) - formatted.should == %{Test #tag?
\nhttps://joindiaspora.com
alert('XSS is evil')
\n" + end + + it 'strips onClick handlers from links' do + expect( + message('[XSS](http://joindiaspora.com/" onClick="$\(\'a\'\).remove\(\))').markdownified + ).to_not match(/ onClick/i) + end + end + + it 'does not barf if message is nil' do + expect(message(nil).markdownified).to eq '' + end + + it 'autolinks standard url links' do + expect( + message("http://joindiaspora.com/" + ).markdownified).to include 'href="http://joindiaspora.com/"' + end + + context 'when formatting status messages' do + it "should leave tags intact" do + expect( + message("I love #markdown").markdownified + ).to match %r{#markdown} + end + + it 'should leave multi-underscore tags intact' do + expect( + message("Here is a #multi_word tag").markdownified + ).to match %r{Here is a #multi_word tag} + + expect( + message("Here is a #multi_word_tag yo").markdownified + ).to match %r{Here is a #multi_word_tag yo} + end + + it "should leave mentions intact" do + expect( + message("Hey @{Bob; #{bob.diaspora_handle}}!", mentioned_people: [bob.person]).markdownified + ).to match(/hovercard/) + end + + it "should leave mentions intact for real diaspora handles" do + new_person = FactoryGirl.create(:person, diaspora_handle: 'maxwell@joindiaspora.com') + expect( + message( + "Hey @{maxwell@joindiaspora.com; #{new_person.diaspora_handle}}!", + mentioned_people: [new_person] + ).markdownified + ).to match(/hovercard/) + end + + it 'should process text with both a hashtag and a link' do + expect( + message("Test #tag?\nhttps://joindiaspora.com\n").markdownified + ).to eq %{Test #tag?
\nhttps://joindiaspora.com
#{entities}
\n" + end + end + end + + describe "#plain_text_without_markdown" do + it 'does not remove markdown in links' do + text = "some text and here comes http://exampe.org/foo_bar_baz a link" + expect(message(text).plain_text_without_markdown).to eq text + end + + it 'does not destroy hashtag that starts a line' do + text = "#hashtag message" + expect(message(text).plain_text_without_markdown).to eq text + end + end +end diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index 47b52139e..1f59a4bb2 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -1,9 +1,6 @@ require 'spec_helper' describe Notifier do - include ActionView::Helpers::TextHelper - include MarkdownifyHelper - let(:person) { FactoryGirl.create(:person) } before do @@ -120,7 +117,7 @@ describe Notifier do end it 'BODY: contains the truncated original post' do - @mail.body.encoded.should include(@sm.formatted_message) + @mail.body.encoded.should include(@sm.message.plain_text) end it 'BODY: contains the name of person liking' do @@ -150,7 +147,7 @@ describe Notifier do end it 'BODY: contains the truncated original post' do - @mail.body.encoded.should include(@sm.formatted_message) + @mail.body.encoded.should include(@sm.message.plain_text) end it 'BODY: contains the name of person liking' do @@ -224,7 +221,7 @@ describe Notifier do end it 'SUBJECT: has a snippet of the post contents, without markdown and without newlines' do - comment_mail.subject.should == "Re: Headline It's really sunny outside today, and this is a super long ..." + comment_mail.subject.should == "Re: Headline" end context 'BODY' do @@ -265,7 +262,7 @@ describe Notifier do end it 'SUBJECT: has a snippet of the post contents, without markdown and without newlines' do - comment_mail.subject.should == "Re: Headline It's really sunny outside today, and this is a super long ..." + comment_mail.subject.should == "Re: Headline" end context 'BODY' do diff --git a/spec/models/services/facebook_spec.rb b/spec/models/services/facebook_spec.rb index 2c1d9d882..221ea5a3c 100644 --- a/spec/models/services/facebook_spec.rb +++ b/spec/models/services/facebook_spec.rb @@ -25,17 +25,17 @@ describe Services::Facebook do end it 'removes text formatting markdown from post text' do - message = "Text with some **bolded** and _italic_ parts." - post = double(:text => message, :photos => []) + message = double + message.should_receive(:plain_text_without_markdown).and_return("") + post = double(message: message, photos: []) post_params = @service.create_post_params(post) - post_params[:message].should match "Text with some bolded and italic parts." end it 'does not add post link when no photos' do - message = "Text with some **bolded** and _italic_ parts." - post = double(:text => message, :photos => []) + message = "Some text." + post = double(message: double(plain_text_without_markdown: message), photos: []) post_params = @service.create_post_params(post) - post_params[:message].should match "Text with some bolded and italic parts." + post_params[:message].should_not include "http" end it 'sets facebook id on post' do diff --git a/spec/models/services/twitter_spec.rb b/spec/models/services/twitter_spec.rb index 463ae8957..5a6bfecbd 100644 --- a/spec/models/services/twitter_spec.rb +++ b/spec/models/services/twitter_spec.rb @@ -38,9 +38,10 @@ describe Services::Twitter do end it 'removes text formatting markdown from post text' do - message = "Text with some **bolded** and _italic_ parts." - post = double(:text => message, :photos => []) - @service.send(:build_twitter_post, post).should match "Text with some bolded and italic parts." + message = double + message.should_receive(:plain_text_without_markdown).and_return("") + post = double(message: message, photos: []) + @service.send(:build_twitter_post, post) end end @@ -53,19 +54,19 @@ describe Services::Twitter do it "should not truncate a short message" do short_message = SecureRandom.hex(20) - short_post = double(:text => short_message, :photos => []) + short_post = double(message: double(plain_text_without_markdown: short_message), photos: []) @service.send(:build_twitter_post, short_post).should match short_message end it "should truncate a long message" do long_message = SecureRandom.hex(220) - long_post = double(:text => long_message, :id => 1, :photos => []) + long_post = double(message: double(plain_text_without_markdown: long_message), id: 1, photos: []) @service.send(:build_twitter_post, long_post).length.should be < long_message.length end it "should not truncate a long message with an http url" do long_message = " http://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + @long_message_end - long_post = double(:text => long_message, :id => 1, :photos => []) + long_post = double(message: double(plain_text_without_markdown: long_message), id: 1, photos: []) @post.text = long_message answer = @service.send(:build_twitter_post, @post) @@ -74,7 +75,7 @@ describe Services::Twitter do it "should not cut links when truncating a post" do long_message = SecureRandom.hex(40) + " http://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + SecureRandom.hex(55) - long_post = double(:text => long_message, :id => 1, :photos => []) + long_post = double(message: double(plain_text_without_markdown: long_message), id: 1, photos: []) answer = @service.send(:build_twitter_post, long_post) answer.should match /\.\.\./ @@ -83,7 +84,7 @@ describe Services::Twitter do it "should append the otherwise-cut link when truncating a post" do long_message = "http://joindiaspora.com/a-very-long-decoy-url.html " + SecureRandom.hex(20) + " http://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + SecureRandom.hex(55) + " http://joindiaspora.com/a-very-long-decoy-url-part-2.html" - long_post = double(:text => long_message, :id => 1, :photos => []) + long_post = double(message: double(plain_text_without_markdown: long_message), id: 1, photos: []) answer = @service.send(:build_twitter_post, long_post) answer.should match /\.\.\./ @@ -99,7 +100,7 @@ describe Services::Twitter do it "should truncate a long message with an ftp url" do long_message = @long_message_start + " ftp://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + @long_message_end - long_post = double(:text => long_message, :id => 1, :photos => []) + long_post = double(message: double(plain_text_without_markdown: long_message), id: 1, photos: []) answer = @service.send(:build_twitter_post, long_post) answer.should match /\.\.\./ @@ -107,7 +108,7 @@ describe Services::Twitter do it "should not truncate a message of maximum length" do exact_size_message = SecureRandom.hex(70) - exact_size_post = double(:text => exact_size_message, :id => 1, :photos => []) + exact_size_post = double(message: double(plain_text_without_markdown: exact_size_message), id: 1, photos: []) answer = @service.send(:build_twitter_post, exact_size_post) answer.should match exact_size_message diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index 7ee8b4bc5..221bd4966 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -150,18 +150,6 @@ STR @sm = FactoryGirl.create(:status_message, :text => @test_string ) end - describe '#formatted_message' do - it 'escapes the message' do - xss = " " - @sm.text << xss - - @sm.formatted_message.should_not include xss - end - it 'is html_safe' do - @sm.formatted_message.html_safe?.should be_true - end - end - describe '#create_mentions' do it 'creates a mention for everyone mentioned in the message' do Diaspora::Mentionable.should_receive(:people_from_string).and_return(@people) diff --git a/spec/presenters/post_presenter_spec.rb b/spec/presenters/post_presenter_spec.rb index dca62936e..19d2bdafb 100644 --- a/spec/presenters/post_presenter_spec.rb +++ b/spec/presenters/post_presenter_spec.rb @@ -65,32 +65,17 @@ describe PostPresenter do describe '#title' do context 'with posts with text' do - context 'with a Markdown header of less than 200 characters on first line'do - it 'returns atx style header' do - @sm = double(:text => "## My title\n Post content...") - @presenter.post = @sm - @presenter.title.should == "## My title" - end - - it 'returns setext style header' do - @sm = double(:text => "My title \n======\n Post content...") - @presenter.post = @sm - @presenter.title.should == "My title \n======" - end - end - - context 'without a Markdown header of less than 200 characters on first line 'do - it 'truncates post to the 20 first characters' do - @sm = double(:text => "Very, very, very long post") - @presenter.post = @sm - @presenter.title.should == "Very, very, very ..." - end + it "delegates to message.title" do + message = double(present?: true) + message.should_receive(:title) + @presenter.post = double(message: message) + @presenter.title end end context 'with posts without text' do it ' displays a messaage with the post class' do - @sm = double(:text => "", :author => bob.person, :author_name => bob.person.name) + @sm = double(message: double(present?: false), author: bob.person, author_name: bob.person.name) @presenter.post = @sm @presenter.title.should == "A post from #{@sm.author.name}" end