diff --git a/spec/requests/notes_spec.rb b/spec/requests/notes_spec.rb index b4df4e8434..5133e8466e 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 { + + sign_in user + + expect do post volunteer_notes_path(volunteer), params: {note: {content: "Very nice!"}} - }.to change(Note, :count).by(1) + 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 { + sign_in user + + expect do post volunteer_notes_path(volunteer), params: {note: {content: "Very nice!"}} - }.not_to change(Note, :count) + 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