~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/launchpad/test_account.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-19 13:23:58 UTC
  • mto: This revision was merged to the branch mainline in revision 6386.
  • Revision ID: jelmer@canonical.com-20111219132358-uvs5a6y92gomzacd
Move importing from future until after doc string, otherwise the doc string will disappear.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2010 Canonical Ltd
 
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests for Launchpad user ID management functions."""
 
18
 
 
19
from cStringIO import StringIO
 
20
 
 
21
from bzrlib import config
 
22
from bzrlib.tests import TestCaseInTempDir, TestCaseWithMemoryTransport
 
23
from bzrlib.plugins.launchpad import account
 
24
 
 
25
 
 
26
class LaunchpadAccountTests(TestCaseInTempDir):
 
27
 
 
28
    def setup_config(self, text):
 
29
        my_config = config.GlobalConfig.from_string(text)
 
30
        return my_config
 
31
 
 
32
    def test_get_lp_login_unconfigured(self):
 
33
        # Test that get_lp_login() returns None if no username has
 
34
        # been configured.
 
35
        my_config = self.setup_config('')
 
36
        self.assertEqual(None, account.get_lp_login(my_config))
 
37
 
 
38
    def test_get_lp_login(self):
 
39
        # Test that get_lp_login() returns the configured username
 
40
        my_config = self.setup_config(
 
41
            '[DEFAULT]\nlaunchpad_username=test-user\n')
 
42
        self.assertEqual('test-user', account.get_lp_login(my_config))
 
43
 
 
44
    def test_set_lp_login(self):
 
45
        # Test that set_lp_login() updates the config file.
 
46
        my_config = self.setup_config('')
 
47
        self.assertEqual(None, my_config.get_user_option('launchpad_username'))
 
48
        account.set_lp_login('test-user', my_config)
 
49
        self.assertEqual(
 
50
            'test-user', my_config.get_user_option('launchpad_username'))
 
51
 
 
52
    def test_unknown_launchpad_username(self):
 
53
        # Test formatting of UnknownLaunchpadUsername exception
 
54
        error = account.UnknownLaunchpadUsername(user='test-user')
 
55
        self.assertEqualDiff('The user name test-user is not registered '
 
56
                             'on Launchpad.', str(error))
 
57
 
 
58
    def test_no_registered_ssh_keys(self):
 
59
        # Test formatting of NoRegisteredSSHKeys exception
 
60
        error = account.NoRegisteredSSHKeys(user='test-user')
 
61
        self.assertEqualDiff('The user test-user has not registered any '
 
62
            'SSH keys with Launchpad.\n'
 
63
            'See <https://launchpad.net/people/+me>',
 
64
            str(error))
 
65
 
 
66
    def test_set_lp_login_updates_authentication_conf(self):
 
67
        self.assertIs(None, account._get_auth_user())
 
68
        account.set_lp_login('foo')
 
69
        self.assertEqual('foo', account._get_auth_user())
 
70
 
 
71
    def test_get_lp_login_does_not_update_for_none_user(self):
 
72
        account.get_lp_login()
 
73
        self.assertIs(None, account._get_auth_user())
 
74
 
 
75
    def test_get_lp_login_updates_authentication_conf(self):
 
76
        account._set_global_option('foo')
 
77
        self.assertIs(None, account._get_auth_user())
 
78
        account.get_lp_login()
 
79
        auth = config.AuthenticationConfig()
 
80
        self.assertEqual('foo', account._get_auth_user(auth))
 
81
        self.assertEqual('foo', auth.get_user('ssh', 'bazaar.launchpad.net'))
 
82
        self.assertEqual('foo', auth.get_user('ssh',
 
83
                                              'bazaar.staging.launchpad.net'))
 
84
 
 
85
    def test_get_lp_login_leaves_existing_credentials(self):
 
86
        auth = config.AuthenticationConfig()
 
87
        auth.set_credentials('Foo', 'bazaar.launchpad.net', 'foo', 'ssh')
 
88
        auth.set_credentials('Bar', 'bazaar.staging.launchpad.net', 'foo',
 
89
                             'ssh')
 
90
        account._set_global_option('foo')
 
91
        account.get_lp_login()
 
92
        auth = config.AuthenticationConfig()
 
93
        credentials = auth.get_credentials('ssh', 'bazaar.launchpad.net')
 
94
        self.assertEqual('Foo', credentials['name'])
 
95
 
 
96
    def test_get_lp_login_errors_on_mismatch(self):
 
97
        account._set_auth_user('foo')
 
98
        account._set_global_option('bar')
 
99
        e = self.assertRaises(account.MismatchedUsernames,
 
100
                              account.get_lp_login)
 
101
        self.assertEqual('bazaar.conf and authentication.conf disagree about'
 
102
            ' launchpad account name.  Please re-run launchpad-login.', str(e))
 
103
 
 
104
 
 
105
class CheckAccountTests(TestCaseWithMemoryTransport):
 
106
 
 
107
    def test_check_lp_login_valid_user(self):
 
108
        transport = self.get_transport()
 
109
        transport.mkdir('~test-user')
 
110
        transport.put_bytes('~test-user/+sshkeys', 'some keys here')
 
111
        account.check_lp_login('test-user', transport)
 
112
 
 
113
    def test_check_lp_login_no_user(self):
 
114
        transport = self.get_transport()
 
115
        self.assertRaises(account.UnknownLaunchpadUsername,
 
116
                          account.check_lp_login, 'test-user', transport)
 
117
 
 
118
    def test_check_lp_login_no_ssh_keys(self):
 
119
        transport = self.get_transport()
 
120
        transport.mkdir('~test-user')
 
121
        transport.put_bytes('~test-user/+sshkeys', '')
 
122
        self.assertRaises(account.NoRegisteredSSHKeys,
 
123
                          account.check_lp_login, 'test-user', transport)