add webfinger and hcard validators

This commit is contained in:
Benjamin Neff 2015-07-28 02:06:09 +02:00
parent c15fee279c
commit fe704fb981
6 changed files with 182 additions and 2 deletions

View file

@ -1,5 +1,6 @@
require "validation"
require "validation/rule/regular_expression"
require "validation/rule/not_empty"
require "validation/rule/uri"
# +valid+ gem namespace
@ -31,5 +32,7 @@ module DiasporaFederation
end
end
require "diaspora_federation/validators/h_card_validator"
require "diaspora_federation/validators/person_validator"
require "diaspora_federation/validators/profile_validator"
require "diaspora_federation/validators/web_finger_validator"

View file

@ -0,0 +1,30 @@
module DiasporaFederation
module Validators
# This validates a {Discovery::HCard}
#
# @todo activate guid and public key validation after all pod have it in
# the hcard.
#
# @note
class HCardValidator < Validation::Validator
include Validation
# rule :guid, :guid
# the name must not contain a semicolon because of mentions
# @{<full_name> ; <diaspora_id>}
rule :full_name, regular_expression: {regex: /\A[^;]{,70}\z/}
rule :first_name, regular_expression: {regex: /\A[^;]{,32}\z/}
rule :last_name, regular_expression: {regex: /\A[^;]{,32}\z/}
# this urls can be relative
rule :photo_large_url, [:not_nil, nilableURI: [:path]]
rule :photo_medium_url, [:not_nil, nilableURI: [:path]]
rule :photo_small_url, [:not_nil, nilableURI: [:path]]
# rule :exported_key, :public_key
rule :searchable, :boolean
end
end
end

View file

@ -0,0 +1,20 @@
module DiasporaFederation
module Validators
# This validates a {Discovery::WebFinger}
#
# @note it does not validate the guid and public key, because it will be
# removed in the webfinger
class WebFingerValidator < Validation::Validator
include Validation
rule :acct_uri, :not_empty
rule :alias_url, [:not_nil, nilableURI: %i(host path)]
rule :hcard_url, [:not_nil, nilableURI: %i(host path)]
rule :seed_url, %i(not_nil nilableURI)
rule :profile_url, [:not_nil, nilableURI: %i(host path)]
rule :atom_url, [:not_nil, nilableURI: %i(host path)]
rule :salmon_url, [:not_nil, nilableURI: %i(host path)]
end
end
end

View file

@ -5,6 +5,7 @@ def r_str
end
FactoryGirl.define do
sequence(:guid) { UUID.generate :compact }
sequence(:diaspora_id) {|n| "person-#{n}-#{r_str}@localhost:3000" }
sequence(:public_key) { OpenSSL::PKey::RSA.generate(1024).public_key.export }
@ -17,8 +18,34 @@ FactoryGirl.define do
end
end
factory :webfinger, class: DiasporaFederation::Discovery::WebFinger do
guid
acct_uri { "acct:#{generate(:diaspora_id)}" }
alias_url "http://localhost:3000/people/0123456789abcdef"
hcard_url "http://localhost:3000/hcard/users/user"
seed_url "http://localhost:3000/"
profile_url "http://localhost:3000/u/user"
atom_url "http://localhost:3000/public/user.atom"
salmon_url "http://localhost:3000/receive/users/0123456789abcdef"
public_key
end
factory :h_card, class: DiasporaFederation::Discovery::HCard do
guid
nickname "some_name"
full_name "my name"
first_name "my name"
last_name nil
url "http://localhost:3000/"
public_key
photo_large_url "/assets/user/default.png"
photo_medium_url "/assets/user/default.png"
photo_small_url "/assets/user/default.png"
searchable true
end
factory :person_entity, class: DiasporaFederation::Entities::Person do
guid UUID.generate :compact
guid
diaspora_id
url "http://localhost:3000/"
exported_key { generate(:public_key) }
@ -30,7 +57,7 @@ FactoryGirl.define do
factory :profile_entity, class: DiasporaFederation::Entities::Profile do
diaspora_id
first_name "my_name"
first_name "my name"
last_name nil
image_url "/assets/user/default.png"
image_url_medium "/assets/user/default.png"

View file

@ -0,0 +1,55 @@
module DiasporaFederation
describe Validators::HCardValidator do
let(:entity) { :h_card }
def hcard_stub(data={})
OpenStruct.new(FactoryGirl.attributes_for(:h_card).merge(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
describe "#full_name" do
it_behaves_like "a name validator" do
let(:property) { :full_name }
let(:length) { 70 }
end
end
%i(first_name last_name).each do |prop|
describe "##{prop}" do
it_behaves_like "a name validator" do
let(:property) { prop }
let(:length) { 32 }
end
end
end
%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
end
it_behaves_like "a url path validator" do
let(:property) { prop }
end
end
end
describe "#searchable" do
it_behaves_like "a boolean validator" do
let(:property) { :searchable }
end
end
end
end

View file

@ -0,0 +1,45 @@
module DiasporaFederation
describe Validators::WebFingerValidator do
let(:entity) { :webfinger }
def webfinger_stub(data={})
OpenStruct.new(FactoryGirl.attributes_for(:webfinger).merge(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
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
end
end
%i(alias_url hcard_url profile_url atom_url salmon_url).each do |prop|
describe "##{prop}" do
it_behaves_like "a url validator without path" do
let(:property) { prop }
end
it_behaves_like "a url path validator" do
let(:property) { prop }
end
end
end
describe "#seed_url" do
it_behaves_like "a url validator without path" do
let(:property) { :seed_url }
end
end
end
end