From 9391d4b9eb7f31ac71a7184cb786a8c535dec34e Mon Sep 17 00:00:00 2001 From: cmrd Senya Date: Tue, 3 Nov 2015 01:23:10 +0300 Subject: [PATCH] Reduce the entity stubs initialization calls to a single entity_stub to make the code more DRY --- .../validators/h_card_validator_spec.rb | 9 +-- .../validators/location_validator_spec.rb | 5 +- .../participation_validator_spec.rb | 12 ++-- .../validators/photo_validator_spec.rb | 27 +++------ .../validators/profile_validator_spec.rb | 9 +-- .../validators/web_finger_validator_spec.rb | 9 +-- spec/support/shared_validator_specs.rb | 58 +++++++++---------- 7 files changed, 46 insertions(+), 83 deletions(-) diff --git a/spec/lib/diaspora_federation/validators/h_card_validator_spec.rb b/spec/lib/diaspora_federation/validators/h_card_validator_spec.rb index a23887b..3811808 100644 --- a/spec/lib/diaspora_federation/validators/h_card_validator_spec.rb +++ b/spec/lib/diaspora_federation/validators/h_card_validator_spec.rb @@ -3,15 +3,10 @@ module DiasporaFederation let(:entity) { :h_card } def hcard_stub(data={}) - OpenStruct.new(FactoryGirl.attributes_for(:h_card).merge(data)) + entity_stub(entity, data) end - it "validates a well-formed instance" do - validator = Validators::HCardValidator.new(hcard_stub) - - expect(validator).to be_valid - expect(validator.errors).to be_empty - end + it_behaves_like "a common validator" describe "#full_name" do it_behaves_like "a name validator" do diff --git a/spec/lib/diaspora_federation/validators/location_validator_spec.rb b/spec/lib/diaspora_federation/validators/location_validator_spec.rb index 0577d63..90e09eb 100644 --- a/spec/lib/diaspora_federation/validators/location_validator_spec.rb +++ b/spec/lib/diaspora_federation/validators/location_validator_spec.rb @@ -6,10 +6,7 @@ module DiasporaFederation context "#lat and #lng" do %i(lat lng).each do |prop| it "must not be empty" do - entity = OpenStruct.new(FactoryGirl.attributes_for(:location_entity)) - entity.public_send("#{prop}=", "") - - validator = Validators::LocationValidator.new(entity) + validator = Validators::LocationValidator.new(entity_stub(entity, prop => "")) expect(validator).not_to be_valid expect(validator.errors).to include(prop) end diff --git a/spec/lib/diaspora_federation/validators/participation_validator_spec.rb b/spec/lib/diaspora_federation/validators/participation_validator_spec.rb index 9bb8bff..590688d 100644 --- a/spec/lib/diaspora_federation/validators/participation_validator_spec.rb +++ b/spec/lib/diaspora_federation/validators/participation_validator_spec.rb @@ -11,8 +11,7 @@ module DiasporaFederation context "#target_type" do it "must not be empty" do - entity = OpenStruct.new(FactoryGirl.attributes_for(:participation_entity, target_type: "")) - validator = Validators::ParticipationValidator.new(entity) + validator = Validators::ParticipationValidator.new(entity_stub(entity, target_type: "")) expect(validator).not_to be_valid expect(validator.errors).to include(:target_type) end @@ -29,12 +28,9 @@ module DiasporaFederation context "#author_signature and #parent_author_signature" do %i(author_signature parent_author_signature).each do |prop| it "must not be empty" do - p = OpenStruct.new(FactoryGirl.attributes_for(:participation_entity)) - p.public_send("#{prop}=", "") - - v = Validators::ParticipationValidator.new(p) - expect(v).not_to be_valid - expect(v.errors).to include(prop) + validator = Validators::ParticipationValidator.new(entity_stub(entity, prop => "")) + expect(validator).not_to be_valid + expect(validator.errors).to include(prop) end end end diff --git a/spec/lib/diaspora_federation/validators/photo_validator_spec.rb b/spec/lib/diaspora_federation/validators/photo_validator_spec.rb index ff1c5e1..626047b 100644 --- a/spec/lib/diaspora_federation/validators/photo_validator_spec.rb +++ b/spec/lib/diaspora_federation/validators/photo_validator_spec.rb @@ -24,12 +24,9 @@ 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 - p = OpenStruct.new(FactoryGirl.attributes_for(:photo_entity)) - p.public_send("#{prop}=", "") - - v = Validators::PhotoValidator.new(p) - expect(v).not_to be_valid - expect(v.errors).to include(prop) + validator = Validators::PhotoValidator.new(entity_stub(entity, prop => "")) + expect(validator).not_to be_valid + expect(validator.errors).to include(prop) end end end @@ -38,23 +35,17 @@ module DiasporaFederation %i(height width).each do |prop| it "validates an integer" do [123, "123"].each do |val| - p = OpenStruct.new(FactoryGirl.attributes_for(:photo_entity)) - p.public_send("#{prop}=", val) - - v = Validators::PhotoValidator.new(p) - expect(v).to be_valid - expect(v.errors).to be_empty + 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| - p = OpenStruct.new(FactoryGirl.attributes_for(:photo_entity)) - p.public_send("#{prop}=", val) - - v = Validators::PhotoValidator.new(p) - expect(v).not_to be_valid - expect(v.errors).to include(prop) + validator = Validators::PhotoValidator.new(entity_stub(entity, prop => val)) + expect(validator).not_to be_valid + expect(validator.errors).to include(prop) end end end diff --git a/spec/lib/diaspora_federation/validators/profile_validator_spec.rb b/spec/lib/diaspora_federation/validators/profile_validator_spec.rb index 8bff965..321f3ae 100644 --- a/spec/lib/diaspora_federation/validators/profile_validator_spec.rb +++ b/spec/lib/diaspora_federation/validators/profile_validator_spec.rb @@ -3,15 +3,10 @@ module DiasporaFederation let(:entity) { :profile_entity } def profile_stub(data={}) - OpenStruct.new(FactoryGirl.attributes_for(:profile_entity).merge(data)) + entity_stub(entity, data) end - it "validates a well-formed instance" do - validator = Validators::ProfileValidator.new(profile_stub) - - expect(validator).to be_valid - expect(validator.errors).to be_empty - end + it_behaves_like "a common validator" it_behaves_like "a diaspora id validator" do let(:property) { :diaspora_id } diff --git a/spec/lib/diaspora_federation/validators/web_finger_validator_spec.rb b/spec/lib/diaspora_federation/validators/web_finger_validator_spec.rb index 00f5e02..e1ec327 100644 --- a/spec/lib/diaspora_federation/validators/web_finger_validator_spec.rb +++ b/spec/lib/diaspora_federation/validators/web_finger_validator_spec.rb @@ -3,15 +3,10 @@ module DiasporaFederation let(:entity) { :webfinger } def webfinger_stub(data={}) - OpenStruct.new(FactoryGirl.attributes_for(:webfinger).merge(data)) + entity_stub(entity, data) end - it "validates a well-formed instance" do - validator = Validators::WebFingerValidator.new(webfinger_stub) - - expect(validator).to be_valid - expect(validator.errors).to be_empty - end + it_behaves_like "a common validator" describe "#acct_uri" do it "fails if it is nil or empty" do diff --git a/spec/support/shared_validator_specs.rb b/spec/support/shared_validator_specs.rb index e3dd4f2..13ec3b1 100644 --- a/spec/support/shared_validator_specs.rb +++ b/spec/support/shared_validator_specs.rb @@ -1,10 +1,4 @@ -def entity_stub(entity, property, val) - instance = OpenStruct.new(FactoryGirl.attributes_for(entity)) - instance.public_send("#{property}=", val) - instance -end - -def entity_stub_data(entity, data={}) +def entity_stub(entity, data={}) OpenStruct.new(FactoryGirl.attributes_for(entity).merge(data)) end @@ -16,7 +10,7 @@ end shared_examples "a common validator" 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.errors).to be_empty end @@ -25,7 +19,7 @@ end shared_examples "a diaspora id validator" do it "must not be nil or empty if mandatory" do [nil, ""].each do |val| - validator = described_class.new(entity_stub(entity, property, val)) + validator = described_class.new(entity_stub(entity, property => val)) if mandatory expect(validator).not_to be_valid @@ -38,7 +32,7 @@ shared_examples "a diaspora id validator" do end 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.errors).to include(property) @@ -47,21 +41,21 @@ end shared_examples "a guid validator" 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.errors).to be_empty end 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.errors).to include(property) end 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.errors).to include(property) @@ -69,7 +63,7 @@ shared_examples "a guid validator" do it "must not be nil or empty" do [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.errors).to include(property) @@ -80,7 +74,7 @@ end shared_examples "a boolean validator" do it "validates a well-formed boolean" do [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.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 ["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.errors).to include(property) @@ -99,7 +93,7 @@ end shared_examples "a public key validator" 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.errors).to include(property) @@ -107,7 +101,7 @@ shared_examples "a public key validator" do it "must not be nil or empty" do [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.errors).to include(property) @@ -118,7 +112,7 @@ 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)) + validator = described_class.new(entity_stub(entity, property => val)) expect(validator).to be_valid expect(validator.errors).to be_empty @@ -126,28 +120,28 @@ shared_examples "a name validator" do end 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.errors).to be_empty end 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.errors).to be_empty end 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.errors).to include(property) end 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.errors).to include(property) @@ -157,7 +151,7 @@ end shared_examples "a length validator" do it "is allowed to be nil or empty" do [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.errors).to be_empty @@ -165,21 +159,21 @@ shared_examples "a length validator" do end 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.errors).to be_empty end 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.errors).to be_empty end 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.errors).to include(property) @@ -189,7 +183,7 @@ end shared_examples "a url validator without path" do it "must not be nil or empty" do [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.errors).to include(property) @@ -197,14 +191,14 @@ shared_examples "a url validator without path" do end 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.errors).to include(property) end 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.errors).to include(property) @@ -213,14 +207,14 @@ end shared_examples "a url path validator" 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.errors).to include(property) end 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.errors).to include(property)