/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6622.7.1 by Colin Watson
Remove `bzr register-branch`, since it has not worked for a long time.
1
# Copyright (C) 2006-2012, 2016-2017 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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.4.1 by Martin Pool
Start lp-register command
16
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
17
import base64
6622.7.2 by Jelmer Vernooij
Merge lp:brz.
18
from io import BytesIO
6973.12.9 by Jelmer Vernooij
More fixes.
19
20
try:
21
    from xmlrpc.client import (
22
        loads as xmlrpc_loads,
23
        Transport,
24
        )
25
except ImportError:  # python < 3
26
    from xmlrpclib import (
27
        loads as xmlrpc_loads,
28
        Transport,
29
        )
0.4.5 by Martin Pool
Add structured test for parameters passed through xmlrpc
30
7078.12.1 by Jelmer Vernooij
Fix registration test on Python 3.
31
from ...sixish import PY3
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
32
from ...tests import TestCaseWithTransport
0.4.1 by Martin Pool
Start lp-register command
33
0.4.5 by Martin Pool
Add structured test for parameters passed through xmlrpc
34
# local import
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
35
from .lp_registration import (
0.4.26 by Martin Pool
(register-branch) Add test for link_branch_to_bug and fix its parameters
36
        BaseRequest,
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
37
        ResolveLaunchpadPathRequest,
0.4.26 by Martin Pool
(register-branch) Add test for link_branch_to_bug and fix its parameters
38
        LaunchpadService,
39
        )
0.4.5 by Martin Pool
Add structured test for parameters passed through xmlrpc
40
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
41
0.4.12 by Martin Pool
doc
42
# TODO: Test that the command-line client, making sure that it'll pass the
43
# request through to a dummy transport, and that the transport will validate
44
# the results passed in.  Not sure how to get the transport object back out to
45
# validate that its OK - may not be necessary.
46
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
47
class InstrumentedXMLRPCConnection(object):
48
    """Stands in place of an http connection for the purposes of testing"""
49
50
    def __init__(self, testcase):
51
        self.testcase = testcase
52
53
    def getreply(self):
54
        """Fake the http reply.
55
56
        :returns: (errcode, errmsg, headers)
57
        """
58
        return (200, 'OK', [])
59
5582.1.1 by Bazaar Builbot Net
Quick-and-dirty hack to fix bug #654733
60
    def getresponse(self, buffering=True):
5582.1.2 by Jelmer Vernooij
Add NEWS entry, comment. Remove unnecessary code.
61
        """Fake the http reply.
62
63
        This is used when running on Python 2.7, where xmlrpclib uses
64
        httplib.HTTPConnection in a different way than before.
65
        """
5582.1.1 by Bazaar Builbot Net
Quick-and-dirty hack to fix bug #654733
66
        class FakeHttpResponse(object):
67
5582.13.1 by Vincent Ladeuil
A bit less dirty and bit more robust so that it also works on FreeBSD8 (and may others).
68
            def __init__(self, status, reason, body):
5582.1.1 by Bazaar Builbot Net
Quick-and-dirty hack to fix bug #654733
69
                self.status = status
70
                self.reason = reason
71
                self.body = body
72
73
            def read(self, size=-1):
74
                return self.body.read(size)
75
5582.13.1 by Vincent Ladeuil
A bit less dirty and bit more robust so that it also works on FreeBSD8 (and may others).
76
            def getheader(self, name, default):
77
                # We don't have headers
78
                return default
79
80
        return FakeHttpResponse(200, 'OK', self.getfile())
5582.1.1 by Bazaar Builbot Net
Quick-and-dirty hack to fix bug #654733
81
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
82
    def getfile(self):
83
        """Return a fake file containing the response content."""
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
84
        return BytesIO(b'''\
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
85
<?xml version="1.0" ?>
86
<methodResponse>
0.4.11 by Martin Pool
Check the correct params are seen by the server
87
    <params>
88
        <param>
89
            <value>
90
                <string>victoria dock</string>
91
            </value>
92
        </param>
93
    </params>
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
94
</methodResponse>''')
95
96
97
6973.12.9 by Jelmer Vernooij
More fixes.
98
class InstrumentedXMLRPCTransport(Transport):
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
99
2027.2.2 by Marien Zwart
Fixes for python 2.5.
100
    # Python 2.5's xmlrpclib looks for this.
101
    _use_datetime = False
7078.12.1 by Jelmer Vernooij
Fix registration test on Python 3.
102
    _use_builtin_types = False
2027.2.2 by Marien Zwart
Fixes for python 2.5.
103
6622.7.1 by Colin Watson
Remove `bzr register-branch`, since it has not worked for a long time.
104
    def __init__(self, testcase):
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
105
        self.testcase = testcase
5582.1.3 by Jelmer Vernooij
Revert _connection change, as it is apparently needed for Python >= 2.7.0 && Python < 2.7.2.
106
        self._connection = (None, None)
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
107
108
    def make_connection(self, host):
109
        host, http_headers, x509 = self.get_host_info(host)
110
        test = self.testcase
0.4.10 by Martin Pool
Move test-specific values out of InstrumentedXMLRPCTransport
111
        self.connected_host = host
6622.7.1 by Colin Watson
Remove `bzr register-branch`, since it has not worked for a long time.
112
        if http_headers:
3376.2.15 by Martin Pool
Fix assertions in Launchpad registration tests
113
            raise AssertionError()
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
114
        return InstrumentedXMLRPCConnection(test)
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
115
7078.12.1 by Jelmer Vernooij
Fix registration test on Python 3.
116
    if PY3:
117
        def send_request(self, host, handler_path, request_body,
118
                         verbose=None):
119
            self.connected_host = host
120
            test = self.testcase
121
            self.got_request = True
122
            unpacked, method = xmlrpc_loads(request_body)
123
            if None in unpacked:
124
                raise AssertionError(
125
                    "xmlrpc result %r shouldn't contain None" % (unpacked,))
126
            self.sent_params = unpacked
127
            return InstrumentedXMLRPCConnection(test)
128
    else:
129
        def send_request(self, connection, handler_path, request_body,
130
                         verbose=None):
131
            test = self.testcase
132
            self.got_request = True
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
133
134
    def send_host(self, conn, host):
135
        pass
136
137
    def send_user_agent(self, conn):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
138
        # TODO: send special user agent string, including breezy version
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
139
        # number
140
        pass
141
142
    def send_content(self, conn, request_body):
6973.12.9 by Jelmer Vernooij
More fixes.
143
        unpacked, method = xmlrpc_loads(request_body)
3376.2.15 by Martin Pool
Fix assertions in Launchpad registration tests
144
        if None in unpacked:
145
            raise AssertionError(
146
                "xmlrpc result %r shouldn't contain None" % (unpacked,))
0.4.10 by Martin Pool
Move test-specific values out of InstrumentedXMLRPCTransport
147
        self.sent_params = unpacked
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
148
149
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
150
class MockLaunchpadService(LaunchpadService):
151
6973.12.11 by Jelmer Vernooij
Fix some more tests.
152
    def send_request(self, method_name, method_params, verbose=None):
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
153
        """Stash away the method details rather than sending them to a real server"""
154
        self.called_method_name = method_name
155
        self.called_method_params = method_params
6622.7.1 by Colin Watson
Remove `bzr register-branch`, since it has not worked for a long time.
156
157
158
class TestResolveLaunchpadPathRequest(TestCaseWithTransport):
0.4.4 by Martin Pool
Start forming xmlrpc requests
159
0.4.23 by Martin Pool
(register-branch) fix ordering of parameters and restore transport-level test.
160
    def setUp(self):
6622.7.1 by Colin Watson
Remove `bzr register-branch`, since it has not worked for a long time.
161
        super(TestResolveLaunchpadPathRequest, self).setUp()
0.4.23 by Martin Pool
(register-branch) fix ordering of parameters and restore transport-level test.
162
        # make sure we have a reproducible standard environment
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
163
        self.overrideEnv('BRZ_LP_XMLRPC_URL', None)
0.4.23 by Martin Pool
(register-branch) fix ordering of parameters and restore transport-level test.
164
165
    def test_onto_transport(self):
6622.7.1 by Colin Watson
Remove `bzr register-branch`, since it has not worked for a long time.
166
        """A request is transmitted across a mock Transport"""
167
        transport = InstrumentedXMLRPCTransport(self)
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
168
        service = LaunchpadService(transport)
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
169
        resolve = ResolveLaunchpadPathRequest('bzr')
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
170
        resolve.submit(service)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
171
        self.assertEqual(transport.connected_host, 'xmlrpc.launchpad.net')
172
        self.assertEqual(len(transport.sent_params), 1)
173
        self.assertEqual(transport.sent_params, ('bzr', ))
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
174
        self.assertTrue(transport.got_request)
175
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
176
    def test_subclass_request(self):
177
        """Define a new type of xmlrpc request"""
178
        class DummyRequest(BaseRequest):
179
            _methodname = 'dummy_request'
180
            def _request_params(self):
181
                return (42,)
182
183
        service = MockLaunchpadService()
184
        service.registrant_email = 'test@launchpad.net'
185
        service.registrant_password = ''
186
        request = DummyRequest()
187
        request.submit(service)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
188
        self.assertEqual(service.called_method_name, 'dummy_request')
189
        self.assertEqual(service.called_method_params, (42,))
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
190
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
191
    def test_mock_resolve_lp_url(self):
192
        test_case = self
193
        class MockService(MockLaunchpadService):
6973.12.11 by Jelmer Vernooij
Fix some more tests.
194
            def send_request(self, method_name, method_params,
195
                             verbose=None):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
196
                test_case.assertEqual(method_name, "resolve_lp_path")
197
                test_case.assertEqual(list(method_params), ['bzr'])
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
198
                return dict(urls=[
199
                        'bzr+ssh://bazaar.launchpad.net~bzr/bzr/trunk',
200
                        'sftp://bazaar.launchpad.net~bzr/bzr/trunk',
201
                        'bzr+http://bazaar.launchpad.net~bzr/bzr/trunk',
202
                        'http://bazaar.launchpad.net~bzr/bzr/trunk'])
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
203
        service = MockService()
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
204
        resolve = ResolveLaunchpadPathRequest('bzr')
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
205
        result = resolve.submit(service)
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
206
        self.assertTrue('urls' in result)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
207
        self.assertEqual(result['urls'], [
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
208
                'bzr+ssh://bazaar.launchpad.net~bzr/bzr/trunk',
209
                'sftp://bazaar.launchpad.net~bzr/bzr/trunk',
210
                'bzr+http://bazaar.launchpad.net~bzr/bzr/trunk',
211
                'http://bazaar.launchpad.net~bzr/bzr/trunk'])