Skip to content
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

Fix: #6087 Display specific error message for course slug collision (revision) #6168

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
12 changes: 8 additions & 4 deletions app/assets/javascripts/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,14 @@ const API = {
this.status = response.statusText;
SentryLogger.obj = this.obj;
SentryLogger.status = this.status;
Sentry.captureMessage('saveCourse failed', {
level: 'error',
extra: SentryLogger
});
try {
Sentry.captureMessage('saveCourse failed', {
level: 'error',
extra: SentryLogger
});
} catch (error) {
console.error('Sentry.captureMessage failed:', error);
}
response.responseText = data;
throw response;
}
Expand Down
11 changes: 8 additions & 3 deletions app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ def update
ensure_passcode_set
UpdateCourseWorker.schedule_edits(course: @course, editing_user: current_user)
render json: { course: @course }
rescue Course::DuplicateCourseSlugError => e
render_error(:unprocessable_entity, 'courses.error.duplicate_slug', slug: e.slug)
rescue Wiki::InvalidWikiError => e
message = I18n.t('courses.error.invalid_wiki', domain: e.domain)
render json: { errors: e, message: },
status: :not_found
render_error(:not_found, 'courses.error.invalid_wiki', domain: e.domain)
end

def render_error(status, error_key, **params)
render json: { errors: params, message: I18n.t(error_key, **params) },
status:
end

def destroy
Expand Down
18 changes: 18 additions & 0 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,22 @@ def check_course_times
def set_needs_update
self.needs_update = true if start_changed?
end

# Handle duplicate slug collision
validate :check_duplicate_slug, on: :update

def check_duplicate_slug
return unless Course.where(slug: slug).where.not(id: id).exists?

raise DuplicateCourseSlugError, slug
end

class DuplicateCourseSlugError < StandardError
attr_reader :slug

def initialize(slug, msg = 'Duplicate Slug')
@slug = slug
super(msg)
end
end
end
5 changes: 4 additions & 1 deletion spec/lib/revision_stat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@

context 'course id' do
context 'not for this course' do
before { course.update(id: 1000) }
before do
allow_any_instance_of(Course).to receive(:check_duplicate_slug).and_return(true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test shouldn't require working around the validation; a better strategy would be to update it so that it doesn't trigger the error when the validation is run. Maybe that means including an updated slug along with the ID.

However, this test is pretty confusing and I don't think it does anything useful. Let's just remove it.

course.update(id: 1000)
end

it 'does not include in scope' do
expect(subject).to eq(0)
Expand Down
20 changes: 20 additions & 0 deletions spec/models/course_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,26 @@
end
end

describe 'validations' do
let!(:existing_course) { create(:course, slug: 'existing-slug') }

context 'when updating a course' do
let!(:duplicate_course) { create(:course, slug: 'duplicate-slug') }

it 'raises Course::DuplicateCourseSlugError if the slug is duplicated' do
# Attempting to update the course with an existing slug should raise an error
expect do
duplicate_course.update!(slug: existing_course.slug)
end.to raise_error(Course::DuplicateCourseSlugError, 'Duplicate Slug')
end

it 'does not raise an error if the slug is unique' do
# Updating the course with a unique slug should pass without errors
expect { existing_course.update!(slug: 'new-unique-slug') }.not_to raise_error
end
end
end

describe '#approved' do
let(:course) { create(:course) }

Expand Down
Loading