Skip to content

Commit

Permalink
test(user-service): add missing test to improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
phungmanhcuong committed Oct 22, 2024
1 parent 3beb0f5 commit 3b490c8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,17 @@ def restrict_invitee_role(users)
# @param [Hash] users The attributes from the client.
# @return [Array<Hash>] 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,
email: value[:email],
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.
Expand Down
13 changes: 13 additions & 0 deletions spec/services/course/user_invitation_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions spec/services/course/user_registration_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3b490c8

Please sign in to comment.