2013-02-05 14:16:51 -05:00
require 'spec_helper'
describe TopicLinkClick do
2013-02-25 19:42:20 +03:00
2013-02-05 14:16:51 -05:00
it { should belong_to :topic_link }
it { should belong_to :user }
it { should validate_presence_of :topic_link_id }
def test_uri
URI . parse ( 'http://test.host' )
end
context 'topic_links' do
before do
2013-02-25 19:42:20 +03:00
@topic = Fabricate ( :topic )
2013-02-05 14:16:51 -05:00
@post = Fabricate ( :post_with_external_links , user : @topic . user , topic : @topic )
2013-02-25 19:42:20 +03:00
TopicLink . extract_from ( @post )
2013-02-05 14:16:51 -05:00
@topic_link = @topic . topic_links . first
2013-02-25 19:42:20 +03:00
end
2013-02-05 14:16:51 -05:00
it 'has 0 clicks at first' do
@topic_link . clicks . should == 0
end
context 'create' do
before do
2013-06-24 18:30:32 -04:00
TopicLinkClick . create ( topic_link : @topic_link , ip_address : '192.168.1.1' )
2013-02-05 14:16:51 -05:00
end
it 'creates the forum topic link click' do
TopicLinkClick . count . should == 1
end
it 'has 0 clicks at first' do
@topic_link . reload
@topic_link . clicks . should == 1
end
it 'serializes and deserializes the IP' do
2013-06-24 18:30:32 -04:00
TopicLinkClick . first . ip_address . to_s . should == '192.168.1.1'
2013-02-05 14:16:51 -05:00
end
end
context 'create_from' do
context 'without a url' do
2013-07-26 12:14:11 -04: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-09-25 17:44:48 +02:00
click . should == nil
2013-02-05 14:16:51 -05:00
end
end
context 'clicking on your own link' do
it " should not record the click " do
lambda {
2014-01-07 09:25:42 +11:00
TopicLinkClick . create_from ( url : @topic_link . url , post_id : @post . id , ip : '127.0.0.0' , user_id : @post . user_id )
2013-02-05 14:16:51 -05:00
} . should_not change ( TopicLinkClick , :count )
end
end
context 'with a valid url and post_id' do
before do
2013-07-26 12:14:11 -04:00
@url = TopicLinkClick . create_from ( url : @topic_link . url , post_id : @post . id , ip : '127.0.0.1' )
2013-02-05 14:16:51 -05:00
@click = TopicLinkClick . last
end
it 'creates a click' do
@click . should be_present
@click . topic_link . should == @topic_link
2013-07-26 12:14:11 -04:00
@url . should == @topic_link . url
end
2013-02-05 14:16:51 -05:00
context " clicking again " do
it " should not record the click due to rate limiting " do
- > { TopicLinkClick . create_from ( url : @topic_link . url , post_id : @post . id , ip : '127.0.0.1' ) } . should_not change ( TopicLinkClick , :count )
end
end
end
2014-01-14 14:59:51 -05: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
@click . should be_present
@click . topic_link . should == @topic_link
@url . should == 'https://twitter.com'
end
end
2013-02-05 14:16:51 -05:00
context 'with a valid url and topic_id' do
before do
2014-01-07 09:25:42 +11:00
@url = TopicLinkClick . create_from ( url : @topic_link . url , topic_id : @topic . id , ip : '127.0.0.3' )
2013-02-05 14:16:51 -05:00
@click = TopicLinkClick . last
end
it 'creates a click' do
@click . should be_present
@click . topic_link . should == @topic_link
2013-07-26 12:14:11 -04:00
@url . should == @topic_link . url
end
2013-02-05 14:16:51 -05:00
2014-01-07 09:25:42 +11:00
end
2013-02-05 14:16:51 -05:00
end
end
end