refactoring validation and write more tests

This commit is contained in:
Benjamin Neff 2015-07-21 02:38:35 +02:00
parent ac8832ee4a
commit 20d4646332
10 changed files with 104 additions and 36 deletions

View file

@ -3,13 +3,6 @@ require "date"
module Validation module Validation
module Rule module Rule
class Birthday class Birthday
attr_reader :params
# no parameters
def initialize
@params = {}
end
def error_key def error_key
:birthday :birthday
end end
@ -25,6 +18,11 @@ module Validation
false false
end end
# This rule has no params
def params
{}
end
end end
end end
end end

View file

@ -1,13 +1,6 @@
module Validation module Validation
module Rule module Rule
class Boolean class Boolean
attr_reader :params
# no parameters
def initialize
@params = {}
end
def error_key def error_key
:numeric :numeric
end end
@ -25,6 +18,11 @@ module Validation
false false
end end
end end
# This rule has no params
def params
{}
end
end end
end end
end end

View file

@ -1,13 +1,6 @@
module Validation module Validation
module Rule module Rule
class Guid class Guid
attr_reader :params
# no parameters
def initialize
@params = {}
end
def error_key def error_key
:guid :guid
end end
@ -15,6 +8,11 @@ module Validation
def valid_value?(value) def valid_value?(value)
value.is_a?(String) && value.downcase =~ /\A[0-9a-z\-_@.:]{16,}\z/ value.is_a?(String) && value.downcase =~ /\A[0-9a-z\-_@.:]{16,}\z/
end end
# This rule has no params
def params
{}
end
end end
end end
end end

View file

@ -1,13 +1,6 @@
module Validation module Validation
module Rule module Rule
class NotNil class NotNil
attr_reader :params
# no parameters
def initialize
@params = {}
end
def error_key def error_key
:not_nil :not_nil
end end
@ -15,6 +8,11 @@ module Validation
def valid_value?(value) def valid_value?(value)
!value.nil? !value.nil?
end end
# This rule has no params
def params
{}
end
end end
end end
end end

View file

@ -1,13 +1,6 @@
module Validation module Validation
module Rule module Rule
class PublicKey class PublicKey
attr_reader :params
# no parameters
def initialize
@params = {}
end
def error_key def error_key
:public_key :public_key
end end
@ -19,6 +12,11 @@ module Validation
(value.strip.start_with?("-----BEGIN RSA PUBLIC KEY-----") && (value.strip.start_with?("-----BEGIN RSA PUBLIC KEY-----") &&
value.strip.end_with?("-----END RSA PUBLIC KEY-----")) value.strip.end_with?("-----END RSA PUBLIC KEY-----"))
end end
# This rule has no params
def params
{}
end
end end
end end
end end

View file

@ -9,7 +9,7 @@ module Validation
# @option params [Fixnum] :maximum maximum allowed tag count # @option params [Fixnum] :maximum maximum allowed tag count
def initialize(params) def initialize(params)
unless params.include?(:maximum) && params[:maximum].is_a?(Fixnum) 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 end
@params = params @params = params

View file

@ -20,6 +20,34 @@ module DiasporaFederation
let(:property) { :guid } let(:property) { :guid }
end 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 context "#exported_key" do
it "fails for malformed rsa key" do it "fails for malformed rsa key" do
instance = OpenStruct.new(FactoryGirl.attributes_for(:person_entity, exported_key: "ASDF")) instance = OpenStruct.new(FactoryGirl.attributes_for(:person_entity, exported_key: "ASDF"))

View file

@ -19,6 +19,13 @@ module DiasporaFederation
%i(first_name last_name).each do |prop| %i(first_name last_name).each do |prop|
describe "##{prop}" do 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 it "allowed to contain special chars" do
validator = Validators::ProfileValidator.new(profile_stub(prop => "cool name ©")) validator = Validators::ProfileValidator.new(profile_stub(prop => "cool name ©"))

View file

@ -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

View file

@ -6,6 +6,15 @@ describe Validation::Rule::TagCount do
}.to raise_error ArgumentError }.to raise_error ArgumentError
end 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 context "validation" do
let(:tag_str) { "#i #love #tags" } let(:tag_str) { "#i #love #tags" }