Skip to content

Commit 29a5b9a

Browse files
authored
Merge pull request #6582 from AudTheCodeWitch/acook/6318/update-supervisors-new-spec
6318 - New Supervisor System Spec Overhaul
2 parents 7eb2dab + c638cb9 commit 29a5b9a

File tree

1 file changed

+74
-17
lines changed

1 file changed

+74
-17
lines changed

spec/system/supervisors/new_spec.rb

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,95 @@
33
require "rails_helper"
44

55
RSpec.describe "supervisors/new", type: :system do
6-
context "when admin" do
6+
context "when logged in as an admin" do
77
let(:admin) { create(:casa_admin) }
8+
let(:new_supervisor_name) { Faker::Name.name }
9+
let(:new_supervisor_email) { Faker::Internet.email }
10+
let(:new_supervisor_phone_number) { "1234567890" }
11+
12+
before do
13+
# Stub the request to the URL shortener service (needed if phone is provided)
14+
stub_request(:post, "https://api.short.io/links")
15+
.to_return(
16+
status: 200,
17+
body: {shortURL: "https://short.url/example"}.to_json,
18+
headers: {"Content-Type" => "application/json"}
19+
)
820

9-
it "allows admin to create a new supervisors" do
1021
sign_in admin
1122
visit new_supervisor_path
23+
end
24+
25+
context "with valid form submission" do
26+
let(:new_supervisor) { User.find_by(email: new_supervisor_email) }
1227

13-
fill_in "Email", with: "[email protected]"
14-
fill_in "Display name", with: "New Supervisor Display Name"
28+
before do
29+
fill_in "Email", with: new_supervisor_email
30+
fill_in "Display name", with: new_supervisor_name
31+
fill_in "Phone number", with: new_supervisor_phone_number
1532

16-
expect {
1733
click_on "Create Supervisor"
18-
}.to change(User, :count).by(1)
34+
end
35+
36+
it "shows a success message" do
37+
expect(page).to have_text("New supervisor created successfully.")
38+
end
39+
40+
it "redirects to the edit supervisor page", :aggregate_failures do
41+
expect(page).to have_text("New supervisor created successfully.") # Guard to ensure redirection happened
42+
expect(page).to have_current_path(edit_supervisor_path(new_supervisor))
43+
end
44+
45+
it "persists the new supervisor with correct attributes", :aggregate_failures do
46+
expect(new_supervisor).to be_present
47+
expect(new_supervisor.display_name).to eq(new_supervisor_name)
48+
expect(new_supervisor.phone_number).to end_with(new_supervisor_phone_number)
49+
expect(new_supervisor.supervisor?).to be(true)
50+
expect(new_supervisor.active?).to be(true)
51+
end
52+
53+
it "sends an invitation email to the new supervisor", :aggregate_failures do
54+
last_email = ActionMailer::Base.deliveries.last
55+
expect(last_email.to).to eq [new_supervisor_email]
56+
expect(last_email.subject).to have_text "CASA Console invitation instructions"
57+
expect(last_email.html_part.body.encoded).to have_text "your new Supervisor account."
58+
end
1959
end
2060

21-
it "sends invitation email to the new supervisor" do
22-
sign_in admin
23-
visit new_supervisor_path
61+
context "with invalid form submission" do
62+
before do
63+
# Don't fill in any fields
64+
click_on "Create Supervisor"
65+
end
2466

25-
fill_in "Email", with: "[email protected]"
26-
fill_in "Display name", with: "New Supervisor Display Name 2"
67+
it "does not create a new user" do
68+
expect(User.count).to eq(1) # Only the admin user exists
69+
end
2770

28-
click_on "Create Supervisor"
71+
it "shows validation error messages" do
72+
expect(page).to have_text "errors prohibited this Supervisor from being saved:"
73+
end
2974

30-
last_email = ActionMailer::Base.deliveries.last
31-
expect(last_email.to).to eq ["[email protected]"]
32-
expect(last_email.subject).to have_text "CASA Console invitation instructions"
33-
expect(last_email.html_part.body.encoded).to have_text "your new Supervisor account."
75+
it "stays on the new supervisor page", :aggregate_failures do
76+
expect(page).to have_text "errors prohibited this Supervisor from being saved:" # Guard to ensure no redirection happened
77+
expect(page).to have_current_path(supervisors_path)
78+
end
79+
end
80+
end
81+
82+
context "when logged in as a supervisor" do
83+
let(:supervisor) { create(:supervisor) }
84+
85+
before { sign_in supervisor }
86+
87+
it "redirects the user with an error message" do
88+
visit new_supervisor_path
89+
90+
expect(page).to have_selector(".alert", text: "Sorry, you are not authorized to perform this action.")
3491
end
3592
end
3693

37-
context "volunteer user" do
94+
context "when logged in as a volunteer" do
3895
let(:volunteer) { create(:volunteer) }
3996

4097
before { sign_in volunteer }

0 commit comments

Comments
 (0)