Browse Source

Tests for username:passwd style urls in HttpGitClient

Mika Mäenpää 8 years ago
parent
commit
5b174e9b62
4 changed files with 75 additions and 4 deletions
  1. 2 2
      .travis.yml
  2. 3 2
      dulwich/client.py
  3. 62 0
      dulwich/tests/test_client.py
  4. 8 0
      tox.ini

+ 2 - 2
.travis.yml

@@ -5,9 +5,9 @@ env:
 matrix:
   include:
     - python: "2.7"
-      env: TEST_REQUIRE="gevent greenlet geventhttpclient fastimport"
+      env: TEST_REQUIRE="gevent greenlet geventhttpclient fastimport mock"
     - python: "pypy"
-      env: TEST_REQUIRE="fastimport"
+      env: TEST_REQUIRE="fastimport mock"
     - python: "3.4"
       env: TEST_REQUIRE="gevent greenlet geventhttpclient fastimport"
     - python: "3.5"

+ 3 - 2
dulwich/client.py

@@ -1040,7 +1040,7 @@ class HttpGitClient(GitClient):
             self.opener = opener
         if username is not None:
             pass_man = urllib2.HTTPPasswordMgrWithDefaultRealm()
-            pass_man.add_password(None, base_url, user, password)
+            pass_man.add_password(None, base_url, username, password)
             self.opener.add_handler(urllib2.HTTPBasicAuthHandler(pass_man))
         GitClient.__init__(self, **kwargs)
 
@@ -1051,10 +1051,11 @@ class HttpGitClient(GitClient):
     def from_parsedurl(cls, parsedurl, **kwargs):
         auth, host = urllib2.splituser(parsedurl.netloc)
         password = parsedurl.password
+        username = parsedurl.username
         # TODO(jelmer): This also strips the username
         parsedurl = parsedurl._replace(netloc=host)
         return cls(urlparse.urlunparse(parsedurl),
-                   password=password, **kwargs)
+                   password=password, username=username, **kwargs)
 
     def __repr__(self):
         return "%s(%r, dumb=%r)" % (type(self).__name__, self._base_url, self.dumb)

+ 62 - 0
dulwich/tests/test_client.py

@@ -24,6 +24,11 @@ import sys
 import shutil
 import tempfile
 
+try:
+    # Python >= 3.3
+    from unittest import mock
+except ImportError:
+    import mock
 
 import dulwich
 from dulwich import (
@@ -465,6 +470,38 @@ class TestGetTransportAndPath(TestCase):
         self.assertTrue(isinstance(c, HttpGitClient))
         self.assertEqual('/jelmer/dulwich', path)
 
+    def test_http_auth(self):
+        url = 'https://user:passwd@github.com/jelmer/dulwich'
+
+        with mock.patch('dulwich.client.HttpGitClient.__init__') as init_mock:
+            init_mock.return_value = None
+            c, path = get_transport_and_path(url)
+
+            self.assertTrue(isinstance(c, HttpGitClient))
+            self.assertEqual('/jelmer/dulwich', path)
+
+            init_mock.assert_called_once_with(
+                'https://github.com/jelmer/dulwich',
+                config=None,
+                username='user',
+                password='passwd')
+
+    def test_http_no_auth(self):
+        url = 'https://github.com/jelmer/dulwich'
+
+        with mock.patch('dulwich.client.HttpGitClient.__init__') as init_mock:
+            init_mock.return_value = None
+            c, path = get_transport_and_path(url)
+
+            self.assertTrue(isinstance(c, HttpGitClient))
+            self.assertEqual('/jelmer/dulwich', path)
+
+            init_mock.assert_called_once_with(
+                'https://github.com/jelmer/dulwich',
+                config=None,
+                username=None,
+                password=None)
+
 
 class TestGetTransportAndPathFromUrl(TestCase):
 
@@ -745,6 +782,31 @@ class HttpGitClientTests(TestCase):
         url = c.get_url(path)
         self.assertEqual('https://github.com/jelmer/dulwich', url)
 
+    def test_get_url_with_username_and_passwd(self):
+        base_url = 'https://github.com/jelmer/dulwich'
+        path = '/jelmer/dulwich'
+        c = HttpGitClient(base_url, username='USERNAME', password='PASSWD')
+
+        url = c.get_url(path)
+        self.assertEqual('https://github.com/jelmer/dulwich', url)
+
+    def test_init_username_passwd_set(self):
+        url = 'https://user:passwd@github.com/jelmer/dulwich'
+
+        with mock.patch('dulwich.client.urllib2.HTTPPasswordMgrWithDefaultRealm.add_password') as passman_mock:
+
+            c = HttpGitClient(url, config=None, username='user', password='passwd')
+            passman_mock.assert_called_once_with(
+                None,
+                url, 'user', 'passwd')
+
+    def test_init_no_username_passwd(self):
+        url = 'https://github.com/jelmer/dulwich'
+
+        with mock.patch('dulwich.client.urllib2.HTTPPasswordMgrWithDefaultRealm.add_password') as passman_mock:
+
+            c = HttpGitClient(url, config=None)
+            self.assertFalse(passman_mock.called)
 
 class TCPGitClientTests(TestCase):
 

+ 8 - 0
tox.ini

@@ -8,10 +8,18 @@ commands = make check
 recreate = True
 whitelist_externals = make
 
+[testenv:py27]
+deps = mock
+
+[testenv:pypy]
+deps = mock
+
 [testenv:py27-noext]
+deps = mock
 commands = make check-noextensions
 
 [testenv:pypy-noext]
+deps = mock
 commands = make check-noextensions
 
 [testenv:py34-noext]