Skip to content

Commit

Permalink
Decouple tests from GovukEnvironment.production?
Browse files Browse the repository at this point in the history
Wherever we were stubbing GovukEnvironment.name, we now always stub
GovukEnvironment.production? to make the tests less brittle.

I'm planning to change the implementation of
GovukEnvironment.production? in a subsequent commit and this change will
avoid the distraction of a bunch of tests failing.
  • Loading branch information
floehopper committed Aug 16, 2023
1 parent a8cb530 commit 40263fe
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion test/helpers/two_step_verification_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TwoStepVerificationHelperTest < ActionView::TestCase

context "in production" do
setup do
GovukEnvironment.stubs(:name).returns(nil)
GovukEnvironment.stubs(:production?).returns(true)
end

should "not include the environment name" do
Expand Down
11 changes: 6 additions & 5 deletions test/models/noisy_batch_invitation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class NoisyBatchInvitationTest < ActionMailer::TestCase
end
end

context "make_noise when instance_name has been set" do
context "make_noise in non-production environment" do
setup do
GovukEnvironment.stubs(:production?).returns(false)
GovukEnvironment.stubs(:name).returns("Test Fools")

user = create(:user, name: "Bob Loblaw")
Expand All @@ -44,22 +45,22 @@ class NoisyBatchInvitationTest < ActionMailer::TestCase
@email = NoisyBatchInvitation.make_noise(@batch_invitation).deliver_now
end

should "from address should include the instance name" do
should "from address should include the environment name" do
assert_match(/".* Signon Test Fools" <noreply-signon-test-fools@.*\.gov\.uk>/, @email[:from].to_s)
end
end

context "work correctly when no instance name is set" do
context "work correctly in production environment" do
setup do
GovukEnvironment.stubs(:name).returns(nil)
GovukEnvironment.stubs(:production?).returns(true)

user = create(:user, name: "Bob Loblaw")
@batch_invitation = create(:batch_invitation, user:)
create(:batch_invitation_user, batch_invitation: @batch_invitation)
@email = NoisyBatchInvitation.make_noise(@batch_invitation).deliver_now
end

should "from address should include the instance name" do
should "from address should not include the environment name" do
assert_match(/".* Signon" <noreply-signon@.*\.gov\.uk>/, @email[:from].to_s)
end
end
Expand Down
8 changes: 6 additions & 2 deletions test/models/user_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def assert_support_present_in_text(link_text, email = @email)

context "in a non-production environment" do
setup do
GovukEnvironment.stubs(:production?).returns(false)
GovukEnvironment.stubs(:name).returns("foobar")
end

Expand All @@ -36,7 +37,7 @@ def assert_support_present_in_text(link_text, email = @email)

context "in the production environment" do
setup do
GovukEnvironment.stubs(:name).returns(nil)
GovukEnvironment.stubs(:production?).returns(true)
end

should "not include the environment in the subject" do
Expand Down Expand Up @@ -119,6 +120,7 @@ def assert_support_present_in_text(link_text, email = @email)

context "email a user to notify of suspension" do
setup do
GovukEnvironment.stubs(:production?).returns(false)
GovukEnvironment.stubs(:name).returns("test")
stub_user = stub(name: "User", email: "user@example.com")
@email = UserMailer.suspension_notification(stub_user)
Expand All @@ -137,8 +139,9 @@ def assert_support_present_in_text(link_text, email = @email)
end
end

context "on a named Signon instance" do
context "on a non-production Signon instance" do
setup do
GovukEnvironment.stubs(:production?).returns(false)
GovukEnvironment.stubs(:name).returns("test")
stub_user = stub(name: "User", email: "user@example.com")
@email = UserMailer.suspension_reminder(stub_user, 3)
Expand All @@ -155,6 +158,7 @@ def assert_support_present_in_text(link_text, email = @email)

context "emailing a user to explain why their account is locked" do
setup do
GovukEnvironment.stubs(:production?).returns(false)
GovukEnvironment.stubs(:name).returns("test")
@the_time = Time.zone.now
user = User.new(name: "User", email: "user@example.com", locked_at: @the_time)
Expand Down
3 changes: 2 additions & 1 deletion test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class UserTest < ActiveSupport::TestCase
include ActiveJob::TestHelper

def setup
GovukEnvironment.stubs(:name).returns(nil)
GovukEnvironment.stubs(:production?).returns(true)

@user = create(:user)
end
Expand All @@ -21,6 +21,7 @@ def setup
end

should "default to false for admins and superadmins in non-production" do
GovukEnvironment.stubs(:production?).returns(false)
GovukEnvironment.stubs(:name).returns("foobar")

assert_not create(:admin_user).require_2sv?
Expand Down

0 comments on commit 40263fe

Please sign in to comment.