Reduce the entity stubs initialization calls to a single entity_stub

to make the code more DRY
This commit is contained in:
cmrd Senya 2015-11-03 01:23:10 +03:00
parent 9cb6fe4d1b
commit 9391d4b9eb
7 changed files with 46 additions and 83 deletions

View file

@ -3,15 +3,10 @@ module DiasporaFederation
let(:entity) { :h_card } let(:entity) { :h_card }
def hcard_stub(data={}) def hcard_stub(data={})
OpenStruct.new(FactoryGirl.attributes_for(:h_card).merge(data)) entity_stub(entity, data)
end end
it "validates a well-formed instance" do it_behaves_like "a common validator"
validator = Validators::HCardValidator.new(hcard_stub)
expect(validator).to be_valid
expect(validator.errors).to be_empty
end
describe "#full_name" do describe "#full_name" do
it_behaves_like "a name validator" do it_behaves_like "a name validator" do

View file

@ -6,10 +6,7 @@ module DiasporaFederation
context "#lat and #lng" do context "#lat and #lng" do
%i(lat lng).each do |prop| %i(lat lng).each do |prop|
it "must not be empty" do it "must not be empty" do
entity = OpenStruct.new(FactoryGirl.attributes_for(:location_entity)) validator = Validators::LocationValidator.new(entity_stub(entity, prop => ""))
entity.public_send("#{prop}=", "")
validator = Validators::LocationValidator.new(entity)
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(prop) expect(validator.errors).to include(prop)
end end

View file

@ -11,8 +11,7 @@ module DiasporaFederation
context "#target_type" do context "#target_type" do
it "must not be empty" do it "must not be empty" do
entity = OpenStruct.new(FactoryGirl.attributes_for(:participation_entity, target_type: "")) validator = Validators::ParticipationValidator.new(entity_stub(entity, target_type: ""))
validator = Validators::ParticipationValidator.new(entity)
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(:target_type) expect(validator.errors).to include(:target_type)
end end
@ -29,12 +28,9 @@ module DiasporaFederation
context "#author_signature and #parent_author_signature" do context "#author_signature and #parent_author_signature" do
%i(author_signature parent_author_signature).each do |prop| %i(author_signature parent_author_signature).each do |prop|
it "must not be empty" do it "must not be empty" do
p = OpenStruct.new(FactoryGirl.attributes_for(:participation_entity)) validator = Validators::ParticipationValidator.new(entity_stub(entity, prop => ""))
p.public_send("#{prop}=", "") expect(validator).not_to be_valid
expect(validator.errors).to include(prop)
v = Validators::ParticipationValidator.new(p)
expect(v).not_to be_valid
expect(v.errors).to include(prop)
end end
end end
end end

View file

@ -24,12 +24,9 @@ module DiasporaFederation
context "#remote_photo_path, #remote_photo_name" do context "#remote_photo_path, #remote_photo_name" do
%i(remote_photo_name remote_photo_path).each do |prop| %i(remote_photo_name remote_photo_path).each do |prop|
it "must not be empty" do it "must not be empty" do
p = OpenStruct.new(FactoryGirl.attributes_for(:photo_entity)) validator = Validators::PhotoValidator.new(entity_stub(entity, prop => ""))
p.public_send("#{prop}=", "") expect(validator).not_to be_valid
expect(validator.errors).to include(prop)
v = Validators::PhotoValidator.new(p)
expect(v).not_to be_valid
expect(v.errors).to include(prop)
end end
end end
end end
@ -38,23 +35,17 @@ module DiasporaFederation
%i(height width).each do |prop| %i(height width).each do |prop|
it "validates an integer" do it "validates an integer" do
[123, "123"].each do |val| [123, "123"].each do |val|
p = OpenStruct.new(FactoryGirl.attributes_for(:photo_entity)) validator = Validators::PhotoValidator.new(entity_stub(entity, prop => val))
p.public_send("#{prop}=", val) expect(validator).to be_valid
expect(validator.errors).to be_empty
v = Validators::PhotoValidator.new(p)
expect(v).to be_valid
expect(v.errors).to be_empty
end end
end end
it "fails for non numeric types" do it "fails for non numeric types" do
[true, :num, "asdf"].each do |val| [true, :num, "asdf"].each do |val|
p = OpenStruct.new(FactoryGirl.attributes_for(:photo_entity)) validator = Validators::PhotoValidator.new(entity_stub(entity, prop => val))
p.public_send("#{prop}=", val) expect(validator).not_to be_valid
expect(validator.errors).to include(prop)
v = Validators::PhotoValidator.new(p)
expect(v).not_to be_valid
expect(v.errors).to include(prop)
end end
end end
end end

View file

@ -3,15 +3,10 @@ module DiasporaFederation
let(:entity) { :profile_entity } let(:entity) { :profile_entity }
def profile_stub(data={}) def profile_stub(data={})
OpenStruct.new(FactoryGirl.attributes_for(:profile_entity).merge(data)) entity_stub(entity, data)
end end
it "validates a well-formed instance" do it_behaves_like "a common validator"
validator = Validators::ProfileValidator.new(profile_stub)
expect(validator).to be_valid
expect(validator.errors).to be_empty
end
it_behaves_like "a diaspora id validator" do it_behaves_like "a diaspora id validator" do
let(:property) { :diaspora_id } let(:property) { :diaspora_id }

View file

@ -3,15 +3,10 @@ module DiasporaFederation
let(:entity) { :webfinger } let(:entity) { :webfinger }
def webfinger_stub(data={}) def webfinger_stub(data={})
OpenStruct.new(FactoryGirl.attributes_for(:webfinger).merge(data)) entity_stub(entity, data)
end end
it "validates a well-formed instance" do it_behaves_like "a common validator"
validator = Validators::WebFingerValidator.new(webfinger_stub)
expect(validator).to be_valid
expect(validator.errors).to be_empty
end
describe "#acct_uri" do describe "#acct_uri" do
it "fails if it is nil or empty" do it "fails if it is nil or empty" do

View file

@ -1,10 +1,4 @@
def entity_stub(entity, property, val) def entity_stub(entity, data={})
instance = OpenStruct.new(FactoryGirl.attributes_for(entity))
instance.public_send("#{property}=", val)
instance
end
def entity_stub_data(entity, data={})
OpenStruct.new(FactoryGirl.attributes_for(entity).merge(data)) OpenStruct.new(FactoryGirl.attributes_for(entity).merge(data))
end end
@ -16,7 +10,7 @@ end
shared_examples "a common validator" do shared_examples "a common validator" do
it "validates a well-formed instance" do it "validates a well-formed instance" do
validator = described_class.new(entity_stub_data(entity)) validator = described_class.new(entity_stub(entity))
expect(validator).to be_valid expect(validator).to be_valid
expect(validator.errors).to be_empty expect(validator.errors).to be_empty
end end
@ -25,7 +19,7 @@ end
shared_examples "a diaspora id validator" do shared_examples "a diaspora id validator" do
it "must not be nil or empty if mandatory" do it "must not be nil or empty if mandatory" do
[nil, ""].each do |val| [nil, ""].each do |val|
validator = described_class.new(entity_stub(entity, property, val)) validator = described_class.new(entity_stub(entity, property => val))
if mandatory if mandatory
expect(validator).not_to be_valid expect(validator).not_to be_valid
@ -38,7 +32,7 @@ shared_examples "a diaspora id validator" do
end end
it "must be a valid diaspora id" do it "must be a valid diaspora id" do
validator = described_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).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
@ -47,21 +41,21 @@ end
shared_examples "a guid validator" do shared_examples "a guid validator" do
it "validates a well-formed guid from redmatrix" do it "validates a well-formed guid from redmatrix" do
validator = described_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).to be_valid
expect(validator.errors).to be_empty expect(validator.errors).to be_empty
end end
it "must be at least 16 chars" do it "must be at least 16 chars" do
validator = described_class.new(entity_stub(entity, property, "aaaaaa")) validator = described_class.new(entity_stub(entity, property => "aaaaaa"))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
end end
it "must only contain [0-9a-z-_@.:]" do it "must only contain [0-9a-z-_@.:]" do
validator = described_class.new(entity_stub(entity, property, "zzz+-#*$$")) validator = described_class.new(entity_stub(entity, property => "zzz+-#*$$"))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
@ -69,7 +63,7 @@ shared_examples "a guid validator" do
it "must not be nil or empty" do it "must not be nil or empty" do
[nil, ""].each do |val| [nil, ""].each do |val|
validator = described_class.new(entity_stub(entity, property, val)) validator = described_class.new(entity_stub(entity, property => val))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
@ -80,7 +74,7 @@ end
shared_examples "a boolean validator" do shared_examples "a boolean validator" do
it "validates a well-formed boolean" do it "validates a well-formed boolean" do
[true, "true", false, "false"].each do |val| [true, "true", false, "false"].each do |val|
validator = described_class.new(entity_stub(entity, property, val)) validator = described_class.new(entity_stub(entity, property => val))
expect(validator).to be_valid expect(validator).to be_valid
expect(validator.errors).to be_empty expect(validator.errors).to be_empty
@ -89,7 +83,7 @@ shared_examples "a boolean validator" do
it "must not be an arbitrary string or other object" do it "must not be an arbitrary string or other object" do
["asdf", Time.zone.today, 1234].each do |val| ["asdf", Time.zone.today, 1234].each do |val|
validator = described_class.new(entity_stub(entity, property, val)) validator = described_class.new(entity_stub(entity, property => val))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
@ -99,7 +93,7 @@ end
shared_examples "a public key validator" do shared_examples "a public key validator" do
it "fails for malformed rsa key" do it "fails for malformed rsa key" do
validator = described_class.new(entity_stub(entity, property, "ASDF")) validator = described_class.new(entity_stub(entity, property => "ASDF"))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
@ -107,7 +101,7 @@ shared_examples "a public key validator" do
it "must not be nil or empty" do it "must not be nil or empty" do
[nil, ""].each do |val| [nil, ""].each do |val|
validator = described_class.new(entity_stub(entity, property, val)) validator = described_class.new(entity_stub(entity, property => val))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
@ -118,7 +112,7 @@ end
shared_examples "a name validator" do shared_examples "a name validator" do
it "is allowed to be nil or empty" do it "is allowed to be nil or empty" do
[nil, ""].each do |val| [nil, ""].each do |val|
validator = described_class.new(entity_stub(entity, property, val)) validator = described_class.new(entity_stub(entity, property => val))
expect(validator).to be_valid expect(validator).to be_valid
expect(validator.errors).to be_empty expect(validator.errors).to be_empty
@ -126,28 +120,28 @@ shared_examples "a name validator" do
end end
it "is allowed to contain special chars" do it "is allowed to contain special chars" do
validator = described_class.new(entity_stub(entity, property, "cool name ©")) validator = described_class.new(entity_stub(entity, property => "cool name ©"))
expect(validator).to be_valid expect(validator).to be_valid
expect(validator.errors).to be_empty expect(validator.errors).to be_empty
end end
it "validates the maximum number of chars" do it "validates the maximum number of chars" do
validator = described_class.new(entity_stub(entity, property, alphanumeric_string(length))) validator = described_class.new(entity_stub(entity, property => alphanumeric_string(length)))
expect(validator).to be_valid expect(validator).to be_valid
expect(validator.errors).to be_empty expect(validator.errors).to be_empty
end end
it "must not exceed the maximum number of chars" do it "must not exceed the maximum number of chars" do
validator = described_class.new(entity_stub(entity, property, alphanumeric_string(length + 1))) validator = described_class.new(entity_stub(entity, property => alphanumeric_string(length + 1)))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
end end
it "must not contain semicolons" do it "must not contain semicolons" do
validator = described_class.new(entity_stub(entity, property, "asdf;qwer;yxcv")) validator = described_class.new(entity_stub(entity, property => "asdf;qwer;yxcv"))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
@ -157,7 +151,7 @@ end
shared_examples "a length validator" do shared_examples "a length validator" do
it "is allowed to be nil or empty" do it "is allowed to be nil or empty" do
[nil, ""].each do |val| [nil, ""].each do |val|
validator = described_class.new(entity_stub(entity, property, val)) validator = described_class.new(entity_stub(entity, property => val))
expect(validator).to be_valid expect(validator).to be_valid
expect(validator.errors).to be_empty expect(validator.errors).to be_empty
@ -165,21 +159,21 @@ shared_examples "a length validator" do
end end
it "is allowed to contain special chars" do it "is allowed to contain special chars" do
validator = described_class.new(entity_stub(entity, property, "cool name ©;:#%")) validator = described_class.new(entity_stub(entity, property => "cool name ©;:#%"))
expect(validator).to be_valid expect(validator).to be_valid
expect(validator.errors).to be_empty expect(validator.errors).to be_empty
end end
it "validates the maximum number of chars" do it "validates the maximum number of chars" do
validator = described_class.new(entity_stub(entity, property, alphanumeric_string(length))) validator = described_class.new(entity_stub(entity, property => alphanumeric_string(length)))
expect(validator).to be_valid expect(validator).to be_valid
expect(validator.errors).to be_empty expect(validator.errors).to be_empty
end end
it "must not exceed the maximum number of chars" do it "must not exceed the maximum number of chars" do
validator = described_class.new(entity_stub(entity, property, alphanumeric_string(length + 1))) validator = described_class.new(entity_stub(entity, property => alphanumeric_string(length + 1)))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
@ -189,7 +183,7 @@ end
shared_examples "a url validator without path" do shared_examples "a url validator without path" do
it "must not be nil or empty" do it "must not be nil or empty" do
[nil, ""].each do |val| [nil, ""].each do |val|
validator = described_class.new(entity_stub(entity, property, val)) validator = described_class.new(entity_stub(entity, property => val))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
@ -197,14 +191,14 @@ shared_examples "a url validator without path" do
end end
it "fails for url with special chars" do it "fails for url with special chars" do
validator = described_class.new(entity_stub(entity, property, "https://asdf$%.com")) validator = described_class.new(entity_stub(entity, property => "https://asdf$%.com"))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
end end
it "fails for url without scheme" do it "fails for url without scheme" do
validator = described_class.new(entity_stub(entity, property, "example.com")) validator = described_class.new(entity_stub(entity, property => "example.com"))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
@ -213,14 +207,14 @@ end
shared_examples "a url path validator" do shared_examples "a url path validator" do
it "fails for url with special chars" do it "fails for url with special chars" do
validator = described_class.new(entity_stub(entity, property, "https://asdf$%.com/some/path")) validator = described_class.new(entity_stub(entity, property => "https://asdf$%.com/some/path"))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)
end end
it "fails for url without path" do it "fails for url without path" do
validator = described_class.new(entity_stub(entity, property, "https://example.com")) validator = described_class.new(entity_stub(entity, property => "https://example.com"))
expect(validator).not_to be_valid expect(validator).not_to be_valid
expect(validator.errors).to include(property) expect(validator.errors).to include(property)