Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fall back to fixture data in production #1152

Merged
merged 1 commit into from
Aug 2, 2023
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
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": "billy.director@email.uk",
},
],
"verify_contacts": {
"ida_support_email": "idasupport@email.uk",
"out_of_hours_email": "outofhours@email.uk",
},
}

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("idasupport@email.uk")
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
Loading