Skip to content

fix checking for blank UUID while importing and exporting #2801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/exercises_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ def import_uuid_check
user = user_from_api_key
return render json: {}, status: :unauthorized if user.nil?

uuid = params[:uuid]
uuid = params[:uuid].presence
exercise = Exercise.find_by(uuid:)

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

render json: {uuid_found: true, update_right: true}
Expand Down
2 changes: 2 additions & 0 deletions app/services/exercise_service/check_external.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def initialize(uuid:, codeharbor_link:)
end

def execute
return {error: false, message: message(false, true)} if @uuid.blank?

response = self.class.connection.post @codeharbor_link.check_uuid_url do |req|
req.headers['Content-Type'] = 'application/json'
req.headers['Authorization'] = "Bearer #{@codeharbor_link.api_key}"
Expand Down
12 changes: 12 additions & 0 deletions spec/controllers/exercises_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,18 @@
expect(response.parsed_body.symbolize_keys[:uuid_found]).to be false
end
end

context 'when uuid is nil' do
let(:exercise) { create(:dummy, uuid: nil) }
let(:uuid) { nil }

it 'renders correct response' do
post_request
expect(response).to have_http_status(:success)

expect(response.parsed_body.symbolize_keys[:uuid_found]).to be false
end
end
end

describe 'POST #import_task' do
Expand Down
12 changes: 12 additions & 0 deletions spec/services/exercise_service/check_external_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,17 @@
expect(check_external_service).to eql(error: true, message: I18n.t('exercises.export_codeharbor.error'))
end
end

context 'with no uuid' do
let(:uuid) { nil }

it 'does not make a request' do
expect(check_external_service).not_to have_requested(:post, codeharbor_link.check_uuid_url)
end

it 'returns the correct hash' do
expect(check_external_service).to eql(error: false, message: I18n.t('exercises.export_codeharbor.check.no_task'))
end
end
end
end