Skip to content

Commit

Permalink
Remove defensive guards
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-niknam authored and deivid-rodriguez committed Aug 24, 2021
1 parent a517770 commit dba130c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 23 deletions.
19 changes: 4 additions & 15 deletions lib/rubygems/printable_uri.rb
Expand Up @@ -16,7 +16,7 @@ def initialize(original_uri)
def parse_uri
@original_uri = Gem::UriParser.parse_uri(@original_uri)
@uri = @original_uri.clone
redact_credential
redact_credential if valid_uri?

self
end
Expand All @@ -30,8 +30,6 @@ def credential_redacted?
end

def original_password
return unless valid_uri?

@original_uri.password
end

Expand All @@ -42,40 +40,31 @@ def to_s
private

def redact_credential
return unless redactable_credential?

if token?
@uri.user = 'REDACTED'
@credential_redacted = true
elsif oauth_basic?
@uri.user = 'REDACTED'
@credential_redacted = true
elsif password?
@uri.password = 'REDACTED'
@credential_redacted = true
end

@credential_redacted = true
end

def redactable_credential?
return false unless valid_uri?

password? || oauth_basic? || token?
end

def password?
return false unless valid_uri?

!!@uri.password
end

def oauth_basic?
return false unless valid_uri?

@uri.password == 'x-oauth-basic'
end

def token?
return false unless valid_uri?

!@uri.user.nil? && @uri.password.nil?
end
end
8 changes: 0 additions & 8 deletions test/rubygems/test_gem_printable_uri.rb
Expand Up @@ -63,10 +63,6 @@ def test_original_password_without_credential
assert_equal nil, Gem::PrintableUri.parse_uri("https://www.example.com").original_password
end

def test_original_password_with_invalid_uri
assert_equal nil, Gem::PrintableUri.parse_uri("https://www.example.com:80index").original_password
end

def test_original_password_with_empty_uri_object
assert_equal nil, Gem::PrintableUri.parse_uri(URI("")).original_password
end
Expand All @@ -75,10 +71,6 @@ def test_original_password_with_valid_uri_object
assert_equal "pass", Gem::PrintableUri.parse_uri(URI("https://user:pass@example.com")).original_password
end

def test_original_password_with_other_objects
assert_equal nil, Gem::PrintableUri.parse_uri(Object.new).original_password
end

def test_to_s_with_user_pass
assert_equal "https://user:REDACTED@example.com", Gem::PrintableUri.parse_uri("https://user:pass@example.com").to_s
end
Expand Down

0 comments on commit dba130c

Please sign in to comment.