/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
6622.7.2 by Jelmer Vernooij
Merge lp:brz.
17
from io import BytesIO
6973.12.9 by Jelmer Vernooij
More fixes.
18
7479.2.1 by Jelmer Vernooij
Drop python2 support.
19
from xmlrpc.client import (
20
    loads as xmlrpc_loads,
21
    Transport,
22
    )
0.4.5 by Martin Pool
Add structured test for parameters passed through xmlrpc
23
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
24
from ...tests import TestCaseWithTransport
0.4.1 by Martin Pool
Start lp-register command
25
0.4.5 by Martin Pool
Add structured test for parameters passed through xmlrpc
26
# local import
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
27
from .lp_registration import (
7143.15.2 by Jelmer Vernooij
Run autopep8.
28
    BaseRequest,
29
    ResolveLaunchpadPathRequest,
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
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
39
class InstrumentedXMLRPCConnection(object):
40
    """Stands in place of an http connection for the purposes of testing"""
41
42
    def __init__(self, testcase):
43
        self.testcase = testcase
44
45
    def getreply(self):
46
        """Fake the http reply.
47
48
        :returns: (errcode, errmsg, headers)
49
        """
50
        return (200, 'OK', [])
51
5582.1.1 by Bazaar Builbot Net
Quick-and-dirty hack to fix bug #654733
52
    def getresponse(self, buffering=True):
5582.1.2 by Jelmer Vernooij
Add NEWS entry, comment. Remove unnecessary code.
53
        """Fake the http reply.
54
55
        This is used when running on Python 2.7, where xmlrpclib uses
56
        httplib.HTTPConnection in a different way than before.
57
        """
5582.1.1 by Bazaar Builbot Net
Quick-and-dirty hack to fix bug #654733
58
        class FakeHttpResponse(object):
59
5582.13.1 by Vincent Ladeuil
A bit less dirty and bit more robust so that it also works on FreeBSD8 (and may others).
60
            def __init__(self, status, reason, body):
5582.1.1 by Bazaar Builbot Net
Quick-and-dirty hack to fix bug #654733
61
                self.status = status
62
                self.reason = reason
63
                self.body = body
64
65
            def read(self, size=-1):
66
                return self.body.read(size)
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 getheader(self, name, default):
69
                # We don't have headers
70
                return default
71
72
        return FakeHttpResponse(200, 'OK', self.getfile())
5582.1.1 by Bazaar Builbot Net
Quick-and-dirty hack to fix bug #654733
73
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
74
    def getfile(self):
75
        """Return a fake file containing the response content."""
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
76
        return BytesIO(b'''\
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
77
<?xml version="1.0" ?>
78
<methodResponse>
0.4.11 by Martin Pool
Check the correct params are seen by the server
79
    <params>
80
        <param>
81
            <value>
82
                <string>victoria dock</string>
83
            </value>
84
        </param>
85
    </params>
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
86
</methodResponse>''')
87
88
6973.12.9 by Jelmer Vernooij
More fixes.
89
class InstrumentedXMLRPCTransport(Transport):
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
90
2027.2.2 by Marien Zwart
Fixes for python 2.5.
91
    # Python 2.5's xmlrpclib looks for this.
92
    _use_datetime = False
7078.12.1 by Jelmer Vernooij
Fix registration test on Python 3.
93
    _use_builtin_types = False
2027.2.2 by Marien Zwart
Fixes for python 2.5.
94
6622.7.1 by Colin Watson
Remove `bzr register-branch`, since it has not worked for a long time.
95
    def __init__(self, testcase):
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
96
        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.
97
        self._connection = (None, None)
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
98
99
    def make_connection(self, host):
100
        host, http_headers, x509 = self.get_host_info(host)
101
        test = self.testcase
0.4.10 by Martin Pool
Move test-specific values out of InstrumentedXMLRPCTransport
102
        self.connected_host = host
6622.7.1 by Colin Watson
Remove `bzr register-branch`, since it has not worked for a long time.
103
        if http_headers:
3376.2.15 by Martin Pool
Fix assertions in Launchpad registration tests
104
            raise AssertionError()
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
105
        return InstrumentedXMLRPCConnection(test)
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
106
7479.2.1 by Jelmer Vernooij
Drop python2 support.
107
    def send_request(self, host, handler_path, request_body,
108
                     verbose=None):
109
        self.connected_host = host
110
        test = self.testcase
111
        self.got_request = True
112
        unpacked, method = xmlrpc_loads(request_body)
113
        if None in unpacked:
114
            raise AssertionError(
115
                "xmlrpc result %r shouldn't contain None" % (unpacked,))
116
        self.sent_params = unpacked
117
        return InstrumentedXMLRPCConnection(test)
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
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'
7143.15.2 by Jelmer Vernooij
Run autopep8.
165
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
166
            def _request_params(self):
167
                return (42,)
168
169
        service = MockLaunchpadService()
170
        service.registrant_email = 'test@launchpad.net'
171
        service.registrant_password = ''
172
        request = DummyRequest()
173
        request.submit(service)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
174
        self.assertEqual(service.called_method_name, 'dummy_request')
175
        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.
176
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
177
    def test_mock_resolve_lp_url(self):
178
        test_case = self
7143.15.2 by Jelmer Vernooij
Run autopep8.
179
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
180
        class MockService(MockLaunchpadService):
6973.12.11 by Jelmer Vernooij
Fix some more tests.
181
            def send_request(self, method_name, method_params,
182
                             verbose=None):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
183
                test_case.assertEqual(method_name, "resolve_lp_path")
184
                test_case.assertEqual(list(method_params), ['bzr'])
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
185
                return dict(urls=[
7143.15.2 by Jelmer Vernooij
Run autopep8.
186
                    'bzr+ssh://bazaar.launchpad.net~bzr/bzr/trunk',
187
                    'sftp://bazaar.launchpad.net~bzr/bzr/trunk',
188
                    'bzr+http://bazaar.launchpad.net~bzr/bzr/trunk',
189
                    'http://bazaar.launchpad.net~bzr/bzr/trunk'])
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
190
        service = MockService()
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
191
        resolve = ResolveLaunchpadPathRequest('bzr')
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
192
        result = resolve.submit(service)
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
193
        self.assertTrue('urls' in result)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
194
        self.assertEqual(result['urls'], [
7143.15.2 by Jelmer Vernooij
Run autopep8.
195
            'bzr+ssh://bazaar.launchpad.net~bzr/bzr/trunk',
196
            'sftp://bazaar.launchpad.net~bzr/bzr/trunk',
197
            'bzr+http://bazaar.launchpad.net~bzr/bzr/trunk',
198
            'http://bazaar.launchpad.net~bzr/bzr/trunk'])