2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-20 03:08:54 +08: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-22 03:29:04 +08:00
|
|
|
results = InlineOneboxer.new([topic.url], skip_cache: true).process
|
2017-07-20 03:08:54 +08: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-22 03:29:04 +08:00
|
|
|
results = InlineOneboxer.new([topic.url], skip_cache: true).process
|
2017-07-20 03:08:54 +08:00
|
|
|
expect(results).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
context "caching" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
2017-07-22 03:29:04 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
InlineOneboxer.purge(topic.url)
|
|
|
|
end
|
|
|
|
|
2017-07-20 03:08:54 +08:00
|
|
|
it "puts an entry in the cache" do
|
|
|
|
expect(InlineOneboxer.cache_lookup(topic.url)).to be_blank
|
|
|
|
|
|
|
|
result = InlineOneboxer.lookup(topic.url)
|
|
|
|
expect(result).to be_present
|
|
|
|
|
|
|
|
cached = InlineOneboxer.cache_lookup(topic.url)
|
|
|
|
expect(cached[:url]).to eq(topic.url)
|
|
|
|
expect(cached[:title]).to eq(topic.title)
|
|
|
|
end
|
2018-11-13 04:14:20 +08: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-13 05:09:20 +08:00
|
|
|
InlineOneboxer.purge(url)
|
2018-11-13 04:14:20 +08: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-20 03:08:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context ".lookup" do
|
|
|
|
it "can lookup one link at a time" do
|
|
|
|
topic = Fabricate(:topic)
|
2017-07-22 03:29:04 +08:00
|
|
|
onebox = InlineOneboxer.lookup(topic.url, skip_cache: true)
|
2017-07-20 03:08:54 +08:00
|
|
|
expect(onebox).to be_present
|
|
|
|
expect(onebox[:url]).to eq(topic.url)
|
|
|
|
expect(onebox[:title]).to eq(topic.title)
|
|
|
|
end
|
2017-07-21 04:01:16 +08: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-22 02:24:28 +08:00
|
|
|
|
|
|
|
it "will return the fancy title" do
|
|
|
|
topic = Fabricate(:topic, title: "Hello :pizza: with an emoji")
|
2017-07-22 03:29:04 +08:00
|
|
|
onebox = InlineOneboxer.lookup(topic.url, skip_cache: true)
|
2017-07-22 02:24:28 +08: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-22 03:29:04 +08: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-03 02:27:21 +08: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 19:35:57 +08:00
|
|
|
to_return(status: 200, body: "<html><head><title>a blog</title></head></html>")
|
2017-08-03 02:27:21 +08: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-30 05:39:41 +08: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 19:35:57 +08:00
|
|
|
to_return(status: 200, body: "<html><head><title>a</title></head></html>")
|
2018-01-30 05:39:41 +08: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-22 03:29:04 +08: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-20 03:08:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|