~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Vincent Ladeuil
  • Date: 2011-06-07 08:12:32 UTC
  • mto: This revision was merged to the branch mainline in revision 5959.
  • Revision ID: v.ladeuil+lp@free.fr-20110607081232-yvtbewkbc6no8wsq
Tweak the http Authorization tests to make some parts easier to reuse (parametrization mostly).

Show diffs side-by-side

added added

removed removed

Lines of Context:
91
91
        ]
92
92
 
93
93
 
 
94
def vary_by_http_auth_scheme():
 
95
    scenarios = [
 
96
        ('basic', dict(_auth_server=http_utils.HTTPBasicAuthServer)),
 
97
        ('digest', dict(_auth_server=http_utils.HTTPDigestAuthServer)),
 
98
        ('basicdigest',
 
99
            dict(_auth_server=http_utils.HTTPBasicAndDigestAuthServer)),
 
100
        ]
 
101
    # Add some attributes common to all scenarios
 
102
    for scenario_id, scenario_dict in scenarios:
 
103
        scenario_dict.update(_username_prompt_prefix='',
 
104
                             _password_prompt_prefix='')
 
105
    return scenarios
 
106
 
 
107
 
94
108
def vary_by_http_proxy_auth_scheme():
95
 
    return [
 
109
    scenarios = [
96
110
        ('basic', dict(_auth_server=http_utils.ProxyBasicAuthServer)),
97
111
        ('digest', dict(_auth_server=http_utils.ProxyDigestAuthServer)),
98
112
        ('basicdigest',
99
113
            dict(_auth_server=http_utils.ProxyBasicAndDigestAuthServer)),
100
114
        ]
101
 
 
102
 
 
103
 
def vary_by_http_auth_scheme():
104
 
    return [
105
 
        ('basic', dict(_auth_server=http_utils.HTTPBasicAuthServer)),
106
 
        ('digest', dict(_auth_server=http_utils.HTTPDigestAuthServer)),
107
 
        ('basicdigest',
108
 
            dict(_auth_server=http_utils.HTTPBasicAndDigestAuthServer)),
109
 
        ]
 
115
    # Add some attributes common to all scenarios
 
116
    for scenario_id, scenario_dict in scenarios:
 
117
        scenario_dict.update(_username_prompt_prefix='Proxy ',
 
118
                             _password_prompt_prefix='Proxy ')
 
119
    return scenarios
110
120
 
111
121
 
112
122
def vary_by_http_activity():
1489
1499
                          self.get_a, self.old_transport, redirected)
1490
1500
 
1491
1501
 
 
1502
def _setup_authentication_config(**kwargs):
 
1503
    conf = config.AuthenticationConfig()
 
1504
    conf._get_config().update({'httptest': kwargs})
 
1505
    conf._save()
 
1506
 
 
1507
 
 
1508
class TestUrllib2AuthHandler(tests.TestCaseWithTransport):
 
1509
    """Unit tests for glue by which urllib2 asks us for authentication"""
 
1510
 
 
1511
    def test_get_user_password_without_port(self):
 
1512
        """We cope if urllib2 doesn't tell us the port.
 
1513
 
 
1514
        See https://bugs.launchpad.net/bzr/+bug/654684
 
1515
        """
 
1516
        user = 'joe'
 
1517
        password = 'foo'
 
1518
        _setup_authentication_config(scheme='http', host='localhost',
 
1519
                                     user=user, password=password)
 
1520
        handler = _urllib2_wrappers.HTTPAuthHandler()
 
1521
        got_pass = handler.get_user_password(dict(
 
1522
            user='joe',
 
1523
            protocol='http',
 
1524
            host='localhost',
 
1525
            path='/',
 
1526
            realm='Realm',
 
1527
            ))
 
1528
        self.assertEquals((user, password), got_pass)
 
1529
 
 
1530
 
1492
1531
class TestAuth(http_utils.TestCaseWithWebserver):
1493
1532
    """Test authentication scheme"""
1494
1533
 
1498
1537
        vary_by_http_auth_scheme(),
1499
1538
        )
1500
1539
 
1501
 
    _auth_header = 'Authorization'
1502
 
    _password_prompt_prefix = ''
1503
 
    _username_prompt_prefix = ''
1504
 
    # Set by load_tests
1505
 
    _auth_server = None
1506
 
 
1507
1540
    def setUp(self):
1508
1541
        super(TestAuth, self).setUp()
1509
1542
        self.server = self.get_readonly_server()
1650
1683
        ui.ui_factory = tests.TestUIFactory(stdin=stdin_content,
1651
1684
                                            stderr=tests.StringIOWrapper())
1652
1685
        # Create a minimal config file with the right password
1653
 
        _setup_authentication_config(
1654
 
            scheme='http', 
1655
 
            port=self.server.port,
1656
 
            user=user,
1657
 
            password=password)
 
1686
        _setup_authentication_config(scheme='http', port=self.server.port,
 
1687
                                     user=user, password=password)
1658
1688
        # Issue a request to the server to connect
1659
1689
        self.assertEqual('contents of a\n',t.get('a').read())
1660
1690
        # stdin should have  been left untouched
1690
1720
        user = 'joe'
1691
1721
        password = 'foo'
1692
1722
        self.server.add_user(user, password)
1693
 
        _setup_authentication_config(
1694
 
            scheme='http', 
1695
 
            port=self.server.port,
1696
 
            user=user,
1697
 
            password=password)
 
1723
        _setup_authentication_config(scheme='http', port=self.server.port,
 
1724
                                     user=user, password=password)
1698
1725
        t = self.get_user_transport(None, None)
1699
1726
        # Issue a request to the server to connect
1700
1727
        self.assertEqual('contents of a\n', t.get('a').read())
1702
1729
        self.assertEqual(1, self.server.auth_required_errors)
1703
1730
 
1704
1731
 
1705
 
def _setup_authentication_config(**kwargs):
1706
 
    conf = config.AuthenticationConfig()
1707
 
    conf._get_config().update({'httptest': kwargs})
1708
 
    conf._save()
1709
 
 
1710
 
 
1711
 
 
1712
 
class TestUrllib2AuthHandler(tests.TestCaseWithTransport):
1713
 
    """Unit tests for glue by which urllib2 asks us for authentication"""
1714
 
 
1715
 
    def test_get_user_password_without_port(self):
1716
 
        """We cope if urllib2 doesn't tell us the port.
1717
 
 
1718
 
        See https://bugs.launchpad.net/bzr/+bug/654684
1719
 
        """
1720
 
        user = 'joe'
1721
 
        password = 'foo'
1722
 
        _setup_authentication_config(
1723
 
            scheme='http', 
1724
 
            host='localhost',
1725
 
            user=user,
1726
 
            password=password)
1727
 
        handler = _urllib2_wrappers.HTTPAuthHandler()
1728
 
        got_pass = handler.get_user_password(dict(
1729
 
            user='joe',
1730
 
            protocol='http',
1731
 
            host='localhost',
1732
 
            path='/',
1733
 
            realm='Realm',
1734
 
            ))
1735
 
        self.assertEquals((user, password), got_pass)
1736
 
 
1737
 
 
1738
1732
class TestProxyAuth(TestAuth):
1739
 
    """Test proxy authentication schemes."""
 
1733
    """Test proxy authentication schemes.
 
1734
 
 
1735
    This inherits from TestAuth to tweak the setUp and filter some failing
 
1736
    tests.
 
1737
    """
1740
1738
 
1741
1739
    scenarios = multiply_scenarios(
1742
1740
        vary_by_http_client_implementation(),
1744
1742
        vary_by_http_proxy_auth_scheme(),
1745
1743
        )
1746
1744
 
1747
 
    _auth_header = 'Proxy-authorization'
1748
 
    _password_prompt_prefix = 'Proxy '
1749
 
    _username_prompt_prefix = 'Proxy '
1750
 
 
1751
1745
    def setUp(self):
1752
1746
        super(TestProxyAuth, self).setUp()
1753
1747
        # Override the contents to avoid false positives