refactor shared examples and use more of them
This commit is contained in:
parent
dd6b938f2e
commit
d7a5e71ce6
5 changed files with 121 additions and 101 deletions
|
|
@ -15,10 +15,12 @@ module Validation
|
|||
|
||||
# Determines if value is a valid public key
|
||||
def valid_value?(value)
|
||||
(value.strip.start_with?("-----BEGIN PUBLIC KEY-----") &&
|
||||
value.strip.end_with?("-----END PUBLIC KEY-----")) ||
|
||||
(value.strip.start_with?("-----BEGIN RSA PUBLIC KEY-----") &&
|
||||
value.strip.end_with?("-----END RSA PUBLIC KEY-----"))
|
||||
!value.nil? && (
|
||||
(value.strip.start_with?("-----BEGIN PUBLIC KEY-----") &&
|
||||
value.strip.end_with?("-----END PUBLIC KEY-----")) ||
|
||||
(value.strip.start_with?("-----BEGIN RSA PUBLIC KEY-----") &&
|
||||
value.strip.end_with?("-----END RSA PUBLIC KEY-----"))
|
||||
)
|
||||
end
|
||||
|
||||
# This rule has no params
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
module DiasporaFederation
|
||||
describe Validators::PersonValidator do
|
||||
let(:entity) { :person_entity }
|
||||
|
||||
it "validates a well-formed instance" do
|
||||
instance = OpenStruct.new(FactoryGirl.attributes_for(:person_entity))
|
||||
validator = Validators::PersonValidator.new(instance)
|
||||
|
|
@ -8,33 +10,17 @@ module DiasporaFederation
|
|||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it_behaves_like "a diaspora_id validator" do
|
||||
let(:entity) { :person_entity }
|
||||
let(:validator_class) { Validators::PersonValidator }
|
||||
it_behaves_like "a diaspora id validator" do
|
||||
let(:property) { :diaspora_id }
|
||||
end
|
||||
|
||||
it_behaves_like "a guid validator" do
|
||||
let(:entity) { :person_entity }
|
||||
let(:validator_class) { Validators::PersonValidator }
|
||||
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)
|
||||
it_behaves_like "a url validator without path" do
|
||||
let(:property) { :url }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -48,22 +34,8 @@ module DiasporaFederation
|
|||
end
|
||||
end
|
||||
|
||||
context "#exported_key" do
|
||||
it "fails for malformed rsa key" do
|
||||
instance = OpenStruct.new(FactoryGirl.attributes_for(:person_entity, exported_key: "ASDF"))
|
||||
validator = Validators::PersonValidator.new(instance)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:exported_key)
|
||||
end
|
||||
|
||||
it "must not be empty" do
|
||||
instance = OpenStruct.new(FactoryGirl.attributes_for(:person_entity, exported_key: ""))
|
||||
validator = Validators::PersonValidator.new(instance)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:exported_key)
|
||||
end
|
||||
it_behaves_like "a public key validator" do
|
||||
let(:property) { :exported_key }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
module DiasporaFederation
|
||||
describe Validators::ProfileValidator do
|
||||
let(:entity) { :profile_entity }
|
||||
|
||||
def profile_stub(data={})
|
||||
OpenStruct.new(FactoryGirl.attributes_for(:profile_entity).merge(data))
|
||||
end
|
||||
|
|
@ -11,40 +13,14 @@ module DiasporaFederation
|
|||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it_behaves_like "a diaspora_id validator" do
|
||||
let(:entity) { :profile_entity }
|
||||
let(:validator_class) { Validators::ProfileValidator }
|
||||
it_behaves_like "a diaspora id validator" do
|
||||
let(:property) { :diaspora_id }
|
||||
end
|
||||
|
||||
%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 ©"))
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "must not exceed 32 chars" do
|
||||
validator = Validators::ProfileValidator.new(profile_stub(prop => "abcdefghijklmnopqrstuvwxyz_aaaaaaaaaa"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(prop)
|
||||
end
|
||||
|
||||
it "must not contain semicolons" do
|
||||
validator = Validators::ProfileValidator.new(profile_stub(prop => "asdf;qwer;yxcv"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(prop)
|
||||
it_behaves_like "a name validator" do
|
||||
let(:property) { prop }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -91,8 +67,6 @@ module DiasporaFederation
|
|||
%i(searchable nsfw).each do |prop|
|
||||
describe "##{prop}" do
|
||||
it_behaves_like "a boolean validator" do
|
||||
let(:entity) { :profile_entity }
|
||||
let(:validator_class) { Validators::ProfileValidator }
|
||||
let(:property) { prop }
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -45,6 +45,22 @@ describe Validation::Rule::Guid do
|
|||
expect(validator).not_to be_valid
|
||||
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)
|
||||
|
||||
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)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,26 +1,21 @@
|
|||
def entity_stub(entity, property, val=nil)
|
||||
def entity_stub(entity, property, val)
|
||||
instance = OpenStruct.new(FactoryGirl.attributes_for(entity))
|
||||
instance.public_send("#{property}=", val) unless val.nil?
|
||||
instance.public_send("#{property}=", val)
|
||||
instance
|
||||
end
|
||||
|
||||
shared_examples "a diaspora_id validator" do
|
||||
it "validates a well-formed diaspora_id" do
|
||||
validator = validator_class.new(entity_stub(entity, property))
|
||||
shared_examples "a diaspora id validator" do
|
||||
it "must not be nil or empty" do
|
||||
[nil, ""].each do |val|
|
||||
validator = described_class.new(entity_stub(entity, property, val))
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "must not be empty" do
|
||||
validator = validator_class.new(entity_stub(entity, property, ""))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
end
|
||||
end
|
||||
|
||||
it "must be a valid diaspora id" do
|
||||
validator = validator_class.new(entity_stub(entity, property, "i am a weird diaspora id @@@ ### 12345"))
|
||||
validator = described_class.new(entity_stub(entity, property, "i am a weird diaspora id @@@ ### 12345"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
|
|
@ -28,46 +23,41 @@ shared_examples "a diaspora_id validator" do
|
|||
end
|
||||
|
||||
shared_examples "a guid validator" do
|
||||
it "validates a well-formed guid" do
|
||||
validator = validator_class.new(entity_stub(entity, property))
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "validates a well-formed guid from redmatrix" do
|
||||
validator = validator_class.new(entity_stub(entity, property, "1234567890ABCDefgh_ijkl-mnopQR@example.com:3000"))
|
||||
validator = described_class.new(entity_stub(entity, property, "1234567890ABCDefgh_ijkl-mnopQR@example.com:3000"))
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "must be at least 16 chars" do
|
||||
validator = validator_class.new(entity_stub(entity, property, "aaaaaa"))
|
||||
validator = described_class.new(entity_stub(entity, property, "aaaaaa"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
end
|
||||
|
||||
it "must only contain [0-9a-z-_@.:]" do
|
||||
validator = validator_class.new(entity_stub(entity, property, "zzz+-#*$$"))
|
||||
validator = described_class.new(entity_stub(entity, property, "zzz+-#*$$"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
end
|
||||
|
||||
it "must not be empty" do
|
||||
validator = validator_class.new(entity_stub(entity, property, ""))
|
||||
it "must not be nil or empty" 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)
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples "a boolean validator" do
|
||||
it "validates a well-formed boolean" do
|
||||
[true, "true", false, "false"].each do |val|
|
||||
validator = validator_class.new(entity_stub(entity, property, val))
|
||||
validator = described_class.new(entity_stub(entity, property, val))
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
|
|
@ -76,10 +66,76 @@ shared_examples "a boolean validator" do
|
|||
|
||||
it "must not be an arbitrary string or other object" do
|
||||
["asdf", Time.zone.today, 1234].each do |val|
|
||||
validator = validator_class.new(entity_stub(entity, property, val))
|
||||
validator = described_class.new(entity_stub(entity, property, val))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples "a public key validator" do
|
||||
it "fails for malformed rsa key" do
|
||||
validator = described_class.new(entity_stub(entity, property, "ASDF"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
end
|
||||
|
||||
it "must not be nil or empty" 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)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples "a name validator" do
|
||||
it "is allowed to be nil or empty" do
|
||||
[nil, ""].each do |val|
|
||||
validator = described_class.new(entity_stub(entity, property, val))
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it "is allowed to contain special chars" do
|
||||
validator = described_class.new(entity_stub(entity, property, "cool name ©"))
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "must not exceed 32 chars" do
|
||||
validator = described_class.new(entity_stub(entity, property, "abcdefghijklmnopqrstuvwxyz_aaaaaaaaaa"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
end
|
||||
|
||||
it "must not contain semicolons" do
|
||||
validator = described_class.new(entity_stub(entity, property, "asdf;qwer;yxcv"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples "a url validator without path" do
|
||||
it "fails for url with special chars" do
|
||||
validator = described_class.new(entity_stub(entity, property, "https://asdf$%.com"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
end
|
||||
|
||||
it "fails for url without scheme" do
|
||||
validator = described_class.new(entity_stub(entity, property, "example.com"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue