summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDaniel Niknam <mhmd.niknam@gmail.com>2021-08-22 01:31:03 +1000
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2021-08-31 19:06:14 +0900
commitb41802421ac7e790a98ed59be67631687fc5808f (patch)
tree87886d29e29bec9f30790ea7cd12a89befc4f746 /test
parent14a9e24f7ea161056b56f4833fd8bbecd7718905 (diff)
[rubygems/rubygems] Introduce `Gem::PrintableUri` that would redact URIs to be used on outputs
We need to redact URI credential in several places and copy pasting the code into each part of it is not ideal. This class is responsible for parsing URI strings and redacting credential from it. Also, it will handle URI object in the same manner. We will be reusing this class whenever we need to print/display a URI to users. URI with the following format will be redacted: - Token: `http://my-secure-token@example.com` => `http://REDACTED@example.com` - Username & Password: `http://my-username:my-secure-password@example.com` => `http://my-username:REDACTED@example.com` - x-oauth-basic: `http://my-secure-token:x-oauth-basic@example.com` => `http://REDACTED:x-oauth-basic@example.com` https://github.com/rubygems/rubygems/commit/f1e45d3a89
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4789
Diffstat (limited to 'test')
-rw-r--r--test/rubygems/test_gem_printable_uri.rb116
1 files changed, 116 insertions, 0 deletions
diff --git a/test/rubygems/test_gem_printable_uri.rb b/test/rubygems/test_gem_printable_uri.rb
new file mode 100644
index 0000000000..59394547b9
--- /dev/null
+++ b/test/rubygems/test_gem_printable_uri.rb
@@ -0,0 +1,116 @@
+require_relative 'helper'
+require 'uri'
+require 'rubygems/printable_uri'
+
+class TestPrintableUri < Gem::TestCase
+ def test_parsed_uri
+ assert_equal true, Gem::PrintableUri.parse_uri("https://www.example.com").parsed_uri?
+ end
+
+ def test_parsed_uri_with_empty_uri_object
+ assert_equal true, Gem::PrintableUri.parse_uri(URI("")).parsed_uri?
+ end
+
+ def test_parsed_uri_with_valid_uri_object
+ assert_equal true, Gem::PrintableUri.parse_uri(URI("https://www.example.com")).parsed_uri?
+ end
+
+ def test_parsed_uri_with_other_objects
+ assert_equal false, Gem::PrintableUri.parse_uri(Object.new).parsed_uri?
+ end
+
+ def test_parsed_uri_with_invalid_uri
+ assert_equal false, Gem::PrintableUri.parse_uri("https://www.example.com:80index").parsed_uri?
+ end
+
+ def test_credential_redacted_with_user_pass
+ assert_equal true, Gem::PrintableUri.parse_uri("https://user:pass@example.com").credential_redacted?
+ end
+
+ def test_credential_redacted_with_token
+ assert_equal true, Gem::PrintableUri.parse_uri("https://token@example.com").credential_redacted?
+ end
+
+ def test_credential_redacted_with_user_x_oauth_basic
+ assert_equal true, Gem::PrintableUri.parse_uri("https://token:x-oauth-basic@example.com").credential_redacted?
+ end
+
+ def test_credential_redacted_without_credential
+ assert_equal false, Gem::PrintableUri.parse_uri("https://www.example.com").credential_redacted?
+ end
+
+ def test_credential_redacted_with_empty_uri_object
+ assert_equal false, Gem::PrintableUri.parse_uri(URI("")).credential_redacted?
+ end
+
+ def test_credential_redacted_with_valid_uri_object
+ assert_equal true, Gem::PrintableUri.parse_uri(URI("https://user:pass@example.com")).credential_redacted?
+ end
+
+ def test_credential_redacted_with_other_objects
+ assert_equal false, Gem::PrintableUri.parse_uri(Object.new).credential_redacted?
+ end
+
+ def test_original_password_user_pass
+ assert_equal "pass", Gem::PrintableUri.parse_uri("https://user:pass@example.com").original_password
+ end
+
+ def test_original_password_with_token
+ assert_equal nil, Gem::PrintableUri.parse_uri("https://token@example.com").original_password
+ end
+
+ 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
+
+ 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
+
+ def test_to_s_with_token
+ assert_equal "https://REDACTED@example.com", Gem::PrintableUri.parse_uri("https://token@example.com").to_s
+ end
+
+ def test_to_s_with_user_x_oauth_basic
+ assert_equal "https://REDACTED:x-oauth-basic@example.com", Gem::PrintableUri.parse_uri("https://token:x-oauth-basic@example.com").to_s
+ end
+
+ def test_to_s_without_credential
+ assert_equal "https://www.example.com", Gem::PrintableUri.parse_uri("https://www.example.com").to_s
+ end
+
+ def test_to_s_with_invalid_uri
+ assert_equal "https://www.example.com:80index", Gem::PrintableUri.parse_uri("https://www.example.com:80index").to_s
+ end
+
+ def test_to_s_with_empty_uri_object
+ assert_equal "", Gem::PrintableUri.parse_uri(URI("")).to_s
+ end
+
+ def test_to_s_with_valid_uri_object
+ assert_equal "https://user:REDACTED@example.com", Gem::PrintableUri.parse_uri(URI("https://user:pass@example.com")).to_s
+ end
+
+ def test_to_s_with_other_objects
+ obj = Object.new
+ obj.stub(:to_s, "my-to-s") do
+ assert_equal "my-to-s", Gem::PrintableUri.parse_uri(obj).to_s
+ end
+ end
+end