diff --git a/spec/factories/users.rb b/spec/factories/users.rb index fb21549d15..df83596cdb 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -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 } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 7679cf071a..f9457855bc 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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([]) diff --git a/spec/requests/users_spec.rb b/spec/requests/users_spec.rb index 1c30bc6123..a00288dab2 100644 --- a/spec/requests/users_spec.rb +++ b/spec/requests/users_spec.rb @@ -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 diff --git a/spec/system/casa_admins/edit_spec.rb b/spec/system/casa_admins/edit_spec.rb index 8647415adb..457ed89512 100644 --- a/spec/system/casa_admins/edit_spec.rb +++ b/spec/system/casa_admins/edit_spec.rb @@ -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" @@ -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 @@ -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: "" diff --git a/spec/system/supervisors/edit_spec.rb b/spec/system/supervisors/edit_spec.rb index e14ea4a1a1..4ce98ea24b 100644 --- a/spec/system/supervisors/edit_spec.rb +++ b/spec/system/supervisors/edit_spec.rb @@ -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 @@ -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) @@ -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) } diff --git a/spec/system/users/edit_spec.rb b/spec/system/users/edit_spec.rb index 3e71fc4e99..580a85ac24 100644 --- a/spec/system/users/edit_spec.rb +++ b/spec/system/users/edit_spec.rb @@ -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 @@ -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 diff --git a/spec/system/volunteers/edit_spec.rb b/spec/system/volunteers/edit_spec.rb index 1b5588857c..62354315f9 100644 --- a/spec/system/volunteers/edit_spec.rb +++ b/spec/system/volunteers/edit_spec.rb @@ -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." @@ -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 diff --git a/spec/system/volunteers/new_spec.rb b/spec/system/volunteers/new_spec.rb index 1fbb6f80be..81241e2669 100644 --- a/spec/system/volunteers/new_spec.rb +++ b/spec/system/volunteers/new_spec.rb @@ -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" @@ -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" diff --git a/spec/views/volunteers/edit.html.erb_spec.rb b/spec/views/volunteers/edit.html.erb_spec.rb index dc75043205..e6b38760f5 100644 --- a/spec/views/volunteers/edit.html.erb_spec.rb +++ b/spec/views/volunteers/edit.html.erb_spec.rb @@ -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) @@ -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) @@ -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)