allow nil for diaspora_id in profile
refactor some tests
This commit is contained in:
parent
ede695b214
commit
8c63655886
12 changed files with 67 additions and 56 deletions
|
|
@ -6,7 +6,7 @@ module DiasporaFederation
|
|||
|
||||
rule :guid, :guid
|
||||
|
||||
rule :diaspora_id, :diaspora_id
|
||||
rule :diaspora_id, %i(not_empty diaspora_id)
|
||||
|
||||
rule :url, %i(not_nil nilableURI)
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ module Validation
|
|||
domain_literal = "\\[#{dcontent}+\\]"
|
||||
domain = "(?:#{dot_atom}|#{domain_literal})"
|
||||
port = "(:[#{digit}]+)?"
|
||||
addr_spec = "#{username}\\@#{domain}#{port}"
|
||||
addr_spec = "(#{username}\\@#{domain}#{port})?"
|
||||
|
||||
/\A#{addr_spec}\z/u
|
||||
end
|
||||
|
|
@ -33,7 +33,7 @@ module Validation
|
|||
|
||||
# Determines if value is a valid diaspora ID
|
||||
def valid_value?(value)
|
||||
!DIASPORA_ID.match(value).nil?
|
||||
value.nil? || !DIASPORA_ID.match(value).nil?
|
||||
end
|
||||
|
||||
# This rule has no params
|
||||
|
|
|
|||
|
|
@ -12,19 +12,20 @@ module DiasporaFederation
|
|||
|
||||
it_behaves_like "a diaspora id validator" do
|
||||
let(:property) { :diaspora_id }
|
||||
let(:mandatory) { true }
|
||||
end
|
||||
|
||||
it_behaves_like "a guid validator" do
|
||||
let(:property) { :guid }
|
||||
end
|
||||
|
||||
context "#url" do
|
||||
describe "#url" do
|
||||
it_behaves_like "a url validator without path" do
|
||||
let(:property) { :url }
|
||||
end
|
||||
end
|
||||
|
||||
context "#profile" do
|
||||
describe "#profile" do
|
||||
it "fails if profile is nil" do
|
||||
instance = OpenStruct.new(FactoryGirl.attributes_for(:person_entity, profile: nil))
|
||||
validator = Validators::PersonValidator.new(instance)
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ module DiasporaFederation
|
|||
|
||||
it_behaves_like "a diaspora id validator" do
|
||||
let(:property) { :diaspora_id }
|
||||
let(:mandatory) { false }
|
||||
end
|
||||
|
||||
%i(first_name last_name).each do |prop|
|
||||
|
|
|
|||
|
|
@ -27,20 +27,14 @@ describe Validation::Rule::Birthday do
|
|||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "validates an empty string" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(birthday: ""))
|
||||
validator.rule(:birthday, :birthday)
|
||||
it "allows nil and empty" do
|
||||
[nil, ""].each do |val|
|
||||
validator = Validation::Validator.new(OpenStruct.new(birthday: val))
|
||||
validator.rule(:birthday, :birthday)
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "validates nil" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(birthday: nil))
|
||||
validator.rule(:birthday, :birthday)
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it "fails for invalid date string" do
|
||||
|
|
|
|||
|
|
@ -63,12 +63,14 @@ describe Validation::Rule::Boolean do
|
|||
end
|
||||
end
|
||||
|
||||
it "fails for nil" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(boolean: nil))
|
||||
validator.rule(:boolean, :boolean)
|
||||
it "fails if nil or empty" do
|
||||
[nil, ""].each do |val|
|
||||
validator = Validation::Validator.new(OpenStruct.new(boolean: val))
|
||||
validator.rule(:boolean, :boolean)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:boolean)
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:boolean)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -74,5 +74,15 @@ describe Validation::Rule::DiasporaId do
|
|||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:diaspora_id)
|
||||
end
|
||||
|
||||
it "allows nil and empty" do
|
||||
[nil, ""].each do |val|
|
||||
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: val))
|
||||
validator.rule(:diaspora_id, :diaspora_id)
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -45,19 +45,13 @@ describe Validation::Rule::Guid do
|
|||
end
|
||||
|
||||
it "fails if the string is empty" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(guid: ""))
|
||||
validator.rule(:guid, :guid)
|
||||
[nil, ""].each do |val|
|
||||
validator = Validation::Validator.new(OpenStruct.new(guid: val))
|
||||
validator.rule(:guid, :guid)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:guid)
|
||||
end
|
||||
|
||||
it "fails if the string is nil" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(guid: nil))
|
||||
validator.rule(:guid, :guid)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:guid)
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:guid)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -34,5 +34,13 @@ describe Validation::Rule::NotNil do
|
|||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:not_nil)
|
||||
end
|
||||
|
||||
it "allows an empty string" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(not_nil: ""))
|
||||
validator.rule(:not_nil, :not_nil)
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -50,20 +50,14 @@ describe Validation::Rule::PublicKey do
|
|||
expect(validator.errors).to include(:key)
|
||||
end
|
||||
|
||||
it "fails if the key is empty" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(key: ""))
|
||||
validator.rule(:key, :public_key)
|
||||
it "fails if the key is nil or empty" do
|
||||
[nil, ""].each do |val|
|
||||
validator = Validation::Validator.new(OpenStruct.new(key: val))
|
||||
validator.rule(:key, :public_key)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:key)
|
||||
end
|
||||
|
||||
it "fails if the key is nil" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(key: nil))
|
||||
validator.rule(:key, :public_key)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:key)
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:key)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -46,12 +46,14 @@ describe Validation::Rule::TagCount do
|
|||
expect(validator.errors).to include(:tags)
|
||||
end
|
||||
|
||||
it "validates if tags are nil" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(tags: nil))
|
||||
validator.rule(:tags, tag_count: {maximum: 5})
|
||||
it "allows nil and empty" do
|
||||
[nil, ""].each do |val|
|
||||
validator = Validation::Validator.new(OpenStruct.new(tags: val))
|
||||
validator.rule(:tags, tag_count: {maximum: 5})
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -11,12 +11,17 @@ def alphanumeric_string(length)
|
|||
end
|
||||
|
||||
shared_examples "a diaspora id validator" do
|
||||
it "must not be nil or empty" do
|
||||
it "must not be nil or empty if mandatory" do
|
||||
[nil, ""].each do |val|
|
||||
validator = described_class.new(entity_stub(entity, property, val))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
if mandatory
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
else
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue