diff --git a/Gemfile b/Gemfile index 14c50289e..9bc8d4487 100644 --- a/Gemfile +++ b/Gemfile @@ -82,6 +82,7 @@ gem "simple_form", "~> 5.0" gem "country_select", "~> 3.1" gem "email_validator" gem "enumerize" +gem "phonelib" # PDF generation gem "prawn" diff --git a/Gemfile.lock b/Gemfile.lock index 8d1711265..c352991ed 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -400,6 +400,7 @@ GEM pg_search (2.3.6) activerecord (>= 5.2) activesupport (>= 5.2) + phonelib (0.9.2) poltergeist (1.18.1) capybara (>= 2.1, < 4) cliver (~> 0.3.1) @@ -777,6 +778,7 @@ DEPENDENCIES pdf-inspector pg pg_search (~> 2.3.3) + phonelib poltergeist prawn prawn-table diff --git a/app/models/user.rb b/app/models/user.rb index 2e946b64d..f6520a219 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -25,17 +25,8 @@ class User < ApplicationRecord validates :phone_number, presence: true, if: -> { first_step? } validates :password, confirmation: true - validates :phone_number, length: { - minimum: 7, - maximum: 20, - message: "This is not a valid telephone number", - }, if: -> { first_step? } - - validates :company_phone_number, length: { - minimum: 7, - maximum: 20, - message: "This is not a valid telephone number", - }, allow_blank: true, if: -> { second_step? } + validates :phone_number, phone: true, if: -> { first_step? } + validates :company_phone_number, phone: { allow_blank: true }, if: -> { second_step? } validates_with AdvancedEmailValidator diff --git a/config/initializers/phonelib.rb b/config/initializers/phonelib.rb new file mode 100644 index 000000000..4802c4220 --- /dev/null +++ b/config/initializers/phonelib.rb @@ -0,0 +1 @@ +Phonelib.default_country = "GB" diff --git a/config/locales/en.yml b/config/locales/en.yml index 611696925..6a14fd886 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -79,6 +79,10 @@ en: models: user: attributes: + phone_number: + invalid: Enter a phone number, like 01635 960 001, 07701 900 982 or +44 808 157 0192 + company_phone_number: + invalid: Enter a phone number, like 01635 960 001, 07701 900 982 or +44 808 157 0192 email: invalid: 'Email is invalid - enter a valid email address' role: diff --git a/spec/factories/user_factory.rb b/spec/factories/user_factory.rb index 03c931f7c..259c6b0a8 100644 --- a/spec/factories/user_factory.rb +++ b/spec/factories/user_factory.rb @@ -6,7 +6,7 @@ email role { "regular" } agreed_with_privacy_policy { "1" } - sequence(:phone_number) { |n| "1111111#{n}" } + sequence(:phone_number) { |n| "020 4551 008#{n.to_s[-1]}" } confirmed_at { Time.zone.now } trait :completed_profile do @@ -18,7 +18,7 @@ company_city { "London" } company_country { "GB" } company_postcode { "SE16 3SA" } - sequence(:company_phone_number) { |n| "7777777#{n}" } + sequence(:company_phone_number) { |n| "020 4551 008#{n.to_s[-1]}" } prefered_method_of_contact { "phone" } qae_info_source { "govuk" } role { "regular" } diff --git a/spec/features/users/account_creation_spec.rb b/spec/features/users/account_creation_spec.rb index 48db13a3c..52e62eda0 100644 --- a/spec/features/users/account_creation_spec.rb +++ b/spec/features/users/account_creation_spec.rb @@ -32,27 +32,30 @@ end context "Account details fulfillment" do + def fill_in_and_submit_account_details_form + fill_in("Title", with: "Mr") + fill_in("First name", with: "FirstName") + fill_in("Last name", with: "LastName") + fill_in("Your job title", with: "job title") + fill_in("Your telephone number", with: phone_number) + + click_button("Save and continue") + end + context "regular user" do let!(:user) { create(:user, role: "regular") } before do create(:settings, :submission_deadlines) login_as(user, scope: :user) + visit root_path + fill_in_and_submit_account_details_form end - let(:phone_number) { "1231233214354235" } + let(:phone_number) { "020 4551 0081" } let(:company_name) { "BitZestyOrg" } it "adds the Account details" do - visit root_path - fill_in("Title", with: "Mr") - fill_in("First name", with: "FirstName") - fill_in("Last name", with: "LastName") - fill_in("Your job title", with: "job title") - fill_in("Your telephone number", with: phone_number) - - click_button("Save and continue") - expect(page).to have_content("Contact preferences") click_button("Save and continue") @@ -63,6 +66,16 @@ expect(user.phone_number).to eq(phone_number) expect(user.completed_registration?).to eq(true) end + + context "with an invalid phone number" do + let(:phone_number) { "020 4551 008" } + + it "displays an error message" do + expect(page).to have_content( + I18n.t("activerecord.errors.models.user.attributes.phone_number.invalid"), + ) + end + end end context "admin user" do @@ -71,27 +84,20 @@ before do create(:settings, :submission_deadlines) login_as(user, scope: :user) + visit root_path + fill_in_and_submit_account_details_form end - let(:phone_number) { "1231233214354235" } + let(:phone_number) { "020 4551 0081" } let(:company_name) { "BitZestyOrg" } it "adds the Account details" do - visit root_path - fill_in("Title", with: "Mr") - fill_in("First name", with: "FirstName") - fill_in("Last name", with: "LastName") - fill_in("Your job title", with: "job title") - fill_in("Your telephone number", with: phone_number) - - click_button("Save and continue") - expect(page).to have_content("Contact preferences") click_button("Save and continue") expect(page).to have_content("Organisation details") fill_in("Name of the organisation", with: company_name) - fill_in("The organisation's main telephone number", with: "9876544") + fill_in("The organisation's main telephone number", with: "020 4551 0082") click_button("Save and continue") @@ -102,6 +108,16 @@ expect(user.phone_number).to eq(phone_number) expect(user.company_name).to eq(company_name) end + + context "with an invalid phone number" do + let(:phone_number) { "020 4551 008" } + + it "displays an error message" do + expect(page).to have_content( + I18n.t("activerecord.errors.models.user.attributes.phone_number.invalid"), + ) + end + end end end end diff --git a/spec/features/users/collaborator_registration_flow_spec.rb b/spec/features/users/collaborator_registration_flow_spec.rb index dfff61278..a0aba90b4 100644 --- a/spec/features/users/collaborator_registration_flow_spec.rb +++ b/spec/features/users/collaborator_registration_flow_spec.rb @@ -8,9 +8,6 @@ before do create(:settings, :submission_deadlines) - end - - it "creates and account and collaborator is able to collaborate" do login_as(acc_admin, scope: :user) visit new_account_collaborator_path @@ -19,7 +16,7 @@ fill_in("First name", with: "First Name") fill_in("Last name", with: "Last Name") fill_in("Job title", with: "job title") - fill_in("Telephone number", with: "1231233214354235") + fill_in("Telephone number", with: "020 4551 0081") fill_in("Email", with: "collab@example.com") first("input#collaborator_role_account_admin").set(true) @@ -35,12 +32,14 @@ visit root_path click_button("Save and continue") + end + it "creates and account and collaborator is able to collaborate" do expect(page).to have_content("Contact preferences") click_button("Save and continue") fill_in "Name of the organisation", with: "Disney" - fill_in "The organisation's main telephone number", with: "012312312" + fill_in "The organisation's main telephone number", with: "020 4551 0082" click_button("Save and continue") # collaborator page @@ -48,4 +47,19 @@ expect(page).to have_content("Applying for a King's Award for your organisation") end + + context "when the company_phone_number is invalid" do + it "displays an error message" do + expect(page).to have_content("Contact preferences") + click_button("Save and continue") + + fill_in "Name of the organisation", with: "Disney" + fill_in "The organisation's main telephone number", with: "020 4551 008" + click_button("Save and continue") + + expect(page).to have_content( + I18n.t("activerecord.errors.models.user.attributes.company_phone_number.invalid"), + ) + end + end end