FIX: Invalid URLs cause post not to save

This commit is contained in:
Sam 2014-06-26 11:38:23 +10:00
parent 6527862195
commit dd8a06187a
2 changed files with 19 additions and 5 deletions

View File

@ -2,6 +2,9 @@ require 'uri'
require_dependency 'slug'
class TopicLink < ActiveRecord::Base
MAX_DOMAIN_LENGTH = 100 unless defined? MAX_DOMAIN_LENGTH
MAX_URL_LENGTH = 500 unless defined? MAX_URL_LENGTH
belongs_to :topic
belongs_to :user
belongs_to :post
@ -126,6 +129,7 @@ class TopicLink < ActiveRecord::Base
# Store the canonical URL
topic = Topic.find_by(id: topic_id)
topic_id = nil unless topic
if topic.present?
url = "#{Discourse.base_url}#{topic.relative_url}"
@ -142,6 +146,9 @@ class TopicLink < ActiveRecord::Base
reflected_post = Post.find_by(topic_id: topic_id, post_number: post_number.to_i)
end
next if url && url.length > MAX_URL_LENGTH
next if parsed && parsed.host && parsed.host.length > MAX_DOMAIN_LENGTH
added_urls << url
TopicLink.create(post_id: post.id,
user_id: post.user_id,

View File

@ -26,19 +26,25 @@ describe TopicLink do
describe 'external links' do
before do
@post = Fabricate(:post_with_external_links, user: @user, topic: @topic)
@post = Fabricate(:post, raw: "
http://a.com/
http://b.com/b
http://#{'a'*200}.com/invalid
http://b.com/#{'a'*500}
", user: @user, topic: @topic)
TopicLink.extract_from(@post)
end
it 'works' do
# has the forum topic links
@topic.topic_links.count.should == 4
@topic.topic_links.count.should == 2
# works with markdown links
@topic.topic_links.exists?(url: "http://forumwarz.com").should be_true
@topic.topic_links.exists?(url: "http://a.com/").should be_true
#works with markdown links followed by a period
@topic.topic_links.exists?(url: "http://www.codinghorror.com/blog").should be_true
@topic.topic_links.exists?(url: "http://b.com/b").should be_true
end
end
@ -53,9 +59,10 @@ describe TopicLink do
@other_post = @other_topic.posts.create(user: @user, raw: "some content for the second post")
@url = "http://#{test_uri.host}/t/#{@other_topic.slug}/#{@other_topic.id}/#{@other_post.post_number}"
@invalid_url = "http://#{test_uri.host}/t/#{@other_topic.slug}/9999999999999999999999999999999"
@topic.posts.create(user: @user, raw: 'initial post')
@post = @topic.posts.create(user: @user, raw: "Link to another topic:\n\n#{@url}\n\n")
@post = @topic.posts.create(user: @user, raw: "Link to another topic:\n\n#{@url}\n\n#{@invalid_url}")
@post.reload
TopicLink.extract_from(@post)