discourse/spec/models/incoming_link_spec.rb

88 lines
2.3 KiB
Ruby
Raw Normal View History

2013-02-05 13:16:51 -06:00
require 'spec_helper'
describe IncomingLink do
it { should belong_to :topic }
it { should validate_presence_of :url }
let :post do
Fabricate(:post)
end
2013-02-05 13:16:51 -06:00
let :topic do
post.topic
end
2013-02-05 13:16:51 -06:00
let :incoming_link do
IncomingLink.create(url: "/t/slug/#{topic.id}/#{post.post_number}",
2013-02-05 13:16:51 -06:00
referer: "http://twitter.com")
end
2013-02-05 13:16:51 -06:00
describe 'local topic link' do
2013-02-05 13:16:51 -06:00
it 'should validate properly' do
Fabricate.build(:incoming_link).should be_valid
end
2013-02-05 13:16:51 -06: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 13:16:51 -06:00
end
end
2013-02-05 13:16:51 -06: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 13:16:51 -06:00
end
end
end
describe 'add' do
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
it "does nothing if referer is empty" do
IncomingLink.expects(:create).never
IncomingLink.add(req('http://somesite.com'))
end
2013-02-05 13:16:51 -06:00
it "does nothing if referer is same as host" do
IncomingLink.expects(:create).never
IncomingLink.add(req('http://somesite.com', 'http://somesite.com'))
2013-02-05 13:16:51 -06:00
end
it "expects to be called with referer and user id" do
IncomingLink.expects(:create).once.returns(true)
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 13:16:51 -06:00
end
end
2013-02-05 13:16:51 -06:00
describe 'non-topic url' do
it 'has nothing set' do
link = Fabricate(:incoming_link_not_topic)
link.topic_id.should be_blank
link.user_id.should be_blank
2013-02-05 13:16:51 -06:00
end
end
end