Merge "Increased coverage of nova/auth/dbdriver.py to 100%. Fixes 828609."

This commit is contained in:
Jenkins 2012-02-07 00:45:17 +00:00 committed by Gerrit Code Review
commit 88eb8b7728
3 changed files with 54 additions and 11 deletions

View File

@ -53,6 +53,7 @@
<sleepsonthefloor@gmail.com> <root@tonbuntu>
<soren.hansen@rackspace.com> <soren@linux2go.dk>
<throughnothing@gmail.com> <will.wolf@rackspace.com>
<tim.simpson@rackspace.com> <tim.simpson4@gmail.com>
<todd@ansolabs.com> <todd@lapex>
<todd@ansolabs.com> <todd@rubidine.com>
<todd@ansolabs.com> <xtoddx@gmail.com>

View File

@ -78,7 +78,7 @@ class DbDriver(object):
try:
user_ref = db.user_create(context.get_admin_context(), values)
return self._db_user_to_auth_user(user_ref)
except exception.Duplicate, e:
except (exception.Duplicate, exception.DBError) as e:
raise exception.UserExists(user=name)
def _db_user_to_auth_user(self, user_ref):
@ -100,8 +100,6 @@ class DbDriver(object):
description=None, member_uids=None):
"""Create a project"""
manager = db.user_get(context.get_admin_context(), manager_uid)
if not manager:
raise exception.UserNotFound(user_id=manager_uid)
# description is a required attribute
if description is None:
@ -114,8 +112,6 @@ class DbDriver(object):
if member_uids is not None:
for member_uid in member_uids:
member = db.user_get(context.get_admin_context(), member_uid)
if not member:
raise exception.UserNotFound(user_id=member_uid)
members.add(member)
values = {'id': name,
@ -146,8 +142,6 @@ class DbDriver(object):
values = {}
if manager_uid:
manager = db.user_get(context.get_admin_context(), manager_uid)
if not manager:
raise exception.UserNotFound(user_id=manager_uid)
values['project_manager'] = manager['id']
if description:
values['description'] = description
@ -234,9 +228,5 @@ class DbDriver(object):
def _validate_user_and_project(self, user_id, project_id):
user = db.user_get(context.get_admin_context(), user_id)
if not user:
raise exception.UserNotFound(user_id=user_id)
project = db.project_get(context.get_admin_context(), project_id)
if not project:
raise exception.ProjectNotFound(project_id=project_id)
return user, project

View File

@ -19,6 +19,7 @@
import unittest
from nova import crypto
from nova import exception
from nova import flags
from nova import log as logging
from nova import test
@ -86,6 +87,9 @@ class user_and_project_generator(object):
class _AuthManagerBaseTestCase(test.TestCase):
user_not_found_type = exception.UserNotFound
def setUp(self):
super(_AuthManagerBaseTestCase, self).setUp()
self.flags(auth_driver=self.auth_driver,
@ -106,6 +110,11 @@ class _AuthManagerBaseTestCase(test.TestCase):
self.assertEqual('classified', u.secret)
self.assertEqual('private-party', u.access)
def test_create_user_twice(self):
self.manager.create_user('test-1')
self.assertRaises(exception.UserExists, self.manager.create_user,
'test-1')
def test_signature_is_valid(self):
with user_generator(self.manager, name='admin', secret='admin',
access='admin'):
@ -197,10 +206,29 @@ class _AuthManagerBaseTestCase(test.TestCase):
self.assertEqual('test1', project.project_manager_id)
self.assertTrue(self.manager.is_project_manager(user, project))
def test_can_create_project_twice(self):
with user_and_project_generator(self.manager) as (user1, project):
self.assertRaises(exception.ProjectExists,
self.manager.create_project, "testproj", "test1")
def test_create_project_assigns_manager_to_members(self):
with user_and_project_generator(self.manager) as (user, project):
self.assertTrue(self.manager.is_project_member(user, project))
def test_create_project_with_manager_and_members(self):
with user_generator(self.manager, name='test2') as user2:
with user_and_project_generator(self.manager,
project_state={'member_users': ['test2']}) as (user1, project):
self.assertTrue(self.manager.is_project_member(
user1, project))
self.assertTrue(self.manager.is_project_member(
user2, project))
def test_create_project_with_manager_and_missing_members(self):
self.assertRaises(self.user_not_found_type,
self.manager.create_project, "testproj", "test1",
member_users="test2")
def test_no_extra_project_members(self):
with user_generator(self.manager, name='test2') as baduser:
with user_and_project_generator(self.manager) as (user, project):
@ -313,6 +341,13 @@ class _AuthManagerBaseTestCase(test.TestCase):
self.assertEqual('test2', project.project_manager_id)
self.assertEqual('new desc', project.description)
def test_can_call_modify_project_but_do_nothing(self):
with user_and_project_generator(self.manager):
self.manager.modify_project('testproj')
project = self.manager.get_project('testproj')
self.assertEqual('test1', project.project_manager_id)
self.assertEqual('testproj', project.description)
def test_modify_project_adds_new_manager(self):
with user_and_project_generator(self.manager):
with user_generator(self.manager, name='test2'):
@ -320,6 +355,12 @@ class _AuthManagerBaseTestCase(test.TestCase):
project = self.manager.get_project('testproj')
self.assertTrue('test2' in project.member_ids)
def test_create_project_with_missing_user(self):
with user_generator(self.manager):
self.assertRaises(self.user_not_found_type,
self.manager.create_project, 'testproj',
'not_real')
def test_can_delete_project(self):
with user_generator(self.manager):
self.manager.create_project('testproj', 'test1')
@ -344,9 +385,19 @@ class _AuthManagerBaseTestCase(test.TestCase):
self.assertEqual('secret', user.secret)
self.assertTrue(user.is_admin())
def test_can_call_modify_user_but_do_nothing(self):
with user_generator(self.manager):
old_user = self.manager.get_user('test1')
self.manager.modify_user('test1')
user = self.manager.get_user('test1')
self.assertEqual(old_user.access, user.access)
self.assertEqual(old_user.secret, user.secret)
self.assertEqual(old_user.is_admin(), user.is_admin())
class AuthManagerLdapTestCase(_AuthManagerBaseTestCase):
auth_driver = 'nova.auth.ldapdriver.FakeLdapDriver'
user_not_found_type = exception.LDAPUserNotFound
def test_reconnect_on_server_failure(self):
self.manager.get_users()
@ -360,6 +411,7 @@ class AuthManagerLdapTestCase(_AuthManagerBaseTestCase):
class AuthManagerDbTestCase(_AuthManagerBaseTestCase):
auth_driver = 'nova.auth.dbdriver.DbDriver'
user_not_found_type = exception.UserNotFound
if __name__ == "__main__":