Create group table.

Introduce 'group' table and table for link user with groups.
Add simple db code and db tests.

Addresses-Spec: https://review.openstack.org/#/c/269893/
Change-Id: Ie04de344a97b468acf2831bae9da0cac39b2a14a
This commit is contained in:
Andrey Pavlov 2016-01-18 11:51:12 +03:00
parent 04f6150b4d
commit 0483b2ac58
5 changed files with 141 additions and 0 deletions

View File

@ -160,3 +160,13 @@ def delete_pubkey(pubkey_id):
def get_user_pubkeys(user_openid):
"""Get public pubkeys for specified user."""
return IMPL.get_user_pubkeys(user_openid)
def add_user_to_group(user_openid, group_id, created_by_user):
"""Add specified user to specified group."""
return IMPL.add_user_to_group(user_openid, group_id, created_by_user)
def remove_user_from_group(user_openid, group_id):
"""Remove specified user from specified group."""
return IMPL.remove_user_from_group(user_openid, group_id)

View File

@ -0,0 +1,52 @@
"""Create group table and group-user links table.
Revision ID: 319ee8fe47c7
Revises: 428e5aef5534
Create Date: 2016-01-15 16:34:00
"""
# revision identifiers, used by Alembic.
revision = '319ee8fe47c7'
down_revision = '428e5aef5534'
MYSQL_CHARSET = 'utf8'
from alembic import op
import sqlalchemy as sa
def upgrade():
"""Upgrade DB."""
op.create_table(
'group',
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime()),
sa.Column('deleted_at', sa.DateTime()),
sa.Column('deleted', sa.Integer, default=0),
sa.Column('id', sa.String(36), nullable=False),
sa.Column('name', sa.String(length=80), nullable=False),
sa.Column('description', sa.Text()),
sa.PrimaryKeyConstraint('id'),
mysql_charset=MYSQL_CHARSET
)
op.create_table(
'user_to_group',
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime()),
sa.Column('deleted_at', sa.DateTime()),
sa.Column('deleted', sa.Integer, default=0),
sa.Column('created_by_user', sa.String(length=128), nullable=False),
sa.Column('_id', sa.Integer(), nullable=False),
sa.Column('group_id', sa.String(36), nullable=False),
sa.Column('user_openid', sa.String(length=128), nullable=False),
sa.PrimaryKeyConstraint('_id'),
sa.ForeignKeyConstraint(['user_openid'], ['user.openid'], ),
sa.ForeignKeyConstraint(['group_id'], ['group.id'], ),
mysql_charset=MYSQL_CHARSET
)
def downgrade():
"""Downgrade DB."""
op.drop_table('user_to_group')
op.drop_table('group')

View File

@ -335,3 +335,24 @@ def get_user_pubkeys(user_openid):
session = get_session()
pubkeys = session.query(models.PubKey).filter_by(openid=user_openid).all()
return _to_dict(pubkeys)
def add_user_to_group(user_openid, group_id, created_by_user):
"""Add specified user to specified group."""
item = models.UserToGroup()
session = get_session()
with session.begin():
item.user_openid = user_openid
item.group_id = group_id
item.created_by_user = created_by_user
item.save(session=session)
def remove_user_from_group(user_openid, group_id):
"""Remove specified user from specified group."""
session = get_session()
with session.begin():
(session.query(models.UserToGroup).
filter_by(user_openid=user_openid).
filter_by(group_id=group_id).
delete(synchronize_session=False))

View File

@ -159,3 +159,37 @@ class PubKey(BASE, RefStackBase): # pragma: no cover
def default_allowed_keys(self):
"""Default keys."""
return 'id', 'openid', 'format', 'pubkey', 'comment'
class Group(BASE, RefStackBase): # pragma: no cover
"""Group definition."""
__tablename__ = 'group'
id = sa.Column(sa.String(36), primary_key=True,
default=lambda: six.text_type(uuid.uuid4()))
name = sa.Column(sa.String(80), nullable=False)
description = sa.Column(sa.Text())
@property
def default_allowed_keys(self):
"""Default keys."""
return 'id', 'name', 'description'
class UserToGroup(BASE, RefStackBase): # pragma: no cover
"""user-group as many-to-many."""
__tablename__ = 'user_to_group'
created_by_user = sa.Column(sa.String(128), nullable=False)
_id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
user_openid = sa.Column(sa.String(128), sa.ForeignKey('user.openid'),
nullable=False, index=True)
group_id = sa.Column(sa.String(36), sa.ForeignKey('group.id'),
nullable=False)
@property
def default_allowed_keys(self):
"""Default keys."""
return 'user_openid', 'group_id'

View File

@ -614,3 +614,27 @@ class DBBackendTestCase(base.BaseTestCase):
session.query.return_value.filter_by.assert_called_once_with(
openid='user_id')
self.assertEqual(keys, actual_keys)
@mock.patch.object(api, 'get_session')
@mock.patch('refstack.db.sqlalchemy.models.UserToGroup')
def test_add_user_to_group(self, mock_model, mock_get_session):
session = mock_get_session.return_value
api.add_user_to_group('user-123', 'GUID', 'user-321')
mock_model.assert_called_once_with()
mock_get_session.assert_called_once_with()
mock_model.return_value.save.assert_called_once_with(session=session)
session.begin.assert_called_once_with()
@mock.patch.object(api, 'get_session')
@mock.patch('refstack.db.sqlalchemy.api.models')
def test_remove_user_from_group(self, mock_models, mock_get_session):
session = mock_get_session.return_value
db.remove_user_from_group('user-123', 'GUID')
session.query.assert_called_once_with(mock_models.UserToGroup)
session.query.return_value.filter_by.assert_has_calls((
mock.call(user_openid='user-123'),
mock.call().filter_by(group_id='GUID'),
mock.call().filter_by().delete(synchronize_session=False)))
session.begin.assert_called_once_with()