From 0a668ae39a106a4eee14f86a68eaebe4efcd8ff4 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 19 Oct 2015 11:20:32 +0200 Subject: [PATCH 1/2] checks valid age --- .../verification/management/document.rb | 21 ++++++++++++++----- .../invalid_document.html.erb | 6 +++++- config/locales/management.en.yml | 1 + config/locales/management.es.yml | 1 + .../management/document_verifications_spec.rb | 10 +++++++++ 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/app/models/verification/management/document.rb b/app/models/verification/management/document.rb index 6c9a8897f..fb23528b8 100644 --- a/app/models/verification/management/document.rb +++ b/app/models/verification/management/document.rb @@ -17,7 +17,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.year < response.date_of_birth.to_date.year end def verified? @@ -28,7 +42,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 From 07117f4a493e0a7d83df71e017eb54530e609d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 19 Oct 2015 12:18:42 +0200 Subject: [PATCH 2/2] includes all date in document age verification --- .../verification/management/document.rb | 3 +- .../verification/management/document_spec.rb | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 spec/models/verification/management/document_spec.rb diff --git a/app/models/verification/management/document.rb b/app/models/verification/management/document.rb index fb23528b8..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 @@ -31,7 +32,7 @@ class Verification::Management::Document end def under_sixteen?(response) - 16.years.ago.year < response.date_of_birth.to_date.year + 16.years.ago < string_to_date(response.date_of_birth) end def verified? 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