/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2898.3.5 by James Henstridge
Add copyright headers to new files
1
# Copyright (C) 2007 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for Launchpad user ID management functions."""
18
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
19
from cStringIO import StringIO
20
21
from bzrlib import config
2898.3.10 by James Henstridge
Make LaunchpadAccountTests a TestCaseInTempDir so that config changes
22
from bzrlib.tests import TestCaseInTempDir, TestCaseWithMemoryTransport
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
23
from bzrlib.plugins.launchpad import account
24
25
2898.3.10 by James Henstridge
Make LaunchpadAccountTests a TestCaseInTempDir so that config changes
26
class LaunchpadAccountTests(TestCaseInTempDir):
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
27
28
    def setup_config(self, text):
29
        my_config = config.GlobalConfig()
30
        config_file = StringIO(text)
31
        my_config._get_parser(config_file)
32
        return my_config
33
2898.3.2 by James Henstridge
Rename functions.
34
    def test_get_lp_login_unconfigured(self):
35
        # Test that get_lp_login() returns None if no username has
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
36
        # been configured.
37
        my_config = self.setup_config('')
2898.3.2 by James Henstridge
Rename functions.
38
        self.assertEqual(None, account.get_lp_login(my_config))
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
39
2898.3.2 by James Henstridge
Rename functions.
40
    def test_get_lp_login(self):
41
        # Test that get_lp_login() returns the configured username
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
42
        my_config = self.setup_config(
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
43
            '[DEFAULT]\nlaunchpad_username=test-user\n')
44
        self.assertEqual('test-user', account.get_lp_login(my_config))
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
45
2898.3.2 by James Henstridge
Rename functions.
46
    def test_set_lp_login(self):
47
        # Test that set_lp_login() updates the config file.
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
48
        my_config = self.setup_config('')
49
        self.assertEqual(None, my_config.get_user_option('launchpad_username'))
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
50
        account.set_lp_login('test-user', my_config)
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
51
        self.assertEqual(
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
52
            'test-user', my_config.get_user_option('launchpad_username'))
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
53
2898.3.7 by James Henstridge
* Add tests for account.py exception messages.
54
    def test_unknown_launchpad_username(self):
55
        # Test formatting of UnknownLaunchpadUsername exception
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
56
        error = account.UnknownLaunchpadUsername(user='test-user')
57
        self.assertEqualDiff('The user name test-user is not registered '
2898.3.7 by James Henstridge
* Add tests for account.py exception messages.
58
                             'on Launchpad.', str(error))
59
60
    def test_no_registered_ssh_keys(self):
61
        # Test formatting of NoRegisteredSSHKeys exception
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
62
        error = account.NoRegisteredSSHKeys(user='test-user')
63
        self.assertEqualDiff('The user test-user has not registered any '
2898.3.7 by James Henstridge
* Add tests for account.py exception messages.
64
                             'SSH keys with Launchpad.', str(error))
65
3777.1.12 by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade.
66
    def test_set_lp_login_updates_authentication_conf(self):
3777.1.20 by Aaron Bentley
Include staging in launchpad-login authentication.
67
        self.assertEqual([None, None], account._get_auth_user().values())
3777.1.12 by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade.
68
        account.set_lp_login('foo')
3777.1.20 by Aaron Bentley
Include staging in launchpad-login authentication.
69
        self.assertEqual({'production': 'foo', 'staging': 'foo'},
70
                         account._get_auth_user())
3777.1.12 by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade.
71
3777.1.18 by Aaron Bentley
Fix None handling wrt auth upgrades
72
    def test_get_lp_login_does_not_update_for_none_user(self):
73
        account.get_lp_login()
3777.1.20 by Aaron Bentley
Include staging in launchpad-login authentication.
74
        self.assertEqual([None, None], account._get_auth_user().values())
3777.1.18 by Aaron Bentley
Fix None handling wrt auth upgrades
75
3777.1.12 by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade.
76
    def test_get_lp_login_updates_authentication_conf(self):
77
        account._set_global_option('foo')
3777.1.20 by Aaron Bentley
Include staging in launchpad-login authentication.
78
        self.assertEqual([None, None], account._get_auth_user().values())
3777.1.12 by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade.
79
        account.get_lp_login()
3777.1.20 by Aaron Bentley
Include staging in launchpad-login authentication.
80
        self.assertEqual({'production': 'foo', 'staging': 'foo'},
81
                         account._get_auth_user())
3777.1.12 by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade.
82
83
    def test_get_lp_login_leaves_existing_credentials(self):
84
        auth = config.AuthenticationConfig()
3777.1.14 by Aaron Bentley
Adjust for new set_credentials signature
85
        auth.set_credentials('Foo', 'bazaar.launchpad.net', 'foo', 'ssh')
3777.1.20 by Aaron Bentley
Include staging in launchpad-login authentication.
86
        auth.set_credentials('Bar', 'bazaar.staging.launchpad.net', 'foo',
87
                             'ssh')
3777.1.12 by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade.
88
        account._set_global_option('foo')
89
        account.get_lp_login()
90
        auth = config.AuthenticationConfig()
91
        credentials = auth.get_credentials('ssh', 'bazaar.launchpad.net')
92
        self.assertEqual('Foo', credentials['name'])
93
94
    def test_get_lp_login_errors_on_mismatch(self):
95
        account._set_auth_user('foo')
96
        account._set_global_option('bar')
97
        e = self.assertRaises(account.MismatchedUsernames,
98
                              account.get_lp_login)
99
        self.assertEqual('bazaar.conf and authentication.conf disagree about'
100
            ' launchpad account name.  Please re-run launchpad-login.', str(e))
101
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
102
103
class CheckAccountTests(TestCaseWithMemoryTransport):
104
2898.3.2 by James Henstridge
Rename functions.
105
    def test_check_lp_login_valid_user(self):
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
106
        transport = self.get_transport()
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
107
        transport.mkdir('~test-user')
108
        transport.put_bytes('~test-user/+sshkeys', 'some keys here')
109
        account.check_lp_login('test-user', transport)
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
110
2898.3.2 by James Henstridge
Rename functions.
111
    def test_check_lp_login_no_user(self):
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
112
        transport = self.get_transport()
113
        self.assertRaises(account.UnknownLaunchpadUsername,
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
114
                          account.check_lp_login, 'test-user', transport)
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
115
2898.3.2 by James Henstridge
Rename functions.
116
    def test_check_lp_login_no_ssh_keys(self):
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
117
        transport = self.get_transport()
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
118
        transport.mkdir('~test-user')
119
        transport.put_bytes('~test-user/+sshkeys', '')
2898.3.1 by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used
120
        self.assertRaises(account.NoRegisteredSSHKeys,
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
121
                          account.check_lp_login, 'test-user', transport)