forked from rubyforgood/casa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase_contacts_spec.rb
126 lines (107 loc) · 3.66 KB
/
case_contacts_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require "rails_helper"
RSpec.describe "/case_contacts", type: :request do
let(:volunteer) { create(:user, :volunteer) }
let(:other_volunteer) { create(:user, :volunteer) }
let(:valid_attributes) do
attributes_for(:case_contact).merge(
creator: volunteer,
casa_case_id: [
create(:casa_case, volunteers: [volunteer]).id,
create(:casa_case, volunteers: [volunteer]).id,
]
)
end
let(:invalid_attributes) do
{
creator: nil,
casa_case_id: [create(:casa_case, volunteers: [volunteer]).id],
contact_types: ["invalid type"],
occurred_at: Time.zone.now
}
end
before { sign_in volunteer }
describe "GET /index" do
it "renders a successful response" do
create(:case_contact)
get case_contacts_url
expect(response).to be_successful
end
end
describe "GET /show" do
it "renders a successful response" do
case_contact = create(:case_contact, creator: volunteer)
get case_contact_url(case_contact)
expect(response).to be_successful
end
end
describe "POST /create" do
context "with valid parameters" do
it "does create two new CaseContacts" do
expect {
post case_contacts_url, params: {case_contact: valid_attributes}
}.to change(CaseContact, :count).by(2)
end
end
context "with invalid parameters" do
it "does not create a new CaseContact" do
expect { post case_contacts_url, params: {case_contact: invalid_attributes} }.to change(
CaseContact,
:count
).by(0)
end
it "renders a successful response (i.e. to display the new template)" do
post case_contacts_url, params: {case_contact: invalid_attributes}
expect(response).to be_successful
end
end
end
describe "PATCH /update" do
context "with valid parameters" do
it "updates the requested case_contact and redirects to the root path" do
case_contact = create(:case_contact, creator: volunteer)
patch case_contact_url(case_contact), params: {
case_contact: {
contact_types: ["attorney"],
}
}
expect(response).to redirect_to(root_path)
case_contact.reload
expect(case_contact.contact_types).to eq(["attorney"])
end
end
context "as another volunteer" do
before { sign_in other_volunteer }
it "does not allow update of case contacts created by other volunteers" do
case_contact = create(:case_contact, creator: volunteer, contact_types: ["therapist"])
patch case_contact_url(case_contact), params: {
case_contact: {
contact_types: ["attorney"],
}
}
expect(response).to redirect_to(root_path)
case_contact.reload
expect(case_contact.contact_types).to eq(["therapist"])
end
end
context "with invalid parameters" do
it "renders a successful response (i.e. to display the edit template)" do
case_contact = create(:case_contact, creator: volunteer)
patch case_contact_url(case_contact), params: {case_contact: invalid_attributes}
expect(response).to be_successful
end
end
end
describe "DELETE /destroy" do
it "destroys the requested case_contact" do
case_contact = create(:case_contact, creator: volunteer)
expect {
delete case_contact_url(case_contact)
}.to change(CaseContact, :count).by(-1)
end
it "redirects to the case_contacts list" do
case_contact = create(:case_contact, creator: volunteer)
delete case_contact_url(case_contact)
expect(response).to redirect_to(case_contacts_url)
end
end
end