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