Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/services/service_provider_seeder.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Update ServiceProvider from config/service_providers.yml (all environments in rake db:seed)
class ServiceProviderSeeder
class ExtraServiceProviderError < StandardError; end

def initialize(rails_env: Rails.env, deploy_env: LoginGov::Hostdata.env)
@rails_env = rails_env
@deploy_env = deploy_env
end

def run
check_for_missing_sps

service_providers.each do |issuer, config|
next unless write_service_provider?(config)
ServiceProvider.find_or_create_by!(issuer: issuer) do |sp|
Expand Down Expand Up @@ -50,4 +54,19 @@ def write_service_provider?(config)

!is_production_or_has_a_restriction || (restrict_env == deploy_env)
end

def check_for_missing_sps
return unless %w[prod staging].include? deploy_env

sps_in_db = ServiceProvider.pluck(:issuer)
sps_in_yaml = service_providers.keys
extra_sps = sps_in_db - sps_in_yaml

return if extra_sps.empty?

extra_sp_error = ExtraServiceProviderError.new(
"Extra service providers found in DB: #{extra_sps.join(', ')}",
)
NewRelic::Agent.notice_error(extra_sp_error)
end
end
30 changes: 29 additions & 1 deletion spec/services/service_provider_seeder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,17 @@
ServiceProvider.find_by(issuer: 'urn:gov:login:test-providers:fake-unrestricted-sp'),
).to eq(nil)
end

it 'sends New Relic an error if the DB has an SP not in the config' do
allow(NewRelic::Agent).to receive(:notice_error)
create(:service_provider, issuer: 'missing_issuer')
run

expect(NewRelic::Agent).to have_received(:notice_error)
end
end

context 'in another environment' do
context 'in the staging environment' do
let(:deploy_env) { 'staging' }

it 'only writes configs with restrict_to_deploy_env for that env, or no restrictions' do
Expand All @@ -102,6 +110,26 @@
),
).to be_present
end

it 'sends New Relic an error if the DB has an SP not in the config' do
allow(NewRelic::Agent).to receive(:notice_error)
create(:service_provider, issuer: 'missing_issuer')
run

expect(NewRelic::Agent).to have_received(:notice_error)
end
end

context 'in another environment' do
let(:deploy_env) { 'int' }

it 'does not send New Relic an error if the DB has an SP not in the config' do
allow(NewRelic::Agent).to receive(:notice_error)
create(:service_provider, issuer: 'missing_issuer')
run

expect(NewRelic::Agent).not_to have_received(:notice_error)
end
end

context 'when a service provider is invalid' do
Expand Down