2019-12-12 20:12:12 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe WildcardUrlChecker do
|
2023-01-09 05:18:21 -06:00
|
|
|
describe ".check_url" do
|
|
|
|
context "when url is valid" do
|
|
|
|
it "returns true" do
|
|
|
|
result1 =
|
|
|
|
described_class.check_url(
|
|
|
|
"https://*.discourse.org",
|
|
|
|
"https://anything.is.possible.discourse.org",
|
|
|
|
)
|
2020-01-02 10:03:13 -06:00
|
|
|
expect(result1).to eq(true)
|
2019-12-12 20:12:12 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result2 =
|
|
|
|
described_class.check_url("https://www.discourse.org", "https://www.discourse.org")
|
2020-01-02 10:03:13 -06:00
|
|
|
expect(result2).to eq(true)
|
2019-12-12 20:12:12 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result3 = described_class.check_url("*", "https://hello.discourse.org")
|
2020-01-02 10:03:13 -06:00
|
|
|
expect(result3).to eq(true)
|
2019-12-12 20:12:12 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result4 =
|
|
|
|
described_class.check_url("discourse://auth_redirect", "discourse://auth_redirect")
|
2020-01-02 10:03:13 -06:00
|
|
|
expect(result4).to eq(true)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result5 =
|
|
|
|
described_class.check_url(
|
|
|
|
"customprotocol://www.discourse.org",
|
|
|
|
"customprotocol://www.discourse.org",
|
|
|
|
)
|
2020-01-02 10:03:13 -06:00
|
|
|
expect(result5).to eq(true)
|
2019-12-12 20:12:12 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
context "when url is invalid" do
|
2020-01-02 10:03:13 -06:00
|
|
|
it "returns false" do
|
2023-01-09 05:18:21 -06:00
|
|
|
result1 =
|
|
|
|
described_class.check_url(
|
|
|
|
"https://*.discourse.org",
|
|
|
|
"https://bad-domain.discourse.org.evil.com",
|
|
|
|
)
|
2020-01-02 10:03:13 -06:00
|
|
|
expect(result1).to eq(false)
|
2019-12-12 20:12:12 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result2 =
|
|
|
|
described_class.check_url(
|
|
|
|
"https://www.discourse.org",
|
|
|
|
"https://www.discourse.org.evil.com",
|
|
|
|
)
|
2020-01-02 10:03:13 -06:00
|
|
|
expect(result2).to eq(false)
|
2019-12-12 20:12:12 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result3 =
|
|
|
|
described_class.check_url("https://www.discourse.org", "https://www.www.discourse.org")
|
2020-01-02 10:03:13 -06:00
|
|
|
expect(result3).to eq(false)
|
2019-12-12 20:12:12 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result4 =
|
|
|
|
described_class.check_url(
|
|
|
|
"https://www.discourse.org",
|
|
|
|
"https://www.discourse.org\nwww.discourse.org.evil.com",
|
|
|
|
)
|
2020-01-02 10:03:13 -06:00
|
|
|
expect(result4).to eq(false)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result5 = described_class.check_url("https://", "https://")
|
2020-01-02 10:03:13 -06:00
|
|
|
expect(result5).to eq(false)
|
2019-12-12 20:12:12 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result6 =
|
|
|
|
described_class.check_url(
|
|
|
|
"invalid$protocol://www.discourse.org",
|
|
|
|
"invalid$protocol://www.discourse.org",
|
|
|
|
)
|
2020-01-02 10:03:13 -06:00
|
|
|
expect(result6).to eq(false)
|
2019-12-12 20:12:12 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result7 = described_class.check_url("noscheme", "noscheme")
|
2020-01-02 10:03:13 -06:00
|
|
|
expect(result7).to eq(false)
|
2019-12-12 20:12:12 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|