Skip to content

Commit

Permalink
Merge pull request #1152 from alphagov/refactor
Browse files Browse the repository at this point in the history
Don't fall back to fixture data in production
  • Loading branch information
ChrisBAshton committed Aug 2, 2023
2 parents abd7efc + d9d668e commit b474139
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/lib/emergency_contact_details.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
class EmergencyContactDetails
def self.fetch
config_str = ENV["EMERGENCY_CONTACT_DETAILS"] || File.read(Rails.root.join("config/emergency_contact_details.json"))
config_str = ENV["EMERGENCY_CONTACT_DETAILS"]
raise MissingEnvVar if config_str.nil?

config = JSON.parse(config_str)
ActiveSupport::HashWithIndifferentAccess.new(config)
end

class MissingEnvVar < StandardError; end
end
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@
config.hosts += [
"support.dev.gov.uk",
]

ENV["EMERGENCY_CONTACT_DETAILS"] = ENV["EMERGENCY_CONTACT_DETAILS"] || File.read(Rails.root.join("config/emergency_contact_details.json"))
end
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@

# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true

ENV["EMERGENCY_CONTACT_DETAILS"] = ENV["EMERGENCY_CONTACT_DETAILS"] || File.read(Rails.root.join("config/emergency_contact_details.json"))
end
40 changes: 40 additions & 0 deletions spec/lib/emergency_contact_details_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "rails_helper"

describe EmergencyContactDetails do
describe ".fetch" do
it "returns a HashWithIndifferentAccess derived from `ENV['EMERGENCY_CONTACT_DETAILS']`" do
contact_details = {
"current_at": "2014-07-01",
"primary_contacts": {
"national_emergencies": {
"phone": "0555 555 555",
},
},
"secondary_contacts": [
{
"name": "Billy Director",
"role": "Director",
"phone": "05555 555 555",
"email": "[email protected]",
},
],
"verify_contacts": {
"ida_support_email": "[email protected]",
"out_of_hours_email": "[email protected]",
},
}

allow(ENV).to receive(:[]).with(anything)
allow(ENV).to receive(:[]).with("EMERGENCY_CONTACT_DETAILS").and_return(contact_details.to_json)
expect(described_class.fetch).to eq(contact_details.to_h.with_indifferent_access)
expect(described_class.fetch[:verify_contacts][:ida_support_email]).to eq("[email protected]")
end

it "raises an exception if `ENV['EMERGENCY_CONTACT_DETAILS']` is not defined" do
allow(ENV).to receive(:[]).with(anything)
allow(ENV).to receive(:[]).with("EMERGENCY_CONTACT_DETAILS").and_return(nil)

expect { described_class.fetch }.to raise_exception(EmergencyContactDetails::MissingEnvVar)
end
end
end

0 comments on commit b474139

Please sign in to comment.