2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2015-08-18 17:15:46 -04:00
|
|
|
|
|
|
|
describe EmbeddableHost do
|
|
|
|
|
|
|
|
it "trims http" do
|
|
|
|
eh = EmbeddableHost.new(host: 'http://example.com')
|
|
|
|
expect(eh).to be_valid
|
|
|
|
expect(eh.host).to eq('example.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "trims https" do
|
|
|
|
eh = EmbeddableHost.new(host: 'https://example.com')
|
|
|
|
expect(eh).to be_valid
|
|
|
|
expect(eh.host).to eq('example.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "trims paths" do
|
|
|
|
eh = EmbeddableHost.new(host: 'https://example.com/1234/45')
|
|
|
|
expect(eh).to be_valid
|
|
|
|
expect(eh.host).to eq('example.com')
|
|
|
|
end
|
|
|
|
|
2016-01-11 11:06:09 -05:00
|
|
|
it "supports ip addresses" do
|
|
|
|
eh = EmbeddableHost.new(host: '192.168.0.1')
|
|
|
|
expect(eh).to be_valid
|
|
|
|
expect(eh.host).to eq('192.168.0.1')
|
|
|
|
end
|
|
|
|
|
2016-07-22 17:12:57 -04:00
|
|
|
it "supports localhost" do
|
|
|
|
eh = EmbeddableHost.new(host: 'localhost')
|
|
|
|
expect(eh).to be_valid
|
|
|
|
expect(eh.host).to eq('localhost')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "supports ports of localhost" do
|
|
|
|
eh = EmbeddableHost.new(host: 'localhost:8080')
|
|
|
|
expect(eh).to be_valid
|
|
|
|
expect(eh.host).to eq('localhost:8080')
|
|
|
|
end
|
|
|
|
|
2017-03-15 17:16:34 -04:00
|
|
|
it "supports ports for ip addresses" do
|
|
|
|
eh = EmbeddableHost.new(host: '192.168.0.1:3000')
|
|
|
|
expect(eh).to be_valid
|
|
|
|
expect(eh.host).to eq('192.168.0.1:3000')
|
|
|
|
end
|
|
|
|
|
2016-07-22 17:12:57 -04:00
|
|
|
it "supports subdomains of localhost" do
|
|
|
|
eh = EmbeddableHost.new(host: 'discourse.localhost')
|
|
|
|
expect(eh).to be_valid
|
|
|
|
expect(eh.host).to eq('discourse.localhost')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "rejects misspellings of localhost" do
|
|
|
|
eh = EmbeddableHost.new(host: 'alocalhost')
|
|
|
|
expect(eh).not_to be_valid
|
|
|
|
end
|
|
|
|
|
2017-02-27 12:17:52 -05:00
|
|
|
describe "it works with ports" do
|
|
|
|
let!(:host) { Fabricate(:embeddable_host, host: 'localhost:8000') }
|
|
|
|
|
|
|
|
it "works as expected" do
|
|
|
|
expect(EmbeddableHost.url_allowed?('http://localhost:8000/eviltrout')).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-23 14:55:52 -04:00
|
|
|
describe "url_allowed?" do
|
2015-08-18 17:15:46 -04:00
|
|
|
let!(:host) { Fabricate(:embeddable_host) }
|
|
|
|
|
|
|
|
it 'works as expected' do
|
2016-08-23 14:55:52 -04:00
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com')).to eq(true)
|
|
|
|
expect(EmbeddableHost.url_allowed?('https://eviltrout.com')).to eq(true)
|
2017-02-27 12:17:52 -05:00
|
|
|
expect(EmbeddableHost.url_allowed?('https://eviltrout.com/انگلیسی')).to eq(true)
|
2016-08-23 14:55:52 -04:00
|
|
|
expect(EmbeddableHost.url_allowed?('https://not-eviltrout.com')).to eq(false)
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'works with multiple hosts' do
|
|
|
|
Fabricate(:embeddable_host, host: 'discourse.org')
|
2016-08-23 14:55:52 -04:00
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com')).to eq(true)
|
|
|
|
expect(EmbeddableHost.url_allowed?('http://discourse.org')).to eq(true)
|
|
|
|
end
|
2017-08-02 16:43:31 -04:00
|
|
|
|
|
|
|
it 'always allow forum own URL' do
|
|
|
|
expect(EmbeddableHost.url_allowed?(Discourse.base_url)).to eq(true)
|
|
|
|
end
|
2016-08-23 14:55:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "path_whitelist" do
|
|
|
|
it "matches the path" do
|
2016-08-26 12:47:21 -04:00
|
|
|
Fabricate(:embeddable_host, path_whitelist: '^/fp/\d{4}/\d{2}/\d{2}/.*$')
|
2016-08-23 14:55:52 -04:00
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com')).to eq(false)
|
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com/fp/2016/08/25/test-page')).to eq(true)
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|
|
|
|
|
2016-08-26 12:47:21 -04:00
|
|
|
it "respects query parameters" do
|
|
|
|
Fabricate(:embeddable_host, path_whitelist: '^/fp$')
|
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com/fp?test=1')).to eq(false)
|
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com/fp')).to eq(true)
|
|
|
|
end
|
2017-02-17 12:39:33 -05:00
|
|
|
|
|
|
|
it "allows multiple records with different paths" do
|
|
|
|
Fabricate(:embeddable_host, path_whitelist: '/rick/.*')
|
|
|
|
Fabricate(:embeddable_host, path_whitelist: '/morty/.*')
|
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com/rick/smith')).to eq(true)
|
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com/morty/sanchez')).to eq(true)
|
|
|
|
end
|
2017-12-12 11:56:28 -05:00
|
|
|
|
|
|
|
it "works with non-english paths" do
|
|
|
|
Fabricate(:embeddable_host, path_whitelist: '/انگلیسی/.*')
|
|
|
|
Fabricate(:embeddable_host, path_whitelist: '/definição/.*')
|
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com/انگلیسی/foo')).to eq(true)
|
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com/definição/foo')).to eq(true)
|
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com/bar/foo')).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works with URL encoded paths" do
|
|
|
|
Fabricate(:embeddable_host, path_whitelist: '/definição/.*')
|
|
|
|
Fabricate(:embeddable_host, path_whitelist: '/ingl%C3%A9s/.*')
|
|
|
|
|
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com/defini%C3%A7%C3%A3o/foo')).to eq(true)
|
|
|
|
expect(EmbeddableHost.url_allowed?('http://eviltrout.com/inglés/foo')).to eq(true)
|
|
|
|
end
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|