Skip to content
This repository was archived by the owner on Jul 24, 2020. It is now read-only.

Commit 07b4bf9

Browse files
committed
Fix broken ToS checkbox handling
Resolves #1227 - add integration specs for ToS checkbox - fix banned user view handling - remove check_tos method and only update user if checkout succeeds - tweak JS confirmation message on manage reservation page
1 parent 4a06ca1 commit 07b4bf9

File tree

5 files changed

+109
-21
lines changed

5 files changed

+109
-21
lines changed

app/assets/javascripts/manage_reservation.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ function validate_checkin(){
3737

3838
function confirm_checkinout(flag){
3939
if (flag){
40-
if( confirm("Oops! We've noticed one of the following issues:\n\nYou checked off procedures for an item you're not checking in/out.\n\n or\n\nYou didn't check off all procedures for an item that you are checking in/out.\n\nAre you sure you want to continue?")){
40+
if( confirm("Oops! We've noticed one of the following issues:\n\n"+
41+
"You didn't check off all procedures for an item your're checking "+
42+
"in/out,\n\n\tor\n\nYou didn't select an item for a reservation you "+
43+
"checked off procedures for.\n\nAre you sure you want to continue?")){
4144
(this).submit();
4245
return false;
4346
} else {

app/controllers/application_controller.rb

-15
Original file line numberDiff line numberDiff line change
@@ -312,21 +312,6 @@ def markdown_help
312312
end
313313
end
314314

315-
# Checks if params[:terms_of_service_accepted] is necessary; if filled-out,
316-
# saves the state of the user; if not filled out and necessary, returns false.
317-
# Otherwise, returns true.
318-
def check_tos(user)
319-
return true if user.terms_of_service_accepted
320-
321-
user.terms_of_service_accepted = params[:terms_of_service_accepted].present?
322-
if user.terms_of_service_accepted
323-
user.save
324-
else
325-
(flash[:error] = 'You must confirm that the user accepts the Terms of '\
326-
'Service.') && false
327-
end
328-
end
329-
330315
private
331316

332317
# modify redirect after signing in

app/controllers/reservations_controller.rb

+19-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def set_user
1414
@user = User.find(params[:user_id])
1515
return unless @user.role == 'banned'
1616
flash[:error] = 'This user is banned and cannot check out equipment.'
17+
params[:banned] = true
1718
end
1819

1920
def set_reservation
@@ -260,7 +261,13 @@ def checkout # rubocop:disable all
260261

261262
## Basic-logic checks, only need to be done once
262263

263-
redirect_to(:back) && return unless check_tos(@user)
264+
# check terms of service
265+
unless @user.terms_of_service_accepted ||
266+
params[:terms_of_service_accepted].present?
267+
flash[:error] = 'You must confirm that the user accepts the Terms of '\
268+
'Service.'
269+
redirect_to(:back) && return
270+
end
264271

265272
if checked_out_reservations.empty?
266273
flash[:error] = 'No reservation selected.'
@@ -297,6 +304,11 @@ def checkout # rubocop:disable all
297304
end
298305
end
299306

307+
# update user with terms of service acceptance now that checkout worked
308+
unless @user.terms_of_service_accepted
309+
@user.update_attributes(terms_of_service_accepted: true)
310+
end
311+
300312
# prep for receipt page and exit
301313
@check_in_set = []
302314
@check_out_set = checked_out_reservations
@@ -355,15 +367,19 @@ def upcoming
355367
end
356368

357369
def manage # initializer
358-
redirect_to(root_path) && return unless flash[:error].nil?
370+
if params[:banned] && current_user.view_mode != 'superuser'
371+
redirect_to(root_path) && return
372+
end
359373
@check_out_set = @user.due_for_checkout
360374
@check_in_set = @user.due_for_checkin
361375

362376
render :manage, layout: 'application'
363377
end
364378

365379
def current
366-
redirect_to(root_path) && return unless flash[:error].nil?
380+
if params[:banned] && current_user.view_mode != 'superuser'
381+
redirect_to(root_path) && return
382+
end
367383
@user_overdue_reservations_set =
368384
[Reservation.overdue.for_reserver(@user)].delete_if(&:empty?)
369385
@user_checked_out_today_reservations_set =

spec/controllers/reservations_controller_spec.rb

+19-2
Original file line numberDiff line numberDiff line change
@@ -1054,16 +1054,33 @@
10541054
before { put :checkout, user_id: @banned.id }
10551055
end
10561056

1057-
context 'when tos returns false' do
1057+
context 'when tos not accepted and not checked off' do
10581058
before do
10591059
request.env['HTTP_REFERER'] = 'where_i_came_from'
10601060
sign_in @admin
1061-
allow(@controller).to receive(:check_tos).and_return(false)
1061+
@user.update_attributes(terms_of_service_accepted: false)
10621062
put :checkout, user_id: @user.id, reservations: {}
10631063
end
10641064
it { expect(response).to redirect_to 'where_i_came_from' }
10651065
end
10661066

1067+
context 'when tos accepted' do
1068+
before do
1069+
sign_in @admin
1070+
@user.update_attributes(terms_of_service_accepted: false)
1071+
@item =
1072+
FactoryGirl.create(:equipment_item,
1073+
equipment_model: @reservation.equipment_model)
1074+
reservations_params =
1075+
{ @reservation.id.to_s => { notes: '',
1076+
equipment_item_id: @item.id } }
1077+
put :checkout, user_id: @user.id, reservations: reservations_params,
1078+
terms_of_service_accepted: true
1079+
end
1080+
1081+
it { expect(response).to be_success }
1082+
end
1083+
10671084
context 'when not all procedures are filled out' do
10681085
before do
10691086
sign_in @admin

spec/features/reservations_spec.rb

+67
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
before(:each) do
181181
@res = FactoryGirl.create :valid_reservation, reserver: @user,
182182
equipment_model: @eq_model
183+
visit manage_reservations_for_user_path(@user)
183184
end
184185

185186
shared_examples 'can handle reservation transactions' do
@@ -209,6 +210,14 @@
209210
end
210211
end
211212

213+
context 'as guest' do
214+
it 'should redirect to the catalog page' do
215+
visit manage_reservations_for_user_path(@user)
216+
expect(page).to have_content 'Sign In'
217+
expect(page.current_url).to eq(new_user_session_url)
218+
end
219+
end
220+
212221
context 'as patron' do
213222
before { sign_in_as_user(@user) }
214223
after { sign_out }
@@ -240,6 +249,64 @@
240249

241250
it_behaves_like 'can handle reservation transactions'
242251
end
252+
253+
context 'ToS checkbox' do
254+
before(:each) do
255+
@user.update_attributes(terms_of_service_accepted: false)
256+
end
257+
258+
shared_examples 'can utilize the ToS checkbox' do
259+
before(:each) { visit manage_reservations_for_user_path(@user) }
260+
261+
it 'fails when the box isn\'t checked off' do
262+
# skip the checkbox
263+
select "#{@eq_model.equipment_items.first.name}",
264+
from: 'Equipment Item'
265+
click_button 'Check-Out Equipment'
266+
267+
expect(page).to have_content 'You must confirm that the user '\
268+
'accepts the Terms of Service.'
269+
expect(page.current_url).to \
270+
eq(manage_reservations_for_user_url(@user))
271+
end
272+
273+
it 'succeeds when the box is checked off' do
274+
check 'terms_of_service_accepted'
275+
select "#{@eq_model.equipment_items.first.name}",
276+
from: 'Equipment Item'
277+
click_button 'Check-Out Equipment'
278+
279+
expect(page).to have_content 'Check-Out Receipt'
280+
expect(page).to have_content current_user.name
281+
@res.reload
282+
expect(@res.equipment_item_id).to \
283+
eq(@eq_model.equipment_items.first.id)
284+
expect(@res.checkout_handler).to eq(current_user)
285+
expect(@res.checked_out).not_to be_nil
286+
end
287+
end
288+
289+
context 'as checkout person' do
290+
before { sign_in_as_user(@checkout_person) }
291+
after { sign_out }
292+
293+
it_behaves_like 'can utilize the ToS checkbox'
294+
end
295+
296+
context 'as admin' do
297+
before { sign_in_as_user(@admin) }
298+
after { sign_out }
299+
300+
it_behaves_like 'can utilize the ToS checkbox'
301+
end
302+
303+
context 'as superuser' do
304+
before { sign_in_as_user(@superuser) }
305+
after { sign_out }
306+
307+
it_behaves_like 'can utilize the ToS checkbox'
308+
end
309+
end
243310
end
244311

245312
context 'renewing reservations' do

0 commit comments

Comments
 (0)