Skip to content

Commit

Permalink
Add spanish citizen number validation
Browse files Browse the repository at this point in the history
  • Loading branch information
fblupi committed Dec 18, 2023
1 parent b7024dc commit 474636f
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .simplecov
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

SimpleCov.start do
root ENV.fetch("ENGINE_ROOT", nil)

add_filter "/spec"
end

SimpleCov.command_name ENV.fetch("COMMAND_NAME", nil) || File.basename(Dir.pwd)

SimpleCov.merge_timeout 1800
47 changes: 47 additions & 0 deletions app/forms/concerns/decidim/account_form_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ module Decidim
module AccountFormOverride
extend ActiveSupport::Concern

DNI_LETTERS = "TRWAGMYFPDXBNJZSQVHLCKE"
DNI_REGEX = /^(\d{8})([A-Z])$/
NIE_REGEX = /^[XYZ]\d{7,8}[A-Z]$/

included do
validate :check_document_id

def map_model(model)
map_model_centers(model)
map_model_extra_user_fields(model)
Expand All @@ -31,6 +37,47 @@ def map_model_extra_user_fields(model)
self.profession = extended_data[:profession]
self.document_id = extended_data[:document_id]
end

def check_document_id
return unless document_id?

case document_type
when "DNI"
errors.add(:document_id, :invalid) unless valid_dni?(scaped_document_id)
when "NIE"
errors.add(:document_id, :invalid) unless valid_nie?
else
errors.add(:document_id, :invalid)
end
end

def scaped_document_id
@scaped_document_id ||= document_id.upcase.delete("^A-Z0-9")
end

def document_type
@document_type ||= if scaped_document_id =~ DNI_REGEX
"DNI"
elsif scaped_document_id =~ NIE_REGEX
"NIE"
end
end

def valid_nie?
nie_prefix = case scaped_document_id[0]
when "X"
0
when "Y"
1
else
2
end
valid_dni?("#{nie_prefix}#{scaped_document_id[1..]}")
end

def valid_dni?(dni)
dni[-1] == DNI_LETTERS[dni.to_i % 23]
end
end
end
end
79 changes: 79 additions & 0 deletions spec/forms/decidim/account_form_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

require "rails_helper"

describe Decidim::AccountForm do
subject do
described_class.new(
name: user.name,
nickname: user.nickname,
email: user.email,
password: user.password,
password_confirmation: user.password,
tos_agreement: "1",
center_id: user.center.id,
scope_id: user.scope.id,
document_id: document_id
).with_context(
current_organization: organization,
current_user: user
)
end

let(:organization) { create :organization, extra_user_fields: { "enabled" => true, "document_id" => { "enabled" => true } } }
let(:user) { create :user, organization: organization }
let!(:center_user) { create :center_user, user: user }
let!(:scope_user) { create :scope_user, user: user }

context "with valid citizen number" do
let(:document_id) { Faker::IDNumber.spanish_citizen_number }

it { is_expected.to be_valid }
end

context "with invalid citizen number" do
let(:document_id) { "12345678-A" }

it { is_expected.not_to be_valid }
end

context "with valid foreign citizen number" do
let(:document_id) { Faker::IDNumber.spanish_foreign_citizen_number }

it { is_expected.to be_valid }
end

context "with invalid foreign citizen number" do
let(:document_id) { "Z-1234567-A" }

it { is_expected.not_to be_valid }
end

context "with invalid document id" do
let(:document_id) { "foo" }

it { is_expected.not_to be_valid }
end

context "with document id disabled" do
let(:organization) { create :organization, extra_user_fields: { "enabled" => false } }

context "with invalid citizen number" do
let(:document_id) { "12345678-A" }

it { is_expected.to be_valid }
end

context "with invalid foreign citizen number" do
let(:document_id) { "Z-1234567-A" }

it { is_expected.to be_valid }
end

context "with invalid document id" do
let(:document_id) { "foo" }

it { is_expected.to be_valid }
end
end
end
1 change: 0 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
Decidim::Dev.dummy_app_path = File.expand_path(File.join(__dir__, ".."))

require "decidim/dev/test/base_spec_helper"
SimpleCov.start "rails" if ENV["SIMPLECOV"]

if ENV["CODECOV"]
require "codecov"
Expand Down

0 comments on commit 474636f

Please sign in to comment.