diff --git a/app/models/enrollment.rb b/app/models/enrollment.rb index 6356e70238a..3ef9d208e1b 100644 --- a/app/models/enrollment.rb +++ b/app/models/enrollment.rb @@ -1573,6 +1573,7 @@ class Enrollment < ActiveRecord::Base end def sync_microsoft_group + return if self.type == 'StudentViewEnrollment' return unless self.root_account.feature_enabled?(:microsoft_group_enrollments_syncing) return unless self.root_account.settings[:microsoft_sync_enabled] diff --git a/lib/microsoft_sync/syncer_steps.rb b/lib/microsoft_sync/syncer_steps.rb index c28db5f5809..81fa0319b28 100644 --- a/lib/microsoft_sync/syncer_steps.rb +++ b/lib/microsoft_sync/syncer_steps.rb @@ -363,7 +363,7 @@ module MicrosoftSync mappings.each { |user_id, aad_id| diff.set_member_mapping(user_id, aad_id) } users_with_mappings = mappings.map(&:first) - enrollments = Enrollment.active + enrollments = Enrollment.active.not_fake .where(course: course, user_id: users_with_mappings) .pluck(:user_id, :type) enrollments.each { |user_id, enrollment_type| diff.set_local_member(user_id, enrollment_type) } diff --git a/spec/lib/microsoft_sync/syncer_steps_spec.rb b/spec/lib/microsoft_sync/syncer_steps_spec.rb index 2f5ccafd6fa..568bb42297f 100644 --- a/spec/lib/microsoft_sync/syncer_steps_spec.rb +++ b/spec/lib/microsoft_sync/syncer_steps_spec.rb @@ -858,6 +858,12 @@ describe MicrosoftSync::SyncerSteps do expect(diff).to have_received(:set_local_member).with(students[2].id, 'StudentEnrollment') end + it 'ignores StudentViewEnrollment (fake) enrollments' do + Enrollment.where(course: course, user: students[0]).update_all(type: 'StudentViewEnrollment') + subject + expect(diff).to_not have_received(:set_local_member).with(students[0].id, anything) + end + it_behaves_like 'a step that executes a diff' do # the latter it_behaves_like must be inside this one because 'a step # that executes a diff' makes the diff return actions. We need actions diff --git a/spec/models/enrollment_spec.rb b/spec/models/enrollment_spec.rb index 3f72a696dfe..96400c892b8 100644 --- a/spec/models/enrollment_spec.rb +++ b/spec/models/enrollment_spec.rb @@ -3478,6 +3478,7 @@ describe Enrollment do describe '#sync_microsoft_group' do let(:course) { course_factory } + let(:enrollment_type) { 'StudentEnrollment' } before :each do MicrosoftSync::Group.create!(course: course) @@ -3486,7 +3487,7 @@ describe Enrollment do # enroll user without running callbacks like update_user_account_associations, # so that the only jobs getting enqueued are the MSFT sync group type def enroll_user - course.enroll_user(user_factory, 'StudentViewEnrollment', skip_touch_user: true) + course.enroll_user(user_factory, enrollment_type, skip_touch_user: true) end context 'when feature flag is off' do @@ -3495,7 +3496,7 @@ describe Enrollment do end it 'does not enqueue a job' do - expect { enroll_user }.not_to change { Delayed::Job.count } + expect { enroll_user }.to not_change { Delayed::Job.where(tag: 'MicrosoftSync::StateMachineJob#run_later').count } end end @@ -3511,7 +3512,7 @@ describe Enrollment do end it 'does not enqueue a job' do - expect { enroll_user }.not_to change { Delayed::Job.count } + expect { enroll_user }.to not_change { Delayed::Job.where(tag: 'MicrosoftSync::StateMachineJob#run_later').count } end end @@ -3522,13 +3523,21 @@ describe Enrollment do end it 'should enqueue a job' do - expect { enroll_user }.to change { Delayed::Job.count }.by 1 + expect { enroll_user }.to change { Delayed::Job.where(tag: 'MicrosoftSync::StateMachineJob#run_later').count }.by 1 end it 'calls MicrosoftSync::Group#enqueue_future_partial_sync' do expect_any_instance_of(MicrosoftSync::Group).to receive(:enqueue_future_partial_sync) enroll_user end + + context 'when the enrollment is a StudentViewEnrollment' do + let(:enrollment_type) { 'StudentViewEnrollment' } + + it 'should not enqueue a job' do + expect { enroll_user }.to not_change { Delayed::Job.where(tag: 'MicrosoftSync::StateMachineJob#run_later').count } + end + end end end end