2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe ScreenedUrl do
|
2023-01-09 05:18:21 -06:00
|
|
|
let(:url) { "http://shopppping.com/bad/drugz" }
|
|
|
|
let(:domain) { "shopppping.com" }
|
2013-08-14 10:05:53 -05:00
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
let(:valid_params) { { url: url, domain: domain } }
|
2013-08-14 10:05:53 -05:00
|
|
|
|
|
|
|
describe "new record" do
|
|
|
|
it "sets a default action_type" do
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(described_class.create(valid_params).action_type).to eq(
|
|
|
|
described_class.actions[:do_nothing],
|
|
|
|
)
|
2013-08-14 10:05:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "last_match_at is null" do
|
2015-01-05 10:04:23 -06:00
|
|
|
expect(described_class.create(valid_params).last_match_at).to eq(nil)
|
2013-08-14 10:05:53 -05:00
|
|
|
end
|
|
|
|
|
2013-10-16 15:39:13 -05:00
|
|
|
it "normalizes the url and domain" do
|
|
|
|
record = described_class.new(valid_params)
|
|
|
|
record.expects(:normalize).once
|
|
|
|
record.valid?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "normalize" do
|
2023-06-21 09:00:19 -05:00
|
|
|
subject(:normalized) do
|
2023-01-09 05:18:21 -06:00
|
|
|
record.normalize
|
|
|
|
record
|
|
|
|
end
|
2013-10-16 15:39:13 -05:00
|
|
|
|
2023-06-21 09:00:19 -05:00
|
|
|
let(:record) { described_class.new(@params) }
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
%w[http:// HTTP:// https:// HTTPS://].each do |prefix|
|
2013-08-14 10:05:53 -05:00
|
|
|
it "strips #{prefix}" do
|
2023-01-09 05:18:21 -06:00
|
|
|
@params = valid_params.merge(url: url.gsub("http://", prefix))
|
2023-06-21 09:00:19 -05:00
|
|
|
expect(normalized.url).to eq(url.gsub("http://", ""))
|
2013-08-14 10:05:53 -05:00
|
|
|
end
|
|
|
|
end
|
2013-09-17 09:25:45 -05:00
|
|
|
|
|
|
|
it "strips trailing slash" do
|
2023-01-09 05:18:21 -06:00
|
|
|
@params = valid_params.merge(url: "silverbullet.in/")
|
2023-06-21 09:00:19 -05:00
|
|
|
expect(normalized.url).to eq("silverbullet.in")
|
2013-09-17 09:25:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "strips trailing slashes" do
|
2023-01-09 05:18:21 -06:00
|
|
|
@params = valid_params.merge(url: "silverbullet.in/buy///")
|
2023-06-21 09:00:19 -05:00
|
|
|
expect(normalized.url).to eq("silverbullet.in/buy")
|
2013-10-16 15:39:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "downcases domains" do
|
2023-01-09 05:18:21 -06:00
|
|
|
record1 =
|
|
|
|
described_class.new(
|
|
|
|
valid_params.merge(domain: "DuB30.com", url: "DuB30.com/Gems/Gems-of-Power"),
|
|
|
|
)
|
2013-10-16 15:39:13 -05:00
|
|
|
record1.normalize
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(record1.domain).to eq("dub30.com")
|
|
|
|
expect(record1.url).to eq("dub30.com/Gems/Gems-of-Power")
|
2015-01-05 10:04:23 -06:00
|
|
|
expect(record1).to be_valid
|
2013-10-16 15:39:13 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
record2 = described_class.new(valid_params.merge(domain: "DuB30.com", url: "DuB30.com"))
|
2013-10-16 15:39:13 -05:00
|
|
|
record2.normalize
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(record2.domain).to eq("dub30.com")
|
|
|
|
expect(record2.url).to eq("dub30.com")
|
2015-01-05 10:04:23 -06:00
|
|
|
expect(record2).to be_valid
|
2013-10-16 15:39:13 -05:00
|
|
|
end
|
|
|
|
|
2013-11-04 15:24:32 -06:00
|
|
|
it "strips www. from domains" do
|
2023-01-09 05:18:21 -06:00
|
|
|
record1 =
|
|
|
|
described_class.new(
|
|
|
|
valid_params.merge(domain: "www.DuB30.com", url: "www.DuB30.com/Gems/Gems-of-Power"),
|
|
|
|
)
|
2013-11-04 15:24:32 -06:00
|
|
|
record1.normalize
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(record1.domain).to eq("dub30.com")
|
2013-11-04 15:24:32 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
record2 =
|
|
|
|
described_class.new(
|
|
|
|
valid_params.merge(domain: "WWW.DuB30.cOM", url: "WWW.DuB30.com/Gems/Gems-of-Power"),
|
|
|
|
)
|
2013-11-04 15:24:32 -06:00
|
|
|
record2.normalize
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(record2.domain).to eq("dub30.com")
|
|
|
|
|
|
|
|
record3 =
|
|
|
|
described_class.new(
|
|
|
|
valid_params.merge(
|
|
|
|
domain: "www.trolls.spammers.com",
|
|
|
|
url: "WWW.DuB30.com/Gems/Gems-of-Power",
|
|
|
|
),
|
|
|
|
)
|
2013-11-04 15:24:32 -06:00
|
|
|
record3.normalize
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(record3.domain).to eq("trolls.spammers.com")
|
2013-11-04 15:24:32 -06:00
|
|
|
end
|
|
|
|
|
2013-10-16 15:39:13 -05:00
|
|
|
it "doesn't modify the url argument" do
|
2023-01-09 05:18:21 -06:00
|
|
|
expect { described_class.new(valid_params).normalize }.to_not change { valid_params[:url] }
|
2013-10-16 15:39:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't modify the domain argument" do
|
|
|
|
params = valid_params.merge(domain: domain.upcase)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect { described_class.new(params).normalize }.to_not change { params[:domain] }
|
2013-10-16 15:39:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "find_match" do
|
|
|
|
it "returns nil when there is no match" do
|
|
|
|
expect(described_class.find_match("http://spamspot.com/buy/it")).to eq(nil)
|
2013-10-16 15:39:13 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "returns the record when there is an exact match" do
|
2013-10-16 15:39:13 -05:00
|
|
|
match = described_class.create(valid_params)
|
2015-01-05 10:04:23 -06:00
|
|
|
expect(described_class.find_match(valid_params[:url])).to eq(match)
|
2013-10-16 15:39:13 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "ignores case of the domain" do
|
|
|
|
match = described_class.create(valid_params.merge(url: "spamexchange.com/Good/Things"))
|
2015-01-05 10:04:23 -06:00
|
|
|
expect(described_class.find_match("http://SPAMExchange.com/Good/Things")).to eq(match)
|
2013-09-17 09:25:45 -05:00
|
|
|
end
|
2013-08-14 10:05:53 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "#watch" do
|
|
|
|
context "when url is not being blocked" do
|
|
|
|
it "creates a new record with default action of :do_nothing" do
|
2013-08-14 10:05:53 -05:00
|
|
|
record = described_class.watch(url, domain)
|
2015-01-05 10:04:23 -06:00
|
|
|
expect(record).not_to be_new_record
|
|
|
|
expect(record.action_type).to eq(described_class.actions[:do_nothing])
|
2013-08-14 10:05:53 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "lets action_type be overridden" do
|
2013-08-14 10:05:53 -05:00
|
|
|
record = described_class.watch(url, domain, action_type: described_class.actions[:block])
|
2015-01-05 10:04:23 -06:00
|
|
|
expect(record).not_to be_new_record
|
|
|
|
expect(record.action_type).to eq(described_class.actions[:block])
|
2013-08-14 10:05:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
context "when url is already being blocked" do
|
2013-08-14 10:05:53 -05:00
|
|
|
let!(:existing) { Fabricate(:screened_url, url: url, domain: domain) }
|
|
|
|
|
|
|
|
it "doesn't create a new record" do
|
|
|
|
expect { described_class.watch(url, domain) }.to_not change { described_class.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the existing record" do
|
2015-01-05 10:04:23 -06:00
|
|
|
expect(described_class.watch(url, domain)).to eq(existing)
|
2013-08-14 10:05:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|