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