2017-07-20 03:08:54 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
require_dependency 'inline_oneboxer'
|
|
|
|
|
|
|
|
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
|
2017-07-22 03:29:04 +08:00
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
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).to be_present
|
|
|
|
expect(cached[:url]).to eq(topic.url)
|
|
|
|
expect(cached[:title]).to eq(topic.title)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|