Skip to content

Commit

Permalink
Adds MockCensusAuthorization for testing the verification on Try
Browse files Browse the repository at this point in the history
  • Loading branch information
andreslucena committed Feb 14, 2020
1 parent d961fa4 commit cdc38d8
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gem "delayed_job_active_record"
gem "daemons"
gem "listen", "~> 3.1"
gem "letter_opener_web", "~> 1.3"
gem "virtus-multiparams"

group :production do
gem "secure_headers"
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ DEPENDENCIES
spring-watcher-listen (~> 2.0)
therubyracer
uglifier (~> 4.1)
virtus-multiparams
web-console (~> 3.5)
wicked_pdf
wkhtmltopdf-binary
Expand Down
87 changes: 87 additions & 0 deletions app/services/mock_census_authorization_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# frozen_string_literal: true
# Checks the authorization against a mock census for demo.
# This is only for testing how the verifications work on a staging server.
# DON'T use this for production
require "digest/md5"

# This class performs a fake check to test verifications on Decidim.
class MockCensusAuthorizationHandler < Decidim::AuthorizationHandler
include ActionView::Helpers::SanitizeHelper
include Virtus::Multiparams

AVAILABLE_GENDERS = %w(man woman non_binary)

attribute :document_number, String
attribute :document_type, Symbol
attribute :postal_code, String
attribute :scope_id, Integer
attribute :date_of_birth, Date
attribute :gender, String

validates :date_of_birth, presence: true
validates :document_type, inclusion: { in: %i(dni nie passport) }, presence: true
validates :document_number, format: { with: /\A[A-z0-9]*\z/ }, presence: true
validates :postal_code, presence: true, format: { with: /\A[0-9]*\z/ }
validates :scope_id, presence: true

validate :over_14

# If you need to store any of the defined attributes in the authorization you
# can do it here.
#
# You must return a Hash that will be serialized to the authorization when
# it's created, and available though authorization.metadata
def metadata
super.merge(
date_of_birth: date_of_birth,
gender: gender,
postal_code: postal_code,
scope: scope.name["ca"],
)
end

def scope
Decidim::Scope.find(scope_id)
end

def census_document_types
%i(dni nie passport).map do |type|
[I18n.t(type, scope: "decidim.mock_census_authorization_handler.document_types"), type]
end
end

def unique_id
Digest::MD5.hexdigest(
"#{document_number&.upcase}-#{Rails.application.secrets.secret_key_base}"
)
end

private

def sanitized_document_type
case document_type&.to_sym
when :dni
"01"
when :passport
"02"
when :nie
"03"
end
end

def over_14
errors.add(:date_of_birth, I18n.t("mock_census_authorization_handler.age_under", min_age: 14)) unless age && age >= 14
end

def age
return nil if date_of_birth.blank?

now = Date.current
extra_year = (now.month > date_of_birth.month) || (
now.month == date_of_birth.month && now.day >= date_of_birth.day
)

now.year - date_of_birth.year - (extra_year ? 0 : 1)
end

end
33 changes: 33 additions & 0 deletions app/views/mock_census_authorization/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div class="field">
<%= form.select :document_type, handler.census_document_types, prompt: true %>
</div>

<div class="field">
<%= form.text_field :document_number %>
</div>

<div class="field date">
<%= form.date_select :date_of_birth, start_year: 1900, end_year: 14.years.ago.year, default: 35.years.ago, prompt: { day: t(".date_select.day"), month: t(".date_select.month"), year: t(".date_select.year") } %>
</div>

<div class="field">
<%= form.text_field :postal_code %>
</div>

<div class="field">
<% parent_scope = Decidim::Scope.where("name->>'ca' = 'Ciutat'").first %>
<%= form.collection_select :scope_id, current_organization.scopes.where(parent: parent_scope), :id, ->(scope){ translated_attribute(scope.name) }, prompt: t(".scope_prompt") %>
</div>

<div class="field">
<%= form.collection_select(
:gender,
MockCensusAuthorizationHandler::AVAILABLE_GENDERS,
:to_s,
->(gender) { t(".genders.#{gender}") },
prompt: t(".gender_prompt")
) %>
<p class="help-text"><%= t ".gender_help" %></p>
</div>

<%= form.hidden_field :handler_name %>
4 changes: 4 additions & 0 deletions config/initializers/decidim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@

Rails.application.config.i18n.available_locales = Decidim.available_locales
Rails.application.config.i18n.default_locale = Decidim.default_locale

Decidim::Verifications.register_workflow(:mock_census_authorization_handler) do |auth|
auth.form = "MockCensusAuthorizationHandler"
end
48 changes: 48 additions & 0 deletions config/locales/mock_census_authorization.ca.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
ca:
activemodel:
attributes:
mock_census_authorization_handler:
date_of_birth: Data de naixement
document_number: Número de document
document_type: Tipus de document
gender: Sexe
postal_code: Codi postal
scope_id: Districte
mock_census_authorization:
form:
date_select:
day: Dia
month: Mes
year: Any
gender_help: Aquestes dades són voluntàries i es demanen en compliment d'allò disposat a l'article 56 de la LLEI 17/2015, del 21 de juliol, d'igualtat efectiva de dones i homes.
gender_prompt: Si us plau, selecciona una opció
genders:
man: Home
non_binary: No binari
woman: Dona
scope_prompt: Si us plau, selecciona el teu districte
mock_census_authorization_handler:
age_under: Has de ser major de %{min_age} anys per validar-te
invalid_document: Número de document incorrecte
decidim:
authorization_handlers:
mock_census_authorization_handler:
explanation: Verifica't amb el padró FALS. NOMES PER TESTEIGS!!
fields:
date_of_birth: Data de naixement
document_number: Número de document
document_type: Tipus de document
postal_code: Codi postal
scope: Districte
name: El padró FALS
mock_census_authorization_handler:
document_types:
dni: DNI
nie: NIE
passport: Passaport
verifications:
authorizations:
first_login:
actions:
mock_census_authorization_handler: Verifica't amb el padró FALS. NOMES PER TESTEIGS!!
49 changes: 49 additions & 0 deletions config/locales/mock_census_authorization.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
en:
activemodel:
attributes:
mock_census_authorization_handler:
date_of_birth: Date of birth
document_number: Document number
document_type: Document type
gender: Sex
postal_code: Postal code
scope_id: District
mock_census_authorization:
form:
date_select:
day: Day
month: Month
year: Year
gender_help: This are voluntary data
gender_prompt: Please, select an option
genders:
man: Man
non_binary: Non binary
woman: Woman
scope_prompt: Please select your district
mock_census_authorization_handler:
age_under: You have to be older than %{min_age} to validate yourself
invalid_document: Document number invalid
decidim:
authorization_handlers:
mock_census_authorization_handler:
explanation: Verify yourself with the FAKE census. TESTING ONLY!!
fields:
date_of_birth: Date of Birth
document_number: Document number
document_type: Document type
postal_code: Postal code
scope_id: District
name: FAKE census
mock_census_authorization_handler:
document_types:
dni: DNI
nie: NIE
passport: Passport
verifications:
authorizations:
first_login:
actions:
mock_census_authorization_handler: Verify yourself with the FAKE census. TESTING ONLY!!

48 changes: 48 additions & 0 deletions config/locales/mock_census_authorization.es.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
es:
activemodel:
attributes:
census_authorization_handler:
date_of_birth: Fecha de nacimiento
document_number: Número de documento
document_type: Tipo de documento
gender: Sexo
postal_code: Código postal
scope_id: Distrito
census_authorization:
form:
date_select:
day: Día
month: Mes
year: Año
gender_help: Estos datos son voluntarios y se piden en cumplimento de lo dispuesto en el articulo 56 de la LEY 17/2015, del 21 de julio, de igualdad efectiva de mujeres y hombres.
gender_prompt: Por favor, selecciona una opción
genders:
man: Hombre
non_binary: No binario
woman: Mujer
scope_prompt: Por favor, selecciona tu distrito
census_authorization_handler:
age_under: Tienes que ser mayor de %{min_age} años para validarte
invalid_document: Número de documento incorrecto
decidim:
authorization_handlers:
census_authorization_handler:
explanation: Verifícate con el padrón FALSO. SOLO PARA PRUEBAS!!
fields:
date_of_birth: Fecha de nacimiento
document_number: Número de documento
document_type: Tipo de documento
postal_code: Código postal
scope_id: Distrito
name: El padrón FALSO
census_authorization_handler:
document_types:
dni: DNI
nie: NIE
passport: Pasaporte
verifications:
authorizations:
first_login:
actions:
census_authorization_handler: Verifícate con el padrón FALSO. SOLO PARA PRUEBAS!!

0 comments on commit cdc38d8

Please sign in to comment.