Skip to content

Commit

Permalink
Add tests for users 'date_of_birth' field
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgumberg committed Nov 3, 2023
1 parent f549792 commit 6107a2d
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 2 deletions.
1 change: 1 addition & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
sequence(:display_name) { |n| "User #{n}" }
password { "12345678" }
password_confirmation { "12345678" }
sequence(:date_of_birth) { |n| Date.new(1969, 7, 24) + n.day }
case_assignments { [] }
phone_number { "" }
confirmed_at { Time.now }
Expand Down
5 changes: 5 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
expect(user.valid?).to be false
end

it "requires date of birth to be in the past" do
user = build(:user, date_of_birth: 10.days.from_now)
expect(user.valid?).to be false
end

it "has an empty old_emails array when initialized" do
user = build(:user)
expect(user.old_emails).to eq([])
Expand Down
3 changes: 2 additions & 1 deletion spec/requests/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
volunteer = build(:volunteer)
sign_in volunteer

patch users_path, params: {user: {display_name: "New Name", address_attributes: {content: "some address"}, phone_number: "+12223334444", sms_notification_event_ids: [SmsNotificationEvent.first.id]}}
patch users_path, params: {user: {display_name: "New Name", address_attributes: {content: "some address"}, phone_number: "+12223334444", date_of_birth: Date.new(1958, 12, 1), sms_notification_event_ids: [SmsNotificationEvent.first.id]}}

expect(volunteer.address.content).to eq "some address"
expect(volunteer.display_name).to eq "New Name"
expect(volunteer.phone_number).to eq "+12223334444"
expect(volunteer.date_of_birth).to eq Date.new(1958, 12, 1)
expect(volunteer.sms_notification_event_ids).to include SmsNotificationEvent.first.id
expect(UserSmsNotificationEvent.count).to eq 1
end
Expand Down
10 changes: 10 additions & 0 deletions spec/system/casa_admins/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
it "can successfully edit user display name and phone number" do
expected_display_name = "Root Admin"
expected_phone_number = "+14398761234"
expected_date_of_birth = "1997/04/16"

visit edit_casa_admin_path(admin)

fill_in "Display name", with: expected_display_name
fill_in "Phone number", with: expected_phone_number
fill_in "Date of birth", with: expected_date_of_birth
check "Receive Monthly Learning Hours Report"

click_on "Submit"
Expand All @@ -24,6 +26,7 @@

expect(admin.display_name).to eq expected_display_name
expect(admin.phone_number).to eq expected_phone_number
expect(admin.date_of_birth.strftime("%Y/%m/%d")).to eq expected_date_of_birth
expect(admin.monthly_learning_hours_report).to be_truthy
end
end
Expand Down Expand Up @@ -69,6 +72,13 @@

it_should_behave_like "shows error for invalid phone numbers"

it "shows error message for invalid date" do
fill_in "Date of birth", with: 8.days.from_now.strftime("%Y/%m/%d")
click_on "Submit"

expect(page).to have_text "Date of birth must be in the past."
end

it "shows error message for empty email" do
fill_in "Email", with: ""
fill_in "Display name", with: ""
Expand Down
27 changes: 26 additions & 1 deletion spec/system/supervisors/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
visit edit_supervisor_path(supervisor)
end
it_should_behave_like "shows error for invalid phone numbers"

it "shows error for invalid date of birth" do
fill_in "Date of birth", with: 5.days.from_now.strftime("%Y/%m/%d")
end
end

it "can go to the supervisor edit page and see red message when there are no active volunteers" do
Expand Down Expand Up @@ -164,12 +168,14 @@
@old_email = @supervisor.email
visit edit_supervisor_path(@supervisor)
fill_in "supervisor_email", with: "new_supervisor_email@example.com"
fill_in "supervisor_phone_number", with: "+14155556876"
fill_in "supervisor_date_of_birth", with: "2003/05/06"

click_on "Submit"
@supervisor.reload
end

it "sends a confirmaton email to the supervisor and displays current email" do
it "sends a confirmation email to the supervisor and displays current email" do
expect(ActionMailer::Base.deliveries.count).to eq(1)
expect(ActionMailer::Base.deliveries.first).to be_a(Mail::Message)
expect(ActionMailer::Base.deliveries.first.body.encoded)
Expand All @@ -190,6 +196,25 @@
end
end

context "when entering invalid information" do
before do
sign_in user
@supervisor = create(:supervisor)
visit edit_supervisor_path(@supervisor)
end

it "shows error message for invalid phone number" do
fill_in "supervisor_phone_number", with: "+1415555676"
click_on "Submit"
expect(page).to have_text "Phone number must be 10 digits or 12 digits including country code (+1)"
end
it "shows error message for invalid date of birth" do
fill_in "supervisor_date_of_birth", with: 5.days.from_now.strftime("%Y/%m/%d")
click_on "Submit"
expect(page).to have_text "Date of birth must be in the past."
end
end

context "when the email exists already" do
let!(:existing_supervisor) { create(:supervisor, casa_org_id: organization.id) }

Expand Down
25 changes: 25 additions & 0 deletions spec/system/users/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,19 @@
expect(page).to have_content "1 error prohibited this Supervisor from being saved:"
expect(page).to have_text("Must add a valid phone number to receive SMS notifications.")
end

it "displays Supervisor error message if invalid date of birth" do
org = create(:casa_org)
supervisor = create(:supervisor, casa_org: org)

sign_in supervisor
visit edit_users_path

fill_in "Date of birth", with: 8.days.from_now.strftime("%Y/%m/%d")
click_on "Update Profile"
expect(page).to have_content "1 error prohibited this Supervisor from being saved:"
expect(page).to have_text("Date of birth must be in the past.")
end
end

context "when admin" do
Expand Down Expand Up @@ -538,5 +551,17 @@
expect(page).to have_content "1 error prohibited this Casa admin from being saved:"
expect(page).to have_text("Must add a valid phone number to receive SMS notifications.")
end

it "displays admin error message if invalid date of birth" do
org = create(:casa_org)
admin = create(:casa_admin, casa_org: org)

sign_in admin
visit edit_users_path

fill_in "Date of birth", with: 8.days.from_now.strftime("%Y/%m/%d")
click_on "Update Profile"
expect(page).to have_text("Date of birth must be in the past.")
end
end
end
16 changes: 16 additions & 0 deletions spec/system/volunteers/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
visit edit_volunteer_path(volunteer)

fill_in "volunteer_display_name", with: "Kamisato Ayato"
fill_in "volunteer_phone_number", with: "+14163248967"
fill_in "volunteer_date_of_birth", with: "1988/07/01"
click_on "Submit"

expect(page).to have_text "Volunteer was successfully updated."
Expand Down Expand Up @@ -74,6 +76,20 @@

expect(page).to have_text "Phone number must be 10 digits or 12 digits including country code (+1)"
end

it "shows error message for invalid date of birth" do
organization = create(:casa_org)
admin = create(:casa_admin, casa_org_id: organization.id)
volunteer = create(:volunteer, :with_assigned_supervisor, casa_org_id: organization.id)

sign_in admin
visit edit_volunteer_path(volunteer)

fill_in "volunteer_date_of_birth", with: 5.days.from_now.strftime("%Y/%m/%d")
click_on "Submit"

expect(page).to have_text "Date of birth must be in the past."
end
end

it "shows error message for duplicate email" do
Expand Down
2 changes: 2 additions & 0 deletions spec/system/volunteers/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

fill_in "Email", with: "new_volunteer@example.com"
fill_in "Display name", with: "New Volunteer Display Name"
fill_in "Date of birth", with: "08/08/2001"

click_on "Create Volunteer"

Expand All @@ -30,6 +31,7 @@

fill_in "Email", with: "new_volunteer2@example.com"
fill_in "Display name", with: "New Volunteer Display Name 2"
fill_in "Date of birth", with: "01/01/2000"

expect do
click_on "Create Volunteer"
Expand Down
50 changes: 50 additions & 0 deletions spec/views/volunteers/edit.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@
expect(rendered).to_not have_field("volunteer_email", readonly: true)
end

it "allows an administrator to edit a volunteers date of birth" do
administrator = build_stubbed :casa_admin
enable_pundit(view, administrator)
org = create :casa_org
volunteer = create :volunteer, casa_org: org
allow(view).to receive(:current_user).and_return(administrator)
allow(view).to receive(:current_organization).and_return(administrator.casa_org)

assign :volunteer, volunteer
assign :supervisors, []

render template: "volunteers/edit"

expect(rendered).to_not have_field("volunteer_date_of_birth", readonly: true)
expect(rendered).to have_field("volunteer_date_of_birth", readonly: false)
end

it "allows a supervisor to edit a volunteers email address" do
supervisor = build_stubbed :supervisor
enable_pundit(view, supervisor)
Expand Down Expand Up @@ -65,6 +82,23 @@
expect(rendered).to have_field("volunteer_phone_number")
end

it "allows a supervisor in the same org to edit a volunteers date of birth" do
org = create :casa_org
supervisor = build_stubbed :supervisor, casa_org: org
enable_pundit(view, supervisor)
volunteer = create :volunteer, casa_org: org
allow(view).to receive(:current_user).and_return(supervisor)
allow(view).to receive(:current_organization).and_return(supervisor.casa_org)

assign :volunteer, volunteer
assign :supervisors, []

render template: "volunteers/edit"

expect(rendered).to_not have_field("volunteer_date_of_birth", readonly: true)
expect(rendered).to have_field("volunteer_date_of_birth", readonly: false)
end

it "does not allow a supervisor from a different org to edit a volunteers phone number" do
different_supervisor = build_stubbed :supervisor
enable_pundit(view, different_supervisor)
Expand All @@ -81,6 +115,22 @@
expect(rendered).not_to have_field("volunteer_phone_number")
end

it "does not allow a supervisor from a different org to edit a volunteers date of birth" do
different_supervisor = build_stubbed :supervisor
enable_pundit(view, different_supervisor)
org = create :casa_org
volunteer = create :volunteer, casa_org: org
allow(view).to receive(:current_user).and_return(different_supervisor)
allow(view).to receive(:current_organization).and_return(different_supervisor.casa_org)

assign :volunteer, volunteer
assign :supervisors, []

render template: "volunteers/edit"

expect(rendered).to_not have_field("volunteer_date_of_birth", readonly: false)
end

it "shows invite and login info" do
supervisor = build_stubbed :supervisor
enable_pundit(view, supervisor)
Expand Down

0 comments on commit 6107a2d

Please sign in to comment.