/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-11-04 18:51:39 UTC
  • mfrom: (2961.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20071104185139-kaio3sneodg2kp71
Authentication ring implementation (read-only)

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
import bzrlib
31
31
from bzrlib import (
 
32
    config,
32
33
    errors,
33
34
    osutils,
34
35
    ui,
78
79
    )
79
80
from bzrlib.transport.http._urllib import HttpTransport_urllib
80
81
from bzrlib.transport.http._urllib2_wrappers import (
81
 
    PasswordManager,
82
82
    ProxyHandler,
83
83
    Request,
84
84
    )
204
204
    def test_invalid_http_urls(self):
205
205
        """Trap invalid construction of urls"""
206
206
        t = self._transport('http://bazaar-vcs.org/bzr/bzr.dev/')
207
 
        self.assertRaises((errors.InvalidURL, errors.ConnectionError),
 
207
        self.assertRaises(errors.InvalidURL,
208
208
                          self._transport,
209
209
                          'http://http://bazaar-vcs.org/bzr/bzr.dev/')
210
210
 
797
797
            osutils.set_or_unset_env(name, value)
798
798
 
799
799
    def _proxied_request(self):
800
 
        handler = ProxyHandler(PasswordManager())
 
800
        handler = ProxyHandler()
801
801
        request = Request('GET','http://baz/buzzle')
802
802
        handler.set_proxy(request, 'http')
803
803
        return request
832
832
    # FIXME: We don't have an https server available, so we don't
833
833
    # test https connections.
834
834
 
835
 
    # FIXME: Once the test suite is better fitted to test
836
 
    # authorization schemes, test proxy authorizations too (see
837
 
    # bug #83954).
838
 
 
839
835
    def setUp(self):
840
836
        TestCaseWithTwoWebservers.setUp(self)
841
837
        self.build_tree_contents([('foo', 'contents of foo\n'),
1210
1206
    either TestCaseWithWebserver or TestCaseWithTwoWebservers.
1211
1207
    """
1212
1208
 
 
1209
    _password_prompt_prefix = ''
 
1210
 
1213
1211
    def setUp(self):
1214
1212
        """Set up the test environment
1215
1213
 
1221
1219
        """
1222
1220
        self.build_tree_contents([('a', 'contents of a\n'),
1223
1221
                                  ('b', 'contents of b\n'),])
1224
 
        self.old_factory = ui.ui_factory
1225
 
        # The following has the unfortunate side-effect of hiding any ouput
1226
 
        # during the tests (including pdb prompts). Feel free to comment them
1227
 
        # for debugging purposes but leave them in place, there are needed to
1228
 
        # run the tests without any console
1229
 
        self.old_stdout = sys.stdout
1230
 
        sys.stdout = StringIOWrapper()
1231
 
        self.addCleanup(self.restoreUIFactory)
1232
 
 
1233
 
    def restoreUIFactory(self):
1234
 
        ui.ui_factory = self.old_factory
1235
 
        sys.stdout = self.old_stdout
1236
1222
 
1237
1223
    def get_user_url(self, user=None, password=None):
1238
1224
        """Build an url embedding user and password"""
1286
1272
    def test_prompt_for_password(self):
1287
1273
        self.server.add_user('joe', 'foo')
1288
1274
        t = self.get_user_transport('joe', None)
1289
 
        ui.ui_factory = TestUIFactory(stdin='foo\n')
 
1275
        stdout = StringIOWrapper()
 
1276
        ui.ui_factory = TestUIFactory(stdin='foo\n', stdout=stdout)
1290
1277
        self.assertEqual('contents of a\n',t.get('a').read())
1291
1278
        # stdin should be empty
1292
1279
        self.assertEqual('', ui.ui_factory.stdin.readline())
 
1280
        self._check_password_prompt(t._unqualified_scheme, 'joe',
 
1281
                                    stdout.getvalue())
1293
1282
        # And we shouldn't prompt again for a different request
1294
1283
        # against the same transport.
1295
1284
        self.assertEqual('contents of b\n',t.get('b').read())
1299
1288
        # Only one 'Authentication Required' error should occur
1300
1289
        self.assertEqual(1, self.server.auth_required_errors)
1301
1290
 
 
1291
    def _check_password_prompt(self, scheme, user, actual_prompt):
 
1292
        expected_prompt = (self._password_prompt_prefix
 
1293
                           + ("%s %s@%s:%d, Realm: '%s' password: "
 
1294
                              % (scheme.upper(),
 
1295
                                 user, self.server.host, self.server.port,
 
1296
                                 self.server.auth_realm)))
 
1297
        self.assertEquals(expected_prompt, actual_prompt)
 
1298
 
 
1299
    def test_no_prompt_for_password_when_using_auth_config(self):
 
1300
        user =' joe'
 
1301
        password = 'foo'
 
1302
        stdin_content = 'bar\n'  # Not the right password
 
1303
        self.server.add_user(user, password)
 
1304
        t = self.get_user_transport(user, None)
 
1305
        ui.ui_factory = TestUIFactory(stdin=stdin_content,
 
1306
                                      stdout=StringIOWrapper())
 
1307
        # Create a minimal config file with the right password
 
1308
        conf = config.AuthenticationConfig()
 
1309
        conf._get_config().update(
 
1310
            {'httptest': {'scheme': 'http', 'port': self.server.port,
 
1311
                          'user': user, 'password': password}})
 
1312
        conf._save()
 
1313
        # Issue a request to the server to connect
 
1314
        self.assertEqual('contents of a\n',t.get('a').read())
 
1315
        # stdin should have  been left untouched
 
1316
        self.assertEqual(stdin_content, ui.ui_factory.stdin.readline())
 
1317
        # Only one 'Authentication Required' error should occur
 
1318
        self.assertEqual(1, self.server.auth_required_errors)
 
1319
 
 
1320
 
1302
1321
 
1303
1322
class TestHTTPAuth(TestAuth):
1304
1323
    """Test HTTP authentication schemes.
1323
1342
    Daughter classes MUST also inherit from TestCaseWithWebserver.
1324
1343
    """
1325
1344
    _auth_header = 'Proxy-authorization'
 
1345
    _password_prompt_prefix = 'Proxy '
 
1346
 
1326
1347
 
1327
1348
    def setUp(self):
1328
1349
        TestCaseWithWebserver.setUp(self)