2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe CommonPasswords do
|
2013-12-20 15:34:34 -06:00
|
|
|
it "the passwords file should exist" do
|
2022-01-05 11:45:08 -06:00
|
|
|
expect(File.exist?(described_class::PASSWORD_FILE)).to eq(true)
|
2013-12-20 15:34:34 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#common_password?" do
|
2023-06-21 09:00:19 -05:00
|
|
|
subject(:common_password) { described_class.common_password? @password }
|
2013-12-20 15:34:34 -06:00
|
|
|
|
2023-06-21 09:00:19 -05:00
|
|
|
before { described_class.stubs(:redis).returns(stub_everything) }
|
2013-12-20 15:34:34 -06:00
|
|
|
|
|
|
|
it "returns false if password isn't in the common passwords list" do
|
2017-07-27 20:20:09 -05:00
|
|
|
described_class.stubs(:password_list).returns(stub_everything(include?: false))
|
2023-01-09 05:18:21 -06:00
|
|
|
@password = "uncommonPassword"
|
2023-06-21 09:00:19 -05:00
|
|
|
expect(common_password).to eq(false)
|
2013-12-20 15:34:34 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if password is nil" do
|
|
|
|
described_class.expects(:password_list).never
|
|
|
|
@password = nil
|
2023-06-21 09:00:19 -05:00
|
|
|
expect(common_password).to eq(false)
|
2013-12-20 15:34:34 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if password is blank" do
|
|
|
|
described_class.expects(:password_list).never
|
|
|
|
@password = ""
|
2023-06-21 09:00:19 -05:00
|
|
|
expect(common_password).to eq(false)
|
2013-12-20 15:34:34 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true if password is in the common passwords list" do
|
2017-07-27 20:20:09 -05:00
|
|
|
described_class.stubs(:password_list).returns(stub_everything(include?: true))
|
2013-12-20 15:34:34 -06:00
|
|
|
@password = "password"
|
2023-06-21 09:00:19 -05:00
|
|
|
expect(common_password).to eq(true)
|
2013-12-20 15:34:34 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "#password_list" do
|
2022-02-17 18:14:38 -06:00
|
|
|
before { Discourse.redis.flushdb }
|
|
|
|
after { Discourse.redis.flushdb }
|
|
|
|
|
2013-12-20 15:34:34 -06:00
|
|
|
it "loads the passwords file if redis doesn't have it" do
|
2022-02-17 18:14:38 -06:00
|
|
|
Discourse.redis.without_namespace.stubs(:scard).returns(0)
|
2023-01-09 05:18:21 -06:00
|
|
|
described_class.expects(:load_passwords).returns(["password"])
|
2013-12-20 15:34:34 -06:00
|
|
|
list = described_class.password_list
|
2015-01-09 10:34:37 -06:00
|
|
|
expect(list).to respond_to(:include?)
|
2013-12-20 15:34:34 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't load the passwords file if redis has it" do
|
2023-01-09 05:18:21 -06:00
|
|
|
Discourse.redis.without_namespace.stubs(:scard).returns(10_000)
|
2013-12-20 15:34:34 -06:00
|
|
|
described_class.expects(:load_passwords).never
|
|
|
|
list = described_class.password_list
|
2015-01-09 10:34:37 -06:00
|
|
|
expect(list).to respond_to(:include?)
|
2013-12-20 15:34:34 -06:00
|
|
|
end
|
2013-12-30 13:25:50 -06:00
|
|
|
|
|
|
|
it "loads the passwords file if redis has an empty list" do
|
2022-02-17 18:14:38 -06:00
|
|
|
Discourse.redis.without_namespace.stubs(:scard).returns(0)
|
2023-12-04 06:45:19 -06:00
|
|
|
described_class.expects(:load_passwords).returns([])
|
2013-12-30 13:25:50 -06:00
|
|
|
list = described_class.password_list
|
2015-01-09 10:34:37 -06:00
|
|
|
expect(list).to respond_to(:include?)
|
2013-12-30 13:25:50 -06:00
|
|
|
end
|
2013-12-20 15:34:34 -06:00
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "missing password file" do
|
2013-12-20 15:34:34 -06:00
|
|
|
it "tolerates it" do
|
|
|
|
File.stubs(:readlines).with(described_class::PASSWORD_FILE).raises(Errno::ENOENT)
|
2015-01-09 10:34:37 -06:00
|
|
|
expect(described_class.common_password?("password")).to eq(false)
|
2013-12-20 15:34:34 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|