Skip to content

Commit

Permalink
Add model validations for phone_numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-l-brockwell committed Oct 22, 2024
1 parent 56ee6f2 commit 7facc1a
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 39 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -777,6 +778,7 @@ DEPENDENCIES
pdf-inspector
pg
pg_search (~> 2.3.3)
phonelib
poltergeist
prawn
prawn-table
Expand Down
13 changes: 2 additions & 11 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions config/initializers/phonelib.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Phonelib.default_country = "GB"
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions spec/factories/user_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" }
Expand Down
58 changes: 37 additions & 21 deletions spec/features/users/account_creation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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
Expand All @@ -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")

Expand All @@ -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
24 changes: 19 additions & 5 deletions spec/features/users/collaborator_registration_flow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: "[email protected]")
first("input#collaborator_role_account_admin").set(true)

Expand All @@ -35,17 +32,34 @@
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
click_button("Save and continue")

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

0 comments on commit 7facc1a

Please sign in to comment.