-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from ustudio/play-2375-python2-sanitize-storag…
…e-uris [PLAY-2375] -- Python 2.7 Support: Sanitize Storage URIs
- Loading branch information
Showing
15 changed files
with
278 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from urlparse import parse_qsl, ParseResult | ||
from urllib import urlencode | ||
|
||
|
||
def _new_uri(parsed_uri, new_netloc, new_query): | ||
return ParseResult( | ||
parsed_uri.scheme, new_netloc, parsed_uri.path, parsed_uri.params, urlencode(new_query), | ||
parsed_uri.fragment) | ||
|
||
|
||
def remove_user_info(parsed_uri): | ||
new_netloc = parsed_uri.hostname | ||
|
||
if parsed_uri.port is not None: | ||
new_netloc = ":".join((new_netloc, str(parsed_uri.port))) | ||
|
||
new_uri = _new_uri(parsed_uri, new_netloc, dict(parse_qsl(parsed_uri.query))) | ||
|
||
return new_uri.geturl() | ||
|
||
|
||
def sanitize_resource_uri(parsed_uri): | ||
new_netloc = parsed_uri.hostname | ||
|
||
if parsed_uri.port is not None: | ||
new_netloc = ":".join((new_netloc, str(parsed_uri.port))) | ||
|
||
new_uri = _new_uri(parsed_uri, new_netloc, {}) | ||
|
||
return new_uri.geturl() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -593,6 +593,20 @@ def test_ftp_get_download_url_returns_none_with_empty_base(self, mock_ftp_class) | |
|
||
mock_ftp_class.assert_not_called() | ||
|
||
@mock.patch("ftplib.FTP", autospec=True) | ||
def test_ftp_get_sanitized_uri(self, mock_ftp_class): | ||
download_url_base = urllib.quote_plus("http://hostname/path/to/") | ||
|
||
ftpuri = "ftp://user:[email protected]/some/dir/file.txt?download_url_base={}".format( | ||
download_url_base) | ||
|
||
storage = storagelib.get_storage(ftpuri) | ||
sanitized_uri = storage.get_sanitized_uri() | ||
|
||
self.assertEqual( | ||
"ftp://ftp.foo.com/some/dir/file.txt?download_url_base={}".format(download_url_base), | ||
sanitized_uri) | ||
|
||
|
||
class TestFTPSStorage(TestCase): | ||
@mock.patch("ftplib.FTP_TLS", autospec=True) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import mock | ||
import storage as storagelib | ||
import threading | ||
|
||
from unittest import TestCase | ||
|
||
import storage as storagelib | ||
from storage.storage import Storage | ||
|
||
|
||
class TestTimeout(TestCase): | ||
@mock.patch("threading.Thread", wraps=threading.Thread) | ||
|
@@ -51,3 +54,23 @@ class MyStorageClass(storagelib.storage.Storage): | |
uri = "{0}://some/uri/path".format(self.scheme) | ||
store_obj = storagelib.get_storage(uri) | ||
self.assertIsInstance(store_obj, MyStorageClass) | ||
|
||
|
||
class TestStorage(TestCase): | ||
def test_get_sanitized_uri_removes_username_and_password(self): | ||
storage = Storage(storage_uri="https://username:password@bucket/path/filename") | ||
sanitized_uri = storage.get_sanitized_uri() | ||
|
||
self.assertEqual("https://bucket/path/filename", sanitized_uri) | ||
|
||
def test_get_sanitized_uri_does_not_preserves_parameters(self): | ||
storage = Storage(storage_uri="https://username:password@bucket/path/filename?other=param") | ||
sanitized_uri = storage.get_sanitized_uri() | ||
|
||
self.assertEqual("https://bucket/path/filename", sanitized_uri) | ||
|
||
def test_get_sanitized_uri_preserves_port_number(self): | ||
storage = Storage(storage_uri="ftp://username:[email protected]:8080/path/filename") | ||
sanitized_uri = storage.get_sanitized_uri() | ||
|
||
self.assertEqual("ftp://ftp.foo.com:8080/path/filename", sanitized_uri) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.