discourse/spec/support/onebox_helpers.rb
Arpit Jalan 283b08d45f
DEV: Absorb onebox gem into core (#12979)
* Move onebox gem in core library

* Update template file path

* Remove warning for onebox gem caching

* Remove onebox version file

* Remove onebox gem

* Add sanitize gem

* Require onebox library in lazy-yt plugin

* Remove onebox web specific code

This code was used in standalone onebox Sinatra application

* Merge Discourse specific AllowlistedGenericOnebox engine in core

* Fix onebox engine filenames to match class name casing

* Move onebox specs from gem into core

* DEV: Rename `response` helper to `onebox_response`

Fixes a naming collision.

* Require rails_helper

* Don't use `before/after(:all)`

* Whitespace

* Remove fakeweb

* Remove poor unit tests

* DEV: Re-add fakeweb, plugins are using it

* Move onebox helpers

* Stub Instagram API

* FIX: Follow additional redirect status codes (#476)

Don’t throw errors if we encounter 303, 307 or 308 HTTP status codes in responses

* Remove an empty file

* DEV: Update the license file

Using the copy from https://choosealicense.com/licenses/gpl-2.0/#

Hopefully this will enable GitHub to show the license UI?

* DEV: Update embedded copyrights

* DEV: Add Onebox copyright notice

* DEV: Add MIT license, convert COPYRIGHT.txt to md

* DEV: Remove an incorrect copyright claim

Co-authored-by: Jarek Radosz <jradosz@gmail.com>
Co-authored-by: jbrw <jamie@goatforce5.org>
2021-05-26 15:11:35 +05:30

75 lines
2.1 KiB
Ruby

# frozen_string_literal: true
module OneboxHelpers
def onebox_response(file)
file = File.join("spec", "fixtures", "onebox", "#{file}.response")
File.exist?(file) ? File.read(file) : ""
end
def inspect_html_fragment(raw_fragment, tag_name, attribute = 'src')
preview = Nokogiri::HTML::DocumentFragment.parse(raw_fragment)
preview.css(tag_name).first[attribute]
end
shared_context "engines" do
before do
fixture = defined?(@onebox_fixture) ? @onebox_fixture : described_class.onebox_name
stub_request(:get, defined?(@uri) ? @uri : @link).to_return(status: 200, body: onebox_response(fixture))
end
let(:onebox) { described_class.new(link) }
let(:html) { onebox.to_html }
let(:data) { Onebox::Helpers.symbolize_keys(onebox.send(:data)) }
let(:link) { @link }
end
shared_examples_for "an engine" do
it "responds to data" do
expect(described_class.private_instance_methods).to include(:data)
end
it "correctly matches the url" do
onebox = Onebox::Matcher.new(link, { allowed_iframe_regexes: [/.*/] }).oneboxed
expect(onebox).to be(described_class)
end
describe "#data" do
it "includes title" do
expect(data[:title]).not_to be_nil
end
it "includes link" do
expect(data[:link]).not_to be_nil
end
it "is serializable" do
expect { Marshal.dump(data) }.to_not raise_error
end
end
end
shared_examples_for "a layout engine" do
describe "#to_html" do
it "includes subname" do
expect(html).to include(%|<aside class="onebox #{described_class.onebox_name}">|)
end
it "includes title" do
expect(html).to include(data[:title])
end
it "includes link" do
expect(html).to include(%|class="link" href="#{data[:link]}|)
end
it "includes badge" do
expect(html).to include(%|<strong class="name">#{data[:badge]}</strong>|)
end
it "includes domain" do
expect(html).to include(%|class="domain" href="#{data[:domain]}|)
end
end
end
end