diff --git a/.rubocop_gradual.lock b/.rubocop_gradual.lock index 81583536..dfb5f80e 100644 --- a/.rubocop_gradual.lock +++ b/.rubocop_gradual.lock @@ -3,9 +3,9 @@ [66, 5, 20, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 2485198147], [78, 5, 74, "Style/InvertibleUnlessCondition: Prefer `if Gem.rubygems_version >= Gem::Version.new(\"2.7.0\")` over `unless Gem.rubygems_version < Gem::Version.new(\"2.7.0\")`.", 2453573257] ], - "lib/oauth2.rb:3930909031": [ - [31, 5, 21, "ThreadSafety/ClassAndModuleAttributes: Avoid mutating class and module attributes.", 622027168], - [34, 11, 7, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 651502127] + "lib/oauth2.rb:3563577000": [ + [34, 5, 21, "ThreadSafety/ClassAndModuleAttributes: Avoid mutating class and module attributes.", 622027168], + [37, 11, 7, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 651502127] ], "lib/oauth2/authenticator.rb:3711266135": [ [42, 5, 113, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 734523108] @@ -28,11 +28,11 @@ [130, 3, 52, "Gemspec/DependencyVersion: Dependency version specification is required.", 3163430777], [131, 3, 48, "Gemspec/DependencyVersion: Dependency version specification is required.", 425065368] ], - "spec/oauth2/access_token_spec.rb:388877639": [ + "spec/oauth2/access_token_spec.rb:2406469319": [ [3, 1, 34, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2/access_token*_spec.rb`.", 1972107547], - [594, 13, 25, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 770233088], - [664, 9, 101, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3022740639], - [668, 9, 79, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 2507338967] + [612, 13, 25, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 770233088], + [682, 9, 101, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3022740639], + [686, 9, 79, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 2507338967] ], "spec/oauth2/authenticator_spec.rb:853320290": [ [3, 1, 36, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2/authenticator*_spec.rb`.", 819808017], @@ -41,7 +41,7 @@ [69, 15, 38, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1480816240], [79, 13, 23, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 2314399065] ], - "spec/oauth2/client_spec.rb:824695973": [ + "spec/oauth2/client_spec.rb:623759711": [ [6, 1, 29, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2/client*_spec.rb`.", 439549885], [174, 7, 492, "RSpec/NoExpectationExample: No expectation found in this example.", 1272021224], [193, 7, 592, "RSpec/NoExpectationExample: No expectation found in this example.", 3428877205], @@ -49,18 +49,18 @@ [221, 15, 20, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1276531672], [236, 15, 43, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1383956904], [251, 15, 43, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 3376202107], - [869, 5, 360, "RSpec/NoExpectationExample: No expectation found in this example.", 536201463], - [878, 5, 461, "RSpec/NoExpectationExample: No expectation found in this example.", 3392600621], - [889, 5, 340, "RSpec/NoExpectationExample: No expectation found in this example.", 244592251], - [934, 63, 2, "RSpec/BeEq: Prefer `be` over `eq`.", 5860785], - [979, 11, 99, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3084776886], - [983, 11, 82, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1524553529], - [991, 7, 89, "RSpec/NoExpectationExample: No expectation found in this example.", 4609419], - [1079, 11, 99, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3084776886], - [1083, 11, 82, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1524553529], - [1163, 17, 12, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 664794325], - [1188, 5, 459, "RSpec/NoExpectationExample: No expectation found in this example.", 2216851076], - [1198, 7, 450, "RSpec/NoExpectationExample: No expectation found in this example.", 2619808549] + [865, 5, 360, "RSpec/NoExpectationExample: No expectation found in this example.", 536201463], + [874, 5, 461, "RSpec/NoExpectationExample: No expectation found in this example.", 3392600621], + [885, 5, 340, "RSpec/NoExpectationExample: No expectation found in this example.", 244592251], + [930, 63, 2, "RSpec/BeEq: Prefer `be` over `eq`.", 5860785], + [975, 11, 99, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3084776886], + [979, 11, 82, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1524553529], + [987, 7, 89, "RSpec/NoExpectationExample: No expectation found in this example.", 4609419], + [1075, 11, 99, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3084776886], + [1079, 11, 82, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1524553529], + [1159, 17, 12, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 664794325], + [1184, 5, 459, "RSpec/NoExpectationExample: No expectation found in this example.", 2216851076], + [1194, 7, 450, "RSpec/NoExpectationExample: No expectation found in this example.", 2619808549] ], "spec/oauth2/error_spec.rb:1209122273": [ [23, 1, 28, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2/error*_spec.rb`.", 3385870076], @@ -97,8 +97,7 @@ "spec/oauth2/version_spec.rb:1001406821": [ [3, 1, 30, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2/version*_spec.rb`.", 1099517182] ], - "spec/oauth2_spec.rb:1511642301": [ - [3, 1, 21, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2*_spec.rb`.", 3359091140], - [5, 68, 2, "RSpec/BeEq: Prefer `be` over `eq`.", 5860785] + "spec/oauth2_spec.rb:2292862590": [ + [3, 1, 21, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2*_spec.rb`.", 3359091140] ] } diff --git a/lib/oauth2.rb b/lib/oauth2.rb index 2f950419..ec88b462 100644 --- a/lib/oauth2.rb +++ b/lib/oauth2.rb @@ -25,7 +25,10 @@ # The namespace of this library module OAuth2 - DEFAULT_CONFIG = SnakyHash::SymbolKeyed.new(silence_extra_tokens_warning: false) + OAUTH_DEBUG = ENV.fetch("OAUTH_DEBUG", "false").casecmp("true").zero? + DEFAULT_CONFIG = SnakyHash::SymbolKeyed.new( + silence_extra_tokens_warning: true, + ) @config = DEFAULT_CONFIG.dup class << self attr_accessor :config diff --git a/lib/oauth2/client.rb b/lib/oauth2/client.rb index c0f1f2a6..430200d0 100644 --- a/lib/oauth2/client.rb +++ b/lib/oauth2/client.rb @@ -449,7 +449,7 @@ def build_access_token_legacy(response, access_token_opts, extract_access_token) end def oauth_debug_logging(builder) - builder.response(:logger, options[:logger], bodies: true) if ENV["OAUTH_DEBUG"] == "true" + builder.response(:logger, options[:logger], bodies: true) if OAuth2::OAUTH_DEBUG end end end diff --git a/spec/oauth2/access_token_spec.rb b/spec/oauth2/access_token_spec.rb index 30119944..f5233898 100644 --- a/spec/oauth2/access_token_spec.rb +++ b/spec/oauth2/access_token_spec.rb @@ -50,6 +50,15 @@ end end + before do + @original_setw = OAuth2.config.silence_extra_tokens_warning + OAuth2.config.silence_extra_tokens_warning = false + end + + after do + OAuth2.config.silence_extra_tokens_warning = @original_setw + end + it "warns on STDERR" do msg = <<-MSG.lstrip OAuth2::AccessToken.from_hash: `hash` contained more than one 'token' key ([:access_token, :id_token]); using :access_token. @@ -89,6 +98,15 @@ end end + before do + @original_setw = OAuth2.config.silence_extra_tokens_warning + OAuth2.config.silence_extra_tokens_warning = false + end + + after do + OAuth2.config.silence_extra_tokens_warning = @original_setw + end + let(:hash) do { id_token: "confusing bug here", diff --git a/spec/oauth2/client_spec.rb b/spec/oauth2/client_spec.rb index 3918dc3b..59d6a71b 100644 --- a/spec/oauth2/client_spec.rb +++ b/spec/oauth2/client_spec.rb @@ -267,13 +267,12 @@ describe "#connection" do context "when debugging" do - include_context "with stubbed env" before do - stub_env("OAUTH_DEBUG" => debug_value) + stub_const("OAuth2::OAUTH_DEBUG", debug_value) end context "when OAUTH_DEBUG=true" do - let(:debug_value) { "true" } + let(:debug_value) { true } it "smoothly handles successive requests" do silence_all do @@ -301,7 +300,7 @@ end context "when OAUTH_DEBUG=false" do - let(:debug_value) { "false" } + let(:debug_value) { false } it "smoothly handles successive requests" do silence_all do @@ -357,24 +356,21 @@ expect(response.headers).to eq("Content-Type" => "text/awesome") end - context "with ENV" do - include_context "with stubbed env" - context "when OAUTH_DEBUG=true" do - before do - stub_env("OAUTH_DEBUG" => "true") - end + context "when silence_extra_tokens_warning=false" do + before do + stub_const("OAuth2::OAUTH_DEBUG", true) + end - it "outputs to $stdout when OAUTH_DEBUG=true" do - output = capture(:stdout) do - subject.request(:get, "/success") - end - logs = [ - "request: GET https://api.example.com/success", - "response: Status 200", - 'response: Content-Type: "text/awesome"', - ] - expect(output).to include(*logs) + it "outputs to $stdout when OAUTH_DEBUG=true" do + output = capture(:stdout) do + subject.request(:get, "/success") end + logs = [ + "request: GET https://api.example.com/success", + "response: Status 200", + 'response: Content-Type: "text/awesome"', + ] + expect(output).to include(*logs) end end diff --git a/spec/oauth2_spec.rb b/spec/oauth2_spec.rb index 51ed31fc..09240997 100644 --- a/spec/oauth2_spec.rb +++ b/spec/oauth2_spec.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true RSpec.describe OAuth2 do - it "has a default config for silence_extra_tokens_warning" do - expect(described_class.config.silence_extra_tokens_warning).to eq(false) + it "silence_extra_tokens_warning default is opposite of OAUTH_DEBUG" do + expect(described_class.config.silence_extra_tokens_warning).to be(true) end describe ".configure" do