Skip to content

Commit 3faece5

Browse files
fix: ensure log_response actually logs the message
1 parent 0af2269 commit 3faece5

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

lib/googleauth/signet.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,12 @@ def log_response token_response
210210
digest = Digest::SHA256.hexdigest response_hash["id_token"]
211211
response_hash["id_token"] = "(sha256:#{digest})"
212212
end
213-
Google::Logging::Message.from(
214-
message: "Received auth token response: #{response_hash}",
215-
"credentialsId" => object_id
216-
)
213+
logger&.debug do
214+
Google::Logging::Message.from(
215+
message: "Received auth token response: #{response_hash}",
216+
"credentialsId" => object_id
217+
)
218+
end
217219
end
218220

219221
def log_auth_error err

spec/googleauth/user_refresh_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,17 +394,54 @@ def cred_json_text_with_universe_domain missing = nil
394394
end
395395
end
396396

397+
describe "logging during revoke" do
398+
let(:response_body) { '{"foo": "bar"}' }
399+
let :stub do
400+
stub_request(:post, "https://oauth2.googleapis.com/revoke")
401+
.with(body: hash_including("token" => "refreshtoken"))
402+
.to_return(status: 200,
403+
body: response_body,
404+
headers: { "Content-Type" => "application/json" })
405+
end
406+
407+
it "logs the response body" do
408+
stub
409+
strio = StringIO.new
410+
logger = Logger.new strio
411+
logger.level = Logger::DEBUG
412+
@client.logger = logger
413+
@client.revoke!
414+
expect(strio.string).to include("Received auth token response")
415+
end
416+
417+
it "logs transient errors when they occur" do
418+
allow_any_instance_of(Faraday::Connection).to receive(:post).and_raise(Faraday::TimeoutError)
419+
strio = StringIO.new
420+
logger = Logger.new strio
421+
@client.logger = logger
422+
423+
# Stub sleep to avoid slow tests
424+
allow(@client).to receive(:sleep)
425+
426+
expect { @client.revoke! }.to raise_error Signet::AuthorizationError
427+
expect(strio.string).to include("Transient error when fetching auth token")
428+
expect(strio.string).to include("Exhausted retries when fetching auth token")
429+
end
430+
end
431+
397432
describe "when errors occurred with request" do
398433
it "should fail with Signet::AuthorizationError if request times out" do
399434
allow_any_instance_of(Faraday::Connection).to receive(:post)
400435
.and_raise(Faraday::TimeoutError)
436+
expect(@client).to receive(:sleep).exactly(5).times.with(kind_of(Numeric))
401437
expect { @client.revoke! }
402438
.to raise_error Signet::AuthorizationError
403439
end
404440

405441
it "should fail with Signet::AuthorizationError if request fails" do
406442
allow_any_instance_of(Faraday::Connection).to receive(:post)
407443
.and_raise(Faraday::ConnectionFailed, nil)
444+
expect(@client).to receive(:sleep).exactly(5).times.with(kind_of(Numeric))
408445
expect { @client.revoke! }
409446
.to raise_error Signet::AuthorizationError
410447
end

0 commit comments

Comments
 (0)