|
3 | 3 | require "rails_helper" |
4 | 4 |
|
5 | 5 | RSpec.describe "supervisors/new", type: :system do |
6 | | - context "when admin" do |
| 6 | + context "when logged in as an admin" do |
7 | 7 | 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 | + ) |
8 | 20 |
|
9 | | - it "allows admin to create a new supervisors" do |
10 | 21 | sign_in admin |
11 | 22 | 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) } |
12 | 27 |
|
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 |
15 | 32 |
|
16 | | - expect { |
17 | 33 | 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 |
19 | 59 | end |
20 | 60 |
|
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 |
24 | 66 |
|
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 |
27 | 70 |
|
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 |
29 | 74 |
|
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.") |
34 | 91 | end |
35 | 92 | end |
36 | 93 |
|
37 | | - context "volunteer user" do |
| 94 | + context "when logged in as a volunteer" do |
38 | 95 | let(:volunteer) { create(:volunteer) } |
39 | 96 |
|
40 | 97 | before { sign_in volunteer } |
|
0 commit comments