add diaspora handle validator and test
This commit is contained in:
parent
83097572ce
commit
ac8832ee4a
6 changed files with 122 additions and 6 deletions
|
|
@ -1,6 +1,4 @@
|
|||
require "validation"
|
||||
require "validation/rule/not_empty"
|
||||
require "validation/rule/email"
|
||||
require "validation/rule/regular_expression"
|
||||
require "validation/rule/uri"
|
||||
|
||||
|
|
@ -17,6 +15,7 @@ end
|
|||
|
||||
require "diaspora_federation/validators/rules/birthday"
|
||||
require "diaspora_federation/validators/rules/boolean"
|
||||
require "diaspora_federation/validators/rules/diaspora_id"
|
||||
require "diaspora_federation/validators/rules/guid"
|
||||
require "diaspora_federation/validators/rules/not_nil"
|
||||
require "diaspora_federation/validators/rules/public_key"
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ module DiasporaFederation
|
|||
|
||||
rule :guid, :guid
|
||||
|
||||
rule :diaspora_handle, %i(not_empty email)
|
||||
rule :diaspora_handle, :diaspora_id
|
||||
|
||||
rule :url, :u_r_i # WTF? :uri -> Uri -> "uninitialized constant Uri", :u_r_i -> URI -> \o/
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ module DiasporaFederation
|
|||
class ProfileValidator < Validation::Validator
|
||||
include Validation
|
||||
|
||||
rule :diaspora_handle, %i(not_empty email)
|
||||
rule :diaspora_handle, :diaspora_id
|
||||
|
||||
# the name must not contain a semicolon because of mentions
|
||||
# @{<name> ; <handle>}
|
||||
|
|
|
|||
43
lib/diaspora_federation/validators/rules/diaspora_id.rb
Normal file
43
lib/diaspora_federation/validators/rules/diaspora_id.rb
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
module Validation
|
||||
module Rule
|
||||
# Diaspora ID validation rule
|
||||
#
|
||||
# This rule is based on https://github.com/zombor/Validator/blob/master/lib/validation/rule/email.rb
|
||||
# which was adapted from https://github.com/emmanuel/aequitas/blob/master/lib/aequitas/rule/format/email_address.rb
|
||||
class DiasporaId
|
||||
DIASPORA_HANDLE = begin
|
||||
letter = "a-zA-Z"
|
||||
digit = "0-9"
|
||||
username = "[#{letter}#{digit}\-\_\.]+"
|
||||
atext = "[#{letter}#{digit}+\=\-\_]"
|
||||
dot_atom = "#{atext}+([.]#{atext}*)*"
|
||||
no_ws_ctl = '\x01-\x08\x11\x12\x14-\x1f\x7f'
|
||||
text = '[\x01-\x09\x11\x12\x14-\x7f]'
|
||||
quoted_pair = "(\\x5c#{text})"
|
||||
dtext = "[#{no_ws_ctl}\\x21-\\x5a\\x5e-\\x7e]"
|
||||
dcontent = "(?:#{dtext}|#{quoted_pair})"
|
||||
domain_literal = "\\[#{dcontent}+\\]"
|
||||
domain = "(?:#{dot_atom}|#{domain_literal})"
|
||||
port = "(:[#{digit}]+)?"
|
||||
addr_spec = "#{username}\@#{domain}#{port}"
|
||||
|
||||
/\A#{addr_spec}\z/u
|
||||
end
|
||||
|
||||
# The error key for this rule
|
||||
def error_key
|
||||
:diaspora_id
|
||||
end
|
||||
|
||||
# Determines if value is a valid email
|
||||
def valid_value?(value)
|
||||
!DIASPORA_HANDLE.match(value).nil?
|
||||
end
|
||||
|
||||
# This rule has no params
|
||||
def params
|
||||
{}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -19,7 +19,7 @@ FactoryGirl.define do
|
|||
|
||||
factory :person_entity, class: DiasporaFederation::Entities::Person do
|
||||
guid UUID.generate :compact
|
||||
diaspora_handle "testing@example.com"
|
||||
diaspora_handle
|
||||
url "http://localhost:3000/"
|
||||
exported_key { generate(:public_key) }
|
||||
profile {
|
||||
|
|
@ -29,7 +29,7 @@ FactoryGirl.define do
|
|||
end
|
||||
|
||||
factory :profile_entity, class: DiasporaFederation::Entities::Profile do
|
||||
diaspora_handle "testing@example.com"
|
||||
diaspora_handle
|
||||
first_name "my_name"
|
||||
last_name nil
|
||||
tag_string "#i #love #tags"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,74 @@
|
|||
describe Validation::Rule::DiasporaId do
|
||||
it "will not accept parameters" do
|
||||
validator = Validation::Validator.new({})
|
||||
expect {
|
||||
validator.rule(:diaspora_id, diaspora_id: {param: true})
|
||||
}.to raise_error ArgumentError
|
||||
end
|
||||
|
||||
context "validation" do
|
||||
it "validates a normal diaspora id" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user@example.com"))
|
||||
validator.rule(:diaspora_id, :diaspora_id)
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "validates a diaspora id with localhost" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user@localhost"))
|
||||
validator.rule(:diaspora_id, :diaspora_id)
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "validates a diaspora id with port" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user@example.com:3000"))
|
||||
validator.rule(:diaspora_id, :diaspora_id)
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "validates a diaspora id with IPv4 address" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user@123.45.67.89"))
|
||||
validator.rule(:diaspora_id, :diaspora_id)
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "validates a diaspora id with IPv6 address" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user@[2001:1234:5678:90ab:cdef::1]"))
|
||||
validator.rule(:diaspora_id, :diaspora_id)
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "validates a diaspora id with . and -" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some-fancy.user@example.com"))
|
||||
validator.rule(:diaspora_id, :diaspora_id)
|
||||
|
||||
expect(validator).to be_valid
|
||||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it "fails if the diaspora id contains a / in the domain-name" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user@example.com/friendica"))
|
||||
validator.rule(:diaspora_id, :diaspora_id)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:diaspora_id)
|
||||
end
|
||||
|
||||
it "fails if the diaspora id contains a special-chars in the username" do
|
||||
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user$^%@example.com"))
|
||||
validator.rule(:diaspora_id, :diaspora_id)
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(:diaspora_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue