|
72 | 72 | expect(response).to be_successful |
73 | 73 | end |
74 | 74 |
|
75 | | - it "does not render for volunteers" do |
| 75 | + it "redirects to root_url and displays an unauthorized message" do |
76 | 76 | sign_in volunteer |
77 | 77 |
|
78 | 78 | 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./) |
80 | 82 | end |
81 | 83 | end |
82 | 84 |
|
|
121 | 123 | } |
122 | 124 | end |
123 | 125 |
|
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 |
130 | 155 | post volunteers_url, params: params |
131 | | - }.to change { ActionMailer::Base.deliveries.count }.by(1) |
132 | 156 |
|
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 |
140 | 163 |
|
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 |
147 | 168 |
|
148 | | - sign_in admin |
149 | | - post volunteers_url, params: params |
| 169 | + post volunteers_url, params: params |
150 | 170 |
|
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 |
157 | 175 |
|
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" |
163 | 182 |
|
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 |
168 | 185 |
|
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 |
175 | 192 |
|
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 |
178 | 196 |
|
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 |
185 | 199 |
|
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 |
191 | 206 |
|
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 |
194 | 210 |
|
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 |
199 | 228 | end |
200 | 229 | end |
201 | 230 |
|
|
210 | 239 | end |
211 | 240 |
|
212 | 241 | 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 | | - |
216 | 242 | sign_in admin |
217 | 243 |
|
218 | 244 | expect { |
|
364 | 390 | expect(volunteer.invitation_created_at.present?).to eq(false) |
365 | 391 |
|
366 | 392 | get resend_invitation_volunteer_path(volunteer) |
| 393 | + |
| 394 | + expect(flash[:notice]).to match(/Invitation sent/) |
367 | 395 | volunteer.reload |
368 | 396 |
|
369 | 397 | expect(volunteer.invitation_created_at.present?).to eq(true) |
370 | 398 | 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") |
372 | 401 | expect(response).to redirect_to(edit_volunteer_path(volunteer)) |
373 | 402 | end |
374 | 403 | end |
|
0 commit comments