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
11 changes: 9 additions & 2 deletions app/services/service_provider_seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def run

def service_providers
file = Rails.root.join('config', 'service_providers.yml').read
file.gsub!('%{env}', deploy_env) if deploy_env
content = ERB.new(file).result
YAML.safe_load(content).fetch(rails_env)
rescue Psych::SyntaxError => syntax_error
Expand All @@ -55,10 +56,16 @@ def write_service_provider?(config)
return true if rails_env != 'production'

restrict_env = config['restrict_to_deploy_env']
in_prod = deploy_env == 'prod'
in_sandbox = !%w[prod staging].include?(deploy_env)
in_staging = deploy_env == 'staging'

is_production_or_has_a_restriction = (deploy_env == 'prod' || restrict_env.present?)
return true if restrict_env == 'prod' && in_prod
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please double check me here but I'm 99% that lines 63-68 are the same, functionally, as the previous code, with the addition of line 65 (restrict_to_deploy_env set to sandbox). I found the old code hard to read / reason about so even though this is more verbose I think it's a bit clearer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very minor, but my immediate thought when glancing at this was that it could be rewritten as a case statement. Either way works, though!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I generally like guard clauses over case statements for Ruby but I had the same thought myself 😄

return true if restrict_env == 'staging' && in_staging
return true if restrict_env == 'sandbox' && in_sandbox
return true if restrict_env.blank? && !in_prod

!is_production_or_has_a_restriction || (restrict_env == deploy_env)
false
end

def check_for_missing_sps
Expand Down
6 changes: 6 additions & 0 deletions config/service_providers.localdev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,9 @@ production:

'urn:gov:login:test-providers:fake-unrestricted-sp':
friendly_name: 'Fake/Test stub SP, env unrestricted'

'urn:gov:login:test-providers:fake-sandbox-sp':
friendly_name: 'Fake/Test stub SP, sandbox SP with env-specific hostname'
redirect_uris:
- 'https://%{env}.example.com'
restrict_to_deploy_env: 'sandbox'
58 changes: 32 additions & 26 deletions spec/services/service_provider_seeder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,32 @@

context 'when running in a production environment' do
let(:rails_env) { 'production' }
let(:sandbox_issuer) { 'urn:gov:login:test-providers:fake-sandbox-sp' }
let(:staging_issuer) { 'urn:gov:login:test-providers:fake-staging-sp' }
let(:prod_issuer) { 'urn:gov:login:test-providers:fake-prod-sp' }
let(:unrestricted_issuer) { 'urn:gov:login:test-providers:fake-unrestricted-sp' }

context 'when %{env} is present in the config file' do
let(:deploy_env) { 'dev' }

it 'is replaced with the deploy_env' do
run

sp = ServiceProvider.find_by(issuer: sandbox_issuer)
expect(sp.redirect_uris).to eq(%w[https://dev.example.com])
end
end

context 'in prod' do
let(:deploy_env) { 'prod' }

it 'only writes configs with restrict_to_deploy_env for prod' do
run

# restrict_to_deploy_env: prod
expect(ServiceProvider.find_by(issuer: 'urn:gov:login:test-providers:fake-prod-sp')).
to be_present

# restrict_to_deploy_env: staging
expect(ServiceProvider.find_by(issuer: 'urn:gov:login:test-providers:fake-staging-sp')).
to eq(nil)

# restrict_to_deploy_env: nil
expect(
ServiceProvider.find_by(issuer: 'urn:gov:login:test-providers:fake-unrestricted-sp'),
).to eq(nil)
expect(ServiceProvider.find_by(issuer: prod_issuer)).to be_present
expect(ServiceProvider.find_by(issuer: sandbox_issuer)).not_to be_present
expect(ServiceProvider.find_by(issuer: staging_issuer)).not_to be_present
expect(ServiceProvider.find_by(issuer: unrestricted_issuer)).not_to be_present
end

it 'sends New Relic an error if the DB has an SP not in the config' do
Expand All @@ -114,20 +121,10 @@
it 'only writes configs with restrict_to_deploy_env for that env, or no restrictions' do
run

# restrict_to_deploy_env: prod
expect(ServiceProvider.find_by(issuer: 'urn:gov:login:test-providers:fake-prod-sp')).
to eq(nil)

# restrict_to_deploy_env: staging
expect(ServiceProvider.find_by(issuer: 'urn:gov:login:test-providers:fake-staging-sp')).
to be_present

# restrict_to_deploy_env: nil
expect(
ServiceProvider.find_by(
issuer: 'urn:gov:login:test-providers:fake-unrestricted-sp',
),
).to be_present
expect(ServiceProvider.find_by(issuer: staging_issuer)).to be_present
expect(ServiceProvider.find_by(issuer: unrestricted_issuer)).to be_present
expect(ServiceProvider.find_by(issuer: sandbox_issuer)).not_to be_present
expect(ServiceProvider.find_by(issuer: prod_issuer)).not_to be_present
end

it 'sends New Relic an error if the DB has an SP not in the config' do
Expand All @@ -142,6 +139,15 @@
context 'in another environment' do
let(:deploy_env) { 'int' }

it 'only writes configs with restrict_to_deploy_env for sandbox' do
run

expect(ServiceProvider.find_by(issuer: sandbox_issuer)).to be_present
expect(ServiceProvider.find_by(issuer: unrestricted_issuer)).to be_present
expect(ServiceProvider.find_by(issuer: staging_issuer)).not_to be_present
expect(ServiceProvider.find_by(issuer: prod_issuer)).not_to be_present
end

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')
Expand Down