2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe TopicLink do
|
|
|
|
|
|
|
|
it { should belong_to :topic }
|
|
|
|
it { should belong_to :post }
|
|
|
|
it { should belong_to :user }
|
|
|
|
it { should have_many :topic_link_clicks }
|
|
|
|
it { should validate_presence_of :url }
|
|
|
|
|
|
|
|
def test_uri
|
|
|
|
URI.parse(Discourse.base_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2013-02-25 11:42:20 -05:00
|
|
|
@topic = Fabricate(:topic, title: 'unique topic name')
|
2013-02-05 14:16:51 -05:00
|
|
|
@user = @topic.user
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can't link to the same topic" do
|
2013-02-25 11:42:20 -05:00
|
|
|
ftl = TopicLink.new(url: "/t/#{@topic.id}",
|
|
|
|
topic_id: @topic.id,
|
2013-02-05 14:16:51 -05:00
|
|
|
link_topic_id: @topic.id)
|
|
|
|
ftl.valid?.should be_false
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'external links' do
|
|
|
|
before do
|
2014-06-25 21:38:23 -04:00
|
|
|
@post = Fabricate(:post, raw: "
|
|
|
|
http://a.com/
|
|
|
|
http://b.com/b
|
|
|
|
http://#{'a'*200}.com/invalid
|
|
|
|
http://b.com/#{'a'*500}
|
|
|
|
", user: @user, topic: @topic)
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
TopicLink.extract_from(@post)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-06-04 22:48:34 -04:00
|
|
|
it 'works' do
|
|
|
|
# has the forum topic links
|
2014-06-25 21:38:23 -04:00
|
|
|
@topic.topic_links.count.should == 2
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-04 22:48:34 -04:00
|
|
|
# works with markdown links
|
2014-06-25 21:38:23 -04:00
|
|
|
@topic.topic_links.exists?(url: "http://a.com/").should be_true
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-04 22:48:34 -04:00
|
|
|
#works with markdown links followed by a period
|
2014-06-25 21:38:23 -04:00
|
|
|
@topic.topic_links.exists?(url: "http://b.com/b").should be_true
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-11 11:11:48 -05:00
|
|
|
describe 'internal links' do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-13 15:22:04 -05:00
|
|
|
context "rendered onebox" do
|
|
|
|
|
|
|
|
before do
|
|
|
|
@other_topic = Fabricate(:topic, user: @user)
|
|
|
|
@other_topic.posts.create(user: @user, raw: "some content for the first post")
|
|
|
|
@other_post = @other_topic.posts.create(user: @user, raw: "some content for the second post")
|
|
|
|
|
|
|
|
@url = "http://#{test_uri.host}/t/#{@other_topic.slug}/#{@other_topic.id}/#{@other_post.post_number}"
|
2014-06-25 21:38:23 -04:00
|
|
|
@invalid_url = "http://#{test_uri.host}/t/#{@other_topic.slug}/9999999999999999999999999999999"
|
2013-02-13 15:22:04 -05:00
|
|
|
|
|
|
|
@topic.posts.create(user: @user, raw: 'initial post')
|
2014-06-25 21:38:23 -04:00
|
|
|
@post = @topic.posts.create(user: @user, raw: "Link to another topic:\n\n#{@url}\n\n#{@invalid_url}")
|
2013-02-13 15:22:04 -05:00
|
|
|
@post.reload
|
2013-08-08 18:14:12 -04:00
|
|
|
|
2013-02-13 15:22:04 -05:00
|
|
|
TopicLink.extract_from(@post)
|
|
|
|
|
|
|
|
@link = @topic.topic_links.first
|
|
|
|
end
|
|
|
|
|
2013-06-04 22:48:34 -04:00
|
|
|
it 'works' do
|
|
|
|
# should have a link
|
2013-02-13 15:22:04 -05:00
|
|
|
@link.should be_present
|
2013-06-04 22:48:34 -04:00
|
|
|
# should be the canonical URL
|
2013-02-13 15:22:04 -05:00
|
|
|
@link.url.should == @url
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-13 15:22:04 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-11 11:11:48 -05:00
|
|
|
context 'topic link' do
|
|
|
|
before do
|
|
|
|
@other_topic = Fabricate(:topic, user: @user)
|
|
|
|
@other_post = @other_topic.posts.create(user: @user, raw: "some content")
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-13 15:22:04 -05:00
|
|
|
@url = "http://#{test_uri.host}/t/#{@other_topic.slug}/#{@other_topic.id}"
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-11 11:11:48 -05:00
|
|
|
@topic.posts.create(user: @user, raw: 'initial post')
|
|
|
|
@post = @topic.posts.create(user: @user, raw: "Link to another topic: #{@url}")
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-11 11:11:48 -05:00
|
|
|
TopicLink.extract_from(@post)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-11 11:11:48 -05:00
|
|
|
@link = @topic.topic_links.first
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-04 22:48:34 -04:00
|
|
|
it 'works' do
|
|
|
|
# extracted the link
|
2013-02-11 11:11:48 -05:00
|
|
|
@link.should be_present
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-04 22:48:34 -04:00
|
|
|
# is set to internal
|
2013-02-11 11:11:48 -05:00
|
|
|
@link.should be_internal
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-04 22:48:34 -04:00
|
|
|
# has the correct url
|
2013-02-11 11:11:48 -05:00
|
|
|
@link.url.should == @url
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-04 22:48:34 -04:00
|
|
|
# has the extracted domain
|
2013-02-11 11:11:48 -05:00
|
|
|
@link.domain.should == test_uri.host
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-04 22:48:34 -04:00
|
|
|
# should have the id of the linked forum
|
2013-02-11 11:11:48 -05:00
|
|
|
@link.link_topic_id == @other_topic.id
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-04 22:48:34 -04:00
|
|
|
# should not be the reflection
|
2013-02-11 11:11:48 -05:00
|
|
|
@link.should_not be_reflection
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-11 11:11:48 -05:00
|
|
|
describe 'reflection in the other topic' do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-11 11:11:48 -05:00
|
|
|
before do
|
|
|
|
@reflection = @other_topic.topic_links.first
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-04 22:48:34 -04:00
|
|
|
it 'works' do
|
|
|
|
# exists
|
2013-02-11 11:11:48 -05:00
|
|
|
@reflection.should be_present
|
|
|
|
@reflection.should be_reflection
|
|
|
|
@reflection.post_id.should be_present
|
|
|
|
@reflection.domain.should == test_uri.host
|
|
|
|
@reflection.url.should == "http://#{test_uri.host}/t/unique-topic-name/#{@topic.id}/#{@post.post_number}"
|
|
|
|
@reflection.link_topic_id.should == @topic.id
|
|
|
|
@reflection.link_post_id.should == @post.id
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-04 22:48:34 -04:00
|
|
|
#has the user id of the original link
|
2013-02-11 11:11:48 -05:00
|
|
|
@reflection.user_id.should == @link.user_id
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-11 11:11:48 -05:00
|
|
|
context 'removing a link' do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-11 11:11:48 -05:00
|
|
|
before do
|
|
|
|
@post.revise(@post.user, "no more linkies")
|
|
|
|
TopicLink.extract_from(@post)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-11 11:11:48 -05:00
|
|
|
it 'should remove the link' do
|
|
|
|
@topic.topic_links.where(post_id: @post.id).should be_blank
|
2013-06-04 22:48:34 -04:00
|
|
|
# should remove the reflected link
|
2013-02-11 11:11:48 -05:00
|
|
|
@reflection = @other_topic.topic_links.should be_blank
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-11 11:11:48 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-11 12:27:32 -05:00
|
|
|
context "link to a user on discourse" do
|
2013-02-11 11:11:48 -05:00
|
|
|
let(:post) { @topic.posts.create(user: @user, raw: "<a href='/users/#{@user.username_lower}'>user</a>") }
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2013-02-11 11:11:48 -05:00
|
|
|
TopicLink.extract_from(post)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'does not extract a link' do
|
2013-02-11 11:11:48 -05:00
|
|
|
@topic.topic_links.should be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-11 11:11:48 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-11 12:27:32 -05:00
|
|
|
context "link to a discourse resource like a FAQ" do
|
|
|
|
let(:post) { @topic.posts.create(user: @user, raw: "<a href='/faq'>faq link here</a>") }
|
|
|
|
before do
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'does not extract a link' do
|
2013-02-11 12:27:32 -05:00
|
|
|
@topic.topic_links.should be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-11 11:11:48 -05:00
|
|
|
context "@mention links" do
|
|
|
|
let(:post) { @topic.posts.create(user: @user, raw: "Hey @#{@user.username_lower}") }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-11 11:11:48 -05:00
|
|
|
before do
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'does not extract a link' do
|
2013-02-11 11:11:48 -05:00
|
|
|
@topic.topic_links.should be_blank
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
|
|
|
|
2013-07-18 19:26:23 -04:00
|
|
|
context "link to a local attachments" do
|
|
|
|
let(:post) { @topic.posts.create(user: @user, raw: '<a class="attachment" href="/uploads/default/208/87bb3d8428eb4783.rb">ruby.rb</a>') }
|
|
|
|
|
|
|
|
it "extracts the link" do
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
link = @topic.topic_links.first
|
|
|
|
# extracted the link
|
|
|
|
link.should be_present
|
|
|
|
# is set to internal
|
|
|
|
link.should be_internal
|
|
|
|
# has the correct url
|
|
|
|
link.url.should == "/uploads/default/208/87bb3d8428eb4783.rb"
|
|
|
|
# should not be the reflection
|
|
|
|
link.should_not be_reflection
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "link to an attachments uploaded on S3" do
|
|
|
|
let(:post) { @topic.posts.create(user: @user, raw: '<a class="attachment" href="//s3.amazonaws.com/bucket/2104a0211c9ce41ed67989a1ed62e9a394c1fbd1446.rb">ruby.rb</a>') }
|
|
|
|
|
|
|
|
it "extracts the link" do
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
link = @topic.topic_links.first
|
|
|
|
# extracted the link
|
|
|
|
link.should be_present
|
|
|
|
# is not internal
|
|
|
|
link.should_not be_internal
|
|
|
|
# has the correct url
|
|
|
|
link.url.should == "//s3.amazonaws.com/bucket/2104a0211c9ce41ed67989a1ed62e9a394c1fbd1446.rb"
|
|
|
|
# should not be the reflection
|
|
|
|
link.should_not be_reflection
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe 'internal link from pm' do
|
|
|
|
before do
|
2013-02-05 14:16:51 -05:00
|
|
|
@pm = Fabricate(:topic, user: @user, archetype: 'private_message')
|
|
|
|
@other_post = @pm.posts.create(user: @user, raw: "some content")
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
@url = "http://#{test_uri.host}/t/topic-slug/#{@topic.id}"
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
@pm.posts.create(user: @user, raw: 'initial post')
|
|
|
|
@linked_post = @pm.posts.create(user: @user, raw: "Link to another topic: #{@url}")
|
|
|
|
|
|
|
|
TopicLink.extract_from(@linked_post)
|
|
|
|
|
|
|
|
@link = @topic.topic_links.first
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not create a reflection' do
|
|
|
|
@topic.topic_links.first.should be_nil
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'should not create a normal link' do
|
|
|
|
@pm.topic_links.first.should_not be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'internal link with non-standard port' do
|
|
|
|
it 'includes the non standard port if present' do
|
|
|
|
@other_topic = Fabricate(:topic, user: @user)
|
|
|
|
SiteSetting.stubs(:port).returns(5678)
|
|
|
|
alternate_uri = URI.parse(Discourse.base_url)
|
|
|
|
@url = "http://#{alternate_uri.host}:5678/t/topic-slug/#{@other_topic.id}"
|
|
|
|
@post = @topic.posts.create(user: @user,
|
|
|
|
raw: "Link to another topic: #{@url}")
|
|
|
|
TopicLink.extract_from(@post)
|
|
|
|
@reflection = @other_topic.topic_links.first
|
|
|
|
@reflection.url.should == "http://#{alternate_uri.host}:5678/t/unique-topic-name/#{@topic.id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-15 12:15:46 -05:00
|
|
|
describe 'counts_for and topic_map' do
|
2013-06-05 02:10:26 -04:00
|
|
|
it 'returns blank without posts' do
|
|
|
|
TopicLink.counts_for(Guardian.new, nil, nil).should be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with data' do
|
|
|
|
|
|
|
|
let(:post) do
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
Fabricate(:post_with_external_links, user: topic.user, topic: topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:counts_for) do
|
|
|
|
TopicLink.counts_for(Guardian.new, post.topic, [post])
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
it 'has the correct results' do
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
topic_link = post.topic.topic_links.first
|
2013-06-24 18:30:32 -04:00
|
|
|
TopicLinkClick.create(topic_link: topic_link, ip_address: '192.168.1.1')
|
2013-06-05 02:10:26 -04:00
|
|
|
|
|
|
|
counts_for[post.id].should be_present
|
|
|
|
counts_for[post.id].find {|l| l[:url] == 'http://google.com'}[:clicks].should == 0
|
|
|
|
counts_for[post.id].first[:clicks].should == 1
|
|
|
|
|
2013-11-15 12:15:46 -05:00
|
|
|
array = TopicLink.topic_map(Guardian.new, post.topic_id)
|
2013-06-05 02:10:26 -04:00
|
|
|
array.length.should == 4
|
|
|
|
array[0]["clicks"].should == "1"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'secures internal links correctly' do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
secret_topic = Fabricate(:topic, category: category)
|
|
|
|
|
|
|
|
url = "http://#{test_uri.host}/t/topic-slug/#{secret_topic.id}"
|
|
|
|
post = Fabricate(:post, raw: "hello test topic #{url}")
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
|
2013-11-15 12:15:46 -05:00
|
|
|
TopicLink.topic_map(Guardian.new, post.topic_id).count.should == 1
|
2013-06-05 02:10:26 -04:00
|
|
|
TopicLink.counts_for(Guardian.new, post.topic, [post]).length.should == 1
|
|
|
|
|
2013-07-13 21:24:16 -04:00
|
|
|
category.set_permissions(:staff => :full)
|
2013-06-05 02:10:26 -04:00
|
|
|
category.save
|
|
|
|
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
|
2013-11-15 12:15:46 -05:00
|
|
|
TopicLink.topic_map(Guardian.new, post.topic_id).count.should == 0
|
|
|
|
TopicLink.topic_map(Guardian.new(admin), post.topic_id).count.should == 1
|
2013-06-05 02:10:26 -04:00
|
|
|
|
|
|
|
TopicLink.counts_for(Guardian.new, post.topic, [post]).length.should == 0
|
|
|
|
TopicLink.counts_for(Guardian.new(admin), post.topic, [post]).length.should == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|