De-avatarify most of room booking (except room owner)
[cds-indico.git] / indico / testing / fixtures / user.py
blob8e9c1af30e7e0d8a23becdae33f13a26d4b46609
1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 import pytest
19 from indico.modules.groups import GroupProxy
20 from indico.modules.groups.models.groups import LocalGroup
21 from indico.modules.rb import settings as rb_settings
22 from indico.modules.users import User
25 @pytest.yield_fixture
26 def create_user(db):
27 """Returns a callable which lets you create dummy users"""
28 _users = set()
30 def _create_user(id_, first_name=u'Guinea', last_name=u'Pig', rb_admin=False, admin=False, email=None, groups=None,
31 legacy=True):
32 user = User.get(id_)
33 if user:
34 return user.as_avatar if legacy else user
35 user = User()
36 user.id = id_
37 user.first_name = first_name
38 user.last_name = last_name
39 user.email = email or u'{}@example.com'.format(id_)
40 user.is_admin = admin
41 user.local_groups = {g.group for g in (groups or ())}
42 db.session.add(user)
43 db.session.flush()
44 if rb_admin:
45 rb_settings.set('admin_principals', rb_settings.get('admin_principals') + [user.as_principal])
46 db.session.flush()
47 _users.add(user)
48 return user.as_avatar if legacy else user
50 yield _create_user
52 admins = set(map(tuple, rb_settings.get('admin_principals')))
53 for user in _users:
54 admins.discard(user.as_principal)
55 db.session.delete(user)
56 rb_settings.set('admin_principals', list(admins))
59 @pytest.fixture
60 def dummy_user(create_user):
61 """Creates a mocked dummy avatar"""
62 return create_user(1337)
65 @pytest.yield_fixture
66 def create_group(db):
67 """Returns a callable which lets you create dummy groups"""
68 _groups = set()
70 def _create_group(id_):
71 group = LocalGroup()
72 group.id = id_
73 group.name = u'dummy-{}'.format(id_)
74 db.session.add(group)
75 db.session.flush()
76 _groups.add(group)
77 return GroupProxy(group.id, _group=group)
79 yield _create_group
81 for group in _groups:
82 db.session.delete(group)
85 @pytest.fixture
86 def dummy_group(create_group):
87 """Creates a mocked dummy group"""
88 return create_group(1337)