diff --git a/app/models/verification/management/document.rb b/app/models/verification/management/document.rb
index 6c9a8897f..ee9e5462d 100644
--- a/app/models/verification/management/document.rb
+++ b/app/models/verification/management/document.rb
@@ -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
\ No newline at end of file
diff --git a/app/views/management/document_verifications/invalid_document.html.erb b/app/views/management/document_verifications/invalid_document.html.erb
index ee7120c9b..3652e1db7 100644
--- a/app/views/management/document_verifications/invalid_document.html.erb
+++ b/app/views/management/document_verifications/invalid_document.html.erb
@@ -1,5 +1,9 @@
- <%= 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 %>
<%= render 'management/user_permissions',
diff --git a/config/locales/management.en.yml b/config/locales/management.en.yml
index 34a30a832..51e869dae 100644
--- a/config/locales/management.en.yml
+++ b/config/locales/management.en.yml
@@ -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 'Register' in the upper-left part of the screen."
verify: "Verify"
diff --git a/config/locales/management.es.yml b/config/locales/management.es.yml
index d56fb625d..f3aa2fbeb 100644
--- a/config/locales/management.es.yml
+++ b/config/locales/management.es.yml
@@ -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 'Registrarse' en la parte superior derecha de la pantalla."
verify: "Verificar usuario"
diff --git a/spec/features/management/document_verifications_spec.rb b/spec/features/management/document_verifications_spec.rb
index 4a437a661..674349815 100644
--- a/spec/features/management/document_verifications_spec.rb
+++ b/spec/features/management/document_verifications_spec.rb
@@ -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
\ No newline at end of file
diff --git a/spec/models/verification/management/document_spec.rb b/spec/models/verification/management/document_spec.rb
new file mode 100644
index 000000000..b4140f7ed
--- /dev/null
+++ b/spec/models/verification/management/document_spec.rb
@@ -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
\ No newline at end of file