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 Failing Course Deletion if Approved/Rejected Enrol Requests Exist #7642

Open
wants to merge 2 commits into
base: master
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
2 changes: 1 addition & 1 deletion app/controllers/course/enrol_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def create
# Allow users to withdraw their requests to register for a course that are pending
# approval/rejection.
def destroy
if @enrol_request.destroy
if @enrol_request.validate_before_destroy && @enrol_request.destroy
head :ok
else
render json: { errors: @enrol_request.errors.full_messages.to_sentence }, status: :bad_request
Expand Down
16 changes: 7 additions & 9 deletions app/models/course/enrol_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class Course::EnrolRequest < ApplicationRecord
validate :validate_no_duplicate_pending_request, on: :create
validates :workflow_state, length: { maximum: 255 }, presence: true

before_destroy :validate_before_destroy

belongs_to :course, inverse_of: :enrol_requests
belongs_to :user, inverse_of: :course_enrol_requests
belongs_to :confirmer, class_name: 'User', inverse_of: nil, optional: true
Expand All @@ -28,6 +26,13 @@ class Course::EnrolRequest < ApplicationRecord

scope :pending, -> { where(workflow_state: :pending) }

def validate_before_destroy
return true if workflow_state == 'pending'

errors.add(:base, :deletion)
false
end

private

# Ensure that there are no enrol requests by users in the course.
Expand All @@ -40,13 +45,6 @@ def validate_no_duplicate_pending_request
errors.add(:base, :existing_pending_request) if existing_request
end

def validate_before_destroy
return true if workflow_state == 'pending'

errors.add(:base, :deletion)
throw(:abort)
end

def approve(_ = nil)
self.confirmed_at = Time.zone.now
self.confirmer = User.stamper
Expand Down
18 changes: 6 additions & 12 deletions spec/models/course/enrol_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,19 @@
let!(:approved_request) { create(:course_enrol_request, :approved, course: course, user: user) }
subject { approved_request.destroy }

it 'cannot be destroyed' do
it 'can be destroyed' do
subject
expect(approved_request).not_to be_destroyed
expect(approved_request.errors[:base]).to include(
'activerecord.errors.models.course/enrol_request.attributes.base.deletion'
)
expect(approved_request).to be_destroyed
end
end

context 'when a request is already rejected' do
let!(:rejected__request) { create(:course_enrol_request, :rejected, course: course, user: user) }
subject { rejected__request.destroy }
let!(:rejected_request) { create(:course_enrol_request, :rejected, course: course, user: user) }
subject { rejected_request.destroy }

it 'cannot be destroyed' do
it 'can be destroyed' do
subject
expect(rejected__request).not_to be_destroyed
expect(rejected__request.errors[:base]).to include(
'activerecord.errors.models.course/enrol_request.attributes.base.deletion'
)
expect(rejected_request).to be_destroyed
end
end

Expand Down