Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add model validations for phone_numbers #3114

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/palace_attendee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class PalaceAttendee < ApplicationRecord
:postcode,
presence: true

validates :phone_number, phone: { allow_blank: true }
validates :disabled_access, inclusion: { in: [true, false], message: "This field cannot be blank" }
validates :has_royal_family_connections, inclusion: { in: [true, false], message: "This field cannot be blank" }

Expand Down
7 changes: 6 additions & 1 deletion app/models/press_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ class PressSummary < ApplicationRecord

validates :form_answer, :token, presence: true
validates :body, presence: true, unless: :contact_details_update?
validates :name, :email, :phone_number,
validates :name, :email,
presence: true,
unless: proc { |c| c.body_update.present? },
if: :applicant_submitted?

validates :phone_number,
phone: true,
unless: proc { |c| c.body_update.present? },
if: :applicant_submitted?

validates :title, :last_name,
presence: true,
unless: proc { |c| c.body_update.present? && form_answer.award_year.year >= 2020 },
Expand Down
14 changes: 2 additions & 12 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,10 @@ class User < ApplicationRecord
validates :first_name, presence: true, if: -> { first_step? }
validates :last_name, presence: true, if: -> { first_step? }
validates :job_title, presence: true, if: -> { first_step? }
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: { allow_blank: 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"
12 changes: 12 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,20 @@ en:
confirmation: "%{attribute} doesn't match."
not_a_number: 'Please enter a number.'
models:
palace_attendee:
attributes:
phone_number:
invalid: Enter a phone number, like 01635 960 001, 07701 900 982 or +44 808 157 0192
press_summary:
attributes:
phone_number:
invalid: Enter a phone number, like 01635 960 001, 07701 900 982 or +44 808 157 0192
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
2 changes: 1 addition & 1 deletion spec/controllers/accounts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:old_password) { "^#ur9EkLm@1W+OaDvgTT" }
let(:new_password) { "^#ur9EkLm@1W+OaDvg" }

let(:user) { create(:user, :completed_profile, password: "^#ur9EkLm@1W+OaDvgTT", password_confirmation: "^#ur9EkLm@1W+OaDvgTT") }
let(:user) { create(:user, :completed_profile, password: old_password, password_confirmation: old_password) }

before do
sign_in user
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/palace_attendees.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
address_3 { "MyString" }
address_4 { "MyString" }
postcode { "MyString" }
phone_number { "MyString" }
sequence(:phone_number) { |n| "020 4551 008#{n.to_s[-1]}" }
disabled_access { false }
additional_info { "MyText" }
has_royal_family_connections { false }
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,21 @@
end

context "js enabled", js: true do
it "adds the single Palace Attendee" do
field_values = []
let(:title) { "Mr" }
let(:first_name) { "Bob" }
let(:last_name) { "Buttons" }
let(:job_name) { "Fisherman" }
let(:post_nominals) { "Yeah that" }
let(:royal_family_connection_details) { "Cleaner" }
let(:address_1) { "123 Fake Street" }
let(:address_2) { "Electric Avenue" }
let(:address_3) { "Mordoor" }
let(:address_4) { "The World" }
let(:postcode) { "DA7 4HE" }
let(:phone_number) { "02083015556" }
let(:dietary_requirements) { "Meat eater" }

it "adds the single Palace Attendee" do
find("#palace-attendees-heading .panel-title a").click
within "#section-palace-attendees" do
find(".form-edit-link").click
Expand All @@ -44,12 +56,19 @@
find("input#palace_attendee_phone_number")
find("input#palace_attendee_has_royal_family_connections_true").set(true)
find("input#palace_attendee_disabled_access_true").set(true)
fill_in "palace_attendee_royal_family_connection_details", with: "connection details"
all("input.form-control").each_with_index do |input, index|
val = "val-#{index}"
field_values << val
input.set(val)
end
fill_in "palace_attendee_royal_family_connection_details", with: royal_family_connection_details
find("#palace_attendee_title").set(title)
find("#palace_attendee_first_name").set(first_name)
find("#palace_attendee_last_name").set(last_name)
find("#palace_attendee_job_name").set(job_name)
find("#palace_attendee_post_nominals").set(post_nominals)
find("#palace_attendee_address_1").set(address_1)
find("#palace_attendee_address_2").set(address_2)
find("#palace_attendee_address_3").set(address_3)
find("#palace_attendee_address_4").set(address_4)
find("#palace_attendee_postcode").set(postcode)
find("#palace_attendee_phone_number").set(phone_number)
find("#palace_attendee_dietary_requirements").set(dietary_requirements)
end

click_button "Save"
Expand All @@ -60,11 +79,19 @@
find("#palace-attendees-heading .panel-title a").click

within "#section-palace-attendees" do
field_values.each do |val|
expect(page).to have_content(val)
end

expect(page).to have_content("connection details")
expect(page).to have_content(title)
expect(page).to have_content(first_name)
expect(page).to have_content(last_name)
expect(page).to have_content(job_name)
expect(page).to have_content(post_nominals)
expect(page).to have_content(address_1)
expect(page).to have_content(address_2)
expect(page).to have_content(address_3)
expect(page).to have_content(address_4)
expect(page).to have_content(postcode)
expect(page).to have_content(phone_number)
expect(page).to have_content(dietary_requirements)
expect(page).to have_content(royal_family_connection_details)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/palace_attendees_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
fill_in "City or town", with: "Test"
fill_in "County", with: "Test"
fill_in "Postcode", with: "Test"
fill_in "Telephone number", with: "Test"
fill_in "Telephone number", with: "02083015556"
disabled_access = find('input[name="palace_invite[palace_attendees_attributes][0][disabled_access]"]', match: :first)
disabled_access.set(true)

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
Loading