Show More
@@ -587,51 +587,17 b' def cleaned_uri(uri):' | |||||
587 | return urllib.quote(uri, safe='@$:/') |
|
587 | return urllib.quote(uri, safe='@$:/') | |
588 |
|
588 | |||
589 |
|
589 | |||
590 | def uri_filter(uri): |
|
|||
591 | """ |
|
|||
592 | Removes user:password from given url string |
|
|||
593 |
|
||||
594 | :param uri: |
|
|||
595 | :rtype: unicode |
|
|||
596 | :returns: filtered list of strings |
|
|||
597 | """ |
|
|||
598 | if not uri: |
|
|||
599 | return '' |
|
|||
600 |
|
||||
601 | proto = '' |
|
|||
602 |
|
||||
603 | for pat in ('https://', 'http://'): |
|
|||
604 | if uri.startswith(pat): |
|
|||
605 | uri = uri[len(pat):] |
|
|||
606 | proto = pat |
|
|||
607 | break |
|
|||
608 |
|
||||
609 | # remove passwords and username |
|
|||
610 | uri = uri[uri.find('@') + 1:] |
|
|||
611 |
|
||||
612 | # get the port |
|
|||
613 | cred_pos = uri.find(':') |
|
|||
614 | if cred_pos == -1: |
|
|||
615 | host, port = uri, None |
|
|||
616 | else: |
|
|||
617 | host, port = uri[:cred_pos], uri[cred_pos + 1:] |
|
|||
618 |
|
||||
619 | return filter(None, [proto, host, port]) |
|
|||
620 |
|
||||
621 |
|
||||
622 | def credentials_filter(uri): |
|
590 | def credentials_filter(uri): | |
623 | """ |
|
591 | """ | |
624 | Returns a url with removed credentials |
|
592 | Returns a url with removed credentials | |
625 |
|
593 | |||
626 | :param uri: |
|
594 | :param uri: | |
627 | """ |
|
595 | """ | |
|
596 | import urlobject | |||
|
597 | url_obj = urlobject.URLObject(cleaned_uri(uri)) | |||
|
598 | url_obj = url_obj.without_password().without_username() | |||
628 |
|
599 | |||
629 | uri = uri_filter(uri) |
|
600 | return url_obj | |
630 | # check if we have port |
|
|||
631 | if len(uri) > 2 and uri[2]: |
|
|||
632 | uri[2] = ':' + uri[2] |
|
|||
633 |
|
||||
634 | return ''.join(uri) |
|
|||
635 |
|
601 | |||
636 |
|
602 | |||
637 | def get_host_info(request): |
|
603 | def get_host_info(request): |
@@ -38,36 +38,29 b' from rhodecode.lib.utils2 import Attribu' | |||||
38 | from rhodecode.model.db import Repository, CacheKey |
|
38 | from rhodecode.model.db import Repository, CacheKey | |
39 |
|
39 | |||
40 |
|
40 | |||
41 | def _urls_for_proto(proto): |
|
41 | TEST_URLS = [ | |
42 | return [ |
|
42 | ('127.0.0.1', '127.0.0.1'), | |
43 |
(' |
|
43 | ('marcink@127.0.0.1', '127.0.0.1'), | |
44 | '%s://127.0.0.1' % proto), |
|
44 | ('marcink:pass@127.0.0.1', '127.0.0.1'), | |
45 |
(' |
|
45 | ('marcink@domain.name:pass@127.0.0.1', '127.0.0.1'), | |
46 | '%s://127.0.0.1' % proto), |
|
46 | ||
47 | ('%s://marcink:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
47 | ('127.0.0.1:8080', '127.0.0.1:8080'), | |
48 | '%s://127.0.0.1' % proto), |
|
48 | ('marcink@127.0.0.1:8080', '127.0.0.1:8080'), | |
49 |
(' |
|
49 | ('marcink:pass@127.0.0.1:8080', '127.0.0.1:8080'), | |
50 | '%s://127.0.0.1:8080' % proto), |
|
50 | ('marcink@domain.name:pass@127.0.0.1:8080', '127.0.0.1:8080'), | |
51 | ('%s://domain.org' % proto, ['%s://' % proto, 'domain.org'], |
|
51 | ||
52 |
|
|
52 | ('domain.org', 'domain.org'), | |
53 |
(' |
|
53 | ('user:pass@domain.org:8080', 'domain.org:8080'), | |
54 | ['%s://' % proto, 'domain.org', '8080'], |
|
54 | ('user@domain.org:pass@domain.org:8080', 'domain.org:8080'), | |
55 | '%s://domain.org:8080' % proto), |
|
|||
56 | ] |
|
55 | ] | |
57 |
|
56 | |||
58 | TEST_URLS = _urls_for_proto('http') + _urls_for_proto('https') |
|
|||
59 |
|
57 | |||
60 |
|
58 | @pytest.mark.parametrize("protocol", ['http://', 'https://']) | ||
61 |
@pytest.mark.parametrize("test_url, expected |
|
59 | @pytest.mark.parametrize("test_url, expected", TEST_URLS) | |
62 |
def test_ |
|
60 | def test_credentials_filter(protocol, test_url, expected): | |
63 | from rhodecode.lib.utils2 import uri_filter |
|
|||
64 | assert uri_filter(test_url) == expected |
|
|||
65 |
|
||||
66 |
|
||||
67 | @pytest.mark.parametrize("test_url, expected, expected_creds", TEST_URLS) |
|
|||
68 | def test_credentials_filter(test_url, expected, expected_creds): |
|
|||
69 | from rhodecode.lib.utils2 import credentials_filter |
|
61 | from rhodecode.lib.utils2 import credentials_filter | |
70 | assert credentials_filter(test_url) == expected_creds |
|
62 | test_url = protocol + test_url | |
|
63 | assert credentials_filter(test_url) == protocol + expected | |||
71 |
|
64 | |||
72 |
|
65 | |||
73 | @pytest.mark.parametrize("str_bool, expected", [ |
|
66 | @pytest.mark.parametrize("str_bool, expected", [ |
General Comments 0
You need to be logged in to leave comments.
Login now