From 3b490c8c482991d162551769c5c3d84b85c262a8 Mon Sep 17 00:00:00 2001 From: Cuong Phung Manh Date: Tue, 22 Oct 2024 15:52:18 +0800 Subject: [PATCH] test(user-service): add missing test to improve coverage --- .../parse_invitation_concern.rb | 6 ++-- .../course/user_invitation_service_spec.rb | 13 +++++++ .../course/user_registration_service_spec.rb | 34 +++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/app/services/concerns/course/user_invitation_service/parse_invitation_concern.rb b/app/services/concerns/course/user_invitation_service/parse_invitation_concern.rb index 4f76cea1daa..97408fae7d0 100644 --- a/app/services/concerns/course/user_invitation_service/parse_invitation_concern.rb +++ b/app/services/concerns/course/user_invitation_service/parse_invitation_concern.rb @@ -79,7 +79,9 @@ def restrict_invitee_role(users) # @param [Hash] users The attributes from the client. # @return [Array] Array of users to be invited def parse_from_form(users) - users.map do |(_, value)| + users.compact.map do |(_, value)| + next if value.nil? + name = value[:name].presence || value[:email] phantom = ActiveRecord::Type::Boolean.new.cast(value[:phantom]) { name: name, @@ -87,7 +89,7 @@ def parse_from_form(users) role: value[:role], phantom: phantom, timeline_algorithm: value[:timeline_algorithm] } - end + end.compact end # Loads the given file, and entries with blanks in either fields are ignored. diff --git a/spec/services/course/user_invitation_service_spec.rb b/spec/services/course/user_invitation_service_spec.rb index 662e8eb7384..296e7027a8d 100644 --- a/spec/services/course/user_invitation_service_spec.rb +++ b/spec/services/course/user_invitation_service_spec.rb @@ -181,6 +181,19 @@ def invite expect(errors.first[:email].first).to match(/invalid/) end end + + context 'when a user is soft-deleted and restored' do + let(:deleted_user) { create(:course_student, course: course, deleted_at: Time.zone.now).user } + let(:new_user_attributes) do + [{ name: deleted_user.name, email: deleted_user.email, role: :student }] + end + + it 'restores the soft-deleted user' do + subject.invite(new_user_attributes) + restored_user = CourseUser.with_deleted.find_by(course_id: course.id, user_id: deleted_user.id) + expect(restored_user).not_to be_nil + end + end end describe '#resend_invitation' do diff --git a/spec/services/course/user_registration_service_spec.rb b/spec/services/course/user_registration_service_spec.rb index 1bfe5011293..529eda1ddcd 100644 --- a/spec/services/course/user_registration_service_spec.rb +++ b/spec/services/course/user_registration_service_spec.rb @@ -260,5 +260,39 @@ def self.registration_with_registration_code expect(registration.course_user).to be_present end end + + describe '#find_or_create_course_user!' do + context 'when there is an existing soft-deleted CourseUser' do + let!(:deleted_course_user) do + create(:course_user, course: course, user: user, name: 'Old Name', role: :student, deleted_at: Time.zone.now) + end + let(:registration) { Course::Registration.new(course: course, user: user) } + + it 'restores and updates the soft-deleted CourseUser' do + subject.send(:find_or_create_course_user!, registration) + restored_user = CourseUser.find_by(course: course, user: user) + + expect(restored_user).to be_present + expect(restored_user.deleted_at).to be_nil + expect(restored_user.name).to eq(user.name) + expect(restored_user.role).to eq('student') + end + end + + context 'when there is no existing soft-deleted CourseUser' do + let(:registration) { Course::Registration.new(course: course, user: user) } + + it 'creates a new CourseUser' do + expect do + subject.send(:find_or_create_course_user!, registration) + end.to(change { CourseUser.count }) + + new_user = CourseUser.find_by(course: course, user: user) + expect(new_user).to be_present + expect(new_user.name).to eq(user.name) + expect(new_user.role).to eq('student') + end + end + end end end