2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe PasswordValidator do
|
2013-12-19 15:12:03 -05:00
|
|
|
subject(:validate) { validator.validate_each(record, :password, @password) }
|
|
|
|
|
2023-06-21 10:00:19 -04:00
|
|
|
let(:validator) { described_class.new(attributes: :password) }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "password required" do
|
2013-12-19 15:12:03 -05:00
|
|
|
let(:record) do
|
|
|
|
u = Fabricate.build(:user, password: @password)
|
|
|
|
u.password_required!
|
|
|
|
u
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2013-12-19 15:12:03 -05:00
|
|
|
|
2024-10-09 21:23:06 -04:00
|
|
|
it "adds an error when password is blank" do
|
|
|
|
@password = ""
|
2016-08-24 12:27:09 -04:00
|
|
|
validate
|
2024-10-09 21:23:06 -04:00
|
|
|
expect(record.errors[:password]).to be_present
|
2016-08-24 12:27:09 -04:00
|
|
|
end
|
2017-11-30 23:19:24 -05:00
|
|
|
|
2024-10-09 21:23:06 -04:00
|
|
|
it "adds an error when password is nil" do
|
|
|
|
@password = nil
|
2024-06-04 03:42:53 -04:00
|
|
|
validate
|
2024-10-09 21:23:06 -04:00
|
|
|
expect(record.errors[:password]).to be_present
|
2024-06-04 03:42:53 -04:00
|
|
|
end
|
|
|
|
|
2017-11-30 23:19:24 -05:00
|
|
|
it "validation required if password is required" do
|
|
|
|
expect(record.password_validation_required?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "validation not required after save until a new password is set" do
|
|
|
|
@password = "myoldpassword"
|
|
|
|
record.save!
|
|
|
|
record.reload
|
|
|
|
expect(record.password_validation_required?).to eq(false)
|
|
|
|
record.password = "mynewpassword"
|
|
|
|
expect(record.password_validation_required?).to eq(true)
|
|
|
|
end
|
2013-12-19 15:12:03 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "password not required" do
|
2013-12-19 15:12:03 -05:00
|
|
|
let(:record) { Fabricate.build(:user, password: @password) }
|
|
|
|
|
|
|
|
it "doesn't add an error if password is not required" do
|
|
|
|
@password = nil
|
|
|
|
validate
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(record.errors[:password]).not_to be_present
|
2013-12-19 15:12:03 -05:00
|
|
|
end
|
2017-11-30 23:19:24 -05:00
|
|
|
|
|
|
|
it "validation required if a password is set" do
|
|
|
|
@password = "mygameshow"
|
|
|
|
expect(record.password_validation_required?).to eq(true)
|
|
|
|
end
|
2013-12-19 15:12:03 -05:00
|
|
|
end
|
|
|
|
end
|