2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-19 15:08:54 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe InlineOneboxer do
|
|
|
|
|
|
|
|
it "should return nothing with empty input" do
|
|
|
|
expect(InlineOneboxer.new([]).process).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can onebox a topic" do
|
|
|
|
topic = Fabricate(:topic)
|
2017-07-21 15:29:04 -04:00
|
|
|
results = InlineOneboxer.new([topic.url], skip_cache: true).process
|
2017-07-19 15:08:54 -04:00
|
|
|
expect(results).to be_present
|
|
|
|
expect(results[0][:url]).to eq(topic.url)
|
|
|
|
expect(results[0][:title]).to eq(topic.title)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't onebox private messages" do
|
|
|
|
topic = Fabricate(:private_message_topic)
|
2017-07-21 15:29:04 -04:00
|
|
|
results = InlineOneboxer.new([topic.url], skip_cache: true).process
|
2017-07-19 15:08:54 -04:00
|
|
|
expect(results).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
context "caching" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
2017-07-21 15:29:04 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
InlineOneboxer.purge(topic.url)
|
|
|
|
end
|
|
|
|
|
2017-07-19 15:08:54 -04:00
|
|
|
it "puts an entry in the cache" do
|
2020-02-12 05:11:28 -05:00
|
|
|
SiteSetting.enable_inline_onebox_on_all_domains = true
|
|
|
|
url = "https://example.com/random-url"
|
|
|
|
stub_request(:get, url).to_return(status: 200, body: "<html><head><title>a blog</title></head></html>")
|
2017-07-19 15:08:54 -04:00
|
|
|
|
2020-02-12 05:11:28 -05:00
|
|
|
InlineOneboxer.purge(url)
|
|
|
|
expect(InlineOneboxer.cache_lookup(url)).to be_blank
|
|
|
|
|
|
|
|
result = InlineOneboxer.lookup(url)
|
2017-07-19 15:08:54 -04:00
|
|
|
expect(result).to be_present
|
|
|
|
|
2020-02-12 05:11:28 -05:00
|
|
|
cached = InlineOneboxer.cache_lookup(url)
|
|
|
|
expect(cached[:url]).to eq(url)
|
|
|
|
expect(cached[:title]).to eq('a blog')
|
2017-07-19 15:08:54 -04:00
|
|
|
end
|
2018-11-12 15:14:20 -05:00
|
|
|
|
|
|
|
it "puts an entry in the cache for failed onebox" do
|
|
|
|
SiteSetting.enable_inline_onebox_on_all_domains = true
|
|
|
|
url = "https://example.com/random-url"
|
|
|
|
|
2018-11-12 16:09:20 -05:00
|
|
|
InlineOneboxer.purge(url)
|
2018-11-12 15:14:20 -05:00
|
|
|
expect(InlineOneboxer.cache_lookup(url)).to be_blank
|
|
|
|
|
|
|
|
result = InlineOneboxer.lookup(url)
|
|
|
|
expect(result).to be_present
|
|
|
|
|
|
|
|
cached = InlineOneboxer.cache_lookup(url)
|
|
|
|
expect(cached[:url]).to eq(url)
|
|
|
|
expect(cached[:title]).to be_nil
|
|
|
|
end
|
2017-07-19 15:08:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context ".lookup" do
|
2020-02-12 05:11:28 -05:00
|
|
|
let(:category) { Fabricate(:private_category, group: Group[:staff]) }
|
|
|
|
let(:category2) { Fabricate(:private_category, group: Group[:staff]) }
|
|
|
|
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
it "can lookup private topics if in same category" do
|
|
|
|
topic = Fabricate(:topic, category: category)
|
|
|
|
topic1 = Fabricate(:topic, category: category)
|
|
|
|
topic2 = Fabricate(:topic, category: category2)
|
|
|
|
|
|
|
|
# Link to `topic` from new topic (same category)
|
|
|
|
onebox = InlineOneboxer.lookup(topic.url, user_id: admin.id, category_id: category.id, skip_cache: true)
|
|
|
|
expect(onebox).to be_present
|
|
|
|
expect(onebox[:url]).to eq(topic.url)
|
|
|
|
expect(onebox[:title]).to eq(topic.title)
|
|
|
|
|
|
|
|
# Link to `topic` from `topic`
|
|
|
|
onebox = InlineOneboxer.lookup(topic.url, user_id: admin.id, category_id: topic.category_id, topic_id: topic.id, skip_cache: true)
|
|
|
|
expect(onebox).to be_present
|
|
|
|
expect(onebox[:url]).to eq(topic.url)
|
|
|
|
expect(onebox[:title]).to eq(topic.title)
|
|
|
|
|
|
|
|
# Link to `topic` from `topic1` (same category)
|
|
|
|
onebox = InlineOneboxer.lookup(topic.url, user_id: admin.id, category_id: topic1.category_id, topic_id: topic1.id, skip_cache: true)
|
|
|
|
expect(onebox).to be_present
|
|
|
|
expect(onebox[:url]).to eq(topic.url)
|
|
|
|
expect(onebox[:title]).to eq(topic.title)
|
|
|
|
|
|
|
|
# Link to `topic` from `topic2` (different category)
|
|
|
|
onebox = InlineOneboxer.lookup(topic.url, user_id: admin.id, category_id: topic2.category_id, topic_id: topic2.id, skip_cache: true)
|
|
|
|
expect(onebox).to be_blank
|
|
|
|
end
|
|
|
|
|
2017-07-19 15:08:54 -04:00
|
|
|
it "can lookup one link at a time" do
|
|
|
|
topic = Fabricate(:topic)
|
2017-07-21 15:29:04 -04:00
|
|
|
onebox = InlineOneboxer.lookup(topic.url, skip_cache: true)
|
2017-07-19 15:08:54 -04:00
|
|
|
expect(onebox).to be_present
|
|
|
|
expect(onebox[:url]).to eq(topic.url)
|
|
|
|
expect(onebox[:title]).to eq(topic.title)
|
|
|
|
end
|
2017-07-20 16:01:16 -04:00
|
|
|
|
|
|
|
it "returns nothing for unknown links" do
|
|
|
|
expect(InlineOneboxer.lookup(nil)).to be_nil
|
|
|
|
expect(InlineOneboxer.lookup("/test")).to be_nil
|
|
|
|
end
|
2017-07-21 14:24:28 -04:00
|
|
|
|
|
|
|
it "will return the fancy title" do
|
|
|
|
topic = Fabricate(:topic, title: "Hello :pizza: with an emoji")
|
2017-07-21 15:29:04 -04:00
|
|
|
onebox = InlineOneboxer.lookup(topic.url, skip_cache: true)
|
2017-07-21 14:24:28 -04:00
|
|
|
expect(onebox).to be_present
|
|
|
|
expect(onebox[:url]).to eq(topic.url)
|
|
|
|
expect(onebox[:title]).to eq("Hello 🍕 with an emoji")
|
|
|
|
end
|
|
|
|
|
2017-07-21 15:29:04 -04:00
|
|
|
it "will not crawl domains that aren't whitelisted" do
|
|
|
|
onebox = InlineOneboxer.lookup("https://eviltrout.com", skip_cache: true)
|
|
|
|
expect(onebox).to be_blank
|
|
|
|
end
|
|
|
|
|
2017-08-02 14:27:21 -04:00
|
|
|
it "will crawl anything if allowed to" do
|
|
|
|
SiteSetting.enable_inline_onebox_on_all_domains = true
|
|
|
|
|
|
|
|
stub_request(:get, "https://eviltrout.com/some-path").
|
2018-02-24 06:35:57 -05:00
|
|
|
to_return(status: 200, body: "<html><head><title>a blog</title></head></html>")
|
2017-08-02 14:27:21 -04:00
|
|
|
|
|
|
|
onebox = InlineOneboxer.lookup(
|
|
|
|
"https://eviltrout.com/some-path",
|
|
|
|
skip_cache: true
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(onebox).to be_present
|
|
|
|
expect(onebox[:url]).to eq("https://eviltrout.com/some-path")
|
|
|
|
expect(onebox[:title]).to eq("a blog")
|
|
|
|
end
|
|
|
|
|
2018-01-29 16:39:41 -05:00
|
|
|
it "will not return a onebox if it does not meet minimal length" do
|
|
|
|
SiteSetting.enable_inline_onebox_on_all_domains = true
|
|
|
|
|
|
|
|
stub_request(:get, "https://eviltrout.com/some-path").
|
2018-02-24 06:35:57 -05:00
|
|
|
to_return(status: 200, body: "<html><head><title>a</title></head></html>")
|
2018-01-29 16:39:41 -05:00
|
|
|
|
|
|
|
onebox = InlineOneboxer.lookup(
|
|
|
|
"https://eviltrout.com/some-path",
|
|
|
|
skip_cache: true
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(onebox).to be_present
|
|
|
|
expect(onebox[:url]).to eq("https://eviltrout.com/some-path")
|
|
|
|
expect(onebox[:title]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2017-07-21 15:29:04 -04:00
|
|
|
it "will lookup whitelisted domains" do
|
|
|
|
SiteSetting.inline_onebox_domains_whitelist = "eviltrout.com"
|
|
|
|
RetrieveTitle.stubs(:crawl).returns("Evil Trout's Blog")
|
|
|
|
|
|
|
|
onebox = InlineOneboxer.lookup(
|
|
|
|
"https://eviltrout.com/some-path",
|
|
|
|
skip_cache: true
|
|
|
|
)
|
|
|
|
expect(onebox).to be_present
|
|
|
|
expect(onebox[:url]).to eq("https://eviltrout.com/some-path")
|
|
|
|
expect(onebox[:title]).to eq("Evil Trout's Blog")
|
|
|
|
end
|
|
|
|
|
2017-07-19 15:08:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|