Skip to content

Commit

Permalink
Fix crash when very invalid date given for batch expiry, add spec cov…
Browse files Browse the repository at this point in the history
…erage
  • Loading branch information
benilovj committed Oct 17, 2024
1 parent b1f3ea7 commit 9ae1c59
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 7 additions & 1 deletion app/controllers/batches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ def new
end

def create
@batch = Batch.new(batch_params.merge(team: current_user.team, vaccine:))
begin
@batch = Batch.new(batch_params.merge(team: current_user.team, vaccine:))
rescue ActiveRecord::MultiparameterAssignmentErrors
@batch =
Batch.new(name: batch_params[:name], team: current_user.team, vaccine:)
@batch.expiry = expiry_validator.date_params_as_struct
end

if !expiry_validator.date_params_valid?
@batch.expiry = expiry_validator.date_params_as_struct
Expand Down
21 changes: 19 additions & 2 deletions spec/features/manage_batches_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
when_i_manage_vaccines
then_i_see_only_active_hpv_vaccines_with_no_batches_set_up

when_i_add_a_new_batch
when_i_try_to_add_a_batch_with_an_invalid_expiry_date
then_i_see_the_error_message

when_i_add_a_valid_new_batch
then_i_see_the_batch_i_just_added_on_the_vaccines_page

when_i_edit_the_expiry_date_of_the_batch
Expand Down Expand Up @@ -53,11 +56,25 @@ def then_i_see_only_active_hpv_vaccines_with_no_batches_set_up
expect(page).not_to have_css("table")
end

def when_i_add_a_new_batch
def when_i_try_to_add_a_batch_with_an_invalid_expiry_date
click_on "Add a batch", match: :first

fill_in "Batch", with: "AB1234"

# expiry date
fill_in "Day", with: "0"
fill_in "Month", with: "0"
fill_in "Year", with: "0"

click_on "Add batch"
end

def then_i_see_the_error_message
expect(page).to have_content("There is a problem")
expect(page).to have_content("Enter a year")
end

def when_i_add_a_valid_new_batch
# expiry date
fill_in "Day", with: "30"
fill_in "Month", with: "3"
Expand Down

0 comments on commit 9ae1c59

Please sign in to comment.