Browse Source

Fix http auth tests to avoid introducing new dependency on mock.

Jelmer Vernooij 8 years ago
parent
commit
f62b7120e3
5 changed files with 31 additions and 48 deletions
  1. 2 2
      .travis.yml
  2. 1 1
      NEWS
  3. 2 0
      dulwich/client.py
  4. 26 37
      dulwich/tests/test_client.py
  5. 0 8
      tox.ini

+ 2 - 2
.travis.yml

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

+ 1 - 1
NEWS

@@ -9,7 +9,7 @@
     directories. (Laurent Rineau)
 
   * Add support for passwords in HTTP URLs.
-    (Jon Bain)
+    (Jon Bain, Mika Mäenpää)
 
 
 0.15.0	2016-10-09

+ 2 - 0
dulwich/client.py

@@ -1033,6 +1033,8 @@ class HttpGitClient(GitClient):
     def __init__(self, base_url, dumb=None, opener=None, config=None,
                  username=None, password=None, **kwargs):
         self._base_url = base_url.rstrip("/") + "/"
+        self._username = username
+        self._password = password
         self.dumb = dumb
         if opener is None:
             self.opener = default_urllib2_opener(config)

+ 26 - 37
dulwich/tests/test_client.py

@@ -24,11 +24,6 @@ import sys
 import shutil
 import tempfile
 
-try:
-    # Python >= 3.3
-    from unittest import mock
-except ImportError:
-    import mock
 
 import dulwich
 from dulwich import (
@@ -473,34 +468,22 @@ class TestGetTransportAndPath(TestCase):
     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)
+        c, path = get_transport_and_path(url)
 
-            init_mock.assert_called_once_with(
-                'https://github.com/jelmer/dulwich',
-                config=None,
-                username='user',
-                password='passwd')
+        self.assertTrue(isinstance(c, HttpGitClient))
+        self.assertEqual('/jelmer/dulwich', path)
+        self.assertEqual('user', c._username)
+        self.assertEqual('passwd', c._password)
 
     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)
+        c, path = get_transport_and_path(url)
 
-            init_mock.assert_called_once_with(
-                'https://github.com/jelmer/dulwich',
-                config=None,
-                username=None,
-                password=None)
+        self.assertTrue(isinstance(c, HttpGitClient))
+        self.assertEqual('/jelmer/dulwich', path)
+        self.assertIs(None, c._username)
+        self.assertIs(None, c._password)
 
 
 class TestGetTransportAndPathFromUrl(TestCase):
@@ -791,22 +774,28 @@ class HttpGitClientTests(TestCase):
         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:
+        url = 'https://github.com/jelmer/dulwich'
 
-            c = HttpGitClient(url, config=None, username='user', password='passwd')
-            passman_mock.assert_called_once_with(
-                None,
-                url, 'user', 'passwd')
+        c = HttpGitClient(url, config=None, username='user', password='passwd')
+        self.assertEqual('user', c._username)
+        self.assertEqual('passwd', c._password)
+        [pw_handler] = [
+            h for h in c.opener.handlers if getattr(h, 'passwd', None) is not None]
+        self.assertEqual(
+            ('user', 'passwd'),
+            pw_handler.passwd.find_user_password(
+                None, 'https://github.com/jelmer/dulwich'))
 
     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.assertIs(None, c._username)
+        self.assertIs(None, c._password)
+        pw_handler = [
+            h for h in c.opener.handlers if getattr(h, 'passwd', None) is not None]
+        self.assertEqual(0, len(pw_handler))
 
-            c = HttpGitClient(url, config=None)
-            self.assertFalse(passman_mock.called)
 
 class TCPGitClientTests(TestCase):
 

+ 0 - 8
tox.ini

@@ -8,18 +8,10 @@ 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]