DRY specs more by introducing "a property with data-types restriction" shared behavior
This commit is contained in:
parent
5a4d742db2
commit
761534f13c
8 changed files with 65 additions and 130 deletions
|
|
@ -2,10 +2,6 @@ module DiasporaFederation
|
|||
describe Validators::HCardValidator do
|
||||
let(:entity) { :h_card }
|
||||
|
||||
def hcard_stub(data={})
|
||||
entity_stub(entity, data)
|
||||
end
|
||||
|
||||
it_behaves_like "a common validator"
|
||||
|
||||
describe "#full_name" do
|
||||
|
|
@ -26,13 +22,10 @@ module DiasporaFederation
|
|||
|
||||
%i(photo_large_url photo_medium_url photo_small_url).each do |prop|
|
||||
describe "##{prop}" do
|
||||
it "must not be nil or empty" do
|
||||
[nil, ""].each do |val|
|
||||
validator = Validators::HCardValidator.new(hcard_stub(prop => val))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(prop)
|
||||
end
|
||||
it_behaves_like "a property with data-types restriction" do
|
||||
let(:property) { prop }
|
||||
let(:wrong_values) { [nil, ""] }
|
||||
let(:correct_values) { [] }
|
||||
end
|
||||
|
||||
it_behaves_like "a url path validator" do
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ module DiasporaFederation
|
|||
|
||||
context "#lat and #lng" do
|
||||
%i(lat lng).each do |prop|
|
||||
it "must not be empty" do
|
||||
validator = Validators::LocationValidator.new(entity_stub(entity, prop => ""))
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(prop)
|
||||
it_behaves_like "a property with data-types restriction" do
|
||||
let(:property) { prop }
|
||||
let(:wrong_values) { [""] }
|
||||
let(:correct_values) { [] }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -9,14 +9,6 @@ module DiasporaFederation
|
|||
let(:mandatory) { true }
|
||||
end
|
||||
|
||||
context "#target_type" do
|
||||
it "must not be empty" do
|
||||
validator = Validators::ParticipationValidator.new(entity_stub(entity, target_type: ""))
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:target_type)
|
||||
end
|
||||
end
|
||||
|
||||
context "#guid, #parent_guid" do
|
||||
%i(guid parent_guid).each do |prop|
|
||||
it_behaves_like "a guid validator" do
|
||||
|
|
@ -25,12 +17,12 @@ module DiasporaFederation
|
|||
end
|
||||
end
|
||||
|
||||
context "#author_signature and #parent_author_signature" do
|
||||
%i(author_signature parent_author_signature).each do |prop|
|
||||
it "must not be empty" do
|
||||
validator = Validators::ParticipationValidator.new(entity_stub(entity, prop => ""))
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(prop)
|
||||
context "#target_type and #author_signature and #parent_author_signature" do
|
||||
%i(target_type author_signature parent_author_signature).each do |prop|
|
||||
it_behaves_like "a property with data-types restriction" do
|
||||
let(:property) { prop }
|
||||
let(:wrong_values) { [""] }
|
||||
let(:correct_values) { [] }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -20,12 +20,10 @@ module DiasporaFederation
|
|||
end
|
||||
|
||||
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)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:profile)
|
||||
it_behaves_like "a property with data-types restriction" do
|
||||
let(:property) { :profile }
|
||||
let(:wrong_values) { [nil] }
|
||||
let(:correct_values) { [] }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -23,30 +23,20 @@ module DiasporaFederation
|
|||
|
||||
context "#remote_photo_path, #remote_photo_name" do
|
||||
%i(remote_photo_name remote_photo_path).each do |prop|
|
||||
it "must not be empty" do
|
||||
validator = Validators::PhotoValidator.new(entity_stub(entity, prop => ""))
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(prop)
|
||||
it_behaves_like "a property with data-types restriction" do
|
||||
let(:property) { prop }
|
||||
let(:wrong_values) { [""] }
|
||||
let(:correct_values) { [] }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "#height, #width" do
|
||||
%i(height width).each do |prop|
|
||||
it "validates an integer" do
|
||||
[123, "123"].each do |val|
|
||||
validator = Validators::PhotoValidator.new(entity_stub(entity, prop => val))
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it "fails for non numeric types" do
|
||||
[true, :num, "asdf"].each do |val|
|
||||
validator = Validators::PhotoValidator.new(entity_stub(entity, prop => val))
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(prop)
|
||||
end
|
||||
it_behaves_like "a property with data-types restriction" do
|
||||
let(:property) { prop }
|
||||
let(:wrong_values) { [true, :num, "asdf"] }
|
||||
let(:correct_values) { [123, "123"] }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -2,10 +2,6 @@ module DiasporaFederation
|
|||
describe Validators::ProfileValidator do
|
||||
let(:entity) { :profile_entity }
|
||||
|
||||
def profile_stub(data={})
|
||||
entity_stub(entity, data)
|
||||
end
|
||||
|
||||
it_behaves_like "a common validator"
|
||||
|
||||
it_behaves_like "a diaspora id validator" do
|
||||
|
|
@ -24,11 +20,10 @@ module DiasporaFederation
|
|||
|
||||
%i(image_url image_url_medium image_url_small).each do |prop|
|
||||
describe "##{prop}" do
|
||||
it "is allowed to be nil" do
|
||||
validator = Validators::ProfileValidator.new(profile_stub(prop => nil))
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
it_behaves_like "a property with data-types restriction" do
|
||||
let(:property) { prop }
|
||||
let(:wrong_values) { [] }
|
||||
let(:correct_values) { [nil] }
|
||||
end
|
||||
|
||||
it_behaves_like "a url path validator" do
|
||||
|
|
@ -59,31 +54,10 @@ module DiasporaFederation
|
|||
end
|
||||
|
||||
describe "#birthday" do
|
||||
it "may be empty or nil" do
|
||||
[nil, ""].each do |val|
|
||||
validator = Validators::ProfileValidator.new(profile_stub(birthday: val))
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it "may be a Date or date string" do
|
||||
[Date.parse("2013-06-29"), "2013-06-29"].each do |val|
|
||||
validator = Validators::ProfileValidator.new(profile_stub(birthday: val))
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it "must not be an arbitrary string or other object" do
|
||||
["asdf asdf", true, 1234].each do |val|
|
||||
validator = Validators::ProfileValidator.new(profile_stub(birthday: val))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:birthday)
|
||||
end
|
||||
it_behaves_like "a property with data-types restriction" do
|
||||
let(:property) { :birthday }
|
||||
let(:wrong_values) { ["asdf asdf", true, 1234] }
|
||||
let(:correct_values) { [nil, "", Date.parse("2013-06-29"), "2013-06-29"] }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -96,12 +70,11 @@ module DiasporaFederation
|
|||
end
|
||||
|
||||
describe "#tag_string" do
|
||||
it "must not contain more than 5 tags" do
|
||||
validator = Validators::ProfileValidator.new(
|
||||
profile_stub(tag_string: "#i #have #too #many #tags #in #my #profile"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:tag_string)
|
||||
# more than 5 tags are not allowed
|
||||
it_behaves_like "a property with data-types restriction" do
|
||||
let(:property) { :tag_string }
|
||||
let(:wrong_values) { ["#i #have #too #many #tags #in #my #profile"] }
|
||||
let(:correct_values) { [] }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -2,20 +2,13 @@ module DiasporaFederation
|
|||
describe Validators::WebFingerValidator do
|
||||
let(:entity) { :webfinger }
|
||||
|
||||
def webfinger_stub(data={})
|
||||
entity_stub(entity, data)
|
||||
end
|
||||
|
||||
it_behaves_like "a common validator"
|
||||
|
||||
describe "#acct_uri" do
|
||||
it "fails if it is nil or empty" do
|
||||
[nil, ""].each do |val|
|
||||
validator = Validators::WebFingerValidator.new(webfinger_stub(acct_uri: val))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:acct_uri)
|
||||
end
|
||||
it_behaves_like "a property with data-types restriction" do
|
||||
let(:property) { :acct_uri }
|
||||
let(:wrong_values) { [nil, ""] }
|
||||
let(:correct_values) { [] }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -34,32 +27,10 @@ module DiasporaFederation
|
|||
# optional urls
|
||||
%i(alias_url salmon_url).each do |prop|
|
||||
describe "##{prop}" do
|
||||
it "is allowed to be nil" do
|
||||
validator = described_class.new(webfinger_stub(prop => nil))
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "must not be empty" do
|
||||
validator = described_class.new(webfinger_stub(prop => ""))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(prop)
|
||||
end
|
||||
|
||||
it "fails for url with special chars" do
|
||||
validator = described_class.new(webfinger_stub(prop => "https://asdf$%.com"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(prop)
|
||||
end
|
||||
|
||||
it "fails for url without scheme" do
|
||||
validator = described_class.new(webfinger_stub(prop => "example.com"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(prop)
|
||||
it_behaves_like "a property with data-types restriction" do
|
||||
let(:property) { prop }
|
||||
let(:wrong_values) { ["", "https://asdf$%.com", "example.com"] }
|
||||
let(:correct_values) { [nil] }
|
||||
end
|
||||
|
||||
it_behaves_like "a url path validator" do
|
||||
|
|
|
|||
|
|
@ -16,6 +16,24 @@ shared_examples "a common validator" do
|
|||
end
|
||||
end
|
||||
|
||||
shared_examples "a property with data-types restriction" do
|
||||
it "fails if a wrong value is supplied" do
|
||||
wrong_values.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
|
||||
|
||||
it "validates if a correct value is supplied" do
|
||||
correct_values.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
|
||||
end
|
||||
|
||||
shared_examples "a diaspora id validator" do
|
||||
it "must not be nil or empty if mandatory" do
|
||||
[nil, ""].each do |val|
|
||||
|
|
|
|||
Loading…
Reference in a new issue