2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe IncomingLink do
|
|
|
|
|
|
|
|
it { should belong_to :topic }
|
|
|
|
it { should validate_presence_of :url }
|
|
|
|
|
2013-06-25 10:15:41 -04:00
|
|
|
let(:post) { Fabricate(:post) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-25 10:15:41 -04:00
|
|
|
let(:topic) { post.topic }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-24 04:05:35 -04:00
|
|
|
let :incoming_link do
|
|
|
|
IncomingLink.create(url: "/t/slug/#{topic.id}/#{post.post_number}",
|
2013-02-05 14:16:51 -05:00
|
|
|
referer: "http://twitter.com")
|
2013-04-24 04:05:35 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-24 04:05:35 -04:00
|
|
|
describe 'local topic link' do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-24 04:05:35 -04:00
|
|
|
it 'should validate properly' do
|
|
|
|
Fabricate.build(:incoming_link).should be_valid
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-24 04:05:35 -04:00
|
|
|
describe 'tracking link counts' do
|
|
|
|
it "increases the incoming link counts" do
|
|
|
|
incoming_link
|
|
|
|
lambda { post.reload }.should change(post, :incoming_link_count).by(1)
|
|
|
|
lambda { topic.reload }.should change(topic, :incoming_link_count).by(1)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-04-24 04:05:35 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-24 04:05:35 -04:00
|
|
|
describe 'saving local link' do
|
|
|
|
it 'has correct info set' do
|
|
|
|
incoming_link.domain.should == "twitter.com"
|
|
|
|
incoming_link.topic_id.should == topic.id
|
|
|
|
incoming_link.post_number.should == post.post_number
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-24 04:05:35 -04:00
|
|
|
describe 'add' do
|
2013-04-26 02:18:41 -04:00
|
|
|
class TestRequest<Rack::Request
|
|
|
|
attr_accessor :remote_ip
|
|
|
|
end
|
|
|
|
def req(url, referer=nil)
|
|
|
|
env = Rack::MockRequest.env_for(url)
|
|
|
|
env['HTTP_REFERER'] = referer if referer
|
|
|
|
TestRequest.new(env)
|
|
|
|
end
|
|
|
|
|
2013-07-05 00:23:29 -04:00
|
|
|
it "does not explode with bad referer" do
|
|
|
|
IncomingLink.add(req('http://sam.com','file:///Applications/Install/75067ABC-C9D1-47B7-8ACE-76AEDE3911B2/Install/'))
|
|
|
|
end
|
|
|
|
|
2013-04-24 04:05:35 -04:00
|
|
|
it "does nothing if referer is empty" do
|
|
|
|
IncomingLink.expects(:create).never
|
2013-04-26 02:18:41 -04:00
|
|
|
IncomingLink.add(req('http://somesite.com'))
|
2013-04-24 04:05:35 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-24 04:05:35 -04:00
|
|
|
it "does nothing if referer is same as host" do
|
|
|
|
IncomingLink.expects(:create).never
|
2013-04-26 02:18:41 -04:00
|
|
|
IncomingLink.add(req('http://somesite.com', 'http://somesite.com'))
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-04-24 04:05:35 -04:00
|
|
|
it "expects to be called with referer and user id" do
|
|
|
|
IncomingLink.expects(:create).once.returns(true)
|
2013-04-26 02:18:41 -04:00
|
|
|
IncomingLink.add(req('http://somesite.com', 'http://some.other.site.com'), build(:user))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is able to look up user_id and log it from the GET params" do
|
|
|
|
user = Fabricate(:user, username: "Bob")
|
|
|
|
IncomingLink.add(req('http://somesite.com?u=bob'))
|
|
|
|
first = IncomingLink.first
|
|
|
|
first.user_id.should == user.id
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-04-24 04:05:35 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-24 04:05:35 -04:00
|
|
|
describe 'non-topic url' do
|
|
|
|
it 'has nothing set' do
|
2013-06-25 10:15:41 -04:00
|
|
|
link = Fabricate.build(:incoming_link_not_topic)
|
2013-04-24 04:05:35 -04:00
|
|
|
link.topic_id.should be_blank
|
|
|
|
link.user_id.should be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|