Skip to content

Commit abf7235

Browse files
Clean up volunteers system invites tests
In general, system tests are expensive to run. After reviewing the test, it turns out that most of the scenarios here are already being tested in the volunteers and invite request specs, so the system tests were redundant. Extracting additional tests from the system tests to the respective request spec keeps the coverage while taking less resources to run. To ensure the new/edit volunteer form fields are still being rendered, and that the Resend invitation button is now displayed after the volunteer has accepted an invitation, these scenarios have been extracted to view tests, which are significantly cheaper to run. The invite expiration is already covered in the volunteers model test. The password setting scenarios are covered in the requests/users/ invitations_spec request tests. Lastly, to keep factories creation to a minimum, the requests/volunteers_spec has been refactored to reuse the exising factories and build new ones instead of creating as much as possible to have a minimum test setup.
1 parent ee8ab93 commit abf7235

File tree

4 files changed

+129
-297
lines changed

4 files changed

+129
-297
lines changed

spec/requests/volunteers_spec.rb

Lines changed: 97 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@
7272
expect(response).to be_successful
7373
end
7474

75-
it "does not render for volunteers" do
75+
it "redirects to root_url and displays an unauthorized message" do
7676
sign_in volunteer
7777

7878
get new_volunteer_path
79-
expect(response).not_to be_successful
79+
expect(response).to redirect_to(root_url)
80+
follow_redirect!
81+
expect(flash[:notice]).to match(/Sorry, you are not authorized to perform this action./)
8082
end
8183
end
8284

@@ -121,81 +123,108 @@
121123
}
122124
end
123125

124-
it "creates a new volunteer and sends account_setup email" do
125-
organization = create(:casa_org)
126-
admin = create(:casa_admin, casa_org: organization)
127-
128-
sign_in admin
129-
expect {
126+
context "when the user is an admin" do
127+
it "creates a new volunteer and sends account_setup email" do
128+
sign_in admin
129+
post volunteers_url, params: params
130+
131+
expect(response).to have_http_status(:redirect)
132+
133+
last_email = ActionMailer::Base.deliveries.last
134+
expect(last_email.to).to eq ["volunteer1@example.com"]
135+
expect(last_email.subject).to eq("CASA Console invitation instructions")
136+
expect(last_email.html_part.body.encoded).to include("your new Volunteer account")
137+
138+
volunteer = Volunteer.last
139+
expect(volunteer.email).to eq("volunteer1@example.com")
140+
expect(volunteer.display_name).to eq("Example")
141+
expect(volunteer.casa_org).to eq(admin.casa_org)
142+
expect(volunteer.invitation_created_at).to be_present
143+
expect(volunteer.invitation_accepted_at).to be_nil
144+
expect(response).to redirect_to edit_volunteer_path(volunteer)
145+
end
146+
147+
it "sends a SMS when phone number exists" do
148+
organization = build(:casa_org, twilio_enabled: true)
149+
admin = build(:casa_admin, casa_org: organization)
150+
params[:volunteer][:phone_number] = "+12222222222"
151+
twilio_activation_success_stub = WebMockHelper.twilio_activation_success_stub("volunteer")
152+
short_io_stub = WebMockHelper.short_io_stub_sms
153+
154+
sign_in admin
130155
post volunteers_url, params: params
131-
}.to change { ActionMailer::Base.deliveries.count }.by(1)
132156

133-
expect(response).to have_http_status(:redirect)
134-
volunteer = Volunteer.last
135-
expect(volunteer.email).to eq("volunteer1@example.com")
136-
expect(volunteer.display_name).to eq("Example")
137-
expect(volunteer.casa_org).to eq(admin.casa_org)
138-
expect(response).to redirect_to edit_volunteer_path(volunteer)
139-
end
157+
expect(short_io_stub).to have_been_requested.times(2)
158+
expect(twilio_activation_success_stub).to have_been_requested.times(1)
159+
expect(response).to have_http_status(:redirect)
160+
follow_redirect!
161+
expect(flash[:notice]).to match(/New volunteer created successfully. SMS has been sent!/)
162+
end
140163

141-
it "sends a SMS when phone number exists" do
142-
organization = create(:casa_org, twilio_enabled: true)
143-
admin = create(:casa_admin, casa_org: organization)
144-
params[:volunteer][:phone_number] = "+12222222222"
145-
twilio_activation_success_stub = WebMockHelper.twilio_activation_success_stub("volunteer")
146-
short_io_stub = WebMockHelper.short_io_stub_sms
164+
it "does not send a SMS when phone number is not provided" do
165+
organization = build(:casa_org, twilio_enabled: true)
166+
admin = build(:casa_admin, casa_org: organization)
167+
sign_in admin
147168

148-
sign_in admin
149-
post volunteers_url, params: params
169+
post volunteers_url, params: params
150170

151-
expect(short_io_stub).to have_been_requested.times(2)
152-
expect(twilio_activation_success_stub).to have_been_requested.times(1)
153-
expect(response).to have_http_status(:redirect)
154-
follow_redirect!
155-
expect(flash[:notice]).to match(/New volunteer created successfully. SMS has been sent!/)
156-
end
171+
expect(response).to have_http_status(:redirect)
172+
follow_redirect!
173+
expect(flash[:notice]).to match(/New volunteer created successfully./)
174+
end
157175

158-
it "does not send a SMS when phone number is not provided" do
159-
organization = create(:casa_org, twilio_enabled: true)
160-
admin = create(:casa_admin, casa_org: organization)
161-
sign_in admin
162-
post volunteers_url, params: params
176+
it "does not send a SMS when Twilio API has an error" do
177+
org = build(:casa_org, twilio_account_sid: "articuno31", twilio_enabled: true)
178+
admin = build(:casa_admin, casa_org: org)
179+
twilio_activation_error_stub = WebMockHelper.twilio_activation_error_stub("volunteer")
180+
short_io_stub = WebMockHelper.short_io_stub_sms
181+
params[:volunteer][:phone_number] = "+12222222222"
163182

164-
expect(response).to have_http_status(:redirect)
165-
follow_redirect!
166-
expect(flash[:notice]).to match(/New volunteer created successfully./)
167-
end
183+
sign_in admin
184+
post volunteers_url, params: params
168185

169-
it "does not send a SMS when Twilio API has an error" do
170-
org = create(:casa_org, twilio_account_sid: "articuno31", twilio_enabled: true)
171-
admin = create(:casa_admin, casa_org: org)
172-
twilio_activation_error_stub = WebMockHelper.twilio_activation_error_stub("volunteer")
173-
short_io_stub = WebMockHelper.short_io_stub_sms
174-
params[:volunteer][:phone_number] = "+12222222222"
186+
expect(short_io_stub).to have_been_requested.times(2) # TODO: why is this called at all?
187+
expect(twilio_activation_error_stub).to have_been_requested.times(1)
188+
expect(response).to have_http_status(:redirect)
189+
follow_redirect!
190+
expect(flash[:notice]).to match(/New volunteer created successfully. SMS not sent. Error: ./)
191+
end
175192

176-
sign_in admin
177-
post volunteers_url, params: params
193+
it "does not send a SMS if the casa_org does not have Twilio enabled" do
194+
params[:volunteer][:phone_number] = "+12222222222"
195+
short_io_stub = WebMockHelper.short_io_stub_sms
178196

179-
expect(short_io_stub).to have_been_requested.times(2) # TODO: why is this called at all?
180-
expect(twilio_activation_error_stub).to have_been_requested.times(1)
181-
expect(response).to have_http_status(:redirect)
182-
follow_redirect!
183-
expect(flash[:notice]).to match(/New volunteer created successfully. SMS not sent. Error: ./)
184-
end
197+
sign_in admin
198+
post volunteers_url, params: params
185199

186-
it "does not send a SMS if the casa_org does not have Twilio enabled" do
187-
org = create(:casa_org, twilio_enabled: false)
188-
admin = build(:casa_admin, casa_org: org)
189-
params[:volunteer][:phone_number] = "+12222222222"
190-
short_io_stub = WebMockHelper.short_io_stub_sms
200+
expect(short_io_stub).to have_been_requested.times(2) # TODO: why is this called at all?
201+
expect(response).to have_http_status(:redirect)
202+
follow_redirect!
203+
expect(flash[:notice]).to match(/New volunteer created successfully./)
204+
end
205+
end
191206

192-
sign_in admin
193-
post volunteers_url, params: params
207+
context "when the user is a supervisor" do
208+
it "creates a new volunteer and sends account_setup email" do
209+
sign_in supervisor
194210

195-
expect(short_io_stub).to have_been_requested.times(2) # TODO: why is this called at all?
196-
expect(response).to have_http_status(:redirect)
197-
follow_redirect!
198-
expect(flash[:notice]).to match(/New volunteer created successfully./)
211+
post volunteers_url, params: params
212+
213+
expect(response).to have_http_status(:redirect)
214+
215+
last_email = ActionMailer::Base.deliveries.last
216+
expect(last_email.to).to eq ["volunteer1@example.com"]
217+
expect(last_email.subject).to eq("CASA Console invitation instructions")
218+
expect(last_email.html_part.body.encoded).to include("your new Volunteer account")
219+
220+
volunteer = Volunteer.last
221+
expect(volunteer.email).to eq("volunteer1@example.com")
222+
expect(volunteer.display_name).to eq("Example")
223+
expect(volunteer.casa_org).to eq(admin.casa_org)
224+
expect(volunteer.invitation_created_at).to be_present
225+
expect(volunteer.invitation_accepted_at).to be_nil
226+
expect(response).to redirect_to edit_volunteer_path(volunteer)
227+
end
199228
end
200229
end
201230

@@ -210,9 +239,6 @@
210239
end
211240

212241
it "does not create a new volunteer" do
213-
org = create(:casa_org, twilio_enabled: false)
214-
admin = build(:casa_admin, casa_org: org)
215-
216242
sign_in admin
217243

218244
expect {
@@ -364,11 +390,14 @@
364390
expect(volunteer.invitation_created_at.present?).to eq(false)
365391

366392
get resend_invitation_volunteer_path(volunteer)
393+
394+
expect(flash[:notice]).to match(/Invitation sent/)
367395
volunteer.reload
368396

369397
expect(volunteer.invitation_created_at.present?).to eq(true)
370398
expect(Devise.mailer.deliveries.count).to eq(1)
371-
expect(Devise.mailer.deliveries.first.subject).to eq(I18n.t("devise.mailer.invitation_instructions.subject"))
399+
expect(Devise.mailer.deliveries.first.to).to eq([volunteer.email])
400+
expect(Devise.mailer.deliveries.first.subject).to eq("CASA Console invitation instructions")
372401
expect(response).to redirect_to(edit_volunteer_path(volunteer))
373402
end
374403
end

0 commit comments

Comments
 (0)