From c15fee279c90de16cde2ca6e9af3f426dca7f0e0 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Tue, 28 Jul 2015 01:46:16 +0200 Subject: [PATCH] extend profile validator --- lib/diaspora_federation/entities/profile.rb | 1 + .../validators/profile_validator.rb | 12 +++- spec/factories.rb | 8 ++- .../validators/profile_validator_spec.rb | 49 ++++++++++++-- spec/support/shared_validator_specs.rb | 65 ++++++++++++++++++- 5 files changed, 125 insertions(+), 10 deletions(-) diff --git a/lib/diaspora_federation/entities/profile.rb b/lib/diaspora_federation/entities/profile.rb index 3a728b8..1ecdd35 100644 --- a/lib/diaspora_federation/entities/profile.rb +++ b/lib/diaspora_federation/entities/profile.rb @@ -23,6 +23,7 @@ module DiasporaFederation # @see HCard#last_name # @return [String] last name property :last_name, default: nil + # @!attribute [r] image_url # @see HCard#photo_large_url # @return [String] url to the big avatar (300x300) diff --git a/lib/diaspora_federation/validators/profile_validator.rb b/lib/diaspora_federation/validators/profile_validator.rb index fd32979..773ee97 100644 --- a/lib/diaspora_federation/validators/profile_validator.rb +++ b/lib/diaspora_federation/validators/profile_validator.rb @@ -11,13 +11,23 @@ module DiasporaFederation rule :first_name, regular_expression: {regex: /\A[^;]{,32}\z/} rule :last_name, regular_expression: {regex: /\A[^;]{,32}\z/} - rule :tag_string, tag_count: {maximum: 5} + # this urls can be relative + rule :image_url, nilableURI: [:path] + rule :image_url_medium, nilableURI: [:path] + rule :image_url_small, nilableURI: [:path] rule :birthday, :birthday + # TODO: replace regex with "length: {maximum: xxx}" but this rule doesn't allow nil now. + rule :gender, regular_expression: {regex: /\A.{,255}\z/} + rule :bio, regular_expression: {regex: /\A.{,65535}\z/} + rule :location, regular_expression: {regex: /\A.{,255}\z/} + rule :searchable, :boolean rule :nsfw, :boolean + + rule :tag_string, tag_count: {maximum: 5} end end end diff --git a/spec/factories.rb b/spec/factories.rb index e46254a..2b38aa7 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -32,9 +32,15 @@ FactoryGirl.define do diaspora_id first_name "my_name" last_name nil - tag_string "#i #love #tags" + image_url "/assets/user/default.png" + image_url_medium "/assets/user/default.png" + image_url_small "/assets/user/default.png" birthday "1988-07-15" + gender "Male" + bio "some text about me" + location "github" searchable true nsfw false + tag_string "#i #love #tags" 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 882538e..9f50174 100644 --- a/spec/lib/diaspora_federation/validators/profile_validator_spec.rb +++ b/spec/lib/diaspora_federation/validators/profile_validator_spec.rb @@ -21,17 +21,44 @@ module DiasporaFederation describe "##{prop}" do it_behaves_like "a name validator" do let(:property) { prop } + let(:length) { 32 } end end 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")) + %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).not_to be_valid - expect(validator.errors).to include(:tag_string) + expect(validator).to be_valid + expect(validator.errors).to be_empty + end + + it_behaves_like "a url path validator" do + let(:property) { prop } + end + end + end + + describe "#gender" do + it_behaves_like "a length validator" do + let(:property) { :gender } + let(:length) { 255 } + end + end + + describe "#bio" do + it_behaves_like "a length validator" do + let(:property) { :bio } + let(:length) { 65_535 } + end + end + + describe "#location" do + it_behaves_like "a length validator" do + let(:property) { :location } + let(:length) { 255 } end end @@ -71,5 +98,15 @@ module DiasporaFederation end end 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) + end + end end end diff --git a/spec/support/shared_validator_specs.rb b/spec/support/shared_validator_specs.rb index e2ce8fc..74a12da 100644 --- a/spec/support/shared_validator_specs.rb +++ b/spec/support/shared_validator_specs.rb @@ -4,6 +4,12 @@ def entity_stub(entity, property, val) instance end +ALPHANUMERIC_RANGE = [*"0".."9", *"A".."Z", *"a".."z"] + +def alphanumeric_string(length) + Array.new(length) { ALPHANUMERIC_RANGE.sample }.join +end + shared_examples "a diaspora id validator" do it "must not be nil or empty" do [nil, ""].each do |val| @@ -109,8 +115,15 @@ shared_examples "a name validator" do expect(validator.errors).to be_empty end - it "must not exceed 32 chars" do - validator = described_class.new(entity_stub(entity, property, "abcdefghijklmnopqrstuvwxyz_aaaaaaaaaa")) + it "validates the maximum number of chars" do + 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))) expect(validator).not_to be_valid expect(validator.errors).to include(property) @@ -124,6 +137,38 @@ shared_examples "a name validator" do end 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)) + + 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 "validates the maximum number of chars" do + 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))) + + expect(validator).not_to be_valid + expect(validator.errors).to include(property) + end +end + shared_examples "a url validator without path" do it "must not be nil or empty" do [nil, ""].each do |val| @@ -148,3 +193,19 @@ shared_examples "a url validator without path" do expect(validator.errors).to include(property) end 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")) + + 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")) + + expect(validator).not_to be_valid + expect(validator.errors).to include(property) + end +end