diff --git a/app/models/verification/management/document.rb b/app/models/verification/management/document.rb index 997ed9c0c..987256d01 100644 --- a/app/models/verification/management/document.rb +++ b/app/models/verification/management/document.rb @@ -2,8 +2,7 @@ class Verification::Management::Document include ActiveModel::Model include ActiveModel::Dates - attr_accessor :document_type - attr_accessor :document_number + attr_accessor :document_type, :document_number, :date_of_birth, :postal_code validates :document_type, :document_number, presence: true @@ -18,7 +17,7 @@ class Verification::Management::Document end def in_census? - response = CensusCaller.new.call(document_type, document_number, nil, nil) + response = CensusCaller.new.call(document_type, document_number, date_of_birth, postal_code) response.valid? && valid_age?(response) end diff --git a/spec/factories/verifications.rb b/spec/factories/verifications.rb index 6ce249d58..4878e46a7 100644 --- a/spec/factories/verifications.rb +++ b/spec/factories/verifications.rb @@ -56,4 +56,11 @@ FactoryBot.define do document_number document_type "dni" end + + factory :verification_document, class: Verification::Management::Document do + document_number + document_type "1" + date_of_birth Date.new(1980, 12, 31) + postal_code "28013" + end end diff --git a/spec/models/verification/management/document_spec.rb b/spec/models/verification/management/document_spec.rb index f50e0bf21..bdb8cd89e 100644 --- a/spec/models/verification/management/document_spec.rb +++ b/spec/models/verification/management/document_spec.rb @@ -1,15 +1,76 @@ require "rails_helper" describe Verification::Management::Document do - let(:min_age) { User.minimum_required_age } - let(:over_minium_age_date_of_birth) { Date.new((min_age + 10).years.ago.year, 12, 31) } - let(:under_minium_age_date_of_birth) { Date.new(min_age.years.ago.year, 12, 31) } - let(:just_minium_age_date_of_birth) { Date.new(min_age.years.ago.year, min_age.years.ago.month, min_age.years.ago.day) } - describe "#valid_age?" do - it "returns false when the user is younger than the user's minimum required age" do - census_response = instance_double("CensusApi::Response", date_of_birth: under_minium_age_date_of_birth) - expect(described_class.new.valid_age?(census_response)).to be false + let(:verification_document) { build(:verification_document, document_number: "12345678Z") } + + describe "validations" do + + it "is valid" do + expect(verification_document).to be_valid + end + + it "is not valid without a document number" do + verification_document.document_number = nil + expect(verification_document).not_to be_valid + end + + it "is not valid without a document type" do + verification_document.document_type = nil + expect(verification_document).not_to be_valid + end + + it "is valid without a date of birth" do + verification_document.date_of_birth = nil + expect(verification_document).to be_valid + end + + it "is valid without a postal code" do + verification_document.postal_code = nil + expect(verification_document).to be_valid + end + + describe "Allowed Age" do + let(:min_age) { User.minimum_required_age } + let(:over_minium_age_date_of_birth) { Date.new((min_age + 10).years.ago.year, 12, 31) } + let(:under_minium_age_date_of_birth) { Date.new(min_age.years.ago.year, 12, 31) } + let(:just_minium_age_date_of_birth) { Date.new(min_age.years.ago.year, min_age.years.ago.month, min_age.years.ago.day) } + + describe "#valid_age?" do + it "returns false when the user is younger than the user's minimum required age" do + census_response = instance_double("CensusApi::Response", date_of_birth: under_minium_age_date_of_birth) + expect(described_class.new.valid_age?(census_response)).to be false + end + + it "returns true when the user has the user's minimum required age" do + census_response = instance_double("CensusApi::Response", date_of_birth: just_minium_age_date_of_birth) + expect(described_class.new.valid_age?(census_response)).to be true + end + + it "returns true when the user is older than the user's minimum required age" do + census_response = instance_double("CensusApi::Response", date_of_birth: over_minium_age_date_of_birth) + expect(described_class.new.valid_age?(census_response)).to be true + end + end + + describe "#under_age?" do + it "returns true when the user is younger than the user's minimum required age" do + census_response = instance_double("CensusApi::Response", date_of_birth: under_minium_age_date_of_birth) + expect(described_class.new.under_age?(census_response)).to be true + end + + it "returns false when the user is user's minimum required age" do + census_response = instance_double("CensusApi::Response", date_of_birth: just_minium_age_date_of_birth) + expect(described_class.new.under_age?(census_response)).to be false + end + + it "returns false when the user is older than user's minimum required age" do + census_response = instance_double("CensusApi::Response", date_of_birth: over_minium_age_date_of_birth) + expect(described_class.new.under_age?(census_response)).to be false + end + end + end + end end it "returns true when the user has the user's minimum required age" do