Files
@ 8d98924c58b1
Branch filter:
Location: kallithea/kallithea/tests/models/test_users.py
8d98924c58b1
4.6 KiB
text/x-python
tests: add as little code as possible in __init__.py
kallithea/tests/__init__.py contained quite a lot of code, including the test
base class TestController. This in itself may be considered bad practice.
Specifically, this poses a problem when using pytest 3.0+, in which asserts
in some files are not automatically rewritten to give improved assert
output. That problem can be fixed by explicitly registering such files for
assertion rewriting, but that register call should be executed _before_ said
files are imported. I.e. if the register call is in
kallithea/tests/__init__.py, assert calls in __init__.py itself can not be
rewritten.
Since the TestController base class does effectively contain asserts, and we
do not want to execute the register call from somewhere outside the
kallithea/tests directory, we need to move the TestController class to
another file (kallithea/tests/base.py) so we can have a register call in
__init__.py before loading base.py.
While not strictly necessary to fix the mentioned pytest problem, we take
the opportunity to fully clean __init__.py and move everything to
the new kallithea/tests/base.py. While doing so, unnecessary imports are
removed, and imports are ordered alphabetically. Explicit imports of symbols
from modules that were already imported as a whole, are removed in favor of
fully qualifying the references (e.g. tempfile._RandomNameSequence).
kallithea/tests/__init__.py contained quite a lot of code, including the test
base class TestController. This in itself may be considered bad practice.
Specifically, this poses a problem when using pytest 3.0+, in which asserts
in some files are not automatically rewritten to give improved assert
output. That problem can be fixed by explicitly registering such files for
assertion rewriting, but that register call should be executed _before_ said
files are imported. I.e. if the register call is in
kallithea/tests/__init__.py, assert calls in __init__.py itself can not be
rewritten.
Since the TestController base class does effectively contain asserts, and we
do not want to execute the register call from somewhere outside the
kallithea/tests directory, we need to move the TestController class to
another file (kallithea/tests/base.py) so we can have a register call in
__init__.py before loading base.py.
While not strictly necessary to fix the mentioned pytest problem, we take
the opportunity to fully clean __init__.py and move everything to
the new kallithea/tests/base.py. While doing so, unnecessary imports are
removed, and imports are ordered alphabetically. Explicit imports of symbols
from modules that were already imported as a whole, are removed in favor of
fully qualifying the references (e.g. tempfile._RandomNameSequence).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | import pytest
from kallithea.tests.base import *
from kallithea.model.db import User, UserGroup, UserGroupMember, UserEmailMap, \
Permission
from kallithea.model.user import UserModel
from kallithea.model.meta import Session
from kallithea.model.user_group import UserGroupModel
from kallithea.tests.fixture import Fixture
fixture = Fixture()
class TestUser(TestController):
@classmethod
def setup_class(cls):
Session.remove()
def teardown_method(self, method):
Session.remove()
def test_create_and_remove(self):
usr = UserModel().create_or_update(username=u'test_user',
password=u'qweqwe',
email=u'u232@example.com',
firstname=u'u1', lastname=u'u1')
Session().commit()
assert User.get_by_username(u'test_user') == usr
assert User.get_by_username(u'test_USER', case_insensitive=True) == usr
assert User.get_by_username(u'test_USER', case_insensitive=False) == None
# make user group
user_group = fixture.create_user_group(u'some_example_group')
Session().commit()
UserGroupModel().add_user_to_group(user_group, usr)
Session().commit()
assert UserGroup.get(user_group.users_group_id) == user_group
assert UserGroupMember.query().count() == 1
UserModel().delete(usr.user_id)
Session().commit()
assert UserGroupMember.query().all() == []
def test_additional_email_as_main(self):
usr = UserModel().create_or_update(username=u'test_user',
password=u'qweqwe',
email=u'main_email@example.com',
firstname=u'u1', lastname=u'u1')
Session().commit()
with pytest.raises(AttributeError):
m = UserEmailMap()
m.email = u'main_email@example.com'
m.user = usr
Session().add(m)
Session().commit()
UserModel().delete(usr.user_id)
Session().commit()
def test_extra_email_map(self):
usr = UserModel().create_or_update(username=u'test_user',
password=u'qweqwe',
email=u'main_email@example.com',
firstname=u'u1', lastname=u'u1')
Session().commit()
m = UserEmailMap()
m.email = u'main_email2@example.com'
m.user = usr
Session().add(m)
Session().commit()
u = User.get_by_email(email='MAIN_email@example.com')
assert usr.user_id == u.user_id
assert usr.username == u.username
u = User.get_by_email(email='main_email@example.com')
assert usr.user_id == u.user_id
assert usr.username == u.username
u = User.get_by_email(email='main_email2@example.com')
assert usr.user_id == u.user_id
assert usr.username == u.username
u = User.get_by_email(email='main_email3@example.com')
assert None == u
u = User.get_by_email(email='main_e%ail@example.com')
assert None == u
u = User.get_by_email(email='main_emai_@example.com')
assert None == u
UserModel().delete(usr.user_id)
Session().commit()
class TestUsers(TestController):
def setup_method(self, method):
self.u1 = UserModel().create_or_update(username=u'u1',
password=u'qweqwe',
email=u'u1@example.com',
firstname=u'u1', lastname=u'u1')
def teardown_method(self, method):
perm = Permission.query().all()
for p in perm:
UserModel().revoke_perm(self.u1, p)
UserModel().delete(self.u1)
Session().commit()
Session.remove()
def test_add_perm(self):
perm = Permission.query().all()[0]
UserModel().grant_perm(self.u1, perm)
Session().commit()
assert UserModel().has_perm(self.u1, perm) == True
def test_has_perm(self):
perm = Permission.query().all()
for p in perm:
has_p = UserModel().has_perm(self.u1, p)
assert False == has_p
def test_revoke_perm(self):
perm = Permission.query().all()[0]
UserModel().grant_perm(self.u1, perm)
Session().commit()
assert UserModel().has_perm(self.u1, perm) == True
#revoke
UserModel().revoke_perm(self.u1, perm)
Session().commit()
assert UserModel().has_perm(self.u1, perm) == False
|