2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe TopicLinkClick do
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
it { is_expected.to belong_to :topic_link }
|
|
|
|
it { is_expected.to belong_to :user }
|
|
|
|
it { is_expected.to validate_presence_of :topic_link_id }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def test_uri
|
|
|
|
URI.parse('http://test.host')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'topic_links' do
|
|
|
|
before do
|
2013-02-26 00:42:20 +08:00
|
|
|
@topic = Fabricate(:topic)
|
2013-02-06 03:16:51 +08:00
|
|
|
@post = Fabricate(:post_with_external_links, user: @topic.user, topic: @topic)
|
2013-02-26 00:42:20 +08:00
|
|
|
TopicLink.extract_from(@post)
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic_link = @topic.topic_links.first
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it 'has 0 clicks at first' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@topic_link.clicks).to eq(0)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'create' do
|
|
|
|
before do
|
2013-06-25 06:30:32 +08:00
|
|
|
TopicLinkClick.create(topic_link: @topic_link, ip_address: '192.168.1.1')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates the forum topic link click' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(TopicLinkClick.count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has 0 clicks at first' do
|
|
|
|
@topic_link.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@topic_link.clicks).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'serializes and deserializes the IP' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(TopicLinkClick.first.ip_address.to_s).to eq('192.168.1.1')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'create_from' do
|
|
|
|
|
|
|
|
context 'without a url' do
|
2013-07-27 00:14:11 +08:00
|
|
|
let(:click) { TopicLinkClick.create_from(url: "url that doesn't exist", post_id: @post.id, ip: '127.0.0.1') }
|
|
|
|
|
|
|
|
it "returns nil" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(click).to eq(nil)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'clicking on your own link' do
|
|
|
|
it "should not record the click" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect {
|
2014-01-07 06:25:42 +08:00
|
|
|
TopicLinkClick.create_from(url: @topic_link.url, post_id: @post.id, ip: '127.0.0.0', user_id: @post.user_id)
|
2014-12-31 22:55:03 +08:00
|
|
|
}.not_to change(TopicLinkClick, :count)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a valid url and post_id' do
|
|
|
|
before do
|
2013-07-27 00:14:11 +08:00
|
|
|
@url = TopicLinkClick.create_from(url: @topic_link.url, post_id: @post.id, ip: '127.0.0.1')
|
2013-02-06 03:16:51 +08:00
|
|
|
@click = TopicLinkClick.last
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a click' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@click).to be_present
|
|
|
|
expect(@click.topic_link).to eq(@topic_link)
|
|
|
|
expect(@url).to eq(@topic_link.url)
|
2013-07-27 00:14:11 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context "clicking again" do
|
|
|
|
it "should not record the click due to rate limiting" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect { TopicLinkClick.create_from(url: @topic_link.url, post_id: @post.id, ip: '127.0.0.1') }.not_to change(TopicLinkClick, :count)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-21 03:01:48 +08:00
|
|
|
context "relative urls" do
|
|
|
|
let(:host) { URI.parse(Discourse.base_url).host }
|
|
|
|
|
|
|
|
it 'returns the url' do
|
|
|
|
url = TopicLinkClick.create_from(url: '/relative-url', post_id: @post.id, ip: '127.0.0.1')
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(url).to eq("/relative-url")
|
2014-11-21 03:01:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds a protocol relative urls with a host' do
|
|
|
|
url = "//#{host}/relative-url"
|
|
|
|
redirect = TopicLinkClick.create_from(url: url)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(redirect).to eq(url)
|
2014-11-21 03:01:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the url if it's on our host" do
|
|
|
|
url = "http://#{host}/relative-url"
|
|
|
|
redirect = TopicLinkClick.create_from(url: url)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(redirect).to eq(url)
|
2014-11-21 03:01:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-15 03:59:51 +08:00
|
|
|
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
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@click).to be_present
|
|
|
|
expect(@click.topic_link).to eq(@topic_link)
|
|
|
|
expect(@url).to eq('https://twitter.com')
|
2014-01-15 03:59:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'with a valid url and topic_id' do
|
|
|
|
before do
|
2014-01-07 06:25:42 +08:00
|
|
|
@url = TopicLinkClick.create_from(url: @topic_link.url, topic_id: @topic.id, ip: '127.0.0.3')
|
2013-02-06 03:16:51 +08:00
|
|
|
@click = TopicLinkClick.last
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a click' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@click).to be_present
|
|
|
|
expect(@click.topic_link).to eq(@topic_link)
|
|
|
|
expect(@url).to eq(@topic_link.url)
|
2013-07-27 00:14:11 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-01-07 06:25:42 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|