From dfbcacc09ab242294317d0f8153f008741de0db1 Mon Sep 17 00:00:00 2001 From: Keith Lawrence Date: Mon, 10 Jul 2023 15:15:38 +0100 Subject: [PATCH] Use GOVUK_ENVIRONMENT as a backstop for DIGITAL_IDENTITY_ENVIRONMENT - If DIGITAL_IDENTITY_ENVIRONMENT is missing, use GOVUK_ENVIRONMENT - Unless it's set to "production", in which case ignore. --- lib/govuk_personalisation/urls.rb | 11 +++++-- spec/govuk_personalisation/urls_spec.rb | 44 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/lib/govuk_personalisation/urls.rb b/lib/govuk_personalisation/urls.rb index b2dfff9..6d30b7e 100644 --- a/lib/govuk_personalisation/urls.rb +++ b/lib/govuk_personalisation/urls.rb @@ -95,11 +95,16 @@ def self.find_external_url(var:, url:) # # @return [String] the domain def self.digital_identity_domain - environment = ENV["DIGITAL_IDENTITY_ENVIRONMENT"] - if environment - "home.#{environment}.account.gov.uk" + if digital_identity_environment + "home.#{digital_identity_environment}.account.gov.uk" else "home.account.gov.uk" end end + + def self.digital_identity_environment + return ENV["DIGITAL_IDENTITY_ENVIRONMENT"] if ENV["DIGITAL_IDENTITY_ENVIRONMENT"] + + ENV["GOVUK_ENVIRONMENT"] == "production" ? nil : ENV["GOVUK_ENVIRONMENT"] + end end diff --git a/spec/govuk_personalisation/urls_spec.rb b/spec/govuk_personalisation/urls_spec.rb index 363fee0..ebaf417 100644 --- a/spec/govuk_personalisation/urls_spec.rb +++ b/spec/govuk_personalisation/urls_spec.rb @@ -61,4 +61,48 @@ end end end + + describe "#digital_identity_domain" do + subject(:url) { described_class.digital_identity_domain } + + it "returns the domain hostname" do + expect(url).to eq("home.account.gov.uk") + end + + context "when the DIGITAL_IDENTITY_ENVIRONMENT env var is set" do + around do |example| + ClimateControl.modify("DIGITAL_IDENTITY_ENVIRONMENT" => "production") do + example.run + end + end + + it "constructs the hostname from the env var" do + expect(url).to eq("home.production.account.gov.uk") + end + end + + context "when the GOVUK_ENVIRONMENT env var is set to production" do + around do |example| + ClimateControl.modify("GOVUK_ENVIRONMENT" => "production") do + example.run + end + end + + it "returns the domain hostname" do + expect(url).to eq("home.account.gov.uk") + end + end + + context "when the GOVUK_ENVIRONMENT env var is not set to production" do + around do |example| + ClimateControl.modify("GOVUK_ENVIRONMENT" => "test") do + example.run + end + end + + it "constructs the hostname from the env var" do + expect(url).to eq("home.test.account.gov.uk") + end + end + end end