Skip to content

Commit feb2b6d

Browse files
authored
Merge pull request #476 from duncantmiller/fix/skip-env-reliability-in-multithreaded-test-servers
Fix skip_env? to fall back to Rails.env when default_env is nil
2 parents f68fe96 + d702043 commit feb2b6d

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

lib/recaptcha.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ def self.with_configuration(config)
5252
end
5353

5454
def self.skip_env?(env)
55-
configuration.skip_verify_env.include?(env || configuration.default_env)
55+
resolved = env || configuration.default_env
56+
resolved ||= Rails.env.to_s if defined?(Rails.env)
57+
configuration.skip_verify_env.include?(resolved)
5658
end
5759

5860
def self.invalid_response?(resp)

test/verify_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,3 +521,39 @@ def assert_flash_error
521521
assert_equal "reCAPTCHA verification failed, please try again.", @controller.flash[:recaptcha_error]
522522
end
523523
end
524+
525+
describe "Recaptcha.skip_env?" do
526+
# test/helper.rb deletes RAILS_ENV and RACK_ENV, so default_env is nil in this suite.
527+
# This mirrors what can happen in multi-threaded servers (e.g. Puma running system tests)
528+
# where the Configuration singleton is initialised before the env var is visible,
529+
# leaving default_env as nil even though Rails.env correctly returns "test".
530+
531+
it "returns true when env argument matches skip_verify_env" do
532+
assert Recaptcha.skip_env?("test")
533+
end
534+
535+
it "returns false when env argument is not in skip_verify_env" do
536+
refute Recaptcha.skip_env?("production")
537+
end
538+
539+
it "returns false when env argument is nil and default_env is nil" do
540+
assert_nil Recaptcha.configuration.default_env
541+
refute Recaptcha.skip_env?(nil)
542+
end
543+
544+
describe "when Rails.env is available and in skip_verify_env" do
545+
before do
546+
rails_stub = Module.new { def self.env; "test"; end }
547+
Object.const_set(:Rails, rails_stub)
548+
end
549+
550+
after do
551+
Object.send(:remove_const, :Rails) if defined?(Rails)
552+
end
553+
554+
it "returns true even when default_env is nil" do
555+
assert_nil Recaptcha.configuration.default_env
556+
assert Recaptcha.skip_env?(nil)
557+
end
558+
end
559+
end

0 commit comments

Comments
 (0)