|
1 | | -require "rails_helper" |
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +RSpec.describe "/custom_org_links", type: :request do |
| 4 | + let(:casa_org) { create(:casa_org) } |
| 5 | + let(:casa_admin) { create :casa_admin, casa_org: casa_org } |
| 6 | + let(:volunteer) { create :volunteer, casa_org: casa_org, active: true } |
| 7 | + |
| 8 | + describe "GET /custom_org_links/new" do |
| 9 | + context "when logged in as admin user" do |
| 10 | + before { sign_in casa_admin } |
| 11 | + |
| 12 | + it "can successfully access a custom org link create page" do |
| 13 | + get new_custom_org_link_path |
| 14 | + expect(response).to be_successful |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + context "when logged in as a non-admin user" do |
| 19 | + before { sign_in volunteer } |
| 20 | + |
| 21 | + it "cannot access a custom org link create page" do |
| 22 | + get new_custom_org_link_path |
| 23 | + expect(response).to redirect_to root_path |
| 24 | + expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action." |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + context "when not logged in" do |
| 29 | + it "cannot access a custom org link create page" do |
| 30 | + get new_custom_org_link_path |
| 31 | + expect(response).to redirect_to new_user_session_path |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + describe "POST /custom_org_links" do |
| 37 | + let(:params) { {custom_org_link: {text: "New Custom Link", url: "http://www.custom.link", active: true}} } |
| 38 | + |
| 39 | + context "when logged in as admin user" do |
| 40 | + let(:expected_custom_link_attributes) { params[:custom_org_link].merge(casa_org_id: casa_org.id).stringify_keys } |
| 41 | + before { sign_in casa_admin } |
| 42 | + |
| 43 | + it "can successfully create a custom org link" do |
| 44 | + expect { post custom_org_links_path, params: params }.to change { CustomOrgLink.count }.by(1) |
| 45 | + expect(CustomOrgLink.last.attributes).to include(**expected_custom_link_attributes) |
| 46 | + expect(response).to redirect_to edit_casa_org_path(casa_org) |
| 47 | + expect(response.request.flash[:notice]).to eq "Custom link was successfully created." |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + context "when logged in as a non-admin user" do |
| 52 | + before { sign_in volunteer } |
| 53 | + |
| 54 | + it "cannot create a custom org link" do |
| 55 | + post custom_org_links_path, params: params |
| 56 | + expect(response).to redirect_to root_path |
| 57 | + expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action." |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context "when not logged in" do |
| 62 | + it "cannot create a custom org link" do |
| 63 | + post custom_org_links_path, params: params |
| 64 | + expect(response).to redirect_to new_user_session_path |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + describe "GET /custom_org_links/:id/edit" do |
| 70 | + context "when logged in as admin user" do |
| 71 | + before { sign_in_as_admin } |
| 72 | + |
| 73 | + it "can successfully access a contact type edit page" do |
| 74 | + get edit_custom_org_link_path(create(:custom_org_link)) |
| 75 | + expect(response).to be_successful |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + context "when logged in as a non-admin user" do |
| 80 | + before { sign_in_as_volunteer } |
| 81 | + |
| 82 | + it "cannot access a contact type edit page" do |
| 83 | + get edit_custom_org_link_path(create(:custom_org_link)) |
| 84 | + expect(response).to redirect_to root_path |
| 85 | + expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action." |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + context "when not logged in" do |
| 90 | + it "cannot access a contact type edit page" do |
| 91 | + get edit_custom_org_link_path(create(:custom_org_link)) |
| 92 | + expect(response).to redirect_to new_user_session_path |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + describe "PUT /custom_org_links/:id" do |
| 98 | + let!(:custom_org_link) { create :custom_org_link, casa_org: casa_org, text: "Existing Link", url: "http://existing.com", active: false } |
| 99 | + let(:params) { {custom_org_link: {text: "New Custom Link", url: "http://www.custom.link", active: true}} } |
| 100 | + |
| 101 | + context "when logged in as admin user" do |
| 102 | + let(:expected_custom_link_attributes) { params[:custom_org_link].merge(casa_org_id: casa_org.id).stringify_keys } |
| 103 | + before { sign_in casa_admin } |
| 104 | + |
| 105 | + it "can successfully update a custom org link" do |
| 106 | + expect { put custom_org_link_path(custom_org_link), params: params }.to_not change { CustomOrgLink.count } |
| 107 | + expect(custom_org_link.reload.attributes).to include(**expected_custom_link_attributes) |
| 108 | + expect(response).to redirect_to edit_casa_org_path(casa_org) |
| 109 | + expect(response.request.flash[:notice]).to eq "Custom link was successfully updated." |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + context "when logged in as a non-admin user" do |
| 114 | + before { sign_in volunteer } |
| 115 | + |
| 116 | + it "cannot update a custom org link" do |
| 117 | + put custom_org_link_path(custom_org_link), params: params |
| 118 | + expect(response).to redirect_to root_path |
| 119 | + expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action." |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + context "when not logged in" do |
| 124 | + it "cannot update a custom org link" do |
| 125 | + put custom_org_link_path(custom_org_link), params: params |
| 126 | + expect(response).to redirect_to new_user_session_path |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + describe "DELETE /custom_org_links/:id" do |
| 132 | + let!(:custom_org_link) { create :custom_org_link, casa_org: casa_org, text: "Existing Link", url: "http://existing.com", active: false } |
| 133 | + |
| 134 | + context "when logged in as admin user" do |
| 135 | + before { sign_in casa_admin } |
| 136 | + |
| 137 | + it "can successfully delete a custom org link" do |
| 138 | + expect { delete custom_org_link_path(custom_org_link) }.to change { CustomOrgLink.count }.by(-1) |
| 139 | + expect(response).to redirect_to edit_casa_org_path(casa_org) |
| 140 | + expect(response.request.flash[:notice]).to eq "Custom link was successfully deleted." |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + context "when logged in as a non-admin user" do |
| 145 | + before { sign_in volunteer } |
| 146 | + |
| 147 | + it "cannot delete a custom org link" do |
| 148 | + delete custom_org_link_path(custom_org_link) |
| 149 | + expect(response).to redirect_to root_path |
| 150 | + expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action." |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + context "when not logged in" do |
| 155 | + it "cannot delete a custom org link" do |
| 156 | + delete custom_org_link_path(custom_org_link) |
| 157 | + expect(response).to redirect_to new_user_session_path |
| 158 | + end |
| 159 | + end |
| 160 | + end |
| 161 | +end |
0 commit comments