Skip to content

Commit 429afed

Browse files
Andrew OberhardtAndrew Oberhardt
authored andcommitted
Added null check, and 404 for bad workshop id in enrollment controller
1 parent 8053428 commit 429afed

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

dashboard/app/controllers/api/v1/pd/workshops_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def adjust_facilitators
7070

7171
new_facilitator_ids.each do |facilitator_id|
7272
facilitator = User.find_by(id: facilitator_id)
73-
next unless facilitator.facilitator?
73+
next unless facilitator && facilitator.facilitator?
7474
@workshop.facilitators << facilitator
7575
end
7676
end

dashboard/app/controllers/pd/workshop_enrollment_controller.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ def new
55
view_options(no_footer: true)
66
@workshop = ::Pd::Workshop.find_by_id params[:workshop_id]
77

8-
if workshop_closed?
8+
if @workshop.nil?
9+
render_404
10+
elsif workshop_closed?
911
render :closed
1012
elsif workshop_full?
1113
render :full
@@ -24,6 +26,11 @@ def new
2426
# POST /pd/workshops/1/enroll
2527
def create
2628
@workshop = ::Pd::Workshop.find_by_id params[:workshop_id]
29+
if @workshop.nil?
30+
render_404
31+
return
32+
end
33+
2734
enrollment_email = enrollment_params[:email]
2835
user = User.find_by_email enrollment_email
2936

dashboard/test/controllers/pd/workshop_enrollment_controller_test.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,9 @@ class Pd::WorkshopEnrollmentControllerTest < ::ActionController::TestCase
6767
assert_template :full
6868
end
6969

70-
test 'enrollments can be created' do
71-
assert_creates(Pd::Enrollment) do
72-
post :create, workshop_id: @workshop.id, pd_enrollment: enrollment_test_params
73-
end
74-
enrollment = Pd::Enrollment.last
75-
refute_nil enrollment.code
76-
assert_redirected_to action: :show, code: enrollment.code
70+
test 'unknown workshop id responds with 404' do
71+
get :new, workshop_id: 'nonsense'
72+
assert_response 404
7773
end
7874

7975
test 'enroll post route' do
@@ -83,6 +79,15 @@ class Pd::WorkshopEnrollmentControllerTest < ::ActionController::TestCase
8379
)
8480
end
8581

82+
test 'enrollments can be created' do
83+
assert_creates(Pd::Enrollment) do
84+
post :create, workshop_id: @workshop.id, pd_enrollment: enrollment_test_params
85+
end
86+
enrollment = Pd::Enrollment.last
87+
refute_nil enrollment.code
88+
assert_redirected_to action: :show, code: enrollment.code
89+
end
90+
8691
test 'creating a duplicate enrollment renders duplicate view' do
8792
params = enrollment_test_params.merge({
8893
name: @existing_enrollment.name,
@@ -136,6 +141,11 @@ class Pd::WorkshopEnrollmentControllerTest < ::ActionController::TestCase
136141
assert_template :new
137142
end
138143

144+
test 'creating an enrollment on an unknown workshop id returns 404' do
145+
post :create, workshop_id: 'nonsense', pd_enrollment: enrollment_test_params
146+
assert_response 404
147+
end
148+
139149
test 'show route' do
140150
assert_routing(
141151
{path: "/pd/workshop_enrollment/#{@existing_enrollment.code}", method: :get},

0 commit comments

Comments
 (0)