|
1 | 1 | require "rails_helper" |
2 | 2 | # hi |
3 | 3 | RSpec.describe "/case_contacts", type: :request do |
4 | | - let(:organization) { build(:casa_org) } |
| 4 | + let(:organization) { create(:casa_org) } |
5 | 5 | let(:admin) { create(:casa_admin, casa_org: organization) } |
6 | 6 | let(:volunteer) { create(:volunteer, casa_org: organization) } |
| 7 | + let(:supervisor) { create(:supervisor, casa_org: organization) } |
| 8 | + let(:casa_case) { create(:casa_case, casa_org: organization) } |
7 | 9 |
|
8 | 10 | before { sign_in admin } |
9 | 11 |
|
|
14 | 16 | response |
15 | 17 | end |
16 | 18 |
|
17 | | - let!(:casa_case) { create(:casa_case, casa_org: organization) } |
18 | 19 | let!(:past_contact) { create(:case_contact, casa_case: casa_case, occurred_at: 3.weeks.ago) } |
19 | 20 | let!(:recent_contact) { create(:case_contact, casa_case: casa_case, occurred_at: 3.days.ago) } |
20 | 21 | let(:filterrific) { {} } |
|
30 | 31 | context "with filters applied" do |
31 | 32 | let(:filterrific) { {occurred_starting_at: 1.week.ago} } |
32 | 33 |
|
33 | | - it "returns all case contacts" do |
| 34 | + it "returns filtered case contacts" do |
34 | 35 | page = request.parsed_body.to_html |
35 | 36 | expect(page).to include(recent_contact.creator.display_name) |
36 | 37 | expect(page).not_to include(past_contact.creator.display_name) |
37 | 38 | end |
38 | 39 | end |
39 | | - end |
40 | 40 |
|
| 41 | + context "with sorting" do |
| 42 | + let(:filterrific) { {sorted_by: "occurred_at_desc"} } |
| 43 | + |
| 44 | + it "returns sorted case contacts" do |
| 45 | + page = request.parsed_body.to_html |
| 46 | + expect(page).to include(recent_contact.creator.display_name) |
| 47 | + expect(page).to include(past_contact.creator.display_name) |
| 48 | + end |
| 49 | + end |
| 50 | + end |
41 | 51 |
|
42 | 52 | context "when logged in as a volunteer" do |
43 | 53 | let(:assigned_case) { create(:casa_case, :with_one_case_assignment, casa_org: organization) } |
44 | 54 | let(:unassigned_case) { casa_case } |
45 | 55 | let(:volunteer) { assigned_case.assigned_volunteers.first } |
46 | 56 | let!(:assigned_case_contact) { create(:case_contact, casa_case: assigned_case, creator: volunteer) } |
47 | | - let!(:unassigned_case_contact) { create(:case_contact, casa_case: unassigned_case, creator: volunteer, duration_minutes: 180) } |
| 57 | + let!(:unassigned_case_contact) { create(:case_contact, casa_case: unassigned_case, creator: volunteer) } |
48 | 58 |
|
49 | 59 | before { sign_in volunteer } |
50 | 60 |
|
51 | 61 | it "returns only currently assigned cases" do |
52 | 62 | page = request.parsed_body.to_html |
53 | | - expect(page).to include("60 minutes") |
54 | | - expect(page).not_to include("3 hours") |
| 63 | + binding.pry |
| 64 | + expect(page).to include(assigned_case_contact.creator.display_name) |
| 65 | + expect(page).not_to include(unassigned_case_contact.creator.display_name) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + context "when logged in as a supervisor" do |
| 70 | + before { sign_in supervisor } |
| 71 | + |
| 72 | + it "returns all case contacts" do |
| 73 | + page = request.parsed_body.to_html |
| 74 | + expect(page).to include(past_contact.creator.display_name, recent_contact.creator.display_name) |
55 | 75 | end |
56 | 76 | end |
57 | 77 | end |
|
86 | 106 | expect(CaseContact.started.last.contact_topic_answers).to be_empty |
87 | 107 | end |
88 | 108 | end |
| 109 | + |
| 110 | + context "with draft case ids" do |
| 111 | + let(:draft_case_ids) { [casa_case.id] } |
| 112 | + |
| 113 | + it "creates case contact with draft case ids" do |
| 114 | + get new_case_contact_path(draft_case_ids: draft_case_ids) |
| 115 | + expect(CaseContact.last.draft_case_ids).to eq(draft_case_ids) |
| 116 | + end |
| 117 | + end |
89 | 118 | end |
90 | 119 |
|
91 | 120 | describe "GET /edit" do |
|
103 | 132 | request |
104 | 133 | expect(response).to redirect_to(case_contact_form_path(:details, case_contact_id: case_contact.id)) |
105 | 134 | end |
| 135 | + |
| 136 | + context "when user is not authorized" do |
| 137 | + let(:unauthorized_volunteer) { create(:volunteer, casa_org: organization) } |
| 138 | + before { sign_in unauthorized_volunteer } |
| 139 | + |
| 140 | + it "redirects to root path" do |
| 141 | + request |
| 142 | + expect(response).to redirect_to(authenticated_user_root_path) |
| 143 | + end |
| 144 | + end |
106 | 145 | end |
107 | 146 |
|
108 | 147 | describe "GET /drafts" do |
|
112 | 151 | response |
113 | 152 | end |
114 | 153 |
|
| 154 | + let!(:draft_contact) { create(:case_contact, status: "started") } |
| 155 | + let!(:active_contact) { create(:case_contact, status: "active") } |
| 156 | + |
115 | 157 | it { is_expected.to have_http_status(:success) } |
116 | 158 |
|
| 159 | + it "returns only draft contacts" do |
| 160 | + page = request.parsed_body.to_html |
| 161 | + expect(page).to include(draft_contact.creator.display_name) |
| 162 | + expect(page).not_to include(active_contact.creator.display_name) |
| 163 | + end |
| 164 | + |
117 | 165 | context "when user is volunteer" do |
118 | 166 | before { sign_in volunteer } |
119 | 167 |
|
|
140 | 188 | it "soft deletes the case_contact" do |
141 | 189 | expect { request }.to change { case_contact.reload.deleted? }.from(false).to(true) |
142 | 190 | end |
| 191 | + |
| 192 | + context "when user is not authorized" do |
| 193 | + let(:unauthorized_volunteer) { create(:volunteer, casa_org: organization) } |
| 194 | + before { sign_in unauthorized_volunteer } |
| 195 | + |
| 196 | + it "redirects to root path" do |
| 197 | + request |
| 198 | + expect(response).to redirect_to(authenticated_user_root_path) |
| 199 | + end |
| 200 | + end |
143 | 201 | end |
144 | 202 |
|
145 | | - describe "GET /restore" do |
| 203 | + describe "POST /restore" do |
146 | 204 | subject(:request) do |
147 | 205 | post restore_case_contact_path(case_contact), headers: {HTTP_REFERER: case_contacts_path} |
148 | 206 |
|
|
160 | 218 | expect(flash[:notice]).to eq("Contact is successfully restored.") |
161 | 219 | end |
162 | 220 |
|
163 | | - it "soft deletes the case_contact" do |
| 221 | + it "restores the case_contact" do |
164 | 222 | expect { request }.to change { case_contact.reload.deleted? }.from(true).to(false) |
165 | 223 | end |
| 224 | + |
| 225 | + context "when user is not authorized" do |
| 226 | + let(:unauthorized_volunteer) { create(:volunteer, casa_org: organization) } |
| 227 | + before { sign_in unauthorized_volunteer } |
| 228 | + |
| 229 | + it "redirects to root path" do |
| 230 | + request |
| 231 | + expect(response).to redirect_to(authenticated_user_root_path) |
| 232 | + end |
| 233 | + end |
166 | 234 | end |
167 | 235 |
|
168 | 236 | xdescribe "GET /leave" do |
|
0 commit comments