test_account.py :  » Development » Bazaar » bzr-2.2b3 » bzrlib » plugins » launchpad » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Bazaar 
Bazaar » bzr 2.2b3 » bzrlib » plugins » launchpad » test_account.py
# Copyright (C) 2007, 2008 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()
        config_file = StringIO(text)
        my_config._get_parser(config_file)
        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)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.