|
1 | 1 | require "rails_helper" |
2 | 2 |
|
3 | 3 | RSpec.describe EmancipationsController, type: :controller do |
4 | | - let(:organization) { build(:casa_org) } |
5 | | - let(:volunteer) { create(:volunteer, :with_casa_cases, casa_org: organization) } |
6 | | - let(:test_case_category) { build(:casa_case_emancipation_category) } |
7 | | - let(:casa_case) { create(:casa_case, casa_org: organization) } |
8 | | - let(:casa_case_id) { casa_case.id.to_s } |
9 | | - let(:params) do |
10 | | - { |
11 | | - casa_case_id: casa_case_id |
12 | | - } |
13 | | - end |
| 4 | + let(:organization) { create(:casa_org) } |
| 5 | + let(:other_org) { create(:casa_org) } |
| 6 | + let(:user) { create(:supervisor, casa_org: organization) } |
| 7 | + let(:casa_case) { create(:casa_case, casa_org: organization, birth_month_year_youth: 20.years.ago) } |
| 8 | + let(:non_transition_case) { create(:casa_case, :pre_transition, casa_org: organization) } |
| 9 | + let(:emancipation_category) { create(:emancipation_category) } |
| 10 | + let(:emancipation_option) { create(:emancipation_option, emancipation_category: emancipation_category) } |
| 11 | + |
| 12 | + before { sign_in user } |
| 13 | + |
| 14 | + describe "GET #show" do |
| 15 | + context "when authenticated and authorized" do |
| 16 | + it "returns http success" do |
| 17 | + get :show, params: { casa_case_id: casa_case.friendly_id } |
| 18 | + expect(response).to have_http_status(:success) |
| 19 | + end |
| 20 | + |
| 21 | + it "assigns @current_case" do |
| 22 | + get :show, params: { casa_case_id: casa_case.friendly_id } |
| 23 | + expect(assigns(:current_case)).to eq(casa_case) |
| 24 | + end |
| 25 | + |
| 26 | + it "assigns @emancipation_form_data with all categories" do |
| 27 | + get :show, params: { casa_case_id: casa_case.friendly_id } |
| 28 | + expect(assigns(:emancipation_form_data)).to match_array(EmancipationCategory.all) |
| 29 | + end |
| 30 | + end |
14 | 31 |
|
15 | | - before do |
16 | | - allow(controller).to receive(:authenticate_user!).and_return(true) |
17 | | - allow(controller).to receive(:current_user).and_return(volunteer) |
| 32 | + context "when case does not exist" do |
| 33 | + it "raises a record not found error" do |
| 34 | + expect { |
| 35 | + get :show, params: { casa_case_id: "nonexistent-case" } |
| 36 | + }.to raise_error(ActiveRecord::RecordNotFound) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + context "when user belongs to a different org" do |
| 41 | + let(:user) { create(:supervisor, casa_org: other_org) } |
| 42 | + |
| 43 | + it "redirects to root with an authorization notice" do |
| 44 | + get :show, params: { casa_case_id: casa_case.friendly_id } |
| 45 | + expect(response).to redirect_to(root_url) |
| 46 | + expect(flash[:notice]).to match(/not authorized/) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context "docx format" do |
| 51 | + it "sends a docx file with the correct filename" do |
| 52 | + get :show, params: { casa_case_id: casa_case.friendly_id }, format: :docx |
| 53 | + expect(response.headers["Content-Disposition"]).to include( |
| 54 | + "#{casa_case.case_number} Emancipation Checklist.docx" |
| 55 | + ) |
| 56 | + end |
| 57 | + end |
18 | 58 | end |
19 | 59 |
|
20 | | - describe "show" do |
21 | | - context "json request" do |
22 | | - subject(:show) { get :show, params: {casa_case_id: casa_case_id, format: :json} } |
| 60 | + describe "POST #save" do |
| 61 | + def post_save(action, check_item_id, case_id: casa_case.friendly_id) |
| 62 | + post :save, params: { |
| 63 | + casa_case_id: case_id, |
| 64 | + check_item_action: action, |
| 65 | + check_item_id: check_item_id |
| 66 | + }, format: :json |
| 67 | + end |
| 68 | + |
| 69 | + # Authorization |
| 70 | + context "when user belongs to a different org" do |
| 71 | + let(:user) { create(:supervisor, casa_org: other_org) } |
| 72 | + |
| 73 | + it "returns unauthorized with a json error message" do |
| 74 | + post_save("add_category", emancipation_category.id) |
| 75 | + expect(response).to have_http_status(:unauthorized) |
| 76 | + expect(json_response["error"]).to match(/not authorized/) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + # Case not found |
| 81 | + context "when casa_case_id does not match any case" do |
| 82 | + it "returns 404 with a descriptive error" do |
| 83 | + post_save("add_category", emancipation_category.id, case_id: "nonexistent-id") |
| 84 | + expect(response).to have_http_status(:not_found) |
| 85 | + expect(json_response["error"]).to match(/Could not find case/) |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + # Non-transitioning case |
| 90 | + context "when the case is not in transition age" do |
| 91 | + it "returns bad_request" do |
| 92 | + post_save("add_category", emancipation_category.id, case_id: non_transition_case.friendly_id) |
| 93 | + expect(response).to have_http_status(:bad_request) |
| 94 | + expect(json_response["error"]).to match(/not marked as transitioning/) |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + # Unsupported action |
| 99 | + context "when check_item_action is not supported" do |
| 100 | + it "returns bad_request with unsupported action message" do |
| 101 | + post_save("unsupported_action", emancipation_category.id) |
| 102 | + expect(response).to have_http_status(:bad_request) |
| 103 | + expect(json_response["error"]).to match(/not a supported action/) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + # ADD_CATEGORY |
| 108 | + context "with action: add_category" do |
| 109 | + it "adds the category to the case and returns success" do |
| 110 | + expect { |
| 111 | + post_save("add_category", emancipation_category.id) |
| 112 | + }.to change { casa_case.emancipation_categories.count }.by(1) |
| 113 | + |
| 114 | + expect(response).to have_http_status(:ok) |
| 115 | + expect(json_response).to eq("success") |
| 116 | + end |
| 117 | + |
| 118 | + it "returns bad_request when category is already associated" do |
| 119 | + casa_case.add_emancipation_category(emancipation_category.id) |
| 120 | + post_save("add_category", emancipation_category.id) |
| 121 | + expect(response).to have_http_status(:bad_request) |
| 122 | + expect(json_response["error"]).to match(/already exists/) |
| 123 | + end |
| 124 | + |
| 125 | + it "returns bad_request when category id does not exist" do |
| 126 | + post_save("add_category", -1) |
| 127 | + expect(response).to have_http_status(:bad_request) |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + # ADD_OPTION |
| 132 | + context "with action: add_option" do |
| 133 | + it "adds the option to the case and returns success" do |
| 134 | + expect { |
| 135 | + post_save("add_option", emancipation_option.id) |
| 136 | + }.to change { casa_case.emancipation_options.count }.by(1) |
| 137 | + |
| 138 | + expect(response).to have_http_status(:ok) |
| 139 | + expect(json_response).to eq("success") |
| 140 | + end |
| 141 | + |
| 142 | + it "returns bad_request when option is already associated" do |
| 143 | + casa_case.add_emancipation_option(emancipation_option.id) |
| 144 | + post_save("add_option", emancipation_option.id) |
| 145 | + expect(response).to have_http_status(:bad_request) |
| 146 | + expect(json_response["error"]).to match(/already exists/) |
| 147 | + end |
23 | 148 |
|
24 | | - context "not_authorized" do |
25 | | - before do |
26 | | - allow_any_instance_of(Volunteer).to receive(:casa_org).and_return nil |
27 | | - end |
| 149 | + it "returns bad_request when option id does not exist" do |
| 150 | + post_save("add_option", -1) |
| 151 | + expect(response).to have_http_status(:bad_request) |
| 152 | + end |
| 153 | + end |
28 | 154 |
|
29 | | - it "responds unauthorized" do |
30 | | - show |
31 | | - expect(response).to have_http_status(:unauthorized) |
32 | | - end |
| 155 | + # DELETE_CATEGORY |
| 156 | + context "with action: delete_category" do |
| 157 | + before do |
| 158 | + casa_case.add_emancipation_category(emancipation_category.id) |
| 159 | + casa_case.add_emancipation_option(emancipation_option.id) |
| 160 | + end |
33 | 161 |
|
34 | | - context "the backtrace ends in 'save'" do |
35 | | - before do |
36 | | - allow_any_instance_of(Organizational::UnknownOrganization).to receive(:backtrace).and_return(["", "", "save'"]) |
37 | | - end |
| 162 | + it "removes the category and its associated options from the case" do |
| 163 | + post_save("delete_category", emancipation_category.id) |
| 164 | + expect(response).to have_http_status(:ok) |
| 165 | + expect(json_response).to eq("success") |
| 166 | + expect(casa_case.reload.emancipation_categories).not_to include(emancipation_category) |
| 167 | + expect(casa_case.reload.emancipation_options).not_to include(emancipation_option) |
| 168 | + end |
38 | 169 |
|
39 | | - it "renders the correct json message" do |
40 | | - show |
41 | | - expect(response).to have_http_status(:unauthorized) |
42 | | - expect(response.body).to eq({error: "Sorry, you are not authorized to perform this action. Did the session expire?"}.to_json) |
43 | | - end |
44 | | - end |
| 170 | + it "returns bad_request when category is not associated with the case" do |
| 171 | + other_category = create(:emancipation_category) |
| 172 | + post_save("delete_category", other_category.id) |
| 173 | + expect(response).to have_http_status(:bad_request) |
| 174 | + expect(json_response["error"]).to match(/does not exist/) |
45 | 175 | end |
46 | 176 | end |
| 177 | + |
| 178 | + # DELETE_OPTION |
| 179 | + context "with action: delete_option" do |
| 180 | + before { casa_case.add_emancipation_option(emancipation_option.id) } |
| 181 | + |
| 182 | + it "removes the option from the case and returns success" do |
| 183 | + expect { |
| 184 | + post_save("delete_option", emancipation_option.id) |
| 185 | + }.to change { casa_case.emancipation_options.count }.by(-1) |
| 186 | + |
| 187 | + expect(response).to have_http_status(:ok) |
| 188 | + expect(json_response).to eq("success") |
| 189 | + end |
| 190 | + |
| 191 | + it "returns bad_request when option is not associated with the case" do |
| 192 | + other_option = create(:emancipation_option, emancipation_category: emancipation_category) |
| 193 | + post_save("delete_option", other_option.id) |
| 194 | + expect(response).to have_http_status(:bad_request) |
| 195 | + expect(json_response["error"]).to match(/does not exist/) |
| 196 | + end |
| 197 | + end |
| 198 | + |
| 199 | + # SET_OPTION |
| 200 | + context "with action: set_option" do |
| 201 | + let(:other_option) { create(:emancipation_option, emancipation_category: emancipation_category) } |
| 202 | + |
| 203 | + before { casa_case.add_emancipation_option(other_option.id) } |
| 204 | + |
| 205 | + it "replaces the existing option in the same category with the new one" do |
| 206 | + post_save("set_option", emancipation_option.id) |
| 207 | + expect(response).to have_http_status(:ok) |
| 208 | + expect(json_response).to eq("success") |
| 209 | + expect(casa_case.reload.emancipation_options).to include(emancipation_option) |
| 210 | + expect(casa_case.reload.emancipation_options).not_to include(other_option) |
| 211 | + end |
| 212 | + |
| 213 | + it "returns bad_request when option id does not exist" do |
| 214 | + post_save("set_option", -1) |
| 215 | + expect(response).to have_http_status(:bad_request) |
| 216 | + end |
| 217 | + end |
| 218 | + end |
| 219 | + |
| 220 | + # JSON error handler for unauthorized access from save |
| 221 | + describe "#not_authorized" do |
| 222 | + let(:user) { create(:supervisor, casa_org: other_org) } |
| 223 | + |
| 224 | + it "renders a json unauthorized error when called from save" do |
| 225 | + post :save, params: { |
| 226 | + casa_case_id: casa_case.friendly_id, |
| 227 | + check_item_action: "add_category", |
| 228 | + check_item_id: emancipation_category.id |
| 229 | + }, format: :json |
| 230 | + |
| 231 | + expect(response).to have_http_status(:unauthorized) |
| 232 | + expect(json_response["error"]).to match(/not authorized/) |
| 233 | + end |
| 234 | + end |
| 235 | + |
| 236 | + # Helper to parse JSON responses |
| 237 | + def json_response |
| 238 | + JSON.parse(response.body) |
47 | 239 | end |
48 | 240 | end |
0 commit comments