From 6d39d839b33b06898d8dbc9cb09ed64972dd319c Mon Sep 17 00:00:00 2001 From: Audrea Cook Date: Thu, 30 Oct 2025 11:52:46 -0600 Subject: [PATCH 01/10] Test: enhance new supervisor creation specs with dynamic data and validations Signed-off-by: Audrea Cook --- spec/system/supervisors/new_spec.rb | 32 ++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/spec/system/supervisors/new_spec.rb b/spec/system/supervisors/new_spec.rb index be1eb54fa5..8dbe7dd86f 100644 --- a/spec/system/supervisors/new_spec.rb +++ b/spec/system/supervisors/new_spec.rb @@ -5,17 +5,39 @@ RSpec.describe "supervisors/new", type: :system do context "when admin" do let(:admin) { create(:casa_admin) } + let(:new_supervisor_name) { Faker::Name.name } + let(:new_supervisor_email) { Faker::Internet.email } + let(:new_supervisor_phone_number) { Faker::PhoneNumber.phone_number } + + before do + # Stub the request to the URL shortener service (needed if phone is provided) + stub_request(:post, "https://api.short.io/links") + .to_return( + status: 200, + body: {shortURL: "https://short.url/example"}.to_json, + headers: {"Content-Type" => "application/json"} + ) + end it "allows admin to create a new supervisors" do sign_in admin visit new_supervisor_path - fill_in "Email", with: "new_supervisor_email@example.com" - fill_in "Display name", with: "New Supervisor Display Name" + fill_in "Email", with: new_supervisor_email + fill_in "Display name", with: new_supervisor_name + fill_in "Phone number", with: new_supervisor_phone_number + + click_on "Create Supervisor" - expect { - click_on "Create Supervisor" - }.to change(User, :count).by(1) + expect(page).to have_text("New supervisor created successfully.") + expect(User.count).to eq(2) # admin + new supervisor + + new_supervisor = User.find_by(email: new_supervisor_email) + expect(new_supervisor).to be_present + expect(new_supervisor.display_name).to eq(new_supervisor_name) + expect(new_supervisor.phone_number).to eq(new_supervisor_phone_number) + expect(new_supervisor.supervisor?).to eq(true) + expect(new_supervisor.active?).to eq(true) end it "sends invitation email to the new supervisor" do From 3312096f098f69b23d8a85279effdccf0229cdd9 Mon Sep 17 00:00:00 2001 From: Audrea Cook Date: Thu, 30 Oct 2025 11:59:26 -0600 Subject: [PATCH 02/10] Test: improve new supervisor creation specs with dynamic email and path expectations Signed-off-by: Audrea Cook --- spec/system/supervisors/new_spec.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/spec/system/supervisors/new_spec.rb b/spec/system/supervisors/new_spec.rb index 8dbe7dd86f..136d68f198 100644 --- a/spec/system/supervisors/new_spec.rb +++ b/spec/system/supervisors/new_spec.rb @@ -31,8 +31,11 @@ expect(page).to have_text("New supervisor created successfully.") expect(User.count).to eq(2) # admin + new supervisor - + new_supervisor = User.find_by(email: new_supervisor_email) + + expect(page).to have_current_path(edit_supervisor_path(new_supervisor)) + expect(new_supervisor).to be_present expect(new_supervisor.display_name).to eq(new_supervisor_name) expect(new_supervisor.phone_number).to eq(new_supervisor_phone_number) @@ -40,17 +43,20 @@ expect(new_supervisor.active?).to eq(true) end - it "sends invitation email to the new supervisor" do + it "sends invitation email to the new supervisor", :aggregate_failures do sign_in admin visit new_supervisor_path - fill_in "Email", with: "new_supervisor_email2@example.com" - fill_in "Display name", with: "New Supervisor Display Name 2" + fill_in "Email", with: new_supervisor_email + fill_in "Display name", with: new_supervisor_name + fill_in "Phone number", with: new_supervisor_phone_number click_on "Create Supervisor" + expect(page).to have_text("New supervisor created successfully.") + last_email = ActionMailer::Base.deliveries.last - expect(last_email.to).to eq ["new_supervisor_email2@example.com"] + expect(last_email.to).to eq [new_supervisor_email] expect(last_email.subject).to have_text "CASA Console invitation instructions" expect(last_email.html_part.body.encoded).to have_text "your new Supervisor account." end From f998c3e6f5a4f4843e083039f86c472bdb40505f Mon Sep 17 00:00:00 2001 From: Audrea Cook Date: Thu, 30 Oct 2025 12:11:25 -0600 Subject: [PATCH 03/10] Test: refactor new supervisor creation specs for clarity and structure Signed-off-by: Audrea Cook --- spec/system/supervisors/new_spec.rb | 70 ++++++++++++++--------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/spec/system/supervisors/new_spec.rb b/spec/system/supervisors/new_spec.rb index 136d68f198..c7d9f2aaef 100644 --- a/spec/system/supervisors/new_spec.rb +++ b/spec/system/supervisors/new_spec.rb @@ -17,48 +17,44 @@ body: {shortURL: "https://short.url/example"}.to_json, headers: {"Content-Type" => "application/json"} ) - end - it "allows admin to create a new supervisors" do sign_in admin visit new_supervisor_path - - fill_in "Email", with: new_supervisor_email - fill_in "Display name", with: new_supervisor_name - fill_in "Phone number", with: new_supervisor_phone_number - - click_on "Create Supervisor" - - expect(page).to have_text("New supervisor created successfully.") - expect(User.count).to eq(2) # admin + new supervisor - - new_supervisor = User.find_by(email: new_supervisor_email) - - expect(page).to have_current_path(edit_supervisor_path(new_supervisor)) - - expect(new_supervisor).to be_present - expect(new_supervisor.display_name).to eq(new_supervisor_name) - expect(new_supervisor.phone_number).to eq(new_supervisor_phone_number) - expect(new_supervisor.supervisor?).to eq(true) - expect(new_supervisor.active?).to eq(true) end - it "sends invitation email to the new supervisor", :aggregate_failures do - sign_in admin - visit new_supervisor_path - - fill_in "Email", with: new_supervisor_email - fill_in "Display name", with: new_supervisor_name - fill_in "Phone number", with: new_supervisor_phone_number - - click_on "Create Supervisor" - - expect(page).to have_text("New supervisor created successfully.") - - last_email = ActionMailer::Base.deliveries.last - expect(last_email.to).to eq [new_supervisor_email] - expect(last_email.subject).to have_text "CASA Console invitation instructions" - expect(last_email.html_part.body.encoded).to have_text "your new Supervisor account." + context "with valid form submission" do + let(:new_supervisor) { User.find_by(email: new_supervisor_email) } + + before do + fill_in "Email", with: new_supervisor_email + fill_in "Display name", with: new_supervisor_name + fill_in "Phone number", with: new_supervisor_phone_number + + click_on "Create Supervisor" + end + + it "shows a success message" do + expect(page).to have_text("New supervisor created successfully.") + end + + it "redirects to the edit supervisor page" do + expect(page).to have_current_path(edit_supervisor_path(new_supervisor)) + end + + it "persists the new supervisor with correct attributes", :aggregate_failures do + expect(new_supervisor).to be_present + expect(new_supervisor.display_name).to eq(new_supervisor_name) + expect(new_supervisor.phone_number).to eq(new_supervisor_phone_number) + expect(new_supervisor.supervisor?).to be(true) + expect(new_supervisor.active?).to be(true) + end + + it "sends an invitation email to the new supervisor", :aggregate_failures do + last_email = ActionMailer::Base.deliveries.last + expect(last_email.to).to eq [new_supervisor_email] + expect(last_email.subject).to have_text "CASA Console invitation instructions" + expect(last_email.html_part.body.encoded).to have_text "your new Supervisor account." + end end end From a853ef4791283553373ffc5c2b65ea4cf19c0404 Mon Sep 17 00:00:00 2001 From: Audrea Cook Date: Thu, 30 Oct 2025 12:13:56 -0600 Subject: [PATCH 04/10] Test: add specs for invalid supervisor form submission handling Signed-off-by: Audrea Cook --- spec/system/supervisors/new_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/system/supervisors/new_spec.rb b/spec/system/supervisors/new_spec.rb index c7d9f2aaef..e093e16b99 100644 --- a/spec/system/supervisors/new_spec.rb +++ b/spec/system/supervisors/new_spec.rb @@ -56,6 +56,25 @@ expect(last_email.html_part.body.encoded).to have_text "your new Supervisor account." end end + + context "with invalid form submission" do + before do + # Don't fill in any fields + click_on "Create Supervisor" + end + + it "does not create a new user" do + expect(User.count).to eq(1) # Only the admin user exists + end + + it "shows validation error messages" do + expect(page).to have_text "errors prohibited this Supervisor from being saved:" + end + + it "stays on the new supervisor page" do + expect(page).to have_current_path(supervisors_path) + end + end end context "volunteer user" do From af7eb89b0d3675a92cc6e8453798a4a19de152f3 Mon Sep 17 00:00:00 2001 From: Audrea Cook Date: Thu, 30 Oct 2025 12:15:57 -0600 Subject: [PATCH 05/10] Test: clarify context description for admin in new supervisor specs Signed-off-by: Audrea Cook --- spec/system/supervisors/new_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/system/supervisors/new_spec.rb b/spec/system/supervisors/new_spec.rb index e093e16b99..2dac2b4434 100644 --- a/spec/system/supervisors/new_spec.rb +++ b/spec/system/supervisors/new_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" RSpec.describe "supervisors/new", type: :system do - context "when admin" do + context "when logged in as an admin" do let(:admin) { create(:casa_admin) } let(:new_supervisor_name) { Faker::Name.name } let(:new_supervisor_email) { Faker::Internet.email } From 6e45500a0ff9e61e2e37726a9d287e4a96c08e39 Mon Sep 17 00:00:00 2001 From: Audrea Cook Date: Thu, 30 Oct 2025 12:16:01 -0600 Subject: [PATCH 06/10] Test: add context for supervisor Signed-off-by: Audrea Cook --- spec/system/supervisors/new_spec.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/spec/system/supervisors/new_spec.rb b/spec/system/supervisors/new_spec.rb index 2dac2b4434..bab8aa9d5f 100644 --- a/spec/system/supervisors/new_spec.rb +++ b/spec/system/supervisors/new_spec.rb @@ -77,7 +77,19 @@ end end - context "volunteer user" do + context "when logged in as supervisor" do + let(:supervisor) { create(:supervisor) } + + before { sign_in supervisor } + + it "redirects the user with an error message" do + visit new_supervisor_path + + expect(page).to have_selector(".alert", text: "Sorry, you are not authorized to perform this action.") + end + end + + context "when logged in as a volunteer" do let(:volunteer) { create(:volunteer) } before { sign_in volunteer } From 11a1fd3c72f8244206637f7a75965ff723306cf9 Mon Sep 17 00:00:00 2001 From: Audrea Cook Date: Thu, 30 Oct 2025 12:21:07 -0600 Subject: [PATCH 07/10] Test: add guard expectations to prevent flakiness Signed-off-by: Audrea Cook --- spec/system/supervisors/new_spec.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/system/supervisors/new_spec.rb b/spec/system/supervisors/new_spec.rb index bab8aa9d5f..eacf31f6ac 100644 --- a/spec/system/supervisors/new_spec.rb +++ b/spec/system/supervisors/new_spec.rb @@ -37,7 +37,8 @@ expect(page).to have_text("New supervisor created successfully.") end - it "redirects to the edit supervisor page" do + it "redirects to the edit supervisor page", :aggregate_failures do + expect(page).to have_text("New supervisor created successfully.") # Guard to ensure redirection happened expect(page).to have_current_path(edit_supervisor_path(new_supervisor)) end @@ -71,7 +72,8 @@ expect(page).to have_text "errors prohibited this Supervisor from being saved:" end - it "stays on the new supervisor page" do + it "stays on the new supervisor page", :aggregate_failures do + expect(page).to have_text "errors prohibited this Supervisor from being saved:" # Guard to ensure no redirection happened expect(page).to have_current_path(supervisors_path) end end From aad5498b355e88aa57e77d73910e3ba68fcfb148 Mon Sep 17 00:00:00 2001 From: Audrea Cook Date: Thu, 30 Oct 2025 12:21:28 -0600 Subject: [PATCH 08/10] Test: reword contexts for clarity Signed-off-by: Audrea Cook --- spec/system/supervisors/new_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/system/supervisors/new_spec.rb b/spec/system/supervisors/new_spec.rb index eacf31f6ac..4f24ab7e8f 100644 --- a/spec/system/supervisors/new_spec.rb +++ b/spec/system/supervisors/new_spec.rb @@ -79,7 +79,7 @@ end end - context "when logged in as supervisor" do + context "when logged in as a supervisor" do let(:supervisor) { create(:supervisor) } before { sign_in supervisor } From d061163ade5866c40729f0deec20be205080a28f Mon Sep 17 00:00:00 2001 From: Audrea Cook Date: Thu, 30 Oct 2025 12:34:25 -0600 Subject: [PATCH 09/10] Test: update phone number expectation to use end_with matcher to prevent normalization flakes Signed-off-by: Audrea Cook --- spec/system/supervisors/new_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/system/supervisors/new_spec.rb b/spec/system/supervisors/new_spec.rb index 4f24ab7e8f..67825bf59a 100644 --- a/spec/system/supervisors/new_spec.rb +++ b/spec/system/supervisors/new_spec.rb @@ -45,7 +45,7 @@ it "persists the new supervisor with correct attributes", :aggregate_failures do expect(new_supervisor).to be_present expect(new_supervisor.display_name).to eq(new_supervisor_name) - expect(new_supervisor.phone_number).to eq(new_supervisor_phone_number) + expect(new_supervisor.phone_number).to end_with(new_supervisor_phone_number) expect(new_supervisor.supervisor?).to be(true) expect(new_supervisor.active?).to be(true) end From c638cb9d0d8ed18068e0e7e1aaa3e03b4c0b67d4 Mon Sep 17 00:00:00 2001 From: Audrea Cook Date: Thu, 30 Oct 2025 13:39:05 -0600 Subject: [PATCH 10/10] Test: update new supervisor phone number to a static value for consistency Signed-off-by: Audrea Cook --- spec/system/supervisors/new_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/system/supervisors/new_spec.rb b/spec/system/supervisors/new_spec.rb index 67825bf59a..c75b65cb30 100644 --- a/spec/system/supervisors/new_spec.rb +++ b/spec/system/supervisors/new_spec.rb @@ -7,7 +7,7 @@ let(:admin) { create(:casa_admin) } let(:new_supervisor_name) { Faker::Name.name } let(:new_supervisor_email) { Faker::Internet.email } - let(:new_supervisor_phone_number) { Faker::PhoneNumber.phone_number } + let(:new_supervisor_phone_number) { "1234567890" } before do # Stub the request to the URL shortener service (needed if phone is provided)