Skip to content

Commit f6d4f03

Browse files
authored
fix checking for blank UUID while importing and exporting (#2801)
1 parent 3d58297 commit f6d4f03

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

app/controllers/exercises_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ def import_uuid_check
150150
user = user_from_api_key
151151
return render json: {}, status: :unauthorized if user.nil?
152152

153-
uuid = params[:uuid]
153+
uuid = params[:uuid].presence
154154
exercise = Exercise.find_by(uuid:)
155155

156-
return render json: {uuid_found: false} if exercise.nil?
156+
return render json: {uuid_found: false} if uuid.blank? || exercise.nil?
157157
return render json: {uuid_found: true, update_right: false} unless ExercisePolicy.new(user, exercise).update?
158158

159159
render json: {uuid_found: true, update_right: true}

app/services/exercise_service/check_external.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ def initialize(uuid:, codeharbor_link:)
99
end
1010

1111
def execute
12+
return {error: false, message: message(false, true)} if @uuid.blank?
13+
1214
response = self.class.connection.post @codeharbor_link.check_uuid_url do |req|
1315
req.headers['Content-Type'] = 'application/json'
1416
req.headers['Authorization'] = "Bearer #{@codeharbor_link.api_key}"

spec/controllers/exercises_controller_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,18 @@
463463
expect(response.parsed_body.symbolize_keys[:uuid_found]).to be false
464464
end
465465
end
466+
467+
context 'when uuid is nil' do
468+
let(:exercise) { create(:dummy, uuid: nil) }
469+
let(:uuid) { nil }
470+
471+
it 'renders correct response' do
472+
post_request
473+
expect(response).to have_http_status(:success)
474+
475+
expect(response.parsed_body.symbolize_keys[:uuid_found]).to be false
476+
end
477+
end
466478
end
467479

468480
describe 'POST #import_task' do

spec/services/exercise_service/check_external_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,17 @@
8484
expect(check_external_service).to eql(error: true, message: I18n.t('exercises.export_codeharbor.error'))
8585
end
8686
end
87+
88+
context 'with no uuid' do
89+
let(:uuid) { nil }
90+
91+
it 'does not make a request' do
92+
expect(check_external_service).not_to have_requested(:post, codeharbor_link.check_uuid_url)
93+
end
94+
95+
it 'returns the correct hash' do
96+
expect(check_external_service).to eql(error: false, message: I18n.t('exercises.export_codeharbor.check.no_task'))
97+
end
98+
end
8799
end
88100
end

0 commit comments

Comments
 (0)