require 'spec_helper' RSpec.describe CanvasSync::Processors::ProvisioningReportProcessor do let(:subject) { CanvasSync::Processors::ProvisioningReportProcessor } describe '#process' do it 'process users' do expect { subject.process('spec/support/fixtures/reports/users.csv', { models: ['users'] }, 1) }.to change { User.count }.by(2) end it 'uses a User row with a sis_id' do subject.process('spec/support/fixtures/reports/users.csv', { models: ['users'] }, 1) expect(User.find_by(canvas_id: 2).sis_id).to eq 'sis_id_2' end it 'processes courses' do expect { subject.process('spec/support/fixtures/reports/courses.csv', { models: ['courses'] }, 1) }.to change { Course.count }.by(2) course = Course.first expect(course.start_at).to eq DateTime.parse("2017-03-27 21:53:18") expect(course.end_at).to eq DateTime.parse("2017-04-27 12:21:18") end it 'processes enrollments' do expect { subject.process('spec/support/fixtures/reports/enrollments.csv', { models: ['enrollments'] }, 1) }.to change { Enrollment.count }.by(2) end it 'processes sections' do expect { subject.process('spec/support/fixtures/reports/sections.csv', { models: ['sections'] }, 1) }.to change { Section.count }.by(2) section = Section.first expect(section.start_at).to eq DateTime.parse("2017-03-27 21:53:18") expect(section.end_at).to eq DateTime.parse("2017-03-27 21:53:18") end it 'processes xlist' do subject.process('spec/support/fixtures/reports/sections.csv', { models: ['sections'] }, 1) expect(Section.find_by(canvas_id: 2).name).to eq "Lame Section" subject.process('spec/support/fixtures/reports/xlist.csv', { models: ['xlist'] }, 1) expect(Section.where.not(canvas_nonxlist_course_id: nil).count).to eq 1 cross_listed_section = Section.where.not(canvas_nonxlist_course_id: nil).first expect(cross_listed_section.canvas_id).to eq 2 expect(cross_listed_section.canvas_nonxlist_course_id).to eq 3 expect(cross_listed_section.canvas_course_id).to eq 2 expect(cross_listed_section.name).to eq "Lame Section" end it 'processes groups' do expect { subject.process('spec/support/fixtures/reports/groups.csv', { models: ['groups'] }, 1) }.to change { Group.count }.by(2) group = Group.find_by_name('Group1') expect(group.workflow_state).to eq 'available' expect(group.canvas_account_id).to eq 1 expect(group.canvas_id).to eq 50 end it 'processes group memberships' do expect { subject.process('spec/support/fixtures/reports/group_memberships.csv', { models: ['group_membership'] }, 1) }.to change { GroupMembership.count }.by(2) group = GroupMembership.find_by_canvas_id(50) expect(group.workflow_state).to eq 'accepted' expect(group.canvas_group_id).to eq 1 expect(group.canvas_user_id).to eq 10 end it 'processes user_observers' do expect { subject.process('spec/support/fixtures/reports/user_observers.csv', { models: ['user_observers'] }, 1) }.to change { UserObserver.count }.by(2) obj = UserObserver.find_by(observed_user_id: 3) expect(obj.workflow_state).to eq 'active' expect(obj.observing_user_id).to eq 21 end it 'processes grading_periods' do expect { subject.process('spec/support/fixtures/reports/grading_periods.csv', { models: ['grading_periods'] }, 1) }.to change { GradingPeriod.count }.by(2) obj = GradingPeriod.find_by(canvas_id: 1) puts obj.inspect expect(obj.title).to eq 'Period 1' expect(obj.weight).to eq 0.2 expect(obj.workflow_state).to eq 'active' end it 'processes grading_period_groups' do expect { subject.process('spec/support/fixtures/reports/grading_period_groups.csv', { models: ['grading_period_groups'] }, 1) }.to change { GradingPeriodGroup.count }.by(1) obj = GradingPeriodGroup.find_by(canvas_id: 1) puts obj.inspect expect(obj.title).to eq 'Test Group' expect(obj.workflow_state).to eq 'active' end it 'model with composite key behaves as expected' do expect { subject.process('spec/support/fixtures/reports/user_observers.csv', { models: ['user_observers'] }, 1) }.to change { UserObserver.count }.by(2) expect { subject.process('spec/support/fixtures/reports/user_observers.csv', { models: ['user_observers'] }, 1) }.to change { UserObserver.count }.by(0) end context 'options[:models] is multiple models' do it 'extracts the ZIP and processes each model' do user_count = User.count course_count = Course.count subject.process('spec/support/fixtures/reports/provisioning_csv', { models: ['courses', 'users'] }, 1) expect(User.count).to eq(user_count + 2) expect(Course.count).to eq(course_count + 2) end end context 'legacy_support is true' do it 'uses the LegacyImporter' do expect(User).to receive(:create_or_update_from_csv).at_least(:once) subject.process('spec/support/fixtures/reports/users.csv', { models: ['users'], legacy_support: true }, 1) end it 'uses the LegacyImporter for specific models' do expect(User).to receive(:create_or_update_from_csv).at_least(:once) expect(Course).to_not receive(:create_or_update_from_csv) subject.process('spec/support/fixtures/reports/users.csv', { models: ['users'], legacy_support: ['users'] }, 1) subject.process('spec/support/fixtures/reports/courses.csv', { models: ['courses'], legacy_support: ['users'] }, 1) end end end end