|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe Salesforce::RoleSyncJob do |
| 6 | + subject(:perform_job) { described_class.perform_now(role_id: role.id) } |
| 7 | + |
| 8 | + let(:role) { create(:role) } |
| 9 | + |
| 10 | + context 'when the job has run' do |
| 11 | + before { perform_job } |
| 12 | + |
| 13 | + it 'syncs all FIELD_MAPPINGS to the correct role values' do |
| 14 | + sf_role = Salesforce::Role.find_by(affiliation_id__c: role.id) |
| 15 | + described_class::FIELD_MAPPINGS.each do |sf_field, role_field| |
| 16 | + expected = Salesforce::Role.type_for_attribute(sf_field).cast(role.send(role_field)) |
| 17 | + expect(sf_role.send(sf_field)).to eq(expected), |
| 18 | + "Expected #{sf_field} to equal role.#{role_field}" |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + context 'when the Salesforce role fails to save' do |
| 24 | + let(:sf_role) { instance_double(Salesforce::Role) } |
| 25 | + |
| 26 | + before do |
| 27 | + allow(Salesforce::Role).to receive(:find_or_initialize_by).with(affiliation_id__c: role.id).and_return(sf_role) |
| 28 | + allow(sf_role).to receive(:attributes=) |
| 29 | + allow(sf_role).to receive(:save!).and_raise(ActiveRecord::RecordInvalid) |
| 30 | + end |
| 31 | + |
| 32 | + it 'raises an error' do |
| 33 | + expect { perform_job }.to raise_error ActiveRecord::RecordInvalid |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + context 'when the role is a student role' do |
| 38 | + let(:role) { create(:student_role) } |
| 39 | + |
| 40 | + it 'does not create a Salesforce role record' do |
| 41 | + perform_job |
| 42 | + expect(Salesforce::Role.find_by(affiliation_id__c: role.id)).to be_nil |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + context 'when SALESFORCE_ENABLED is false' do |
| 47 | + around do |example| |
| 48 | + ClimateControl.modify(SALESFORCE_ENABLED: 'false') do |
| 49 | + example.run |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + it 'discards the job without syncing' do |
| 54 | + perform_job |
| 55 | + expect(Salesforce::Role.find_by(affiliation_id__c: role.id)).to be_nil |
| 56 | + end |
| 57 | + end |
| 58 | +end |
0 commit comments