/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

Get rid of relative imports in Launchpad plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for Launchpad user ID management functions."""
18
18
 
19
19
from cStringIO import StringIO
20
20
 
21
21
from bzrlib import config
22
 
from bzrlib.tests import TestCaseInTempDir, TestCaseWithMemoryTransport
 
22
from bzrlib.tests import TestCase, TestCaseWithMemoryTransport
23
23
from bzrlib.plugins.launchpad import account
24
24
 
25
25
 
26
 
class LaunchpadAccountTests(TestCaseInTempDir):
 
26
class LaunchpadAccountTests(TestCase):
27
27
 
28
28
    def setup_config(self, text):
29
29
        my_config = config.GlobalConfig()
40
40
    def test_get_lp_login(self):
41
41
        # Test that get_lp_login() returns the configured username
42
42
        my_config = self.setup_config(
43
 
            '[DEFAULT]\nlaunchpad_username=test-user\n')
44
 
        self.assertEqual('test-user', account.get_lp_login(my_config))
 
43
            '[DEFAULT]\nlaunchpad_username=test_user\n')
 
44
        self.assertEqual('test_user', account.get_lp_login(my_config))
45
45
 
46
46
    def test_set_lp_login(self):
47
47
        # Test that set_lp_login() updates the config file.
48
48
        my_config = self.setup_config('')
49
49
        self.assertEqual(None, my_config.get_user_option('launchpad_username'))
50
 
        account.set_lp_login('test-user', my_config)
 
50
        account.set_lp_login('test_user', my_config)
51
51
        self.assertEqual(
52
 
            'test-user', my_config.get_user_option('launchpad_username'))
 
52
            'test_user', my_config.get_user_option('launchpad_username'))
53
53
 
54
54
    def test_unknown_launchpad_username(self):
55
55
        # Test formatting of UnknownLaunchpadUsername exception
56
 
        error = account.UnknownLaunchpadUsername(user='test-user')
57
 
        self.assertEqualDiff('The user name test-user is not registered '
 
56
        error = account.UnknownLaunchpadUsername(user='test_user')
 
57
        self.assertEqualDiff('The user name test_user is not registered '
58
58
                             'on Launchpad.', str(error))
59
59
 
60
60
    def test_no_registered_ssh_keys(self):
61
61
        # Test formatting of NoRegisteredSSHKeys exception
62
 
        error = account.NoRegisteredSSHKeys(user='test-user')
63
 
        self.assertEqualDiff('The user test-user has not registered any '
64
 
            'SSH keys with Launchpad.\n'
65
 
            'See <https://launchpad.net/people/+me>',
66
 
            str(error))
67
 
 
68
 
    def test_set_lp_login_updates_authentication_conf(self):
69
 
        self.assertIs(None, account._get_auth_user())
70
 
        account.set_lp_login('foo')
71
 
        self.assertEqual('foo', account._get_auth_user())
72
 
 
73
 
    def test_get_lp_login_does_not_update_for_none_user(self):
74
 
        account.get_lp_login()
75
 
        self.assertIs(None, account._get_auth_user())
76
 
 
77
 
    def test_get_lp_login_updates_authentication_conf(self):
78
 
        account._set_global_option('foo')
79
 
        self.assertIs(None, account._get_auth_user())
80
 
        account.get_lp_login()
81
 
        auth = config.AuthenticationConfig()
82
 
        self.assertEqual('foo', account._get_auth_user(auth))
83
 
        self.assertEqual('foo', auth.get_user('ssh', 'bazaar.launchpad.net'))
84
 
        self.assertEqual('foo', auth.get_user('ssh',
85
 
                                              'bazaar.staging.launchpad.net'))
86
 
 
87
 
    def test_get_lp_login_leaves_existing_credentials(self):
88
 
        auth = config.AuthenticationConfig()
89
 
        auth.set_credentials('Foo', 'bazaar.launchpad.net', 'foo', 'ssh')
90
 
        auth.set_credentials('Bar', 'bazaar.staging.launchpad.net', 'foo',
91
 
                             'ssh')
92
 
        account._set_global_option('foo')
93
 
        account.get_lp_login()
94
 
        auth = config.AuthenticationConfig()
95
 
        credentials = auth.get_credentials('ssh', 'bazaar.launchpad.net')
96
 
        self.assertEqual('Foo', credentials['name'])
97
 
 
98
 
    def test_get_lp_login_errors_on_mismatch(self):
99
 
        account._set_auth_user('foo')
100
 
        account._set_global_option('bar')
101
 
        e = self.assertRaises(account.MismatchedUsernames,
102
 
                              account.get_lp_login)
103
 
        self.assertEqual('bazaar.conf and authentication.conf disagree about'
104
 
            ' launchpad account name.  Please re-run launchpad-login.', str(e))
 
62
        error = account.NoRegisteredSSHKeys(user='test_user')
 
63
        self.assertEqualDiff('The user test_user has not registered any '
 
64
                             'SSH keys with Launchpad.', str(error))
105
65
 
106
66
 
107
67
class CheckAccountTests(TestCaseWithMemoryTransport):
108
68
 
109
69
    def test_check_lp_login_valid_user(self):
110
70
        transport = self.get_transport()
111
 
        transport.mkdir('~test-user')
112
 
        transport.put_bytes('~test-user/+sshkeys', 'some keys here')
113
 
        account.check_lp_login('test-user', transport)
 
71
        transport.mkdir('~test_user')
 
72
        transport.put_bytes('~test_user/+sshkeys', 'some keys here')
 
73
        account.check_lp_login('test_user', transport)
114
74
 
115
75
    def test_check_lp_login_no_user(self):
116
76
        transport = self.get_transport()
117
77
        self.assertRaises(account.UnknownLaunchpadUsername,
118
 
                          account.check_lp_login, 'test-user', transport)
 
78
                          account.check_lp_login, 'test_user', transport)
119
79
 
120
80
    def test_check_lp_login_no_ssh_keys(self):
121
81
        transport = self.get_transport()
122
 
        transport.mkdir('~test-user')
123
 
        transport.put_bytes('~test-user/+sshkeys', '')
 
82
        transport.mkdir('~test_user')
 
83
        transport.put_bytes('~test_user/+sshkeys', '')
124
84
        self.assertRaises(account.NoRegisteredSSHKeys,
125
 
                          account.check_lp_login, 'test-user', transport)
 
85
                          account.check_lp_login, 'test_user', transport)