From 62837cc1dc89b9d139a040cba89a62071e9c2eee Mon Sep 17 00:00:00 2001 From: Yago Santos Date: Sun, 3 May 2026 17:51:14 -0400 Subject: [PATCH 1/2] refactor(tests): create shared examples to describe same behaviour across actions --- spec/requests/notes_spec.rb | 352 ++++++++++++++---------------------- 1 file changed, 134 insertions(+), 218 deletions(-) diff --git a/spec/requests/notes_spec.rb b/spec/requests/notes_spec.rb index b4df4e8434..fc6e58eaee 100644 --- a/spec/requests/notes_spec.rb +++ b/spec/requests/notes_spec.rb @@ -1,163 +1,179 @@ require "rails_helper" RSpec.describe "/volunteers/notes", type: :request do - describe "POST /create" do - context "when logged in as admin" do - it "can create a note for volunteer in same organization" do - organization = create(:casa_org) - admin = create(:casa_admin, casa_org: organization) + RSpec.shared_examples "create" do + let(:organization) { create(:casa_org) } + + context "when in the same organization" do + it "can create a note for volunteer" do volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - sign_in admin - expect { - post volunteer_notes_path(volunteer), params: {note: {content: "Very nice!"}} - }.to change(Note, :count).by(1) + + sign_in user + + expect do + post volunteer_notes_path(volunteer), params: { note: { content: "Very nice!" } } + end.to change(Note, :count).by(1) expect(response).to redirect_to edit_volunteer_path(volunteer) expect(Note.last.content).to eq "Very nice!" end + end - it "cannot create a note for volunteer in different organization" do - organization = create(:casa_org) + context "when in a different organization" do + it "cannot create a note for volunteer" do other_organization = create(:casa_org) - admin = create(:casa_admin, casa_org: organization) volunteer = create(:volunteer, casa_org: other_organization) - sign_in admin - expect { - post volunteer_notes_path(volunteer), params: {note: {content: "Very nice!"}} - }.not_to change(Note, :count) + sign_in user + + expect do + post volunteer_notes_path(volunteer), params: { note: { content: "Very nice!" } } + end.not_to change(Note, :count) expect(response).to redirect_to root_path end end + end - context "when logged in as a supervisor" do - it "can create a note for volunteer in same organization" do - organization = create(:casa_org) - supervisor = create(:supervisor, casa_org: organization) - volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - sign_in supervisor - expect { - post volunteer_notes_path(volunteer), params: {note: {content: "Very nice!"}} - }.to change(Note, :count).by(1) - expect(response).to redirect_to edit_volunteer_path(volunteer) - expect(Note.last.content).to eq "Very nice!" + RSpec.shared_examples "edit" do + let(:organization) { create(:casa_org) } + + context "when in the same organization" do + let(:volunteer) { create(:volunteer, :with_assigned_supervisor, casa_org: organization) } + + it "is successful if note belongs for volunteer" do + note = create(:note, notable: volunteer) + + sign_in user + get edit_volunteer_note_path(volunteer, note) + + expect(response).to be_successful end - it "cannot create a note for volunteer in different organization" do - organization = create(:casa_org) - other_organization = create(:casa_org) - supervisor = create(:supervisor, casa_org: organization) - volunteer = create(:volunteer, casa_org: other_organization) + it "redirects to root path if note does not belong to volunteer" do + other_volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) + note = create(:note, notable: other_volunteer) + + sign_in user + get edit_volunteer_note_path(volunteer, note) - sign_in supervisor - expect { - post volunteer_notes_path(volunteer), params: {note: {content: "Very nice!"}} - }.not_to change(Note, :count) expect(response).to redirect_to root_path end end - context "when logged in as volunteer" do - it "cannot create a note" do - organization = create(:casa_org) - volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) + context "when in a different organization" do + it "redirects to root path" do + other_organization = create(:casa_org) + volunteer = create(:volunteer, casa_org: other_organization) + note = create(:note, notable: volunteer) + + sign_in user + get edit_volunteer_note_path(volunteer, note) - sign_in volunteer - expect { - post volunteer_notes_path(volunteer), params: {note: {content: "Very nice!"}} - }.not_to change(Note, :count) expect(response).to redirect_to root_path end end end - describe "GET /edit" do - context "when logged in as admin" do - context "when volunteer in same organization" do - it "is successful if note belongs to volunteer" do - organization = create(:casa_org) - admin = create(:casa_admin, casa_org: organization) - volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - note = create(:note, notable: volunteer) + RSpec.shared_examples "update" do + let(:organization) { create(:casa_org) } - sign_in admin - get edit_volunteer_note_path(volunteer, note) + context "when in the same organization" do + it "updates note and redirects to edit volunteer page" do + volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) + note = create(:note, notable: volunteer, creator: user, content: "Good job.") - expect(response).to be_successful - end + sign_in user + patch volunteer_note_path(volunteer, note), params: { note: { content: "Very nice!" } } - it "redirects to root path if note does not belong to volunteer" do - organization = create(:casa_org) - admin = create(:casa_admin, casa_org: organization) - volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - other_volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - note = create(:note, notable: other_volunteer) + expect(response).to redirect_to(edit_volunteer_path(volunteer)) + expect(note.reload.content).to eq "Very nice!" + end + end - sign_in admin - get edit_volunteer_note_path(volunteer, note) + context "when in a different organization" do + it "does not update note and redirects to root path" do + other_organization = create(:casa_org) + volunteer = create(:volunteer, casa_org: other_organization) + note = create(:note, notable: volunteer, content: "Good job.") - expect(response).to redirect_to root_path - end + sign_in user + patch volunteer_note_path(volunteer, note), params: { note: { content: "Very nice!" } } + + expect(response).to redirect_to root_path + expect(note.reload.content).to eq "Good job." end + end + end - context "when volunteer in different organization" do - it "redirects to root path" do - organization = create(:casa_org) - other_organization = create(:casa_org) - admin = create(:casa_admin, casa_org: organization) + RSpec.shared_examples "delete" do + let(:organization) { create(:casa_org) } - volunteer = create(:volunteer, casa_org: other_organization) - note = create(:note, notable: volunteer) + context "when in the same organization" do + it "can delete notes about a volunteer" do + volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) + note = create(:note, notable: volunteer) - sign_in admin - get edit_volunteer_note_path(volunteer, note) + sign_in user - expect(response).to redirect_to root_path - end + expect do + delete volunteer_note_path(volunteer, note) + end.to change(Note, :count).by(-1) + expect(response).to redirect_to edit_volunteer_path(volunteer) end end - context "when logged in as supervisor" do - context "when volunteer in same organization" do - it "is successful if note belongs to volunteer" do - organization = create(:casa_org) - supervisor = create(:supervisor, casa_org: organization) - volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - note = create(:note, notable: volunteer) - - sign_in supervisor - get edit_volunteer_note_path(volunteer, note) + context "when in a different organization" do + it "cannot delete notes about a volunteer" do + other_organization = create(:casa_org) + volunteer = create(:volunteer, casa_org: other_organization) + note = create(:note, notable: volunteer) - expect(response).to be_successful - end + sign_in user - it "redirects to root path if note does not belong to volunteer" do - organization = create(:casa_org) - supervisor = create(:supervisor, casa_org: organization) - volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - other_volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - note = create(:note, notable: other_volunteer) + expect do + delete volunteer_note_path(volunteer, note) + end.not_to change(Note, :count) + expect(response).to redirect_to root_path + end + end + end - sign_in supervisor - get edit_volunteer_note_path(volunteer, note) + describe "POST /create" do + context "when logged in as admin" do + it_behaves_like "create" do + let(:user) { create(:casa_admin, casa_org: organization) } + end + end - expect(response).to redirect_to root_path - end + context "when logged in as a supervisor" do + it_behaves_like "create" do + let(:user) { create(:supervisor, casa_org: organization) } end + end - context "when volunteer in different organization" do - it "redirects to root path" do - organization = create(:casa_org) - other_organization = create(:casa_org) - supervisor = create(:supervisor, casa_org: organization) + context "when logged in as volunteer" do + it "cannot create a note" do + organization = create(:casa_org) + volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - volunteer = create(:volunteer, casa_org: other_organization) - note = create(:note, notable: volunteer) + sign_in volunteer + expect { + post volunteer_notes_path(volunteer), params: { note: { content: "Very nice!" } } + }.not_to change(Note, :count) + expect(response).to redirect_to root_path + end + end + end - sign_in supervisor - get edit_volunteer_note_path(volunteer, note) + describe "GET /edit" do + context "when logged in as admin" do + it_behaves_like "edit" do + let(:user) { create(:casa_admin, casa_org: organization) } + end + end - expect(response).to redirect_to root_path - end + context "when logged in as supervisor" do + it_behaves_like "edit" do + let(:user) { create(:supervisor, casa_org: organization) } end end @@ -179,68 +195,14 @@ describe "PATCH /update" do context "when logged in as an admin" do - context "when volunteer in same org" do - it "updates note and redirects to edit volunteer page" do - organization = create(:casa_org) - admin = create(:casa_admin, casa_org: organization) - volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - note = create(:note, notable: volunteer, creator: admin, content: "Good job.") - - sign_in admin - patch volunteer_note_path(volunteer, note), params: {note: {content: "Very nice!"}} - - expect(response).to redirect_to(edit_volunteer_path(volunteer)) - expect(note.reload.content).to eq "Very nice!" - end - end - - context "when volunteer in different org" do - it "does not update note and redirects to root path" do - organization = create(:casa_org) - other_organization = create(:casa_org) - admin = create(:casa_admin, casa_org: organization) - volunteer = create(:volunteer, casa_org: other_organization) - note = create(:note, notable: volunteer, content: "Good job.") - - sign_in admin - patch volunteer_note_path(volunteer, note), params: {note: {content: "Very nice!"}} - - expect(response).to redirect_to root_path - expect(note.reload.content).to eq "Good job." - end + it_behaves_like "update" do + let(:user) { create(:casa_admin, casa_org: organization) } end end context "when logged in as a supervisor" do - context "when volunteer in same org" do - it "updates note and redirects to edit volunteer page" do - organization = create(:casa_org) - supervisor = create(:supervisor, casa_org: organization) - volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - note = create(:note, notable: volunteer, content: "Good job.") - - sign_in supervisor - patch volunteer_note_path(volunteer, note), params: {note: {content: "Very nice!"}} - - expect(response).to redirect_to(edit_volunteer_path(volunteer)) - expect(note.reload.content).to eq "Very nice!" - end - end - - context "when volunteer in different org" do - it "does not update note and redirects to root path" do - organization = create(:casa_org) - other_organization = create(:casa_org) - supervisor = create(:supervisor, casa_org: organization) - volunteer = create(:volunteer, casa_org: other_organization) - note = create(:note, notable: volunteer, content: "Good job.") - - sign_in supervisor - patch volunteer_note_path(volunteer, note), params: {note: {content: "Very nice!"}} - - expect(response).to redirect_to root_path - expect(note.reload.content).to eq "Good job." - end + it_behaves_like "update" do + let(:user) { create(:supervisor, casa_org: organization) } end end @@ -263,60 +225,14 @@ describe "DELETE /destroy" do context "when logged in as an admin" do - it "can delete notes about a volunteer in same organization" do - organization = create(:casa_org) - admin = create(:casa_admin, casa_org: organization) - volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - note = create(:note, notable: volunteer) - - sign_in admin - expect { - delete volunteer_note_path(volunteer, note) - }.to change(Note, :count).by(-1) - expect(response).to redirect_to edit_volunteer_path(volunteer) - end - - it "cannot delete notes about a volunteer in different organization" do - organization = create(:casa_org) - other_organization = create(:casa_org) - admin = create(:casa_admin, casa_org: organization) - volunteer = create(:volunteer, casa_org: other_organization) - note = create(:note, notable: volunteer) - - sign_in admin - expect { - delete volunteer_note_path(volunteer, note) - }.not_to change(Note, :count) - expect(response).to redirect_to root_path + it_behaves_like "delete" do + let(:user) { create(:casa_admin, casa_org: organization) } end end context "when logged in as a supervisor" do - it "can delete notes about a volunteer in same organization" do - organization = create(:casa_org) - supervisor = create(:supervisor, casa_org: organization) - volunteer = create(:volunteer, :with_assigned_supervisor, casa_org: organization) - note = create(:note, notable: volunteer) - - sign_in supervisor - expect { - delete volunteer_note_path(volunteer, note) - }.to change(Note, :count).by(-1) - expect(response).to redirect_to edit_volunteer_path(volunteer) - end - - it "cannot delete notes about a volunteer in different organization" do - organization = create(:casa_org) - other_organization = create(:casa_org) - supervisor = create(:supervisor, casa_org: organization) - volunteer = create(:volunteer, casa_org: other_organization) - note = create(:note, notable: volunteer) - - sign_in supervisor - expect { - delete volunteer_note_path(volunteer, note) - }.not_to change(Note, :count) - expect(response).to redirect_to root_path + it_behaves_like "delete" do + let(:user) { create(:supervisor, casa_org: organization) } end end From 3949885bc3daad03af563802fccc63b5ecb108c9 Mon Sep 17 00:00:00 2001 From: Yago Santos Date: Sun, 3 May 2026 17:56:22 -0400 Subject: [PATCH 2/2] fix: address ruby linting issues --- spec/requests/notes_spec.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/spec/requests/notes_spec.rb b/spec/requests/notes_spec.rb index fc6e58eaee..5133e8466e 100644 --- a/spec/requests/notes_spec.rb +++ b/spec/requests/notes_spec.rb @@ -2,7 +2,7 @@ RSpec.describe "/volunteers/notes", type: :request do RSpec.shared_examples "create" do - let(:organization) { create(:casa_org) } + let(:organization) { create(:casa_org) } context "when in the same organization" do it "can create a note for volunteer" do @@ -11,7 +11,7 @@ sign_in user expect do - post volunteer_notes_path(volunteer), params: { note: { content: "Very nice!" } } + post volunteer_notes_path(volunteer), params: {note: {content: "Very nice!"}} end.to change(Note, :count).by(1) expect(response).to redirect_to edit_volunteer_path(volunteer) expect(Note.last.content).to eq "Very nice!" @@ -26,7 +26,7 @@ sign_in user expect do - post volunteer_notes_path(volunteer), params: { note: { content: "Very nice!" } } + post volunteer_notes_path(volunteer), params: {note: {content: "Very nice!"}} end.not_to change(Note, :count) expect(response).to redirect_to root_path end @@ -34,7 +34,7 @@ end RSpec.shared_examples "edit" do - let(:organization) { create(:casa_org) } + let(:organization) { create(:casa_org) } context "when in the same organization" do let(:volunteer) { create(:volunteer, :with_assigned_supervisor, casa_org: organization) } @@ -74,7 +74,7 @@ end RSpec.shared_examples "update" do - let(:organization) { create(:casa_org) } + let(:organization) { create(:casa_org) } context "when in the same organization" do it "updates note and redirects to edit volunteer page" do @@ -82,7 +82,7 @@ note = create(:note, notable: volunteer, creator: user, content: "Good job.") sign_in user - patch volunteer_note_path(volunteer, note), params: { note: { content: "Very nice!" } } + patch volunteer_note_path(volunteer, note), params: {note: {content: "Very nice!"}} expect(response).to redirect_to(edit_volunteer_path(volunteer)) expect(note.reload.content).to eq "Very nice!" @@ -96,7 +96,7 @@ note = create(:note, notable: volunteer, content: "Good job.") sign_in user - patch volunteer_note_path(volunteer, note), params: { note: { content: "Very nice!" } } + patch volunteer_note_path(volunteer, note), params: {note: {content: "Very nice!"}} expect(response).to redirect_to root_path expect(note.reload.content).to eq "Good job." @@ -105,7 +105,7 @@ end RSpec.shared_examples "delete" do - let(:organization) { create(:casa_org) } + let(:organization) { create(:casa_org) } context "when in the same organization" do it "can delete notes about a volunteer" do @@ -157,7 +157,7 @@ sign_in volunteer expect { - post volunteer_notes_path(volunteer), params: { note: { content: "Very nice!" } } + post volunteer_notes_path(volunteer), params: {note: {content: "Very nice!"}} }.not_to change(Note, :count) expect(response).to redirect_to root_path end