2017-07-19 15:08:54 -04: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-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
|
2017-07-21 15:29:04 -04:00
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
InlineOneboxer.purge(topic.url)
|
|
|
|
end
|
|
|
|
|
2017-07-19 15:08:54 -04: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-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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-07-20 16:01:16 -04:00
|
|
|
|
2017-07-19 15:08:54 -04:00
|
|
|
end
|
|
|
|
|