FIX: Accept HTTPS or HTTP urls on redirect

This commit is contained in:
Robin Ward 2014-01-14 14:59:51 -05:00
parent 8e5b736caa
commit 4f6283ba56
2 changed files with 23 additions and 1 deletions

View File

@ -11,8 +11,17 @@ class TopicLinkClick < ActiveRecord::Base
# Create a click from a URL and post_id
def self.create_from(args={})
# Find the forum topic link
# If the URL is absolute, allow HTTPS and HTTP versions of it
if args[:url] =~ /^http/
http_url = args[:url].sub(/^https/, 'http')
https_url = args[:url].sub(/^http[^s]/, 'https')
link = TopicLink.select([:id, :user_id]).where('url = ? OR url = ?', http_url, https_url)
else
link = TopicLink.select([:id, :user_id]).where(url: args[:url])
end
# Find the forum topic link
link = link.where(post_id: args[:post_id]) if args[:post_id].present?
# If we don't have a post, just find the first occurance of the link

View File

@ -79,6 +79,19 @@ describe TopicLinkClick do
end
end
context 'with a HTTPS version of the same URL' do
before do
@url = TopicLinkClick.create_from(url: 'https://twitter.com', topic_id: @topic.id, ip: '127.0.0.3')
@click = TopicLinkClick.last
end
it 'creates a click' do
@click.should be_present
@click.topic_link.should == @topic_link
@url.should == 'https://twitter.com'
end
end
context 'with a valid url and topic_id' do
before do
@url = TopicLinkClick.create_from(url: @topic_link.url, topic_id: @topic.id, ip: '127.0.0.3')