2015-10-11 04:41:23 -05:00
|
|
|
require 'rails_helper'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
describe TopicLink do
|
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
it { is_expected.to validate_presence_of :url }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
def test_uri
|
|
|
|
URI.parse(Discourse.base_url)
|
|
|
|
end
|
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
let(:topic) do
|
|
|
|
Fabricate(:topic, title: 'unique topic name')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user) do
|
|
|
|
topic.user
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can't link to the same topic" do
|
2014-07-10 23:17:01 -05:00
|
|
|
ftl = TopicLink.new(url: "/t/#{topic.id}",
|
|
|
|
topic_id: topic.id,
|
|
|
|
link_topic_id: topic.id)
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(ftl.valid?).to eq(false)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'external links' do
|
|
|
|
before do
|
2014-07-10 23:17:01 -05:00
|
|
|
post = Fabricate(:post, raw: "
|
2014-06-25 20:38:23 -05:00
|
|
|
http://a.com/
|
|
|
|
http://b.com/b
|
|
|
|
http://#{'a'*200}.com/invalid
|
|
|
|
http://b.com/#{'a'*500}
|
2014-07-10 23:17:01 -05:00
|
|
|
", user: user, topic: topic)
|
2014-06-25 20:38:23 -05:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
TopicLink.extract_from(post)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-06-04 21:48:34 -05:00
|
|
|
it 'works' do
|
|
|
|
# has the forum topic links
|
2015-09-25 13:07:04 -05:00
|
|
|
expect(topic.topic_links.count).to eq(3)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-06-04 21:48:34 -05:00
|
|
|
# works with markdown links
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(topic.topic_links.exists?(url: "http://a.com/")).to eq(true)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-06-04 21:48:34 -05:00
|
|
|
#works with markdown links followed by a period
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(topic.topic_links.exists?(url: "http://b.com/b")).to eq(true)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-11 10:11:48 -06:00
|
|
|
describe 'internal links' do
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
it "extracts onebox" 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")
|
2013-02-13 14:22:04 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
url = "http://#{test_uri.host}/t/#{other_topic.slug}/#{other_topic.id}/#{other_post.post_number}"
|
|
|
|
invalid_url = "http://#{test_uri.host}/t/#{other_topic.slug}/9999999999999999999999999999999"
|
2013-08-08 17:14:12 -05:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
topic.posts.create(user: user, raw: 'initial post')
|
|
|
|
post = topic.posts.create(user: user, raw: "Link to another topic:\n\n#{url}\n\n#{invalid_url}")
|
|
|
|
post.reload
|
2013-02-13 14:22:04 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
TopicLink.extract_from(post)
|
2013-02-13 14:22:04 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
link = topic.topic_links.first
|
|
|
|
# should have a link
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(link).to be_present
|
2014-07-10 23:17:01 -05:00
|
|
|
# should be the canonical URL
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(link.url).to eq(url)
|
2013-02-13 14:22:04 -06:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
context 'topic link' do
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
let(:other_topic) do
|
|
|
|
Fabricate(:topic, user: user)
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
let(:post) do
|
|
|
|
other_topic.posts.create(user: user, raw: "some content")
|
2013-02-11 10:11:48 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-06-04 21:48:34 -05:00
|
|
|
it 'works' do
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
# ensure other_topic has a post
|
|
|
|
post
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
url = "http://#{test_uri.host}/t/#{other_topic.slug}/#{other_topic.id}"
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
topic.posts.create(user: user, raw: 'initial post')
|
|
|
|
linked_post = topic.posts.create(user: user, raw: "Link to another topic: #{url}")
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2016-06-10 02:24:30 -05:00
|
|
|
# this is subtle, but we had a bug were second time
|
|
|
|
# TopicLink.extract_from was called a reflection was nuked
|
|
|
|
2.times do
|
|
|
|
topic.reload
|
|
|
|
TopicLink.extract_from(linked_post)
|
|
|
|
|
|
|
|
link = topic.topic_links.first
|
|
|
|
expect(link).to be_present
|
|
|
|
expect(link).to be_internal
|
|
|
|
expect(link.url).to eq(url)
|
|
|
|
expect(link.domain).to eq(test_uri.host)
|
|
|
|
link.link_topic_id == other_topic.id
|
|
|
|
expect(link).not_to be_reflection
|
|
|
|
|
|
|
|
reflection = other_topic.topic_links.first
|
|
|
|
|
|
|
|
expect(reflection).to be_present
|
|
|
|
expect(reflection).to be_reflection
|
|
|
|
expect(reflection.post_id).to be_present
|
|
|
|
expect(reflection.domain).to eq(test_uri.host)
|
|
|
|
expect(reflection.url).to eq("http://#{test_uri.host}/t/unique-topic-name/#{topic.id}/#{linked_post.post_number}")
|
|
|
|
expect(reflection.link_topic_id).to eq(topic.id)
|
|
|
|
expect(reflection.link_post_id).to eq(linked_post.id)
|
|
|
|
|
|
|
|
expect(reflection.user_id).to eq(link.user_id)
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2016-06-08 15:08:41 -05:00
|
|
|
linked_post.revise(post.user, { raw: "no more linkies https://eviltrout.com" })
|
|
|
|
expect(other_topic.topic_links.where(link_post_id: linked_post.id)).to be_blank
|
2013-02-25 10:42:20 -06:00
|
|
|
end
|
2013-02-11 10:11:48 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-02-11 11:27:32 -06:00
|
|
|
context "link to a user on discourse" do
|
2014-07-10 23:17:01 -05:00
|
|
|
let(:post) { topic.posts.create(user: user, raw: "<a href='/users/#{user.username_lower}'>user</a>") }
|
2013-02-05 13:16:51 -06:00
|
|
|
before do
|
2013-02-11 10:11:48 -06:00
|
|
|
TopicLink.extract_from(post)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-02-25 10:42:20 -06:00
|
|
|
it 'does not extract a link' do
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(topic.topic_links).to be_blank
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-02-11 10:11:48 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-02-11 11:27:32 -06:00
|
|
|
context "link to a discourse resource like a FAQ" do
|
2014-07-10 23:17:01 -05:00
|
|
|
let(:post) { topic.posts.create(user: user, raw: "<a href='/faq'>faq link here</a>") }
|
2013-02-11 11:27:32 -06:00
|
|
|
before do
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
end
|
|
|
|
|
2013-02-25 10:42:20 -06:00
|
|
|
it 'does not extract a link' do
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(topic.topic_links).to be_present
|
2013-02-11 11:27:32 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
context "mention links" do
|
|
|
|
let(:post) { topic.posts.create(user: user, raw: "Hey #{user.username_lower}") }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-02-11 10:11:48 -06:00
|
|
|
before do
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-02-25 10:42:20 -06:00
|
|
|
it 'does not extract a link' do
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(topic.topic_links).to be_blank
|
2014-07-10 23:17:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-04 17:43:11 -05:00
|
|
|
context "mail link" do
|
|
|
|
let(:post) { topic.posts.create(user: user, raw: "[email]bar@example.com[/email]") }
|
|
|
|
|
|
|
|
it 'does not extract a link' do
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
expect(topic.topic_links).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
context "quote links" do
|
|
|
|
it "sets quote correctly" do
|
|
|
|
linked_post = topic.posts.create(user: user, raw: "my test post")
|
|
|
|
quoting_post = Fabricate(:post, raw: "[quote=\"#{user.username}, post: #{linked_post.post_number}, topic: #{topic.id}\"]\nquote\n[/quote]")
|
|
|
|
|
|
|
|
TopicLink.extract_from(quoting_post)
|
|
|
|
link = quoting_post.topic.topic_links.first
|
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(link.link_post_id).to eq(linked_post.id)
|
|
|
|
expect(link.quote).to eq(true)
|
2013-02-11 10:11:48 -06:00
|
|
|
end
|
2013-02-25 10:42:20 -06:00
|
|
|
end
|
|
|
|
|
2013-07-18 18:26:23 -05:00
|
|
|
context "link to a local attachments" do
|
2014-07-10 23:17:01 -05:00
|
|
|
let(:post) { topic.posts.create(user: user, raw: '<a class="attachment" href="/uploads/default/208/87bb3d8428eb4783.rb">ruby.rb</a>') }
|
2013-07-18 18:26:23 -05:00
|
|
|
|
|
|
|
it "extracts the link" do
|
|
|
|
TopicLink.extract_from(post)
|
2014-07-10 23:17:01 -05:00
|
|
|
link = topic.topic_links.first
|
2013-07-18 18:26:23 -05:00
|
|
|
# extracted the link
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(link).to be_present
|
2013-07-18 18:26:23 -05:00
|
|
|
# is set to internal
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(link).to be_internal
|
2013-07-18 18:26:23 -05:00
|
|
|
# has the correct url
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(link.url).to eq("/uploads/default/208/87bb3d8428eb4783.rb")
|
2013-07-18 18:26:23 -05:00
|
|
|
# should not be the reflection
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(link).not_to be_reflection
|
2013-07-18 18:26:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "link to an attachments uploaded on S3" do
|
2014-07-10 23:17:01 -05:00
|
|
|
let(:post) { topic.posts.create(user: user, raw: '<a class="attachment" href="//s3.amazonaws.com/bucket/2104a0211c9ce41ed67989a1ed62e9a394c1fbd1446.rb">ruby.rb</a>') }
|
2013-07-18 18:26:23 -05:00
|
|
|
|
|
|
|
it "extracts the link" do
|
|
|
|
TopicLink.extract_from(post)
|
2014-07-10 23:17:01 -05:00
|
|
|
link = topic.topic_links.first
|
2013-07-18 18:26:23 -05:00
|
|
|
# extracted the link
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(link).to be_present
|
2013-07-18 18:26:23 -05:00
|
|
|
# is not internal
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(link).not_to be_internal
|
2013-07-18 18:26:23 -05:00
|
|
|
# has the correct url
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(link.url).to eq("//s3.amazonaws.com/bucket/2104a0211c9ce41ed67989a1ed62e9a394c1fbd1446.rb")
|
2013-07-18 18:26:23 -05:00
|
|
|
# should not be the reflection
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(link).not_to be_reflection
|
2013-07-18 18:26:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-02-25 10:42:20 -06:00
|
|
|
describe 'internal link from pm' do
|
2014-07-10 23:17:01 -05:00
|
|
|
it 'works' do
|
2014-09-11 02:39:20 -05:00
|
|
|
pm = Fabricate(:topic, user: user, category_id: nil, archetype: 'private_message')
|
2014-07-10 23:17:01 -05:00
|
|
|
pm.posts.create(user: user, raw: "some content")
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
url = "http://#{test_uri.host}/t/topic-slug/#{topic.id}"
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
pm.posts.create(user: user, raw: 'initial post')
|
|
|
|
linked_post = pm.posts.create(user: user, raw: "Link to another topic: #{url}")
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-07-10 23:17:01 -05:00
|
|
|
TopicLink.extract_from(linked_post)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(topic.topic_links.first).to eq(nil)
|
|
|
|
expect(pm.topic_links.first).not_to eq(nil)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'internal link with non-standard port' do
|
|
|
|
it 'includes the non standard port if present' do
|
2014-07-10 23:17:01 -05:00
|
|
|
other_topic = Fabricate(:topic, user: user)
|
|
|
|
SiteSetting.port = 5678
|
2013-02-05 13:16:51 -06:00
|
|
|
alternate_uri = URI.parse(Discourse.base_url)
|
2014-07-10 23:17:01 -05:00
|
|
|
|
|
|
|
url = "http://#{alternate_uri.host}:5678/t/topic-slug/#{other_topic.id}"
|
2016-06-08 15:08:41 -05:00
|
|
|
post = topic.posts.create(user: user, raw: "Link to another topic: #{url}")
|
2014-07-10 23:17:01 -05:00
|
|
|
TopicLink.extract_from(post)
|
|
|
|
reflection = other_topic.topic_links.first
|
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(reflection.url).to eq("http://#{alternate_uri.host}:5678/t/unique-topic-name/#{topic.id}")
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-06 15:58:35 -05:00
|
|
|
describe 'query methods' do
|
2013-06-05 01:10:26 -05:00
|
|
|
it 'returns blank without posts' do
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(TopicLink.counts_for(Guardian.new, nil, nil)).to be_blank
|
2013-06-05 01:10:26 -05:00
|
|
|
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
|
|
|
|
|
2016-06-06 15:58:35 -05:00
|
|
|
it 'creates a valid topic lookup' do
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
|
|
|
|
lookup = TopicLink.duplicate_lookup(post.topic)
|
|
|
|
expect(lookup).to be_present
|
|
|
|
expect(lookup['google.com']).to be_present
|
|
|
|
|
|
|
|
ch = lookup['www.codinghorror.com/blog']
|
|
|
|
expect(ch).to be_present
|
|
|
|
expect(ch[:domain]).to eq('www.codinghorror.com')
|
|
|
|
expect(ch[:username]).to eq(post.username)
|
|
|
|
expect(ch[:posted_at]).to be_present
|
2016-06-09 13:31:32 -05:00
|
|
|
expect(ch[:post_number]).to be_present
|
2016-06-06 15:58:35 -05:00
|
|
|
end
|
2013-06-05 01:10:26 -05:00
|
|
|
|
|
|
|
it 'has the correct results' do
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
topic_link = post.topic.topic_links.first
|
2013-06-24 17:30:32 -05:00
|
|
|
TopicLinkClick.create(topic_link: topic_link, ip_address: '192.168.1.1')
|
2013-06-05 01:10:26 -05:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(counts_for[post.id]).to be_present
|
|
|
|
expect(counts_for[post.id].find {|l| l[:url] == 'http://google.com'}[:clicks]).to eq(0)
|
|
|
|
expect(counts_for[post.id].first[:clicks]).to eq(1)
|
2013-06-05 01:10:26 -05:00
|
|
|
|
2013-11-15 11:15:46 -06:00
|
|
|
array = TopicLink.topic_map(Guardian.new, post.topic_id)
|
2016-06-06 15:58:35 -05:00
|
|
|
expect(array.length).to eq(5)
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(array[0]["clicks"]).to eq("1")
|
2013-06-05 01:10:26 -05:00
|
|
|
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)
|
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(TopicLink.topic_map(Guardian.new, post.topic_id).count).to eq(1)
|
|
|
|
expect(TopicLink.counts_for(Guardian.new, post.topic, [post]).length).to eq(1)
|
2013-06-05 01:10:26 -05:00
|
|
|
|
2013-07-13 20:24:16 -05:00
|
|
|
category.set_permissions(:staff => :full)
|
2013-06-05 01:10:26 -05:00
|
|
|
category.save
|
|
|
|
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(TopicLink.topic_map(Guardian.new, post.topic_id).count).to eq(0)
|
|
|
|
expect(TopicLink.topic_map(Guardian.new(admin), post.topic_id).count).to eq(1)
|
2013-06-05 01:10:26 -05:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(TopicLink.counts_for(Guardian.new, post.topic, [post]).length).to eq(0)
|
|
|
|
expect(TopicLink.counts_for(Guardian.new(admin), post.topic, [post]).length).to eq(1)
|
2013-06-05 01:10:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|