Skip to content

Commit

Permalink
Merge pull request #3217 from dodona-edu/fix/catch-bad-api-usage
Browse files Browse the repository at this point in the history
Catch some bad API usage that should not result in internal error
  • Loading branch information
chvp authored Nov 22, 2021
2 parents 238e0a6 + c155e12 commit 8c86abc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class ApplicationController < ActionController::Base
MAX_STORED_URL_LENGTH = 1024

rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
rescue_from ActionDispatch::Http::Parameters::ParseError, with: :bad_request
rescue_from ActionController::ParameterMissing, with: :bad_request

protect_from_forgery with: :null_session

Expand Down Expand Up @@ -120,6 +122,12 @@ def user_not_authorized
end
end

def bad_request
# rubocop:disable Rails/RenderInline
render status: :bad_request, inline: 'Bad request'
# rubocop:enable Rails/RenderInline
end

def set_locale
helpers.locale = params[:locale] || current_user&.lang || I18n.default_locale
end
Expand Down
7 changes: 6 additions & 1 deletion test/controllers/submissions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,18 @@ class SubmissionsControllerTest < ActionDispatch::IntegrationTest
assert_response :unprocessable_entity
end

test 'create submission should respond bad_request without an exercise' do
test 'create submission should respond unprocessable_entity without an exercise' do
attrs = generate_attr_hash
attrs.delete(:exercise_id)
create_request(attr_hash: attrs)
assert_response :unprocessable_entity
end

test 'create submission should respond bad_request without a hash' do
post submissions_url
assert_response :bad_request
end

test 'create submission within course' do
attrs = generate_attr_hash
course = courses(:course1)
Expand Down

0 comments on commit 8c86abc

Please sign in to comment.