extend profile validator
This commit is contained in:
parent
416f322cc7
commit
c15fee279c
5 changed files with 125 additions and 10 deletions
|
|
@ -23,6 +23,7 @@ module DiasporaFederation
|
||||||
# @see HCard#last_name
|
# @see HCard#last_name
|
||||||
# @return [String] last name
|
# @return [String] last name
|
||||||
property :last_name, default: nil
|
property :last_name, default: nil
|
||||||
|
|
||||||
# @!attribute [r] image_url
|
# @!attribute [r] image_url
|
||||||
# @see HCard#photo_large_url
|
# @see HCard#photo_large_url
|
||||||
# @return [String] url to the big avatar (300x300)
|
# @return [String] url to the big avatar (300x300)
|
||||||
|
|
|
||||||
|
|
@ -11,13 +11,23 @@ module DiasporaFederation
|
||||||
rule :first_name, regular_expression: {regex: /\A[^;]{,32}\z/}
|
rule :first_name, regular_expression: {regex: /\A[^;]{,32}\z/}
|
||||||
rule :last_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
|
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 :searchable, :boolean
|
||||||
|
|
||||||
rule :nsfw, :boolean
|
rule :nsfw, :boolean
|
||||||
|
|
||||||
|
rule :tag_string, tag_count: {maximum: 5}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -32,9 +32,15 @@ FactoryGirl.define do
|
||||||
diaspora_id
|
diaspora_id
|
||||||
first_name "my_name"
|
first_name "my_name"
|
||||||
last_name nil
|
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"
|
birthday "1988-07-15"
|
||||||
|
gender "Male"
|
||||||
|
bio "some text about me"
|
||||||
|
location "github"
|
||||||
searchable true
|
searchable true
|
||||||
nsfw false
|
nsfw false
|
||||||
|
tag_string "#i #love #tags"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -21,17 +21,44 @@ module DiasporaFederation
|
||||||
describe "##{prop}" do
|
describe "##{prop}" do
|
||||||
it_behaves_like "a name validator" do
|
it_behaves_like "a name validator" do
|
||||||
let(:property) { prop }
|
let(:property) { prop }
|
||||||
|
let(:length) { 32 }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#tag_string" do
|
%i(image_url image_url_medium image_url_small).each do |prop|
|
||||||
it "must not contain more than 5 tags" do
|
describe "##{prop}" do
|
||||||
validator = Validators::ProfileValidator.new(
|
it "is allowed to be nil" do
|
||||||
profile_stub(tag_string: "#i #have #too #many #tags #in #my #profile"))
|
validator = Validators::ProfileValidator.new(profile_stub(prop => nil))
|
||||||
|
|
||||||
expect(validator).not_to be_valid
|
expect(validator).to be_valid
|
||||||
expect(validator.errors).to include(:tag_string)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -71,5 +98,15 @@ module DiasporaFederation
|
||||||
end
|
end
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,12 @@ def entity_stub(entity, property, val)
|
||||||
instance
|
instance
|
||||||
end
|
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
|
shared_examples "a diaspora id 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|
|
||||||
|
|
@ -109,8 +115,15 @@ shared_examples "a name validator" do
|
||||||
expect(validator.errors).to be_empty
|
expect(validator.errors).to be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
it "must not exceed 32 chars" do
|
it "validates the maximum number of chars" do
|
||||||
validator = described_class.new(entity_stub(entity, property, "abcdefghijklmnopqrstuvwxyz_aaaaaaaaaa"))
|
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).not_to be_valid
|
||||||
expect(validator.errors).to include(property)
|
expect(validator.errors).to include(property)
|
||||||
|
|
@ -124,6 +137,38 @@ shared_examples "a name validator" do
|
||||||
end
|
end
|
||||||
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
|
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|
|
||||||
|
|
@ -148,3 +193,19 @@ shared_examples "a url validator without path" do
|
||||||
expect(validator.errors).to include(property)
|
expect(validator.errors).to include(property)
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue