2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe TopicLink do
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
it { is_expected.to validate_presence_of :url }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def test_uri
|
|
|
|
URI.parse(Discourse.base_url)
|
|
|
|
end
|
|
|
|
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:topic) do
|
2014-07-11 12:17:01 +08:00
|
|
|
Fabricate(:topic, title: 'unique topic name')
|
|
|
|
end
|
|
|
|
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) do
|
2014-07-11 12:17:01 +08:00
|
|
|
topic.user
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:post) { Fabricate(:post) }
|
2016-06-13 13:13:39 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it "can't link to the same topic" do
|
2014-07-11 12:17:01 +08:00
|
|
|
ftl = TopicLink.new(url: "/t/#{topic.id}",
|
|
|
|
topic_id: topic.id,
|
|
|
|
link_topic_id: topic.id)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(ftl.valid?).to eq(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'external links' do
|
2020-05-13 14:05:39 +08:00
|
|
|
it 'correctly handles links' do
|
|
|
|
|
|
|
|
non_png = "https://b.com/#{SecureRandom.hex}"
|
|
|
|
|
|
|
|
# prepare a title for one of the links
|
|
|
|
stub_request(:get, non_png).
|
|
|
|
with(headers: {
|
|
|
|
'Accept' => '*/*',
|
|
|
|
'Accept-Encoding' => 'gzip',
|
|
|
|
'Host' => 'b.com',
|
|
|
|
}).
|
|
|
|
to_return(status: 200, body: "<html><head><title>amazing</title></head></html>", headers: {})
|
|
|
|
|
|
|
|
# so we run crawl_topic_links
|
|
|
|
Jobs.run_immediately!
|
|
|
|
|
|
|
|
png_title = "#{SecureRandom.hex}.png"
|
|
|
|
png = "https://awesome.com/#{png_title}"
|
|
|
|
|
|
|
|
post = Fabricate(:post, raw: <<~RAW, user: user, topic: topic)
|
2018-08-14 18:23:32 +08:00
|
|
|
http://a.com/
|
2020-05-13 14:05:39 +08:00
|
|
|
#{non_png}
|
2018-08-14 18:23:32 +08:00
|
|
|
http://#{'a' * 200}.com/invalid
|
|
|
|
//b.com/#{'a' * 500}
|
2020-05-13 14:05:39 +08:00
|
|
|
#{png}
|
2018-08-14 18:23:32 +08:00
|
|
|
RAW
|
2014-06-26 09:38:23 +08:00
|
|
|
|
2020-05-13 14:05:39 +08:00
|
|
|
TopicLink.extract_from(post)
|
|
|
|
|
|
|
|
# we have a special rule for images title where we pull them out of the filename
|
|
|
|
expect(topic.topic_links.where(url: png).pluck(:title).first).to eq(png_title)
|
|
|
|
expect(topic.topic_links.where(url: non_png).pluck(:title).first).to eq("amazing")
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-08-14 18:23:32 +08:00
|
|
|
expect(topic.topic_links.pluck(:url)).to contain_exactly(
|
2020-05-13 14:05:39 +08:00
|
|
|
png,
|
|
|
|
non_png,
|
2019-01-03 19:59:22 +08:00
|
|
|
"http://a.com/",
|
2018-08-14 18:23:32 +08:00
|
|
|
"//b.com/#{'a' * 500}"[0...TopicLink.max_url_length]
|
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-12-06 01:16:27 +08:00
|
|
|
old_ids = topic.topic_links.pluck(:id)
|
|
|
|
|
2020-05-13 14:05:39 +08:00
|
|
|
TopicLink.extract_from(post)
|
2018-12-06 01:16:27 +08:00
|
|
|
|
|
|
|
new_ids = topic.topic_links.pluck(:id)
|
|
|
|
|
|
|
|
expect(new_ids).to contain_exactly(*old_ids)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-12 00:11:48 +08:00
|
|
|
describe 'internal links' do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-11 12:17:01 +08: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-14 04:22:04 +08:00
|
|
|
|
2014-07-11 12:17:01 +08: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-09 06:14:12 +08:00
|
|
|
|
2014-07-11 12:17:01 +08: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-14 04:22:04 +08:00
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
TopicLink.extract_from(post)
|
2013-02-14 04:22:04 +08:00
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
link = topic.topic_links.first
|
|
|
|
# should have a link
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(link).to be_present
|
2014-07-11 12:17:01 +08:00
|
|
|
# should be the canonical URL
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(link.url).to eq(url)
|
2013-02-14 04:22:04 +08:00
|
|
|
end
|
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
context 'topic link' do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:other_topic) do
|
2014-07-11 12:17:01 +08:00
|
|
|
Fabricate(:topic, user: user)
|
|
|
|
end
|
2021-06-02 02:02:53 +08:00
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
let(:post) do
|
|
|
|
other_topic.posts.create(user: user, raw: "some content")
|
2013-02-12 00:11:48 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-05 10:48:34 +08:00
|
|
|
it 'works' do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
# ensure other_topic has a post
|
|
|
|
post
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
url = "http://#{test_uri.host}/t/#{other_topic.slug}/#{other_topic.id}"
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
topic.posts.create(user: user, raw: 'initial post')
|
|
|
|
linked_post = topic.posts.create(user: user, raw: "Link to another topic: #{url}")
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-06-10 15:24:30 +08:00
|
|
|
# this is subtle, but we had a bug were second time
|
|
|
|
# TopicLink.extract_from was called a reflection was nuked
|
|
|
|
2.times do
|
|
|
|
TopicLink.extract_from(linked_post)
|
|
|
|
|
2020-05-13 14:05:39 +08:00
|
|
|
topic.reload
|
|
|
|
other_topic.reload
|
|
|
|
|
2016-06-10 15:24:30 +08:00
|
|
|
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)
|
2020-05-13 14:05:39 +08:00
|
|
|
expect(link.link_topic_id). to eq(other_topic.id)
|
2016-06-10 15:24:30 +08:00
|
|
|
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-06 03:16:51 +08:00
|
|
|
|
2016-06-15 13:45:07 +08:00
|
|
|
PostOwnerChanger.new(
|
|
|
|
post_ids: [linked_post.id],
|
|
|
|
topic_id: topic.id,
|
|
|
|
acting_user: user,
|
|
|
|
new_owner: Fabricate(:user)
|
|
|
|
).change_owner!
|
|
|
|
|
|
|
|
TopicLink.extract_from(linked_post)
|
|
|
|
expect(topic.topic_links.first.url).to eq(url)
|
|
|
|
|
2016-06-09 04:08:41 +08:00
|
|
|
linked_post.revise(post.user, raw: "no more linkies https://eviltrout.com")
|
2017-08-31 12:06:56 +08:00
|
|
|
expect(other_topic.reload.topic_links.where(link_post_id: linked_post.id)).to be_blank
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2019-12-04 14:13:20 +08:00
|
|
|
|
|
|
|
it 'works without id' do
|
|
|
|
post
|
|
|
|
url = "http://#{test_uri.host}/t/#{other_topic.slug}"
|
|
|
|
topic.posts.create(user: user, raw: 'initial post')
|
|
|
|
linked_post = topic.posts.create(user: user, raw: "Link to another topic: #{url}")
|
|
|
|
|
|
|
|
TopicLink.extract_from(linked_post)
|
|
|
|
link = topic.topic_links.first
|
|
|
|
|
|
|
|
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
|
2021-06-02 02:02:53 +08:00
|
|
|
|
|
|
|
it "doesn't work for a deleted post" do
|
|
|
|
post
|
|
|
|
url = "http://#{test_uri.host}/t/#{other_topic.slug}/#{other_topic.id}"
|
|
|
|
|
|
|
|
topic.posts.create(user: user, raw: 'initial post')
|
|
|
|
linked_post = topic.posts.create(user: user, raw: "Link to another topic: #{url}")
|
|
|
|
TopicLink.extract_from(linked_post)
|
|
|
|
expect(other_topic.reload.topic_links.where(link_post_id: linked_post.id).count).to eq(1)
|
|
|
|
|
|
|
|
PostDestroyer.new(moderator, linked_post).destroy
|
|
|
|
TopicLink.extract_from(linked_post)
|
|
|
|
expect(other_topic.reload.topic_links.where(link_post_id: linked_post.id)).to be_blank
|
|
|
|
|
|
|
|
end
|
FIX: PG::StringDataRightTruncation when linking posts (#13134)
Users who use encoded slugs on their sites sometimes run into 500 error when pasting a link to another topic in a post. The problem happens when generating a backward "reflection" link that would appear in a linked topic. Link URL restricted on the database level to 500 chars in length. At first glance, it should work since we have a restriction on topic title length.
But it doesn't work when a site uses encoded slugs, like here (take a look at the URL). The link to a topic, in this case, can be much longer than 500 characters.
By the way, an error happens only when generating a "reflection" link and doesn't happen with a direct link, we truncate that link. It works because, in this case, the original long link is still present in the post body and can be used for navigation. But we can't do the same for backward "reflection" links (without rewriting their implementation), the whole link must be saved to the database.
The simplest and cleanest solution will be just to remove the restriction on the database level. Abuse is impossible here since we are already protected by the restriction on topic title length. There aren’t performance benefits in using length-constrained columns in Postgres, in fact, length-constrained columns need a few extra CPU cycles to check the length when storing data.
2021-06-02 19:27:04 +08:00
|
|
|
|
|
|
|
it 'truncates long links' do
|
|
|
|
SiteSetting.slug_generation_method = 'encoded'
|
|
|
|
long_title = "Καλημερα σε ολους και ολες" * 9 # 234 chars, but the encoded slug will be 1224 chars in length
|
|
|
|
other_topic = Fabricate(:topic, user: user, title: long_title)
|
|
|
|
expect(other_topic.slug.length).to be > TopicLink.max_url_length
|
|
|
|
other_topic.posts.create(user: user, raw: 'initial post')
|
|
|
|
other_topic_url = "http://#{test_uri.host}/t/#{other_topic.slug}/#{other_topic.id}"
|
|
|
|
|
|
|
|
post_with_link = topic.posts.create(user: user, raw: "Link to another topic: #{other_topic_url}")
|
|
|
|
TopicLink.extract_from(post_with_link)
|
|
|
|
topic.reload
|
|
|
|
link = topic.topic_links.first
|
|
|
|
|
|
|
|
expect(link.url.length).to eq(TopicLink.max_url_length)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not truncate reflection links' do
|
|
|
|
SiteSetting.slug_generation_method = 'encoded'
|
|
|
|
long_title = "Καλημερα σε ολους και ολες" * 9 # 234 chars, but the encoded slug will be 1224 chars in length
|
|
|
|
topic = Fabricate(:topic, user: user, title: long_title)
|
|
|
|
expect(topic.slug.length).to be > TopicLink.max_url_length
|
|
|
|
topic_url = "http://#{test_uri.host}/t/#{topic.slug}/#{topic.id}"
|
|
|
|
|
|
|
|
other_topic = Fabricate(:topic, user: user)
|
|
|
|
other_topic.posts.create(user: user, raw: 'initial post')
|
|
|
|
other_topic_url = "http://#{test_uri.host}/t/#{other_topic.slug}/#{other_topic.id}"
|
|
|
|
|
|
|
|
post_with_link = topic.posts.create(user: user, raw: "Link to another topic: #{other_topic_url}")
|
|
|
|
expect { TopicLink.extract_from(post_with_link) }.to_not raise_error
|
|
|
|
|
|
|
|
other_topic.reload
|
|
|
|
reflection_link = other_topic.topic_links.first
|
|
|
|
expect(reflection_link.url.length).to be > (TopicLink.max_url_length)
|
|
|
|
expect(reflection_link.url).to eq(topic_url)
|
|
|
|
end
|
2013-02-12 00:11:48 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-12 01:27:32 +08:00
|
|
|
context "link to a user on discourse" do
|
2017-03-29 02:27:54 +08:00
|
|
|
let(:post) { topic.posts.create(user: user, raw: "<a href='/u/#{user.username_lower}'>user</a>") }
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
2013-02-12 00:11:48 +08:00
|
|
|
TopicLink.extract_from(post)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it 'does not extract a link' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(topic.topic_links).to be_blank
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-12 00:11:48 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-12 01:27:32 +08:00
|
|
|
context "link to a discourse resource like a FAQ" do
|
2014-07-11 12:17:01 +08:00
|
|
|
let(:post) { topic.posts.create(user: user, raw: "<a href='/faq'>faq link here</a>") }
|
2013-02-12 01:27:32 +08:00
|
|
|
before do
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it 'does not extract a link' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(topic.topic_links).to be_present
|
2013-02-12 01:27:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
context "mention links" do
|
|
|
|
let(:post) { topic.posts.create(user: user, raw: "Hey #{user.username_lower}") }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-12 00:11:48 +08:00
|
|
|
before do
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it 'does not extract a link' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(topic.topic_links).to be_blank
|
2014-07-11 12:17:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-19 18:29:35 +08:00
|
|
|
context "email address" do
|
|
|
|
it "does not extract a link" do
|
|
|
|
post = topic.posts.create(user: user, raw: "Valid email: foo@bar.com\n\nInvalid email: rfc822;name@domain.com")
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
expect(topic.topic_links).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-05 06:43:11 +08: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-11 12:17:01 +08: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 22:55:03 +08:00
|
|
|
expect(link.link_post_id).to eq(linked_post.id)
|
|
|
|
expect(link.quote).to eq(true)
|
2013-02-12 00:11:48 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
|
|
|
|
2013-07-19 07:26:23 +08:00
|
|
|
context "link to a local attachments" do
|
2017-06-19 23:09:54 +08:00
|
|
|
let(:post) { topic.posts.create(user: user, raw: '<a class="attachment" href="/uploads/default/208/87bb3d8428eb4783.rb?foo=bar">ruby.rb</a>') }
|
2013-07-19 07:26:23 +08:00
|
|
|
|
|
|
|
it "extracts the link" do
|
|
|
|
TopicLink.extract_from(post)
|
2014-07-11 12:17:01 +08:00
|
|
|
link = topic.topic_links.first
|
2013-07-19 07:26:23 +08:00
|
|
|
# extracted the link
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(link).to be_present
|
2013-07-19 07:26:23 +08:00
|
|
|
# is set to internal
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(link).to be_internal
|
2013-07-19 07:26:23 +08:00
|
|
|
# has the correct url
|
2017-06-19 23:09:54 +08:00
|
|
|
expect(link.url).to eq("/uploads/default/208/87bb3d8428eb4783.rb?foo=bar")
|
2013-07-19 07:26:23 +08:00
|
|
|
# should not be the reflection
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(link).not_to be_reflection
|
2017-06-19 23:09:54 +08:00
|
|
|
# should have file extension
|
|
|
|
expect(link.extension).to eq('rb')
|
2013-07-19 07:26:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "link to an attachments uploaded on S3" do
|
2014-07-11 12:17:01 +08:00
|
|
|
let(:post) { topic.posts.create(user: user, raw: '<a class="attachment" href="//s3.amazonaws.com/bucket/2104a0211c9ce41ed67989a1ed62e9a394c1fbd1446.rb">ruby.rb</a>') }
|
2013-07-19 07:26:23 +08:00
|
|
|
|
|
|
|
it "extracts the link" do
|
|
|
|
TopicLink.extract_from(post)
|
2014-07-11 12:17:01 +08:00
|
|
|
link = topic.topic_links.first
|
2013-07-19 07:26:23 +08:00
|
|
|
# extracted the link
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(link).to be_present
|
2013-07-19 07:26:23 +08:00
|
|
|
# is not internal
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(link).not_to be_internal
|
2013-07-19 07:26:23 +08:00
|
|
|
# has the correct url
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(link.url).to eq("//s3.amazonaws.com/bucket/2104a0211c9ce41ed67989a1ed62e9a394c1fbd1446.rb")
|
2013-07-19 07:26:23 +08:00
|
|
|
# should not be the reflection
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(link).not_to be_reflection
|
2017-06-19 23:09:54 +08:00
|
|
|
# should have file extension
|
|
|
|
expect(link.extension).to eq('rb')
|
2013-07-19 07:26:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
describe 'internal link from pm' do
|
2014-07-11 12:17:01 +08:00
|
|
|
it 'works' do
|
2014-09-11 15:39:20 +08:00
|
|
|
pm = Fabricate(:topic, user: user, category_id: nil, archetype: 'private_message')
|
2014-07-11 12:17:01 +08:00
|
|
|
pm.posts.create(user: user, raw: "some content")
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
url = "http://#{test_uri.host}/t/topic-slug/#{topic.id}"
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
pm.posts.create(user: user, raw: 'initial post')
|
|
|
|
linked_post = pm.posts.create(user: user, raw: "Link to another topic: #{url}")
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
TopicLink.extract_from(linked_post)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(topic.topic_links.first).to eq(nil)
|
|
|
|
expect(pm.topic_links.first).not_to eq(nil)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-08-22 16:58:01 +08:00
|
|
|
describe 'internal link from unlisted topic' do
|
|
|
|
it 'works' do
|
|
|
|
unlisted_topic = Fabricate(:topic, user: user, visible: false)
|
|
|
|
url = "http://#{test_uri.host}/t/topic-slug/#{topic.id}"
|
|
|
|
|
|
|
|
unlisted_topic.posts.create(user: user, raw: 'initial post')
|
|
|
|
linked_post = unlisted_topic.posts.create(user: user, raw: "Link to another topic: #{url}")
|
|
|
|
|
|
|
|
TopicLink.extract_from(linked_post)
|
|
|
|
|
|
|
|
expect(topic.topic_links.first).to eq(nil)
|
|
|
|
expect(unlisted_topic.topic_links.first).not_to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe 'internal link with non-standard port' do
|
|
|
|
it 'includes the non standard port if present' do
|
2014-07-11 12:17:01 +08:00
|
|
|
other_topic = Fabricate(:topic, user: user)
|
|
|
|
SiteSetting.port = 5678
|
2013-02-06 03:16:51 +08:00
|
|
|
alternate_uri = URI.parse(Discourse.base_url)
|
2014-07-11 12:17:01 +08:00
|
|
|
|
|
|
|
url = "http://#{alternate_uri.host}:5678/t/topic-slug/#{other_topic.id}"
|
2016-06-09 04:08:41 +08:00
|
|
|
post = topic.posts.create(user: user, raw: "Link to another topic: #{url}")
|
2014-07-11 12:17:01 +08:00
|
|
|
TopicLink.extract_from(post)
|
|
|
|
reflection = other_topic.topic_links.first
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(reflection.url).to eq("http://#{alternate_uri.host}:5678/t/unique-topic-name/#{topic.id}")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-07 04:58:35 +08:00
|
|
|
describe 'query methods' do
|
2013-06-05 14:10:26 +08:00
|
|
|
it 'returns blank without posts' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(TopicLink.counts_for(Guardian.new, nil, nil)).to be_blank
|
2013-06-05 14:10:26 +08: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-07 04:58:35 +08: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-10 02:31:32 +08:00
|
|
|
expect(ch[:post_number]).to be_present
|
2016-06-07 04:58:35 +08:00
|
|
|
end
|
2013-06-05 14:10:26 +08:00
|
|
|
|
|
|
|
it 'has the correct results' do
|
|
|
|
TopicLink.extract_from(post)
|
2018-07-18 12:14:50 +08:00
|
|
|
topic_link_first = post.topic.topic_links.first
|
|
|
|
TopicLinkClick.create!(topic_link: topic_link_first, ip_address: '192.168.1.1')
|
|
|
|
TopicLinkClick.create!(topic_link: topic_link_first, ip_address: '192.168.1.2')
|
|
|
|
topic_link_second = post.topic.topic_links.second
|
|
|
|
TopicLinkClick.create!(topic_link: topic_link_second, ip_address: '192.168.1.1')
|
2013-06-05 14:10:26 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(counts_for[post.id]).to be_present
|
2018-07-18 12:14:50 +08:00
|
|
|
expect(counts_for[post.id].first[:clicks]).to eq(2)
|
|
|
|
expect(counts_for[post.id].second[:clicks]).to eq(1)
|
2013-06-05 14:10:26 +08:00
|
|
|
|
2013-11-16 01:15:46 +08:00
|
|
|
array = TopicLink.topic_map(Guardian.new, post.topic_id)
|
2018-07-18 12:14:50 +08:00
|
|
|
expect(array.length).to eq(2)
|
|
|
|
expect(array[0].clicks).to eq(2)
|
|
|
|
expect(array[1].clicks).to eq(1)
|
2013-06-05 14:10:26 +08: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)
|
2018-07-18 12:14:50 +08:00
|
|
|
TopicLinkClick.create!(topic_link: post.topic.topic_links.first, ip_address: '192.168.1.1')
|
2013-06-05 14:10:26 +08:00
|
|
|
|
2014-12-31 22:55:03 +08: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 14:10:26 +08:00
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
category.set_permissions(staff: :full)
|
2013-06-05 14:10:26 +08:00
|
|
|
category.save
|
|
|
|
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
|
2014-12-31 22:55:03 +08: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 14:10:26 +08:00
|
|
|
|
2014-12-31 22:55:03 +08: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 14:10:26 +08:00
|
|
|
end
|
|
|
|
|
2017-09-01 02:14:54 +08:00
|
|
|
it 'does not include links from whisper' do
|
|
|
|
url = "https://blog.codinghorror.com/hacker-hack-thyself/"
|
|
|
|
post = Fabricate(:post, raw: "whisper post... #{url}", post_type: Post.types[:whisper])
|
|
|
|
TopicLink.extract_from(post)
|
|
|
|
|
|
|
|
expect(TopicLink.topic_map(Guardian.new, post.topic_id).count).to eq(0)
|
|
|
|
end
|
2013-06-05 14:10:26 +08:00
|
|
|
end
|
2016-06-13 13:13:39 +08:00
|
|
|
|
|
|
|
describe ".duplicate_lookup" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user, username: "junkrat") }
|
2016-06-13 13:13:39 +08:00
|
|
|
|
|
|
|
let(:post_with_internal_link) do
|
|
|
|
Fabricate(:post, user: user, raw: "Check out this topic #{post.topic.url}/122131")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the right response" do
|
|
|
|
TopicLink.extract_from(post_with_internal_link)
|
|
|
|
|
|
|
|
result = TopicLink.duplicate_lookup(post_with_internal_link.topic)
|
|
|
|
expect(result.count).to eq(1)
|
|
|
|
|
|
|
|
lookup = result["test.localhost/t/#{post.topic.slug}/#{post.topic.id}/122131"]
|
|
|
|
|
|
|
|
expect(lookup[:domain]).to eq("test.localhost")
|
|
|
|
expect(lookup[:username]).to eq("junkrat")
|
|
|
|
expect(lookup[:posted_at].to_s).to eq(post_with_internal_link.created_at.to_s)
|
|
|
|
expect(lookup[:post_number]).to eq(1)
|
|
|
|
|
|
|
|
result = TopicLink.duplicate_lookup(post.topic)
|
2016-06-13 13:16:24 +08:00
|
|
|
expect(result).to eq({})
|
2016-06-13 13:13:39 +08:00
|
|
|
end
|
|
|
|
end
|
2017-10-19 21:26:37 +08:00
|
|
|
|
|
|
|
it "works with invalid link target" do
|
|
|
|
post = Fabricate(:post, raw: '<a href="http:geturl">http:geturl</a>', user: user, topic: topic, cook_method: Post.cook_methods[:raw_html])
|
|
|
|
expect { TopicLink.extract_from(post) }.to_not raise_error
|
|
|
|
end
|
2013-06-05 14:10:26 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|