Merge pull request #617 from AyuntamientoMadrid/document-verification-age-validation
checks valid age
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
class Verification::Management::Document
|
||||
include ActiveModel::Model
|
||||
include ActiveModel::Dates
|
||||
|
||||
attr_accessor :document_type
|
||||
attr_accessor :document_number
|
||||
@@ -17,7 +18,21 @@ class Verification::Management::Document
|
||||
end
|
||||
|
||||
def in_census?
|
||||
CensusApi.new.call(document_type, document_number).valid?
|
||||
response = CensusApi.new.call(document_type, document_number)
|
||||
response.valid? && valid_age?(response)
|
||||
end
|
||||
|
||||
def valid_age?(response)
|
||||
if under_sixteen?(response)
|
||||
errors.add(:age, true)
|
||||
return false
|
||||
else
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
def under_sixteen?(response)
|
||||
16.years.ago < string_to_date(response.date_of_birth)
|
||||
end
|
||||
|
||||
def verified?
|
||||
@@ -28,7 +43,4 @@ class Verification::Management::Document
|
||||
user.update(verified_at: Time.now) if user?
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
@@ -1,5 +1,9 @@
|
||||
<div class="alert-box alert radius">
|
||||
<%= t("management.document_verifications.not_in_census") %>
|
||||
<% if @document_verification.errors[:age].any? %>
|
||||
<%= t("management.document_verifications.under_age") %>
|
||||
<% else %>
|
||||
<%= t("management.document_verifications.not_in_census") %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= render 'management/user_permissions',
|
||||
|
||||
@@ -41,6 +41,7 @@ en:
|
||||
already_verified: "This user account is already verified."
|
||||
in_census_has_following_permissions: "This user can participate in the website with the following permissions:"
|
||||
not_in_census: "This document is not registered in Madrid."
|
||||
under_age: "You must be over 16 to verify your account."
|
||||
not_in_census_info: "Citizens not in the Census can participate in the website with the following permissions:"
|
||||
has_no_account_html: "In order to create an account, go to %{link} and click in <b>'Register'</b> in the upper-left part of the screen."
|
||||
verify: "Verify"
|
||||
|
||||
@@ -41,6 +41,7 @@ es:
|
||||
already_verified: "Esta cuenta de usuario ya está verificada."
|
||||
in_census_has_following_permissions: "Este usuario puede participar en el Portal de Gobierno Abierto del Ayuntamiento de Madrid con las siguientes posibilidades:"
|
||||
not_in_census: "Este documento no está registrado en el Padrón Municipal de Madrid."
|
||||
under_age: "Debes ser mayor de 16 años para verificar tu cuenta."
|
||||
not_in_census_info: "Las personas no empadronadas en Madrid pueden participar en el Portal de Gobierno Abierto del Ayuntamiento de Madrid con las siguientes posibilidades:"
|
||||
has_no_account_html: "Para crear un usuario entre en %{link} y haga clic en la opción <b>'Registrarse'</b> en la parte superior derecha de la pantalla."
|
||||
verify: "Verificar usuario"
|
||||
|
||||
@@ -62,4 +62,14 @@ feature 'DocumentVerifications' do
|
||||
expect(page).to have_content "Document number: 12345H"
|
||||
end
|
||||
|
||||
scenario 'User age is checked' do
|
||||
expect_any_instance_of(Verification::Management::Document).to receive(:under_sixteen?).and_return(true)
|
||||
|
||||
visit management_document_verifications_path
|
||||
fill_in 'document_verification_document_number', with: '1234'
|
||||
click_button 'Check'
|
||||
|
||||
expect(page).to have_content "You must be over 16 to verify your account."
|
||||
end
|
||||
|
||||
end
|
||||
37
spec/models/verification/management/document_spec.rb
Normal file
37
spec/models/verification/management/document_spec.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Verification::Management::Document do
|
||||
describe "#valid_age?" do
|
||||
it "returns false when the user is younger than sixteen years old" do
|
||||
census_response = double(date_of_birth: "31-12-#{16.years.ago.year}")
|
||||
expect(Verification::Management::Document.new.valid_age?(census_response)).to be false
|
||||
end
|
||||
|
||||
it "returns true when the user is sixteen years old" do
|
||||
census_response = double(date_of_birth: 16.years.ago.strftime("%d-%m-%Y"))
|
||||
expect(Verification::Management::Document.new.valid_age?(census_response)).to be true
|
||||
end
|
||||
|
||||
it "returns true when the user is older than sixteen years old" do
|
||||
census_response = double(date_of_birth: "31-12-#{33.years.ago.year}")
|
||||
expect(Verification::Management::Document.new.valid_age?(census_response)).to be true
|
||||
end
|
||||
end
|
||||
|
||||
describe "#under_sixteen?" do
|
||||
it "returns true when the user is younger than sixteen years old" do
|
||||
census_response = double(date_of_birth: "31-12-#{16.years.ago.year}")
|
||||
expect(Verification::Management::Document.new.under_sixteen?(census_response)).to be true
|
||||
end
|
||||
|
||||
it "returns false when the user is sixteen years old" do
|
||||
census_response = double(date_of_birth: 16.years.ago.strftime("%d-%m-%Y"))
|
||||
expect(Verification::Management::Document.new.under_sixteen?(census_response)).to be false
|
||||
end
|
||||
|
||||
it "returns false when the user is older than sixteen years old" do
|
||||
census_response = double(date_of_birth: "31-12-#{33.years.ago.year}")
|
||||
expect(Verification::Management::Document.new.under_sixteen?(census_response)).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user