2019-04-30 10:27:42 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 05:27:38 +03:00
|
|
|
RSpec.describe EmbeddableHost do
|
2015-08-18 17:15:46 -04:00
|
|
|
it "trims http" do
|
2023-01-09 11:18:21 +00:00
|
|
|
eh = EmbeddableHost.new(host: "http://example.com")
|
2015-08-18 17:15:46 -04:00
|
|
|
expect(eh).to be_valid
|
2023-01-09 11:18:21 +00:00
|
|
|
expect(eh.host).to eq("example.com")
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "trims https" do
|
2023-01-09 11:18:21 +00:00
|
|
|
eh = EmbeddableHost.new(host: "https://example.com")
|
2015-08-18 17:15:46 -04:00
|
|
|
expect(eh).to be_valid
|
2023-01-09 11:18:21 +00:00
|
|
|
expect(eh.host).to eq("example.com")
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "trims paths" do
|
2023-01-09 11:18:21 +00:00
|
|
|
eh = EmbeddableHost.new(host: "https://example.com/1234/45")
|
2015-08-18 17:15:46 -04:00
|
|
|
expect(eh).to be_valid
|
2023-01-09 11:18:21 +00:00
|
|
|
expect(eh.host).to eq("example.com")
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|
|
|
|
|
2016-01-11 11:06:09 -05:00
|
|
|
it "supports ip addresses" do
|
2023-01-09 11:18:21 +00:00
|
|
|
eh = EmbeddableHost.new(host: "192.168.0.1")
|
2016-01-11 11:06:09 -05:00
|
|
|
expect(eh).to be_valid
|
2023-01-09 11:18:21 +00:00
|
|
|
expect(eh.host).to eq("192.168.0.1")
|
2016-01-11 11:06:09 -05:00
|
|
|
end
|
|
|
|
|
2016-07-22 17:12:57 -04:00
|
|
|
it "supports localhost" do
|
2023-01-09 11:18:21 +00:00
|
|
|
eh = EmbeddableHost.new(host: "localhost")
|
2016-07-22 17:12:57 -04:00
|
|
|
expect(eh).to be_valid
|
2023-01-09 11:18:21 +00:00
|
|
|
expect(eh.host).to eq("localhost")
|
2016-07-22 17:12:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports ports of localhost" do
|
2023-01-09 11:18:21 +00:00
|
|
|
eh = EmbeddableHost.new(host: "localhost:8080")
|
2016-07-22 17:12:57 -04:00
|
|
|
expect(eh).to be_valid
|
2023-01-09 11:18:21 +00:00
|
|
|
expect(eh.host).to eq("localhost:8080")
|
2016-07-22 17:12:57 -04:00
|
|
|
end
|
|
|
|
|
2017-03-15 18:16:34 -03:00
|
|
|
it "supports ports for ip addresses" do
|
2023-01-09 11:18:21 +00:00
|
|
|
eh = EmbeddableHost.new(host: "192.168.0.1:3000")
|
2017-03-15 18:16:34 -03:00
|
|
|
expect(eh).to be_valid
|
2023-01-09 11:18:21 +00:00
|
|
|
expect(eh.host).to eq("192.168.0.1:3000")
|
2017-03-15 18:16:34 -03:00
|
|
|
end
|
|
|
|
|
2016-07-22 17:12:57 -04:00
|
|
|
it "supports subdomains of localhost" do
|
2023-01-09 11:18:21 +00:00
|
|
|
eh = EmbeddableHost.new(host: "discourse.localhost")
|
2016-07-22 17:12:57 -04:00
|
|
|
expect(eh).to be_valid
|
2023-01-09 11:18:21 +00:00
|
|
|
expect(eh.host).to eq("discourse.localhost")
|
2016-07-22 17:12:57 -04:00
|
|
|
end
|
|
|
|
|
2020-06-19 00:58:47 +07:00
|
|
|
it "supports multiple hyphens" do
|
2023-01-09 11:18:21 +00:00
|
|
|
eh = EmbeddableHost.new(host: "deploy-preview-1--example.example.app")
|
2020-06-19 00:58:47 +07:00
|
|
|
expect(eh).to be_valid
|
2023-01-09 11:18:21 +00:00
|
|
|
expect(eh.host).to eq("deploy-preview-1--example.example.app")
|
2020-06-19 00:58:47 +07:00
|
|
|
end
|
|
|
|
|
2016-07-22 17:12:57 -04:00
|
|
|
it "rejects misspellings of localhost" do
|
2023-01-09 11:18:21 +00:00
|
|
|
eh = EmbeddableHost.new(host: "alocalhost")
|
2016-07-22 17:12:57 -04:00
|
|
|
expect(eh).not_to be_valid
|
|
|
|
end
|
|
|
|
|
2017-02-27 12:17:52 -05:00
|
|
|
describe "it works with ports" do
|
2023-01-09 11:18:21 +00:00
|
|
|
fab!(:host) { Fabricate(:embeddable_host, host: "localhost:8000") }
|
2017-02-27 12:17:52 -05:00
|
|
|
|
|
|
|
it "works as expected" do
|
2023-01-09 11:18:21 +00:00
|
|
|
expect(EmbeddableHost.url_allowed?("http://localhost:8000/eviltrout")).to eq(true)
|
2017-02-27 12:17:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-29 20:51:59 -04:00
|
|
|
it "doesn't allow forum own URL if no hosts exist" do
|
|
|
|
expect(EmbeddableHost.url_allowed?(Discourse.base_url)).to eq(false)
|
|
|
|
end
|
|
|
|
|
2016-08-23 14:55:52 -04:00
|
|
|
describe "url_allowed?" do
|
2019-05-07 03:12:20 +00:00
|
|
|
fab!(:host) { Fabricate(:embeddable_host) }
|
2015-08-18 17:15:46 -04:00
|
|
|
|
2023-01-09 11:18:21 +00:00
|
|
|
it "works as expected" do
|
|
|
|
expect(EmbeddableHost.url_allowed?("http://eviltrout.com")).to eq(true)
|
|
|
|
expect(EmbeddableHost.url_allowed?("https://eviltrout.com")).to eq(true)
|
|
|
|
expect(EmbeddableHost.url_allowed?("https://eviltrout.com/انگلیسی")).to eq(true)
|
|
|
|
expect(EmbeddableHost.url_allowed?("https://not-eviltrout.com")).to eq(false)
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|
|
|
|
|
2023-01-09 11:18:21 +00:00
|
|
|
it "works with multiple hosts" do
|
|
|
|
Fabricate(:embeddable_host, host: "discourse.org")
|
|
|
|
expect(EmbeddableHost.url_allowed?("http://eviltrout.com")).to eq(true)
|
|
|
|
expect(EmbeddableHost.url_allowed?("http://discourse.org")).to eq(true)
|
2016-08-23 14:55:52 -04:00
|
|
|
end
|
2017-08-02 17:43:31 -03:00
|
|
|
|
2023-01-25 13:50:45 +02:00
|
|
|
it "does not allow forum own URL" do
|
|
|
|
expect(EmbeddableHost.url_allowed?(Discourse.base_url)).to eq(false)
|
2017-08-02 17:43:31 -03:00
|
|
|
end
|
2016-08-23 14:55:52 -04:00
|
|
|
end
|
|
|
|
|
2020-07-27 10:23:54 +10:00
|
|
|
describe "allowed_paths" do
|
2016-08-23 14:55:52 -04:00
|
|
|
it "matches the path" do
|
2020-07-27 10:23:54 +10:00
|
|
|
Fabricate(:embeddable_host, allowed_paths: '^/fp/\d{4}/\d{2}/\d{2}/.*$')
|
2023-01-09 11:18:21 +00: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
|
2023-01-09 11:18:21 +00:00
|
|
|
Fabricate(:embeddable_host, allowed_paths: "^/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)
|
2016-08-26 12:47:21 -04:00
|
|
|
end
|
2017-02-17 12:39:33 -05:00
|
|
|
|
|
|
|
it "allows multiple records with different paths" do
|
2023-01-09 11:18:21 +00:00
|
|
|
Fabricate(:embeddable_host, allowed_paths: "/rick/.*")
|
|
|
|
Fabricate(:embeddable_host, allowed_paths: "/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)
|
2017-02-17 12:39:33 -05:00
|
|
|
end
|
2017-12-12 17:56:28 +01:00
|
|
|
|
|
|
|
it "works with non-english paths" do
|
2023-01-09 11:18:21 +00:00
|
|
|
Fabricate(:embeddable_host, allowed_paths: "/انگلیسی/.*")
|
|
|
|
Fabricate(:embeddable_host, allowed_paths: "/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)
|
2017-12-12 17:56:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "works with URL encoded paths" do
|
2023-01-09 11:18:21 +00:00
|
|
|
Fabricate(:embeddable_host, allowed_paths: "/definição/.*")
|
|
|
|
Fabricate(:embeddable_host, allowed_paths: "/ingl%C3%A9s/.*")
|
2017-12-12 17:56:28 +01:00
|
|
|
|
2023-01-09 11:18:21 +00:00
|
|
|
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)
|
2017-12-12 17:56:28 +01:00
|
|
|
end
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|
|
|
|
|
2019-03-29 17:05:51 +01:00
|
|
|
describe "reset_embedding_settings" do
|
|
|
|
it "resets all embedding related settings when last embeddable host is removed" do
|
|
|
|
host = Fabricate(:embeddable_host)
|
|
|
|
host2 = Fabricate(:embeddable_host)
|
|
|
|
|
|
|
|
SiteSetting.embed_post_limit = 300
|
|
|
|
|
|
|
|
host2.destroy
|
|
|
|
|
|
|
|
expect(SiteSetting.embed_post_limit).to eq(300)
|
|
|
|
|
|
|
|
host.destroy
|
|
|
|
|
|
|
|
expect(SiteSetting.embed_post_limit).to eq(SiteSetting.defaults[:embed_post_limit])
|
|
|
|
end
|
|
|
|
end
|
2022-02-07 11:25:42 +08:00
|
|
|
|
2023-01-09 11:18:21 +00:00
|
|
|
describe ".record_for_url" do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:embeddable_host)
|
2022-02-07 11:25:42 +08:00
|
|
|
|
2023-01-09 11:18:21 +00:00
|
|
|
it "returns the right record if given URL matches host" do
|
|
|
|
expect(EmbeddableHost.record_for_url("https://#{embeddable_host.host}")).to eq(
|
|
|
|
embeddable_host,
|
|
|
|
)
|
2022-02-07 11:25:42 +08:00
|
|
|
end
|
|
|
|
|
2023-01-09 11:18:21 +00:00
|
|
|
it "returns false if URL is malformed" do
|
2022-02-07 11:25:42 +08:00
|
|
|
expect(EmbeddableHost.record_for_url("@@@@@")).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|