~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Alexander Belchenko
  • Date: 2007-11-19 22:54:30 UTC
  • mfrom: (3006 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3008.
  • Revision ID: bialix@ukr.net-20071119225430-x0ewosrsagis0yno
merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for indirect branch urls through Launchpad.net"""
18
18
 
 
19
import xmlrpclib
 
20
 
19
21
from bzrlib import (
20
22
    errors,
21
 
    transport,
22
23
    )
 
24
from bzrlib.branch import Branch
 
25
from bzrlib.tests import TestCase, TestCaseWithMemoryTransport
23
26
from bzrlib.transport import get_transport
24
 
from bzrlib.tests import TestCase, TestSkipped
 
27
from bzrlib.plugins.launchpad.lp_indirect import (
 
28
    LaunchpadTransport)
 
29
from bzrlib.plugins.launchpad.account import get_lp_login
 
30
 
 
31
 
 
32
class FakeResolveFactory(object):
 
33
    def __init__(self, test, expected_path, result):
 
34
        self._test = test
 
35
        self._expected_path = expected_path
 
36
        self._result = result
 
37
 
 
38
    def __call__(self, path):
 
39
        self._test.assertEqual(self._expected_path, path)
 
40
        return self
 
41
 
 
42
    def submit(self, service):
 
43
        return self._result
25
44
 
26
45
 
27
46
class IndirectUrlTests(TestCase):
29
48
 
30
49
    def test_short_form(self):
31
50
        """A launchpad url should map to a http url"""
32
 
        url = 'lp:apt'
33
 
        t = get_transport(url)
34
 
        self.assertEquals(t.base, 'http://code.launchpad.net/apt/')
 
51
        factory = FakeResolveFactory(
 
52
            self, 'apt', dict(urls=[
 
53
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
 
54
        transport = LaunchpadTransport('lp:///')
 
55
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
 
56
                          transport._resolve('lp:apt', factory))
35
57
 
36
58
    def test_indirect_through_url(self):
37
59
        """A launchpad url should map to a http url"""
38
 
        # These can change to use the smartserver protocol or something 
39
 
        # else in the future.
40
 
        url = 'lp:///apt'
41
 
        t = get_transport(url)
42
 
        real_url = t.base
43
 
        self.assertEquals(real_url, 'http://code.launchpad.net/apt/')
 
60
        factory = FakeResolveFactory(
 
61
            self, 'apt', dict(urls=[
 
62
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
 
63
        transport = LaunchpadTransport('lp:///')
 
64
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
 
65
                          transport._resolve('lp:///apt', factory))
 
66
 
 
67
    def test_indirect_skip_bad_schemes(self):
 
68
        factory = FakeResolveFactory(
 
69
            self, 'apt', dict(urls=[
 
70
                    'bad-scheme://bazaar.launchpad.net/~apt/apt/devel',
 
71
                    'http://bazaar.launchpad.net/~apt/apt/devel',
 
72
                    'http://another/location']))
 
73
        transport = LaunchpadTransport('lp:///')
 
74
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
 
75
                          transport._resolve('lp:///apt', factory))
 
76
 
 
77
    def test_indirect_no_matching_schemes(self):
 
78
        # If the XMLRPC call does not return any protocols we support,
 
79
        # invalidURL is raised.
 
80
        factory = FakeResolveFactory(
 
81
            self, 'apt', dict(urls=[
 
82
                    'bad-scheme://bazaar.launchpad.net/~apt/apt/devel']))
 
83
        transport = LaunchpadTransport('lp:///')
 
84
        self.assertRaises(errors.InvalidURL,
 
85
                          transport._resolve, 'lp:///apt', factory)
 
86
 
 
87
    def test_indirect_fault(self):
 
88
        # Test that XMLRPC faults get converted to InvalidURL errors.
 
89
        factory = FakeResolveFactory(self, 'apt', None)
 
90
        def submit(service):
 
91
            raise xmlrpclib.Fault(42, 'something went wrong')
 
92
        factory.submit = submit
 
93
        transport = LaunchpadTransport('lp:///')
 
94
        self.assertRaises(errors.InvalidURL,
 
95
                          transport._resolve, 'lp:///apt', factory)
 
96
 
 
97
    def test_skip_bzr_ssh_launchpad_net_when_anonymous(self):
 
98
        # Test that bzr+ssh://bazaar.launchpad.net gets skipped if
 
99
        # Bazaar does not know the user's Launchpad ID:
 
100
        self.assertEqual(None, get_lp_login())
 
101
        factory = FakeResolveFactory(
 
102
            self, 'apt', dict(urls=[
 
103
                    'bzr+ssh://bazaar.launchpad.net/~apt/apt/devel',
 
104
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
 
105
        transport = LaunchpadTransport('lp:///')
 
106
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
 
107
                          transport._resolve('lp:///apt', factory))
 
108
 
 
109
    def test_rewrite_bzr_ssh_launchpad_net(self):
 
110
        # Test that bzr+ssh URLs get rewritten to include the user's
 
111
        # Launchpad ID (assuming we know the Launchpad ID).
 
112
        factory = FakeResolveFactory(
 
113
            self, 'apt', dict(urls=[
 
114
                    'bzr+ssh://bazaar.launchpad.net/~apt/apt/devel',
 
115
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
 
116
        transport = LaunchpadTransport('lp:///')
 
117
        self.assertEquals(
 
118
            'bzr+ssh://username@bazaar.launchpad.net/~apt/apt/devel',
 
119
            transport._resolve('lp:///apt', factory, _lp_login='username'))
 
120
 
 
121
    def test_no_rewrite_of_other_bzr_ssh(self):
 
122
        # Test that we don't rewrite bzr+ssh URLs for other 
 
123
        self.assertEqual(None, get_lp_login())
 
124
        factory = FakeResolveFactory(
 
125
            self, 'apt', dict(urls=[
 
126
                    'bzr+ssh://example.com/~apt/apt/devel',
 
127
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
 
128
        transport = LaunchpadTransport('lp:///')
 
129
        self.assertEquals('bzr+ssh://example.com/~apt/apt/devel',
 
130
                          transport._resolve('lp:///apt', factory))
44
131
 
45
132
    # TODO: check we get an error if the url is unreasonable
46
133
    def test_error_for_bad_indirection(self):
47
134
        self.assertRaises(errors.InvalidURL,
48
 
            get_transport,
49
 
            'lp://ratotehunoahu')
50
 
 
 
135
            LaunchpadTransport, 'lp://ratotehunoahu')
 
136
 
 
137
    def catch_redirect(self, methodname, *args):
 
138
        transport = LaunchpadTransport('lp:///apt')
 
139
        def _resolve(abspath):
 
140
            self.assertEqual('lp:///apt', abspath)
 
141
            return 'http://example.com/~apt/apt/devel'
 
142
        transport._resolve = _resolve
 
143
        try:
 
144
            getattr(transport, methodname)(*args)
 
145
        except errors.RedirectRequested, exc:
 
146
            return exc
 
147
        else:
 
148
            raise self.failException('RedirectRequested not raised')
 
149
 
 
150
    def test_redirect_on_get(self):
 
151
        exc = self.catch_redirect('get', '.bzr/branch-format')
 
152
        self.assertEqual('lp:///apt/.bzr/branch-format', exc.source)
 
153
        self.assertEqual(
 
154
            'http://example.com/~apt/apt/devel/.bzr/branch-format', exc.target)
 
155
 
 
156
    def test_redirect_on_mkdir(self):
 
157
        exc = self.catch_redirect('mkdir', '.')
 
158
        self.assertEqual('lp:///apt', exc.source)
 
159
        self.assertEqual(
 
160
            'http://example.com/~apt/apt/devel', exc.target)
 
161
 
 
162
 
 
163
class IndirectOpenBranchTests(TestCaseWithMemoryTransport):
 
164
 
 
165
    def test_indirect_open_branch(self):
 
166
        # Test that opening an lp: branch redirects to the real location.
 
167
        target_branch = self.make_branch('target')
 
168
        transport = get_transport('lp:///apt')
 
169
        def _resolve(abspath):
 
170
            self.assertEqual('lp:///apt', abspath)
 
171
            return target_branch.base.rstrip('/')
 
172
        transport._resolve = _resolve
 
173
        branch = Branch.open_from_transport(transport)
 
174
        self.assertEqual(target_branch.base, branch.base)