~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Copyright (C) 2007-2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for Launchpad user ID management functions."""

from cStringIO import StringIO

from bzrlib import config
from bzrlib.tests import TestCaseInTempDir, TestCaseWithMemoryTransport
from bzrlib.plugins.launchpad import account


class LaunchpadAccountTests(TestCaseInTempDir):

    def setup_config(self, text):
        my_config = config.GlobalConfig.from_string(text)
        return my_config

    def test_get_lp_login_unconfigured(self):
        # Test that get_lp_login() returns None if no username has
        # been configured.
        my_config = self.setup_config('')
        self.assertEqual(None, account.get_lp_login(my_config))

    def test_get_lp_login(self):
        # Test that get_lp_login() returns the configured username
        my_config = self.setup_config(
            '[DEFAULT]\nlaunchpad_username=test-user\n')
        self.assertEqual('test-user', account.get_lp_login(my_config))

    def test_set_lp_login(self):
        # Test that set_lp_login() updates the config file.
        my_config = self.setup_config('')
        self.assertEqual(None, my_config.get_user_option('launchpad_username'))
        account.set_lp_login('test-user', my_config)
        self.assertEqual(
            'test-user', my_config.get_user_option('launchpad_username'))

    def test_unknown_launchpad_username(self):
        # Test formatting of UnknownLaunchpadUsername exception
        error = account.UnknownLaunchpadUsername(user='test-user')
        self.assertEqualDiff('The user name test-user is not registered '
                             'on Launchpad.', str(error))

    def test_no_registered_ssh_keys(self):
        # Test formatting of NoRegisteredSSHKeys exception
        error = account.NoRegisteredSSHKeys(user='test-user')
        self.assertEqualDiff('The user test-user has not registered any '
            'SSH keys with Launchpad.\n'
            'See <https://launchpad.net/people/+me>',
            str(error))

    def test_set_lp_login_updates_authentication_conf(self):
        self.assertIs(None, account._get_auth_user())
        account.set_lp_login('foo')
        self.assertEqual('foo', account._get_auth_user())

    def test_get_lp_login_does_not_update_for_none_user(self):
        account.get_lp_login()
        self.assertIs(None, account._get_auth_user())

    def test_get_lp_login_updates_authentication_conf(self):
        account._set_global_option('foo')
        self.assertIs(None, account._get_auth_user())
        account.get_lp_login()
        auth = config.AuthenticationConfig()
        self.assertEqual('foo', account._get_auth_user(auth))
        self.assertEqual('foo', auth.get_user('ssh', 'bazaar.launchpad.net'))
        self.assertEqual('foo', auth.get_user('ssh',
                                              'bazaar.staging.launchpad.net'))

    def test_get_lp_login_leaves_existing_credentials(self):
        auth = config.AuthenticationConfig()
        auth.set_credentials('Foo', 'bazaar.launchpad.net', 'foo', 'ssh')
        auth.set_credentials('Bar', 'bazaar.staging.launchpad.net', 'foo',
                             'ssh')
        account._set_global_option('foo')
        account.get_lp_login()
        auth = config.AuthenticationConfig()
        credentials = auth.get_credentials('ssh', 'bazaar.launchpad.net')
        self.assertEqual('Foo', credentials['name'])

    def test_get_lp_login_errors_on_mismatch(self):
        account._set_auth_user('foo')
        account._set_global_option('bar')
        e = self.assertRaises(account.MismatchedUsernames,
                              account.get_lp_login)
        self.assertEqual('bazaar.conf and authentication.conf disagree about'
            ' launchpad account name.  Please re-run launchpad-login.', str(e))


class CheckAccountTests(TestCaseWithMemoryTransport):

    def test_check_lp_login_valid_user(self):
        transport = self.get_transport()
        transport.mkdir('~test-user')
        transport.put_bytes('~test-user/+sshkeys', 'some keys here')
        account.check_lp_login('test-user', transport)

    def test_check_lp_login_no_user(self):
        transport = self.get_transport()
        self.assertRaises(account.UnknownLaunchpadUsername,
                          account.check_lp_login, 'test-user', transport)

    def test_check_lp_login_no_ssh_keys(self):
        transport = self.get_transport()
        transport.mkdir('~test-user')
        transport.put_bytes('~test-user/+sshkeys', '')
        self.assertRaises(account.NoRegisteredSSHKeys,
                          account.check_lp_login, 'test-user', transport)