-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1152 from alphagov/refactor
Don't fall back to fixture data in production
- Loading branch information
Showing
4 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |