refactoring validation and write more tests
This commit is contained in:
parent
ac8832ee4a
commit
20d4646332
10 changed files with 104 additions and 36 deletions
|
|
@ -3,13 +3,6 @@ require "date"
|
|||
module Validation
|
||||
module Rule
|
||||
class Birthday
|
||||
attr_reader :params
|
||||
|
||||
# no parameters
|
||||
def initialize
|
||||
@params = {}
|
||||
end
|
||||
|
||||
def error_key
|
||||
:birthday
|
||||
end
|
||||
|
|
@ -25,6 +18,11 @@ module Validation
|
|||
|
||||
false
|
||||
end
|
||||
|
||||
# This rule has no params
|
||||
def params
|
||||
{}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,13 +1,6 @@
|
|||
module Validation
|
||||
module Rule
|
||||
class Boolean
|
||||
attr_reader :params
|
||||
|
||||
# no parameters
|
||||
def initialize
|
||||
@params = {}
|
||||
end
|
||||
|
||||
def error_key
|
||||
:numeric
|
||||
end
|
||||
|
|
@ -25,6 +18,11 @@ module Validation
|
|||
false
|
||||
end
|
||||
end
|
||||
|
||||
# This rule has no params
|
||||
def params
|
||||
{}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,13 +1,6 @@
|
|||
module Validation
|
||||
module Rule
|
||||
class Guid
|
||||
attr_reader :params
|
||||
|
||||
# no parameters
|
||||
def initialize
|
||||
@params = {}
|
||||
end
|
||||
|
||||
def error_key
|
||||
:guid
|
||||
end
|
||||
|
|
@ -15,6 +8,11 @@ module Validation
|
|||
def valid_value?(value)
|
||||
value.is_a?(String) && value.downcase =~ /\A[0-9a-z\-_@.:]{16,}\z/
|
||||
end
|
||||
|
||||
# This rule has no params
|
||||
def params
|
||||
{}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,13 +1,6 @@
|
|||
module Validation
|
||||
module Rule
|
||||
class NotNil
|
||||
attr_reader :params
|
||||
|
||||
# no parameters
|
||||
def initialize
|
||||
@params = {}
|
||||
end
|
||||
|
||||
def error_key
|
||||
:not_nil
|
||||
end
|
||||
|
|
@ -15,6 +8,11 @@ module Validation
|
|||
def valid_value?(value)
|
||||
!value.nil?
|
||||
end
|
||||
|
||||
# This rule has no params
|
||||
def params
|
||||
{}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,13 +1,6 @@
|
|||
module Validation
|
||||
module Rule
|
||||
class PublicKey
|
||||
attr_reader :params
|
||||
|
||||
# no parameters
|
||||
def initialize
|
||||
@params = {}
|
||||
end
|
||||
|
||||
def error_key
|
||||
:public_key
|
||||
end
|
||||
|
|
@ -19,6 +12,11 @@ module Validation
|
|||
(value.strip.start_with?("-----BEGIN RSA PUBLIC KEY-----") &&
|
||||
value.strip.end_with?("-----END RSA PUBLIC KEY-----"))
|
||||
end
|
||||
|
||||
# This rule has no params
|
||||
def params
|
||||
{}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ module Validation
|
|||
# @option params [Fixnum] :maximum maximum allowed tag count
|
||||
def initialize(params)
|
||||
unless params.include?(:maximum) && params[:maximum].is_a?(Fixnum)
|
||||
raise "A number has to be specified for :maximum"
|
||||
raise ArgumentError, "A number has to be specified for :maximum"
|
||||
end
|
||||
|
||||
@params = params
|
||||
|
|
|
|||
|
|
@ -20,6 +20,34 @@ module DiasporaFederation
|
|||
let(:property) { :guid }
|
||||
end
|
||||
|
||||
context "#url" do
|
||||
it "fails for url with special chars" do
|
||||
instance = OpenStruct.new(FactoryGirl.attributes_for(:person_entity, url: "https://asdf$%.com"))
|
||||
validator = Validators::PersonValidator.new(instance)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:url)
|
||||
end
|
||||
|
||||
it "fails for url without scheme" do
|
||||
instance = OpenStruct.new(FactoryGirl.attributes_for(:person_entity, url: "example.com"))
|
||||
validator = Validators::PersonValidator.new(instance)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:url)
|
||||
end
|
||||
end
|
||||
|
||||
context "#profile" do
|
||||
it "fails if profile is nil" do
|
||||
instance = OpenStruct.new(FactoryGirl.attributes_for(:person_entity, profile: nil))
|
||||
validator = Validators::PersonValidator.new(instance)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:profile)
|
||||
end
|
||||
end
|
||||
|
||||
context "#exported_key" do
|
||||
it "fails for malformed rsa key" do
|
||||
instance = OpenStruct.new(FactoryGirl.attributes_for(:person_entity, exported_key: "ASDF"))
|
||||
|
|
|
|||
|
|
@ -19,6 +19,13 @@ module DiasporaFederation
|
|||
|
||||
%i(first_name last_name).each do |prop|
|
||||
describe "##{prop}" do
|
||||
it "allowed to be nil" do
|
||||
validator = Validators::ProfileValidator.new(profile_stub(prop => nil))
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "allowed to contain special chars" do
|
||||
validator = Validators::ProfileValidator.new(profile_stub(prop => "cool name ©"))
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
describe Validation::Rule::NotNil do
|
||||
it "will not accept parameters" do
|
||||
validator = Validation::Validator.new({})
|
||||
expect {
|
||||
validator.rule(:not_nil, not_nil: {param: true})
|
||||
}.to raise_error ArgumentError
|
||||
end
|
||||
|
||||
context "validation" do
|
||||
it "validates a string " do
|
||||
validator = Validation::Validator.new(OpenStruct.new(not_nil: "abcd"))
|
||||
validator.rule(:not_nil, :not_nil)
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "validates a object " do
|
||||
validator = Validation::Validator.new(OpenStruct.new(not_nil: Object.new))
|
||||
validator.rule(:not_nil, :not_nil)
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "fails if it is nil" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(not_nil: nil))
|
||||
validator.rule(:not_nil, :not_nil)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:not_nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -6,6 +6,15 @@ describe Validation::Rule::TagCount do
|
|||
}.to raise_error ArgumentError
|
||||
end
|
||||
|
||||
it "requires a integer as parameter" do
|
||||
validator = Validation::Validator.new({})
|
||||
[nil, "", 5.5].each do |val|
|
||||
expect {
|
||||
validator.rule(:tags, tag_count: {maximum: val})
|
||||
}.to raise_error ArgumentError, "A number has to be specified for :maximum"
|
||||
end
|
||||
end
|
||||
|
||||
context "validation" do
|
||||
let(:tag_str) { "#i #love #tags" }
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue