Merge pull request #545 from AyuntamientoMadrid/verification_postal_codes

Verification postal codes
This commit is contained in:
Juanjo Bazán
2015-09-24 13:09:14 +02:00
6 changed files with 65 additions and 11 deletions

View File

@@ -14,6 +14,7 @@ class Verification::Residence
validate :allowed_age
validate :document_number_uniqueness
validate :postal_code_in_madrid
validate :residence_in_madrid
def initialize(attrs={})
@@ -30,10 +31,19 @@ class Verification::Residence
residence_verified_at: Time.now)
end
def allowed_age
return if errors[:date_of_birth].any?
errors.add(:date_of_birth, I18n.t('verification.residence.new.error_not_allowed_age')) unless self.date_of_birth <= 16.years.ago
end
def document_number_uniqueness
errors.add(:document_number, I18n.t('errors.messages.taken')) if User.where(document_number: document_number).any?
end
def postal_code_in_madrid
errors.add(:postal_code, I18n.t('verification.residence.new.error_not_allowed_postal_code')) unless valid_postal_code?
end
def residence_in_madrid
return if errors.any?
self.date_of_birth = date_to_string(date_of_birth)
@@ -48,11 +58,6 @@ class Verification::Residence
self.date_of_birth = string_to_date(date_of_birth)
end
def allowed_age
return if errors[:date_of_birth].any?
errors.add(:date_of_birth, I18n.t('verification.residence.new.error_not_allowed_age')) unless self.date_of_birth <= 16.years.ago
end
def store_failed_attempt
FailedCensusCall.create({
user: user,
@@ -69,4 +74,8 @@ class Verification::Residence
self.document_number = self.document_number.gsub(/[^a-z0-9]+/i, "").upcase unless self.document_number.blank?
end
def valid_postal_code?
postal_code =~ /^280/
end
end

View File

@@ -23,6 +23,7 @@ en:
form_errors: "prevented your residence verification"
error_verifying_census: "The census of the city of Madrid could not verify your information. Pero revise de information and try again or get in touch with us."
error_not_allowed_age: "You need yo be at least 16 years old"
error_not_allowed_postal_code: "Please, to verify your account you need to be in the census of the Madrid town."
create:
flash:
success: "Residence verified"

View File

@@ -23,6 +23,7 @@ es:
form_errors: "evitaron verificar tu residencia"
error_verifying_census: "El Padrón de Madrid no pudo verificar tu información. Revisa la información ó ponte en contacto con nosotros."
error_not_allowed_age: "Hay que tener al menos 16 años"
error_not_allowed_postal_code: "Por favor, para verificarte debes estar empadronado en el municipio de Madrid."
create:
flash:
success: "Residencia verificada"

View File

@@ -48,7 +48,7 @@ FactoryGirl.define do
terms_of_service '1'
trait :invalid do
postal_code "12345"
postal_code "28001"
end
end

View File

@@ -32,6 +32,26 @@ feature 'Residence' do
expect(page).to have_content /\d errors? prevented your residence verification/
end
scenario 'Error on postal code not in Madrid census' do
user = create(:user)
login_as(user)
visit account_path
click_link 'Verify my account'
fill_in 'residence_document_number', with: "12345678Z"
select 'Spanish ID', from: 'residence_document_type'
select '1997', from: 'residence_date_of_birth_1i'
select 'January', from: 'residence_date_of_birth_2i'
select '1', from: 'residence_date_of_birth_3i'
fill_in 'residence_postal_code', with: '12345'
check 'residence_terms_of_service'
click_button 'Verify residence'
expect(page).to have_content 'Please, to verify your account you need to be in the census of the Madrid town.'
end
scenario 'Error on Madrid census' do
user = create(:user)
login_as(user)

View File

@@ -24,9 +24,32 @@ describe Verification::Residence do
end
it "should validate user has allowed age" do
residence = Verification::Residence.new({"date_of_birth(3i)"=>"1", "date_of_birth(2i)"=>"1", "date_of_birth(1i)"=>"#{5.year.ago.year}"})
expect(residence).to_not be_valid
expect(residence.errors[:date_of_birth]).to include("You need yo be at least 16 years old")
residence = Verification::Residence.new({"date_of_birth(3i)"=>"1", "date_of_birth(2i)"=>"1", "date_of_birth(1i)"=>"#{5.year.ago.year}"})
expect(residence).to_not be_valid
expect(residence.errors[:date_of_birth]).to include("You need yo be at least 16 years old")
end
describe "postal code" do
it "should be valid with postal codes starting with 280" do
residence.postal_code = "28012"
residence.valid?
expect(residence.errors[:postal_code].size).to eq(0)
residence.postal_code = "28023"
residence.valid?
expect(residence.errors[:postal_code].size).to eq(0)
end
it "should not be valid with postal codes not starting with 280" do
residence.postal_code = "12345"
residence.valid?
expect(residence.errors[:postal_code].size).to eq(1)
residence.postal_code = "13280"
residence.valid?
expect(residence.errors[:postal_code].size).to eq(1)
expect(residence.errors[:postal_code]).to include("Please, to verify your account you need to be in the census of the Madrid town.")
end
end
it "should validate uniquness of document_number" do
@@ -75,7 +98,7 @@ describe Verification::Residence do
describe "tries" do
it "should increase tries after a call to the Census" do
residence.postal_code = "12345"
residence.postal_code = "28011"
residence.valid?
expect(residence.user.lock.tries).to eq(1)
end
@@ -98,7 +121,7 @@ describe Verification::Residence do
document_number: "12345678Z",
document_type: "1",
date_of_birth: Date.new(1980, 12, 31),
postal_code: "12345"
postal_code: "28001"
})
end
end