Skip to content

Commit

Permalink
Use validation helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzz212zzZ committed Apr 26, 2024
1 parent adc3bc1 commit e94a196
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions app/controllers/api/v1/lmss_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Api
module V1
class LmssController < BaseController
include CanvasValidationHelper
before_action :validate_ids!, only: [:create]

def index
Expand Down Expand Up @@ -56,8 +57,8 @@ def validate_ids!
render json: { error: e.message }, status: :bad_request
return
else
unless params[:course_id].to_s.match?(/\A\d+\z/) && params[:lms_id].to_s.match?(/\A\d+\z/)
render json: { error: 'course_id and lms_id must be integers' }, status: :bad_request
unless is_valid_course_id(params[:course_id].to_i) && is_valid_lms_id(params[:lms_id].to_i)
render json: { error: 'Invalid course_id or lms_id' }, status: :bad_request
return
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/api/v1/lmss_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def json_response
end
end

context 'when course_id and lms_id are not integers' do
context 'when course_id and lms_id are not invalid' do
it 'returns status :bad_request' do
post :create, params: { course_id: 'not_an_int', lms_id: 'also_not_an_int', external_course_id: @external_course_id }
post :create, params: { course_id: '-1', lms_id: '-1', external_course_id: @external_course_id }
expect(response).to have_http_status(:bad_request)
expect(response.body).to include('course_id and lms_id must be integers')
expect(response.body).to include('Invalid course_id or lms_id')
end
end

Expand Down

0 comments on commit e94a196

Please sign in to comment.