Skip to content

Commit 29562d0

Browse files
committed
WIP bad syntax specs
1 parent 5ddfa9a commit 29562d0

5 files changed

Lines changed: 192 additions & 26 deletions

File tree

app/controllers/case_contacts_controller.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@ def index
2020
) || return
2121

2222
@pagy, @filtered_case_contacts = pagy(@filterrific.find)
23-
case_contacts = CaseContact.case_hash_from_cases(@filtered_case_contacts)
24-
case_contacts = case_contacts.select { |k, _v| current_user.casa_cases.pluck(:id).include?(k) } if current_user.volunteer?
25-
case_contacts = case_contacts.select { |k, _v| k == params[:casa_case_id].to_i } if params[:casa_case_id].present?
23+
24+
casa_case_id_to_case_contacts = CaseContact.case_hash_from_cases(@filtered_case_contacts)
25+
binding.pry
26+
if params[:casa_case_id].present?
27+
binding.pry
28+
casa_case_id_to_case_contacts = casa_case_id_to_case_contacts.select { |k, _v| k == params[:casa_case_id].to_i }
29+
end
30+
if current_user.volunteer?
31+
binding.pry
32+
current_user_casa_case_ids = current_user.casa_cases.pluck(:id)
33+
casa_case_id_to_case_contacts = casa_case_id_to_case_contacts.select { |case_contact_id, _cases| current_user_casa_case_ids.include?(case_contact_id) }
34+
end
2635

27-
@presenter = CaseContactPresenter.new(case_contacts)
36+
@presenter = CaseContactPresenter.new(casa_case_id_to_case_contacts)
2837
end
2938

3039
def drafts
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require "rails_helper"
2+
3+
RSpec.describe CaseContactsController, type: :controller do
4+
describe "GET #index" do
5+
# Create test data in before block to keep tests DRY
6+
before do
7+
sign_in user
8+
9+
@casa_case1 = create(:casa_case)
10+
@casa_case2 = create(:casa_case)
11+
12+
@contact1 = create(:case_contact, casa_case: @casa_case1)
13+
@contact2 = create(:case_contact, casa_case: @casa_case1)
14+
@contact3 = create(:case_contact, casa_case: @casa_case2)
15+
end
16+
17+
context "when casa_case_id param is present" do
18+
it "filters case contacts for the specified casa case" do
19+
get :index, params: { casa_case_id: @casa_case1.id }
20+
21+
# Access the instance variable that was set in the controller
22+
case_contacts_hash = assigns(:casa_case_id_to_case_contacts)
23+
24+
# Should only include contacts from casa_case1
25+
expect(case_contacts_hash.keys).to contain_exactly(@casa_case1.id)
26+
expect(case_contacts_hash[@casa_case1.id]).to contain_exactly(@contact1, @contact2)
27+
end
28+
end
29+
30+
context "when casa_case_id param is not present" do
31+
it "includes case contacts for all casa cases" do
32+
get :index
33+
34+
case_contacts_hash = assigns(:casa_case_id_to_case_contacts)
35+
36+
# Should include contacts from both cases
37+
expect(case_contacts_hash.keys).to contain_exactly(@casa_case1.id, @casa_case2.id)
38+
expect(case_contacts_hash[@casa_case1.id]).to contain_exactly(@contact1, @contact2)
39+
expect(case_contacts_hash[@casa_case2.id]).to contain_exactly(@contact3)
40+
end
41+
end
42+
end
43+
end

spec/models/case_contact_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,4 +656,32 @@
656656
end
657657
end
658658
end
659+
660+
describe '.case_hash_from_cases' do
661+
it 'returns a hash mapping casa case ids to their case contacts' do
662+
# Create test data
663+
casa_case1 = create(:casa_case)
664+
casa_case2 = create(:casa_case)
665+
666+
contact1 = create(:case_contact, casa_case: casa_case1)
667+
contact2 = create(:case_contact, casa_case: casa_case1)
668+
contact3 = create(:case_contact, casa_case: casa_case2)
669+
670+
contacts = [contact1, contact2, contact3]
671+
672+
# Call the method
673+
result = described_class.case_hash_from_cases(contacts)
674+
675+
# Verify the result
676+
expect(result).to eq({
677+
casa_case1.id => [contact1, contact2],
678+
casa_case2.id => [contact3]
679+
})
680+
end
681+
682+
it 'returns empty hash for empty contacts' do
683+
result = described_class.case_hash_from_cases([])
684+
expect(result).to eq({})
685+
end
686+
end
659687
end

spec/requests/casa_cases_spec.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require "rails_helper"
2-
2+
# hi
33
RSpec.describe "/casa_cases", type: :request do
44
let(:date_in_care) { Date.today }
55
let(:organization) { build(:casa_org) }
@@ -611,9 +611,24 @@
611611
end
612612

613613
describe "GET /index" do
614-
it "shows only cases assigned to user" do
615-
mine = build(:casa_case, casa_org: organization, case_number: SecureRandom.hex(32))
616-
other = build(:casa_case, casa_org: organization, case_number: SecureRandom.hex(32))
614+
context "with casa_case_id" do
615+
it "shows only cases assigned to user" do
616+
mine = build(:casa_case, casa_org: organization, case_number: SecureRandom.hex(32))
617+
other = build(:casa_case, casa_org: organization, case_number: SecureRandom.hex(32))
618+
619+
user.casa_cases << mine
620+
621+
get casa_cases_url(casa_case_id: mine.id)
622+
623+
expect(response).to be_successful
624+
expect(response.body).to include(mine.case_number)
625+
expect(response.body).not_to include(other.case_number)
626+
end
627+
628+
context "without casa_case_id" do
629+
it "shows only cases assigned to user" do
630+
mine = build(:casa_case, casa_org: organization, case_number: SecureRandom.hex(32))
631+
other = build(:casa_case, casa_org: organization, case_number: SecureRandom.hex(32))
617632

618633
user.casa_cases << mine
619634

spec/requests/case_contacts_spec.rb

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
require "rails_helper"
2-
2+
# hi
33
RSpec.describe "/case_contacts", type: :request do
4-
let(:organization) { build(:casa_org) }
4+
let(:organization) { create(:casa_org) }
55
let(:admin) { create(:casa_admin, casa_org: organization) }
66
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) }
79

810
before { sign_in admin }
911

@@ -14,25 +16,36 @@
1416
response
1517
end
1618

17-
let!(:casa_case) { create(:casa_case, casa_org: organization) }
1819
let!(:past_contact) { create(:case_contact, casa_case: casa_case, occurred_at: 3.weeks.ago) }
1920
let!(:recent_contact) { create(:case_contact, casa_case: casa_case, occurred_at: 3.days.ago) }
2021
let(:filterrific) { {} }
2122

2223
it { is_expected.to have_http_status(:success) }
2324

24-
it "returns all case contacts" do
25-
page = request.parsed_body.to_html
26-
expect(page).to include(past_contact.creator.display_name, recent_contact.creator.display_name)
27-
end
28-
29-
context "with filters applied" do
30-
let(:filterrific) { {occurred_starting_at: 1.week.ago} }
31-
25+
context "when logged in as an admin" do
3226
it "returns all case contacts" do
3327
page = request.parsed_body.to_html
34-
expect(page).to include(recent_contact.creator.display_name)
35-
expect(page).not_to include(past_contact.creator.display_name)
28+
expect(page).to include(past_contact.creator.display_name, recent_contact.creator.display_name)
29+
end
30+
31+
context "with filters applied" do
32+
let(:filterrific) { {occurred_starting_at: 1.week.ago} }
33+
34+
it "returns filtered case contacts" do
35+
page = request.parsed_body.to_html
36+
expect(page).to include(recent_contact.creator.display_name)
37+
expect(page).not_to include(past_contact.creator.display_name)
38+
end
39+
end
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
3649
end
3750
end
3851

@@ -41,14 +54,24 @@
4154
let(:unassigned_case) { casa_case }
4255
let(:volunteer) { assigned_case.assigned_volunteers.first }
4356
let!(:assigned_case_contact) { create(:case_contact, casa_case: assigned_case, creator: volunteer) }
44-
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) }
4558

4659
before { sign_in volunteer }
4760

4861
it "returns only currently assigned cases" do
4962
page = request.parsed_body.to_html
50-
expect(page).to include("60 minutes")
51-
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)
5275
end
5376
end
5477
end
@@ -83,6 +106,15 @@
83106
expect(CaseContact.started.last.contact_topic_answers).to be_empty
84107
end
85108
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
86118
end
87119

88120
describe "GET /edit" do
@@ -100,6 +132,16 @@
100132
request
101133
expect(response).to redirect_to(case_contact_form_path(:details, case_contact_id: case_contact.id))
102134
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
103145
end
104146

105147
describe "GET /drafts" do
@@ -109,8 +151,17 @@
109151
response
110152
end
111153

154+
let!(:draft_contact) { create(:case_contact, status: "started") }
155+
let!(:active_contact) { create(:case_contact, status: "active") }
156+
112157
it { is_expected.to have_http_status(:success) }
113158

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+
114165
context "when user is volunteer" do
115166
before { sign_in volunteer }
116167

@@ -137,9 +188,19 @@
137188
it "soft deletes the case_contact" do
138189
expect { request }.to change { case_contact.reload.deleted? }.from(false).to(true)
139190
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
140201
end
141202

142-
describe "GET /restore" do
203+
describe "POST /restore" do
143204
subject(:request) do
144205
post restore_case_contact_path(case_contact), headers: {HTTP_REFERER: case_contacts_path}
145206

@@ -157,9 +218,19 @@
157218
expect(flash[:notice]).to eq("Contact is successfully restored.")
158219
end
159220

160-
it "soft deletes the case_contact" do
221+
it "restores the case_contact" do
161222
expect { request }.to change { case_contact.reload.deleted? }.from(true).to(false)
162223
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
163234
end
164235

165236
xdescribe "GET /leave" do

0 commit comments

Comments
 (0)