/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2006-2012, 2016 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
2978.5.1 by John Arbash Meinel
Fix bug #162494, 'bzr register-branch' needs proper auth handling.
18
import urlparse
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 ... import (
2978.5.1 by John Arbash Meinel
Fix bug #162494, 'bzr register-branch' needs proper auth handling.
22
    config,
23
    tests,
24
    ui,
25
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
26
from ...sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
27
    BytesIO,
28
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
29
from ...tests import TestCaseWithTransport
0.4.1 by Martin Pool
Start lp-register command
30
0.4.5 by Martin Pool
Add structured test for parameters passed through xmlrpc
31
# local import
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
32
from .lp_registration import (
0.4.26 by Martin Pool
(register-branch) Add test for link_branch_to_bug and fix its parameters
33
        BaseRequest,
34
        BranchBugLinkRequest,
35
        BranchRegistrationRequest,
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
46
# TODO: Add test for (and implement) other command-line options to set
0.4.14 by Martin Pool
Update xmlrpc api
47
# project, author_email, description.
0.4.12 by Martin Pool
doc
48
49
# TODO: project_id is not properly handled -- must be passed in rpc or path.
50
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
51
class InstrumentedXMLRPCConnection(object):
52
    """Stands in place of an http connection for the purposes of testing"""
53
54
    def __init__(self, testcase):
55
        self.testcase = testcase
56
57
    def getreply(self):
58
        """Fake the http reply.
59
60
        :returns: (errcode, errmsg, headers)
61
        """
62
        return (200, 'OK', [])
63
5582.1.1 by Bazaar Builbot Net
Quick-and-dirty hack to fix bug #654733
64
    def getresponse(self, buffering=True):
5582.1.2 by Jelmer Vernooij
Add NEWS entry, comment. Remove unnecessary code.
65
        """Fake the http reply.
66
67
        This is used when running on Python 2.7, where xmlrpclib uses
68
        httplib.HTTPConnection in a different way than before.
69
        """
5582.1.1 by Bazaar Builbot Net
Quick-and-dirty hack to fix bug #654733
70
        class FakeHttpResponse(object):
71
5582.13.1 by Vincent Ladeuil
A bit less dirty and bit more robust so that it also works on FreeBSD8 (and may others).
72
            def __init__(self, status, reason, body):
5582.1.1 by Bazaar Builbot Net
Quick-and-dirty hack to fix bug #654733
73
                self.status = status
74
                self.reason = reason
75
                self.body = body
76
77
            def read(self, size=-1):
78
                return self.body.read(size)
79
5582.13.1 by Vincent Ladeuil
A bit less dirty and bit more robust so that it also works on FreeBSD8 (and may others).
80
            def getheader(self, name, default):
81
                # We don't have headers
82
                return default
83
84
        return FakeHttpResponse(200, 'OK', self.getfile())
5582.1.1 by Bazaar Builbot Net
Quick-and-dirty hack to fix bug #654733
85
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
86
    def getfile(self):
87
        """Return a fake file containing the response content."""
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
88
        return BytesIO(b'''\
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
89
<?xml version="1.0" ?>
90
<methodResponse>
0.4.11 by Martin Pool
Check the correct params are seen by the server
91
    <params>
92
        <param>
93
            <value>
94
                <string>victoria dock</string>
95
            </value>
96
        </param>
97
    </params>
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
98
</methodResponse>''')
99
100
101
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
102
class InstrumentedXMLRPCTransport(xmlrpclib.Transport):
103
2027.2.2 by Marien Zwart
Fixes for python 2.5.
104
    # Python 2.5's xmlrpclib looks for this.
105
    _use_datetime = False
106
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
107
    def __init__(self, testcase, expect_auth):
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
108
        self.testcase = testcase
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
109
        self.expect_auth = expect_auth
5582.1.3 by Jelmer Vernooij
Revert _connection change, as it is apparently needed for Python >= 2.7.0 && Python < 2.7.2.
110
        self._connection = (None, None)
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
111
112
    def make_connection(self, host):
113
        host, http_headers, x509 = self.get_host_info(host)
114
        test = self.testcase
0.4.10 by Martin Pool
Move test-specific values out of InstrumentedXMLRPCTransport
115
        self.connected_host = host
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
116
        if self.expect_auth:
117
            auth_hdrs = [v for k,v in http_headers if k == 'Authorization']
3376.2.15 by Martin Pool
Fix assertions in Launchpad registration tests
118
            if len(auth_hdrs) != 1:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
119
                raise AssertionError("multiple auth headers: %r"
120
                    % (auth_hdrs,))
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
121
            authinfo = auth_hdrs[0]
122
            expected_auth = 'testuser@launchpad.net:testpassword'
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
123
            test.assertEqual(authinfo,
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
124
                    'Basic ' + base64.encodestring(expected_auth).strip())
3376.2.15 by Martin Pool
Fix assertions in Launchpad registration tests
125
        elif http_headers:
126
            raise AssertionError()
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
127
        return InstrumentedXMLRPCConnection(test)
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
128
129
    def send_request(self, connection, handler_path, request_body):
130
        test = self.testcase
131
        self.got_request = True
132
133
    def send_host(self, conn, host):
134
        pass
135
136
    def send_user_agent(self, conn):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
137
        # 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.
138
        # number
139
        pass
140
141
    def send_content(self, conn, request_body):
142
        unpacked, method = xmlrpclib.loads(request_body)
3376.2.15 by Martin Pool
Fix assertions in Launchpad registration tests
143
        if None in unpacked:
144
            raise AssertionError(
145
                "xmlrpc result %r shouldn't contain None" % (unpacked,))
0.4.10 by Martin Pool
Move test-specific values out of InstrumentedXMLRPCTransport
146
        self.sent_params = unpacked
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
147
148
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
149
class MockLaunchpadService(LaunchpadService):
150
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
151
    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.
152
        """Stash away the method details rather than sending them to a real server"""
153
        self.called_method_name = method_name
154
        self.called_method_params = method_params
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
155
        self.called_authenticated = authenticated
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
156
157
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
158
class TestBranchRegistration(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):
161
        super(TestBranchRegistration, self).setUp()
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
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
165
    def test_register_help(self):
166
        """register-branch accepts --help"""
2581.1.4 by Martin Pool
fixup run_bzr syntax in launchpad plugin
167
        out, err = self.run_bzr(['register-branch', '--help'])
0.4.2 by Martin Pool
Rename command to 'register-branch'
168
        self.assertContainsRe(out, r'Register a branch')
169
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
170
    def test_register_no_url_no_branch(self):
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
171
        """register-branch command requires parameters"""
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
172
        self.make_repository('.')
173
        self.run_bzr_error(
174
            ['register-branch requires a public branch url - '
6622.1.31 by Jelmer Vernooij
Fix more tests.
175
             'see brz help register-branch'],
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
176
            'register-branch')
177
178
    def test_register_no_url_in_published_branch_no_error(self):
179
        b = self.make_branch('.')
6404.6.7 by Vincent Ladeuil
Change set/remove to require a lock for the branch config files.
180
        b.set_public_branch('http://test-server.com/bzr/branch')
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
181
        out, err = self.run_bzr(['register-branch', '--dry-run'])
182
        self.assertEqual('Branch registered.\n', out)
183
        self.assertEqual('', err)
184
185
    def test_register_no_url_in_unpublished_branch_errors(self):
186
        b = self.make_branch('.')
187
        out, err = self.run_bzr_error(['no public branch'],
188
            ['register-branch', '--dry-run'])
189
        self.assertEqual('', out)
0.4.3 by Martin Pool
More command line processing
190
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
191
    def test_register_dry_run(self):
2581.1.4 by Martin Pool
fixup run_bzr syntax in launchpad plugin
192
        out, err = self.run_bzr(['register-branch',
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
193
                                'http://test-server.com/bzr/branch',
2581.1.4 by Martin Pool
fixup run_bzr syntax in launchpad plugin
194
                                '--dry-run'])
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
195
        self.assertEqual(out, 'Branch registered.\n')
0.4.4 by Martin Pool
Start forming xmlrpc requests
196
0.4.23 by Martin Pool
(register-branch) fix ordering of parameters and restore transport-level test.
197
    def test_onto_transport(self):
4634.165.3 by Vincent Ladeuil
Delete all references to edge.launchpad.net in code and associated tests.
198
        """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.
199
        # 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.
200
        transport = InstrumentedXMLRPCTransport(self, expect_auth=True)
0.4.30 by Martin Pool
Fix wierd syntax errors in test
201
        service = LaunchpadService(transport)
0.4.23 by Martin Pool
(register-branch) fix ordering of parameters and restore transport-level test.
202
        service.registrant_email = 'testuser@launchpad.net'
203
        service.registrant_password = 'testpassword'
0.4.7 by Martin Pool
Start making provision to test using a mock xmlrpc transport.
204
        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.
205
                'branch-id',
206
                'my test branch',
207
                'description',
208
                'author@launchpad.net',
209
                'product')
0.4.20 by Bjorn Tillenius
try to fix the tests.
210
        rego.submit(service)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
211
        self.assertEqual(transport.connected_host, 'xmlrpc.launchpad.net')
212
        self.assertEqual(len(transport.sent_params), 6)
213
        self.assertEqual(transport.sent_params,
0.4.23 by Martin Pool
(register-branch) fix ordering of parameters and restore transport-level test.
214
                ('http://test-server.com/bzr/branch',  # branch_url
215
                 'branch-id',                          # branch_name
216
                 'my test branch',                     # branch_title
217
                 'description',
218
                 'author@launchpad.net',
219
                 'product'))
0.4.8 by Martin Pool
More instrumentation of xmlrpc requests
220
        self.assertTrue(transport.got_request)
0.4.13 by Martin Pool
Update xmlrpc api to pass product name as a parameter.
221
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
222
    def test_onto_transport_unauthenticated(self):
4634.165.3 by Vincent Ladeuil
Delete all references to edge.launchpad.net in code and associated tests.
223
        """An unauthenticated request is transmitted across a mock Transport"""
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
224
        transport = InstrumentedXMLRPCTransport(self, expect_auth=False)
225
        service = LaunchpadService(transport)
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
226
        resolve = ResolveLaunchpadPathRequest('bzr')
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
227
        resolve.submit(service)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
228
        self.assertEqual(transport.connected_host, 'xmlrpc.launchpad.net')
229
        self.assertEqual(len(transport.sent_params), 1)
230
        self.assertEqual(transport.sent_params, ('bzr', ))
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
231
        self.assertTrue(transport.got_request)
232
0.4.22 by Martin Pool
(register-branch) Update tests to be in-process calls to a mock server.
233
    def test_subclass_request(self):
234
        """Define a new type of xmlrpc request"""
235
        class DummyRequest(BaseRequest):
236
            _methodname = 'dummy_request'
237
            def _request_params(self):
238
                return (42,)
239
240
        service = MockLaunchpadService()
241
        service.registrant_email = 'test@launchpad.net'
242
        service.registrant_password = ''
243
        request = DummyRequest()
244
        request.submit(service)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
245
        self.assertEqual(service.called_method_name, 'dummy_request')
246
        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.
247
0.4.24 by Martin Pool
(register-branch) additional test case against mock server
248
    def test_mock_server_registration(self):
249
        """Send registration to mock server"""
250
        test_case = self
251
        class MockRegistrationService(MockLaunchpadService):
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
252
            def send_request(self, method_name, method_params, authenticated):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
253
                test_case.assertEqual(method_name, "register_branch")
254
                test_case.assertEqual(list(method_params),
0.4.24 by Martin Pool
(register-branch) additional test case against mock server
255
                        ['url', 'name', 'title', 'description', 'email', 'name'])
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
256
                test_case.assertEqual(authenticated, True)
0.4.24 by Martin Pool
(register-branch) additional test case against mock server
257
                return 'result'
258
        service = MockRegistrationService()
259
        rego = BranchRegistrationRequest('url', 'name', 'title',
260
                        'description', 'email', 'name')
261
        result = rego.submit(service)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
262
        self.assertEqual(result, 'result')
0.4.25 by Martin Pool
(register-branch) additional test for registration with defaults
263
264
    def test_mock_server_registration_with_defaults(self):
265
        """Send registration to mock server"""
266
        test_case = self
267
        class MockRegistrationService(MockLaunchpadService):
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
268
            def send_request(self, method_name, method_params, authenticated):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
269
                test_case.assertEqual(method_name, "register_branch")
270
                test_case.assertEqual(list(method_params),
0.4.25 by Martin Pool
(register-branch) additional test for registration with defaults
271
                        ['http://server/branch', 'branch', '', '', '', ''])
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
272
                test_case.assertEqual(authenticated, True)
0.4.25 by Martin Pool
(register-branch) additional test for registration with defaults
273
                return 'result'
274
        service = MockRegistrationService()
275
        rego = BranchRegistrationRequest('http://server/branch')
276
        result = rego.submit(service)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
277
        self.assertEqual(result, 'result')
0.4.26 by Martin Pool
(register-branch) Add test for link_branch_to_bug and fix its parameters
278
279
    def test_mock_bug_branch_link(self):
280
        """Send bug-branch link to mock server"""
281
        test_case = self
282
        class MockService(MockLaunchpadService):
2898.4.1 by James Henstridge
Make it possible to make unauthenticated XML-RPC requests.
283
            def send_request(self, method_name, method_params, authenticated):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
284
                test_case.assertEqual(method_name, "link_branch_to_bug")
285
                test_case.assertEqual(list(method_params),
0.4.26 by Martin Pool
(register-branch) Add test for link_branch_to_bug and fix its parameters
286
                        ['http://server/branch', 1234, ''])
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
287
                test_case.assertEqual(authenticated, True)
0.4.26 by Martin Pool
(register-branch) Add test for link_branch_to_bug and fix its parameters
288
                return 'http://launchpad.net/bug/1234'
289
        service = MockService()
290
        rego = BranchBugLinkRequest('http://server/branch', 1234)
291
        result = rego.submit(service)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
292
        self.assertEqual(result, 'http://launchpad.net/bug/1234')
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
293
294
    def test_mock_resolve_lp_url(self):
295
        test_case = self
296
        class MockService(MockLaunchpadService):
297
            def send_request(self, method_name, method_params, authenticated):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
298
                test_case.assertEqual(method_name, "resolve_lp_path")
299
                test_case.assertEqual(list(method_params), ['bzr'])
300
                test_case.assertEqual(authenticated, False)
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
301
                return dict(urls=[
302
                        'bzr+ssh://bazaar.launchpad.net~bzr/bzr/trunk',
303
                        'sftp://bazaar.launchpad.net~bzr/bzr/trunk',
304
                        'bzr+http://bazaar.launchpad.net~bzr/bzr/trunk',
305
                        'http://bazaar.launchpad.net~bzr/bzr/trunk'])
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
306
        service = MockService()
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
307
        resolve = ResolveLaunchpadPathRequest('bzr')
2898.4.2 by James Henstridge
Add ResolveLaunchpadURLRequest() class to handle lp: URL resolution.
308
        result = resolve.submit(service)
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
309
        self.assertTrue('urls' in result)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
310
        self.assertEqual(result['urls'], [
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
311
                'bzr+ssh://bazaar.launchpad.net~bzr/bzr/trunk',
312
                'sftp://bazaar.launchpad.net~bzr/bzr/trunk',
313
                'bzr+http://bazaar.launchpad.net~bzr/bzr/trunk',
314
                'http://bazaar.launchpad.net~bzr/bzr/trunk'])
2978.5.2 by John Arbash Meinel
merge bzr.dev 2991
315
2978.5.1 by John Arbash Meinel
Fix bug #162494, 'bzr register-branch' needs proper auth handling.
316
317
class TestGatherUserCredentials(tests.TestCaseInTempDir):
318
319
    def setUp(self):
320
        super(TestGatherUserCredentials, self).setUp()
321
        # make sure we have a reproducible standard environment
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
322
        self.overrideEnv('BRZ_LP_XMLRPC_URL', None)
2978.5.1 by John Arbash Meinel
Fix bug #162494, 'bzr register-branch' needs proper auth handling.
323
324
    def test_gather_user_credentials_has_password(self):
325
        service = LaunchpadService()
326
        service.registrant_password = 'mypassword'
327
        # This should be a basic no-op, since we already have the password
328
        service.gather_user_credentials()
329
        self.assertEqual('mypassword', service.registrant_password)
330
331
    def test_gather_user_credentials_from_auth_conf(self):
332
        auth_path = config.authentication_config_filename()
333
        service = LaunchpadService()
6449.6.1 by Jelmer Vernooij
Use config stacks in a few more places in the test suite.
334
        g_conf = config.GlobalStack()
335
        g_conf.set('email', 'Test User <test@user.com>')
6499.3.4 by Vincent Ladeuil
Fix failing tests, they were either brittle or requiring the config to be saved.
336
        g_conf.store.save()
337
        # FIXME: auth_path base dir exists only because bazaar.conf has just
338
        # been saved, brittle... -- vila 20120731
2978.5.1 by John Arbash Meinel
Fix bug #162494, 'bzr register-branch' needs proper auth handling.
339
        f = open(auth_path, 'wb')
340
        try:
341
            scheme, hostinfo = urlparse.urlsplit(service.service_url)[:2]
342
            f.write('[section]\n'
343
                    'scheme=%s\n'
344
                    'host=%s\n'
345
                    'user=test@user.com\n'
346
                    'password=testpass\n'
347
                    % (scheme, hostinfo))
348
        finally:
349
            f.close()
350
        self.assertIs(None, service.registrant_password)
351
        service.gather_user_credentials()
352
        self.assertEqual('test@user.com', service.registrant_email)
353
        self.assertEqual('testpass', service.registrant_password)
354
355
    def test_gather_user_credentials_prompts(self):
356
        service = LaunchpadService()
357
        self.assertIs(None, service.registrant_password)
6449.6.1 by Jelmer Vernooij
Use config stacks in a few more places in the test suite.
358
        g_conf = config.GlobalStack()
359
        g_conf.set('email', 'Test User <test@user.com>')
6499.3.4 by Vincent Ladeuil
Fix failing tests, they were either brittle or requiring the config to be saved.
360
        g_conf.store.save()
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
361
        ui.ui_factory = tests.TestUIFactory(stdin=u'userpass\n')
2978.5.1 by John Arbash Meinel
Fix bug #162494, 'bzr register-branch' needs proper auth handling.
362
        self.assertIs(None, service.registrant_password)
363
        service.gather_user_credentials()
364
        self.assertEqual('test@user.com', service.registrant_email)
365
        self.assertEqual('userpass', service.registrant_password)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
366
        self.assertEqual('', ui.ui_factory.stdout.getvalue())
367
        self.assertContainsRe(ui.ui_factory.stderr.getvalue(),
2978.5.1 by John Arbash Meinel
Fix bug #162494, 'bzr register-branch' needs proper auth handling.
368
                             'launchpad.net password for test@user\\.com')
369