Skip to content

Commit

Permalink
Don't leak global state outside of test example (pupilfirst#655)
Browse files Browse the repository at this point in the history
It is possible, dependending on the order in which specs are executed,
that the Rails.application.secrets.multitenancy setting leaks out of the
context and affects other specs.

With around hook, global setting is restored after the test, even if the
test eventually fails.
  • Loading branch information
mostlyobvious authored Mar 4, 2021
1 parent c042379 commit a322c3d
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
let!(:third_school_domain) { create :domain, :primary, fqdn: 'www.third.localhost', school: third_school }

context 'when multitenancy is turned on' do
before do
Rails.application.secrets.multitenancy = true
around(:each) do |example|
begin
current = Rails.application.secrets.multitenancy
Rails.application.secrets.multitenancy = true
example.run
ensure
Rails.application.secrets.multitenancy = current
end
end

context 'when the current domain belongs to second school' do
Expand Down Expand Up @@ -40,8 +46,14 @@ def current_domain
end

context 'when multitenancy is turned off' do
before do
Rails.application.secrets.multitenancy = false
around(:each) do |example|
begin
current = Rails.application.secrets.multitenancy
Rails.application.secrets.multitenancy = false
example.run
ensure
Rails.application.secrets.multitenancy = current
end
end

it 'returns the first school' do
Expand Down

0 comments on commit a322c3d

Please sign in to comment.