~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/launchpad/test_lp_directory.py

  • Committer: Aaron Bentley
  • Date: 2008-10-16 21:27:10 UTC
  • mfrom: (0.15.26 unshelve)
  • mto: (0.16.74 shelf-ui)
  • mto: This revision was merged to the branch mainline in revision 3820.
  • Revision ID: aaron@aaronbentley.com-20081016212710-h9av3nhk76dvmsv5
Merge with unshelve

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007, 2008 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Tests for directory lookup through Launchpad.net"""
 
18
 
 
19
import xmlrpclib
 
20
 
 
21
from bzrlib import (
 
22
    errors,
 
23
    )
 
24
from bzrlib.branch import Branch
 
25
from bzrlib.directory_service import directories
 
26
from bzrlib.tests import TestCase, TestCaseWithMemoryTransport
 
27
from bzrlib.transport import get_transport
 
28
from bzrlib.plugins.launchpad import _register_directory
 
29
from bzrlib.plugins.launchpad.lp_directory import (
 
30
    LaunchpadDirectory)
 
31
from bzrlib.plugins.launchpad.account import get_lp_login
 
32
 
 
33
 
 
34
class FakeResolveFactory(object):
 
35
    def __init__(self, test, expected_path, result):
 
36
        self._test = test
 
37
        self._expected_path = expected_path
 
38
        self._result = result
 
39
 
 
40
    def __call__(self, path):
 
41
        self._test.assertEqual(self._expected_path, path)
 
42
        return self
 
43
 
 
44
    def submit(self, service):
 
45
        self._service_url = service.service_url
 
46
        return self._result
 
47
 
 
48
 
 
49
class DirectoryUrlTests(TestCase):
 
50
    """Tests for branch urls through Launchpad.net directory"""
 
51
 
 
52
    def test_short_form(self):
 
53
        """A launchpad url should map to a http url"""
 
54
        factory = FakeResolveFactory(
 
55
            self, 'apt', dict(urls=[
 
56
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
 
57
        directory = LaunchpadDirectory()
 
58
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
 
59
                          directory._resolve('lp:apt', factory))
 
60
        # Make sure that resolve went to the production server.
 
61
        self.assertEquals('https://xmlrpc.edge.launchpad.net/bazaar/',
 
62
                          factory._service_url)
 
63
 
 
64
    def test_staging(self):
 
65
        """A launchpad url should map to a http url"""
 
66
        factory = FakeResolveFactory(
 
67
            self, 'apt', dict(urls=[
 
68
                    'http://bazaar.staging.launchpad.net/~apt/apt/devel']))
 
69
        url = 'lp://staging/apt'
 
70
        directory = LaunchpadDirectory()
 
71
        self.assertEquals('http://bazaar.staging.launchpad.net/~apt/apt/devel',
 
72
                          directory._resolve(url, factory))
 
73
        # Make sure that resolve went to the staging server.
 
74
        self.assertEquals('https://xmlrpc.staging.launchpad.net/bazaar/',
 
75
                          factory._service_url)
 
76
 
 
77
    def test_url_from_directory(self):
 
78
        """A launchpad url should map to a http url"""
 
79
        factory = FakeResolveFactory(
 
80
            self, 'apt', dict(urls=[
 
81
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
 
82
        directory = LaunchpadDirectory()
 
83
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
 
84
                          directory._resolve('lp:///apt', factory))
 
85
 
 
86
    def test_directory_skip_bad_schemes(self):
 
87
        factory = FakeResolveFactory(
 
88
            self, 'apt', dict(urls=[
 
89
                    'bad-scheme://bazaar.launchpad.net/~apt/apt/devel',
 
90
                    'http://bazaar.launchpad.net/~apt/apt/devel',
 
91
                    'http://another/location']))
 
92
        directory = LaunchpadDirectory()
 
93
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
 
94
                          directory._resolve('lp:///apt', factory))
 
95
 
 
96
    def test_directory_no_matching_schemes(self):
 
97
        # If the XMLRPC call does not return any protocols we support,
 
98
        # invalidURL is raised.
 
99
        factory = FakeResolveFactory(
 
100
            self, 'apt', dict(urls=[
 
101
                    'bad-scheme://bazaar.launchpad.net/~apt/apt/devel']))
 
102
        directory = LaunchpadDirectory()
 
103
        self.assertRaises(errors.InvalidURL,
 
104
                          directory._resolve, 'lp:///apt', factory)
 
105
 
 
106
    def test_directory_fault(self):
 
107
        # Test that XMLRPC faults get converted to InvalidURL errors.
 
108
        factory = FakeResolveFactory(self, 'apt', None)
 
109
        def submit(service):
 
110
            raise xmlrpclib.Fault(42, 'something went wrong')
 
111
        factory.submit = submit
 
112
        directory = LaunchpadDirectory()
 
113
        self.assertRaises(errors.InvalidURL,
 
114
                          directory._resolve, 'lp:///apt', factory)
 
115
 
 
116
    def test_skip_bzr_ssh_launchpad_net_when_anonymous(self):
 
117
        # Test that bzr+ssh://bazaar.launchpad.net gets skipped if
 
118
        # Bazaar does not know the user's Launchpad ID:
 
119
        self.assertEqual(None, get_lp_login())
 
120
        factory = FakeResolveFactory(
 
121
            self, 'apt', dict(urls=[
 
122
                    'bzr+ssh://bazaar.launchpad.net/~apt/apt/devel',
 
123
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
 
124
        directory = LaunchpadDirectory()
 
125
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
 
126
                          directory._resolve('lp:///apt', factory))
 
127
 
 
128
    def test_skip_sftp_launchpad_net_when_anonymous(self):
 
129
        # Test that sftp://bazaar.launchpad.net gets skipped if
 
130
        # Bazaar does not know the user's Launchpad ID:
 
131
        self.assertEqual(None, get_lp_login())
 
132
        factory = FakeResolveFactory(
 
133
            self, 'apt', dict(urls=[
 
134
                    'sftp://bazaar.launchpad.net/~apt/apt/devel',
 
135
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
 
136
        directory = LaunchpadDirectory()
 
137
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
 
138
                          directory._resolve('lp:///apt', factory))
 
139
 
 
140
    def test_rewrite_bzr_ssh_launchpad_net(self):
 
141
        # Test that bzr+ssh URLs get rewritten to include the user's
 
142
        # Launchpad ID (assuming we know the Launchpad ID).
 
143
        factory = FakeResolveFactory(
 
144
            self, 'apt', dict(urls=[
 
145
                    'bzr+ssh://bazaar.launchpad.net/~apt/apt/devel',
 
146
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
 
147
        directory = LaunchpadDirectory()
 
148
        self.assertEquals(
 
149
            'bzr+ssh://username@bazaar.launchpad.net/~apt/apt/devel',
 
150
            directory._resolve('lp:///apt', factory, _lp_login='username'))
 
151
 
 
152
    def test_no_rewrite_of_other_bzr_ssh(self):
 
153
        # Test that we don't rewrite bzr+ssh URLs for other 
 
154
        self.assertEqual(None, get_lp_login())
 
155
        factory = FakeResolveFactory(
 
156
            self, 'apt', dict(urls=[
 
157
                    'bzr+ssh://example.com/~apt/apt/devel',
 
158
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
 
159
        directory = LaunchpadDirectory()
 
160
        self.assertEquals('bzr+ssh://example.com/~apt/apt/devel',
 
161
                          directory._resolve('lp:///apt', factory))
 
162
 
 
163
    # TODO: check we get an error if the url is unreasonable
 
164
    def test_error_for_bad_url(self):
 
165
        directory = LaunchpadDirectory()
 
166
        self.assertRaises(errors.InvalidURL,
 
167
            directory._resolve, 'lp://ratotehunoahu')
 
168
 
 
169
 
 
170
class DirectoryOpenBranchTests(TestCaseWithMemoryTransport):
 
171
 
 
172
    def test_directory_open_branch(self):
 
173
        # Test that opening an lp: branch redirects to the real location.
 
174
        target_branch = self.make_branch('target')
 
175
        class FooService(object):
 
176
            """A directory service that maps the name to a FILE url"""
 
177
 
 
178
            def look_up(self, name, url):
 
179
                if 'lp:///apt' == url:
 
180
                    return target_branch.base.rstrip('/')
 
181
                return '!unexpected look_up value!'
 
182
 
 
183
        directories.remove('lp:')
 
184
        directories.register('lp:', FooService, 'Map lp URLs to local urls')
 
185
        self.addCleanup(_register_directory)
 
186
        self.addCleanup(lambda: directories.remove('lp:'))
 
187
        transport = get_transport('lp:///apt')
 
188
        branch = Branch.open_from_transport(transport)
 
189
        self.assertEqual(target_branch.base, branch.base)