/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2006 Canonical Ltd
0.4.1 by Martin Pool
Start lp-register command
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
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
17
import base64
0.4.13 by Martin Pool
Update xmlrpc api to pass product name as a parameter.
18
import os
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
19
from StringIO import StringIO
0.4.5 by Martin Pool
Add structured test for parameters passed through xmlrpc
20
import xmlrpclib
21
0.4.13 by Martin Pool
Update xmlrpc api to pass product name as a parameter.
22
from bzrlib.tests import TestCase, TestSkipped
0.4.1 by Martin Pool
Start lp-register command
23
0.4.5 by Martin Pool
Add structured test for parameters passed through xmlrpc
24
# local import
2898.3.8 by James Henstridge
Get rid of relative imports in Launchpad plugin.
25
from bzrlib.plugins.launchpad.lp_registration import (
0.4.26 by Martin Pool
(register-branch) Add test for link_branch_to_bug and fix its parameters
26
        BaseRequest,
27
        BranchBugLinkRequest,
28
        BranchRegistrationRequest,
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
29
        ResolveLaunchpadPathRequest,
0.4.26 by Martin Pool
(register-branch) Add test for link_branch_to_bug and fix its parameters
30
        LaunchpadService,
31
        )
0.4.5 by Martin Pool
Add structured test for parameters passed through xmlrpc
32
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
33
0.4.12 by Martin Pool
doc
34
# TODO: Test that the command-line client, making sure that it'll pass the
35
# request through to a dummy transport, and that the transport will validate
36
# the results passed in.  Not sure how to get the transport object back out to
37
# validate that its OK - may not be necessary.
38
39
# TODO: Add test for (and implement) other command-line options to set
0.4.14 by Martin Pool
Update xmlrpc api
40
# project, author_email, description.
0.4.12 by Martin Pool
doc
41
42
# TODO: project_id is not properly handled -- must be passed in rpc or path.
43
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
44
class InstrumentedXMLRPCConnection(object):
45
    """Stands in place of an http connection for the purposes of testing"""
46
47
    def __init__(self, testcase):
48
        self.testcase = testcase
49
50
    def getreply(self):
51
        """Fake the http reply.
52
53
        :returns: (errcode, errmsg, headers)
54
        """
55
        return (200, 'OK', [])
56
57
    def getfile(self):
58
        """Return a fake file containing the response content."""
59
        return StringIO('''\
60
<?xml version="1.0" ?>
61
<methodResponse>
0.4.11 by Martin Pool
Check the correct params are seen by the server
62
    <params>
63
        <param>
64
            <value>
65
                <string>victoria dock</string>
66
            </value>
67
        </param>
68
    </params>
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
69
</methodResponse>''')
70
71
72
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
73
class InstrumentedXMLRPCTransport(xmlrpclib.Transport):
74
2027.2.2 by Marien Zwart
Fixes for python 2.5.
75
    # Python 2.5's xmlrpclib looks for this.
76
    _use_datetime = False
77
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
78
    def __init__(self, testcase, expect_auth):
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
79
        self.testcase = testcase
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
80
        self.expect_auth = expect_auth
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
81
82
    def make_connection(self, host):
83
        host, http_headers, x509 = self.get_host_info(host)
84
        test = self.testcase
0.4.10 by Martin Pool
Move test-specific values out of InstrumentedXMLRPCTransport
85
        self.connected_host = host
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
86
        if self.expect_auth:
87
            auth_hdrs = [v for k,v in http_headers if k == 'Authorization']
88
            assert len(auth_hdrs) == 1
89
            authinfo = auth_hdrs[0]
90
            expected_auth = 'testuser@launchpad.net:testpassword'
91
            test.assertEquals(authinfo,
92
                    'Basic ' + base64.encodestring(expected_auth).strip())
93
        else:
94
            assert not http_headers
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
95
        return InstrumentedXMLRPCConnection(test)
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
96
97
    def send_request(self, connection, handler_path, request_body):
98
        test = self.testcase
99
        self.got_request = True
100
101
    def send_host(self, conn, host):
102
        pass
103
104
    def send_user_agent(self, conn):
105
        # TODO: send special user agent string, including bzrlib version
106
        # number
107
        pass
108
109
    def send_content(self, conn, request_body):
110
        unpacked, method = xmlrpclib.loads(request_body)
0.4.10 by Martin Pool
Move test-specific values out of InstrumentedXMLRPCTransport
111
        assert None not in unpacked, \
112
                "xmlrpc result %r shouldn't contain None" % (unpacked,)
113
        self.sent_params = unpacked
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
114
115
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
116
class MockLaunchpadService(LaunchpadService):
117
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
118
    def send_request(self, method_name, method_params, authenticated):
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
119
        """Stash away the method details rather than sending them to a real server"""
120
        self.called_method_name = method_name
121
        self.called_method_params = method_params
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
122
        self.called_authenticated = authenticated
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
123
124
0.4.1 by Martin Pool
Start lp-register command
125
class TestBranchRegistration(TestCase):
0.4.4 by Martin Pool
Start forming xmlrpc requests
126
    SAMPLE_URL = 'http://bazaar-vcs.org/bzr/bzr.dev/'
0.4.6 by Martin Pool
Put the rest of the parameters into the registration request.
127
    SAMPLE_OWNER = 'jhacker@foo.com'
128
    SAMPLE_BRANCH_ID = 'bzr.dev'
0.4.4 by Martin Pool
Start forming xmlrpc requests
129
0.4.23 by Martin Pool
(register-branch) fix ordering of parameters and restore transport-level test.
130
    def setUp(self):
131
        super(TestBranchRegistration, self).setUp()
132
        # make sure we have a reproducible standard environment
133
        if 'BZR_LP_XMLRPC_URL' in os.environ:
134
            del os.environ['BZR_LP_XMLRPC_URL']
135
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
136
    def test_register_help(self):
137
        """register-branch accepts --help"""
2581.1.4 by Martin Pool
fixup run_bzr syntax in launchpad plugin
138
        out, err = self.run_bzr(['register-branch', '--help'])
0.4.2 by Martin Pool
Rename command to 'register-branch'
139
        self.assertContainsRe(out, r'Register a branch')
140
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
141
    def test_register_no_url(self):
142
        """register-branch command requires parameters"""
0.4.3 by Martin Pool
More command line processing
143
        self.run_bzr('register-branch', retcode=3)
144
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
145
    def test_register_dry_run(self):
2581.1.4 by Martin Pool
fixup run_bzr syntax in launchpad plugin
146
        out, err = self.run_bzr(['register-branch',
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
147
                                'http://test-server.com/bzr/branch',
2581.1.4 by Martin Pool
fixup run_bzr syntax in launchpad plugin
148
                                '--dry-run'])
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
149
        self.assertEquals(out, 'Branch registered.\n')
0.4.4 by Martin Pool
Start forming xmlrpc requests
150
0.4.23 by Martin Pool
(register-branch) fix ordering of parameters and restore transport-level test.
151
    def test_onto_transport(self):
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
152
        """Test how the request is sent by transmitting across a mock Transport"""
0.4.23 by Martin Pool
(register-branch) fix ordering of parameters and restore transport-level test.
153
        # use a real transport, but intercept at the http/xml layer
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
154
        transport = InstrumentedXMLRPCTransport(self, expect_auth=True)
0.4.30 by Martin Pool
Fix wierd syntax errors in test
155
        service = LaunchpadService(transport)
0.4.23 by Martin Pool
(register-branch) fix ordering of parameters and restore transport-level test.
156
        service.registrant_email = 'testuser@launchpad.net'
157
        service.registrant_password = 'testpassword'
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
158
        rego = BranchRegistrationRequest('http://test-server.com/bzr/branch',
0.4.23 by Martin Pool
(register-branch) fix ordering of parameters and restore transport-level test.
159
                'branch-id',
160
                'my test branch',
161
                'description',
162
                'author@launchpad.net',
163
                'product')
0.4.20 by Bjorn Tillenius
try to fix the tests.
164
        rego.submit(service)
0.4.10 by Martin Pool
Move test-specific values out of InstrumentedXMLRPCTransport
165
        self.assertEquals(transport.connected_host, 'xmlrpc.launchpad.net')
0.4.14 by Martin Pool
Update xmlrpc api
166
        self.assertEquals(len(transport.sent_params), 6)
0.4.11 by Martin Pool
Check the correct params are seen by the server
167
        self.assertEquals(transport.sent_params,
0.4.23 by Martin Pool
(register-branch) fix ordering of parameters and restore transport-level test.
168
                ('http://test-server.com/bzr/branch',  # branch_url
169
                 'branch-id',                          # branch_name
170
                 'my test branch',                     # branch_title
171
                 'description',
172
                 'author@launchpad.net',
173
                 'product'))
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
174
        self.assertTrue(transport.got_request)
0.4.13 by Martin Pool
Update xmlrpc api to pass product name as a parameter.
175
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
176
    def test_onto_transport_unauthenticated(self):
177
        """Test how an unauthenticated request is transmitted across a mock Transport"""
178
        transport = InstrumentedXMLRPCTransport(self, expect_auth=False)
179
        service = LaunchpadService(transport)
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
180
        resolve = ResolveLaunchpadPathRequest('bzr')
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
181
        resolve.submit(service)
182
        self.assertEquals(transport.connected_host, 'xmlrpc.launchpad.net')
183
        self.assertEquals(len(transport.sent_params), 1)
184
        self.assertEquals(transport.sent_params, ('bzr', ))
185
        self.assertTrue(transport.got_request)
186
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
187
    def test_subclass_request(self):
188
        """Define a new type of xmlrpc request"""
189
        class DummyRequest(BaseRequest):
190
            _methodname = 'dummy_request'
191
            def _request_params(self):
192
                return (42,)
193
194
        service = MockLaunchpadService()
195
        service.registrant_email = 'test@launchpad.net'
196
        service.registrant_password = ''
197
        request = DummyRequest()
198
        request.submit(service)
199
        self.assertEquals(service.called_method_name, 'dummy_request')
200
        self.assertEquals(service.called_method_params, (42,))
201
0.4.24 by Martin Pool
(register-branch) additional test case against mock server
202
    def test_mock_server_registration(self):
203
        """Send registration to mock server"""
204
        test_case = self
205
        class MockRegistrationService(MockLaunchpadService):
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
206
            def send_request(self, method_name, method_params, authenticated):
0.4.24 by Martin Pool
(register-branch) additional test case against mock server
207
                test_case.assertEquals(method_name, "register_branch")
208
                test_case.assertEquals(list(method_params),
209
                        ['url', 'name', 'title', 'description', 'email', 'name'])
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
210
                test_case.assertEquals(authenticated, True)
0.4.24 by Martin Pool
(register-branch) additional test case against mock server
211
                return 'result'
212
        service = MockRegistrationService()
213
        rego = BranchRegistrationRequest('url', 'name', 'title',
214
                        'description', 'email', 'name')
215
        result = rego.submit(service)
216
        self.assertEquals(result, 'result')
0.4.25 by Martin Pool
(register-branch) additional test for registration with defaults
217
218
    def test_mock_server_registration_with_defaults(self):
219
        """Send registration to mock server"""
220
        test_case = self
221
        class MockRegistrationService(MockLaunchpadService):
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
222
            def send_request(self, method_name, method_params, authenticated):
0.4.25 by Martin Pool
(register-branch) additional test for registration with defaults
223
                test_case.assertEquals(method_name, "register_branch")
224
                test_case.assertEquals(list(method_params),
225
                        ['http://server/branch', 'branch', '', '', '', ''])
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
226
                test_case.assertEquals(authenticated, True)
0.4.25 by Martin Pool
(register-branch) additional test for registration with defaults
227
                return 'result'
228
        service = MockRegistrationService()
229
        rego = BranchRegistrationRequest('http://server/branch')
230
        result = rego.submit(service)
231
        self.assertEquals(result, 'result')
0.4.26 by Martin Pool
(register-branch) Add test for link_branch_to_bug and fix its parameters
232
233
    def test_mock_bug_branch_link(self):
234
        """Send bug-branch link to mock server"""
235
        test_case = self
236
        class MockService(MockLaunchpadService):
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
237
            def send_request(self, method_name, method_params, authenticated):
0.4.26 by Martin Pool
(register-branch) Add test for link_branch_to_bug and fix its parameters
238
                test_case.assertEquals(method_name, "link_branch_to_bug")
239
                test_case.assertEquals(list(method_params),
240
                        ['http://server/branch', 1234, ''])
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
241
                test_case.assertEquals(authenticated, True)
0.4.26 by Martin Pool
(register-branch) Add test for link_branch_to_bug and fix its parameters
242
                return 'http://launchpad.net/bug/1234'
243
        service = MockService()
244
        rego = BranchBugLinkRequest('http://server/branch', 1234)
245
        result = rego.submit(service)
246
        self.assertEquals(result, 'http://launchpad.net/bug/1234')
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
247
248
    def test_mock_resolve_lp_url(self):
249
        test_case = self
250
        class MockService(MockLaunchpadService):
251
            def send_request(self, method_name, method_params, authenticated):
2898.4.7 by James Henstridge
Fix up tests.
252
                test_case.assertEquals(method_name, "resolve_lp_path")
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
253
                test_case.assertEquals(list(method_params), ['bzr'])
254
                test_case.assertEquals(authenticated, False)
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
255
                return dict(urls=[
256
                        'bzr+ssh://bazaar.launchpad.net~bzr/bzr/trunk',
257
                        'sftp://bazaar.launchpad.net~bzr/bzr/trunk',
258
                        'bzr+http://bazaar.launchpad.net~bzr/bzr/trunk',
259
                        'http://bazaar.launchpad.net~bzr/bzr/trunk'])
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
260
        service = MockService()
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
261
        resolve = ResolveLaunchpadPathRequest('bzr')
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
262
        result = resolve.submit(service)
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
263
        self.assertTrue('urls' in result)
264
        self.assertEquals(result['urls'], [
265
                'bzr+ssh://bazaar.launchpad.net~bzr/bzr/trunk',
266
                'sftp://bazaar.launchpad.net~bzr/bzr/trunk',
267
                'bzr+http://bazaar.launchpad.net~bzr/bzr/trunk',
268
                'http://bazaar.launchpad.net~bzr/bzr/trunk'])