mirror of
https://github.com/discourse/discourse.git
synced 2025-06-04 06:56:01 +08:00
Add a way to validate min and max value of an integer site setting
This commit is contained in:
@ -2,17 +2,19 @@ require 'spec_helper'
|
||||
|
||||
describe EmailSettingValidator do
|
||||
describe '#valid_value?' do
|
||||
subject(:validator) { described_class.new }
|
||||
|
||||
it "returns true for blank values" do
|
||||
described_class.valid_value?('').should == true
|
||||
described_class.valid_value?(nil).should == true
|
||||
validator.valid_value?('').should == true
|
||||
validator.valid_value?(nil).should == true
|
||||
end
|
||||
|
||||
it "returns true if value is a valid email address" do
|
||||
described_class.valid_value?('vader@example.com').should == true
|
||||
validator.valid_value?('vader@example.com').should == true
|
||||
end
|
||||
|
||||
it "returns false if value is not a valid email address" do
|
||||
described_class.valid_value?('my house').should == false
|
||||
validator.valid_value?('my house').should == false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
89
spec/components/validators/integer_setting_validator_spec.rb
Normal file
89
spec/components/validators/integer_setting_validator_spec.rb
Normal file
@ -0,0 +1,89 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe IntegerSettingValidator do
|
||||
describe '#valid_value?' do
|
||||
|
||||
shared_examples "for all IntegerSettingValidator opts" do
|
||||
it "returns false for blank values" do
|
||||
validator.valid_value?('').should == false
|
||||
validator.valid_value?(nil).should == false
|
||||
end
|
||||
|
||||
it "returns false if value is not a valid integer" do
|
||||
validator.valid_value?('two').should == false
|
||||
end
|
||||
end
|
||||
|
||||
context "without min and max" do
|
||||
subject(:validator) { described_class.new }
|
||||
|
||||
include_examples "for all IntegerSettingValidator opts"
|
||||
|
||||
it "returns true if value is a valid integer" do
|
||||
validator.valid_value?(1).should == true
|
||||
validator.valid_value?(-1).should == true
|
||||
validator.valid_value?('1').should == true
|
||||
validator.valid_value?('-1').should == true
|
||||
end
|
||||
end
|
||||
|
||||
context "with min" do
|
||||
subject(:validator) { described_class.new(min: 2) }
|
||||
|
||||
include_examples "for all IntegerSettingValidator opts"
|
||||
|
||||
it "returns true if value is equal to min" do
|
||||
validator.valid_value?(2).should == true
|
||||
validator.valid_value?('2').should == true
|
||||
end
|
||||
|
||||
it "returns true if value is greater than min" do
|
||||
validator.valid_value?(3).should == true
|
||||
validator.valid_value?('3').should == true
|
||||
end
|
||||
|
||||
it "returns false if value is less than min" do
|
||||
validator.valid_value?(1).should == false
|
||||
validator.valid_value?('1').should == false
|
||||
end
|
||||
end
|
||||
|
||||
context "with max" do
|
||||
subject(:validator) { described_class.new(max: 3) }
|
||||
|
||||
include_examples "for all IntegerSettingValidator opts"
|
||||
|
||||
it "returns true if value is equal to max" do
|
||||
validator.valid_value?(3).should == true
|
||||
validator.valid_value?('3').should == true
|
||||
end
|
||||
|
||||
it "returns true if value is less than max" do
|
||||
validator.valid_value?(2).should == true
|
||||
validator.valid_value?('2').should == true
|
||||
end
|
||||
|
||||
it "returns false if value is greater than min" do
|
||||
validator.valid_value?(4).should == false
|
||||
validator.valid_value?('4').should == false
|
||||
end
|
||||
end
|
||||
|
||||
context "with min and max" do
|
||||
subject(:validator) { described_class.new(min: -1, max: 3) }
|
||||
|
||||
include_examples "for all IntegerSettingValidator opts"
|
||||
|
||||
it "returns true if value is in range" do
|
||||
validator.valid_value?(-1).should == true
|
||||
validator.valid_value?(0).should == true
|
||||
validator.valid_value?(3).should == true
|
||||
end
|
||||
|
||||
it "returns false if value is out of range" do
|
||||
validator.valid_value?(4).should == false
|
||||
validator.valid_value?(-2).should == false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -2,18 +2,20 @@ require 'spec_helper'
|
||||
|
||||
describe UsernameSettingValidator do
|
||||
describe '#valid_value?' do
|
||||
subject(:validator) { described_class.new }
|
||||
|
||||
it "returns true for blank values" do
|
||||
described_class.valid_value?('').should == true
|
||||
described_class.valid_value?(nil).should == true
|
||||
validator.valid_value?('').should == true
|
||||
validator.valid_value?(nil).should == true
|
||||
end
|
||||
|
||||
it "returns true if value matches an existing user's username" do
|
||||
Fabricate(:user, username: 'vader')
|
||||
described_class.valid_value?('vader').should == true
|
||||
validator.valid_value?('vader').should == true
|
||||
end
|
||||
|
||||
it "returns false if value does not match a user's username" do
|
||||
described_class.valid_value?('no way').should == false
|
||||
validator.valid_value?('no way').should == false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user