Skip to content

Commit 1badbdc

Browse files
committed
Skip Salesforce integration specs when Heroku Connect DB is unavailable
Step 1: Tag all three Salesforce job specs with requires_salesforce_db: true. Add a filter_run_excluding rule in rails_helper so these specs are automatically skipped when SALESFORCE_CONNECT_DB is not set (i.e. the developer is running tests locally without the heroku-connect Docker service). CI and Docker environments, which set SALESFORCE_CONNECT_DB, continue to run the full Salesforce spec suite unchanged. Step 2: Make SALESFORCE_ENABLED opt-in (default false) Previously salesforce_sync? returned true unless SALESFORCE_ENABLED was explicitly set to 'false'. This caused GoodJob (which runs inline in test mode) to execute SchoolSyncJob and RoleSyncJob immediately whenever a school or role was created in any test — hitting Salesforce tables that don't exist in the test database. Change the check to opt-in: salesforce_sync? now returns true only when SALESFORCE_ENABLED == 'true'. Production and Docker setups already set this explicitly, so behaviour there is unchanged. Update the model specs to set SALESFORCE_ENABLED=true for the describe block that asserts jobs are enqueued, and update the feature flags spec to reflect the new default-false behaviour. Step 3: Guard after_commit callbacks with feature flag condition Move the FeatureFlags.salesforce_sync? check (and the student? check for Role) from inside do_salesforce_sync into the after_commit :if condition. This prevents the callback from firing at all when Salesforce sync is disabled, avoiding any interaction with the queue adapter or Salesforce models in tests that don't enable the feature.
1 parent 1ecfb53 commit 1badbdc

10 files changed

Lines changed: 20 additions & 13 deletions

File tree

app/models/role.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Role < ApplicationRecord
1616
}
1717
)
1818

19-
after_commit :do_salesforce_sync, on: %i[create update]
19+
after_commit :do_salesforce_sync, on: %i[create update], if: -> { FeatureFlags.salesforce_sync? && !student? }
2020

2121
private
2222

@@ -42,9 +42,6 @@ def users_can_only_have_roles_in_one_school
4242
end
4343

4444
def do_salesforce_sync
45-
return unless FeatureFlags.salesforce_sync?
46-
return if student? # We never sync student roles to Salesforce
47-
4845
Salesforce::RoleSyncJob.perform_later(role_id: id)
4946
end
5047
end

app/models/school.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class School < ApplicationRecord
5555

5656
after_create :generate_code!
5757

58-
after_commit :do_salesforce_sync, on: %i[create update]
58+
after_commit :do_salesforce_sync, on: %i[create update], if: -> { FeatureFlags.salesforce_sync? }
5959

6060
def self.find_for_user!(user)
6161
school = Role.find_by(user_id: user.id)&.school || find_by(creator_id: user.id, rejected_at: nil)
@@ -174,8 +174,6 @@ def format_uk_postal_code
174174
end
175175

176176
def do_salesforce_sync
177-
return unless FeatureFlags.salesforce_sync?
178-
179177
Salesforce::SchoolSyncJob.perform_later(school_id: id)
180178
Salesforce::ContactSyncJob.perform_later(school_id: id)
181179
end

lib/feature_flags.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ def self.immediate_school_onboarding?
66
end
77

88
def self.salesforce_sync?
9-
ENV['SALESFORCE_ENABLED'] != 'false'
9+
ENV['SALESFORCE_ENABLED'] == 'true'
1010
end
1111
end

spec/jobs/salesforce/contact_sync_job_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'rails_helper'
44

5-
RSpec.describe Salesforce::ContactSyncJob do
5+
RSpec.describe Salesforce::ContactSyncJob, requires_salesforce_db: true do
66
subject(:perform_job) { described_class.perform_now(school_id: school.id) }
77

88
let(:school) { create(:school, creator_agree_to_ux_contact: true) }

spec/jobs/salesforce/role_sync_job_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'rails_helper'
44

5-
RSpec.describe Salesforce::RoleSyncJob do
5+
RSpec.describe Salesforce::RoleSyncJob, requires_salesforce_db: true do
66
subject(:perform_job) { described_class.perform_now(role_id: role.id) }
77

88
let(:role) { create(:role) }

spec/jobs/salesforce/school_sync_job_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'rails_helper'
44

5-
RSpec.describe Salesforce::SchoolSyncJob do
5+
RSpec.describe Salesforce::SchoolSyncJob, requires_salesforce_db: true do
66
subject(:perform_job) { described_class.perform_now(school_id: school.id) }
77

88
let(:school) { create(:school) }

spec/lib/feature_flags_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
end
3737

3838
describe '.salesforce_sync?' do
39-
it 'returns true when ENV is not set' do
39+
it 'returns false when ENV is not set' do
4040
ClimateControl.modify(SALESFORCE_ENABLED: nil) do
41-
expect(described_class.salesforce_sync?).to be(true)
41+
expect(described_class.salesforce_sync?).to be(false)
4242
end
4343
end
4444

spec/models/role_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@
167167
end
168168

169169
describe 'salesforce sync' do
170+
around do |example|
171+
ClimateControl.modify(SALESFORCE_ENABLED: 'true') { example.run }
172+
end
173+
170174
it 'enqueues a Salesforce::RoleSyncJob on create' do
171175
expect { create(:role) }.to have_enqueued_job(Salesforce::RoleSyncJob)
172176
end

spec/models/school_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,10 @@
629629
end
630630

631631
describe 'salesforce sync' do
632+
around do |example|
633+
ClimateControl.modify(SALESFORCE_ENABLED: 'true') { example.run }
634+
end
635+
632636
it 'enqueues Salesforce::SchoolSyncJob on create' do
633637
expect { create(:school) }.to have_enqueued_job(Salesforce::SchoolSyncJob)
634638
end

spec/rails_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
metadata[:type] = :graphql_query
7373
end
7474

75+
# Salesforce integration specs require the Heroku Connect service (i.e. SALESFORCE_CONNECT_DB
76+
# must be set). Skip them automatically when running locally without the service.
77+
config.filter_run_excluding requires_salesforce_db: true unless ENV.key?('SALESFORCE_CONNECT_DB')
78+
7579
# Filter lines from Rails gems in backtraces.
7680
config.filter_rails_from_backtrace!
7781
# arbitrary gems may also be filtered via:

0 commit comments

Comments
 (0)