Add tag/attr to create_account. More unit tests

This commit is contained in:
Griatch 2019-02-10 02:08:21 +01:00
parent 0dfea46d5c
commit c4faa6879b
5 changed files with 114 additions and 18 deletions

View file

@ -85,7 +85,8 @@ Web/Django standard initiative (@strikaco)
### Utils ### Utils
- Added more unit tests. - `evennia.utils.create.account` now also takes `tags` and `attrs` keywords.
- Added many more unit tests.
## Evennia 0.8 (2018) ## Evennia 0.8 (2018)

View file

@ -1340,18 +1340,27 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
# list of targets - make list to disconnect from db # list of targets - make list to disconnect from db
characters = list(tar for tar in target if tar) if target else [] characters = list(tar for tar in target if tar) if target else []
sessions = self.sessions.all() sessions = self.sessions.all()
if not sessions:
# no sessions, nothing to report
return ""
is_su = self.is_superuser is_su = self.is_superuser
# text shown when looking in the ooc area # text shown when looking in the ooc area
result = ["Account |g%s|n (you are Out-of-Character)" % self.key] result = ["Account |g%s|n (you are Out-of-Character)" % self.key]
nsess = len(sessions) nsess = len(sessions)
result.append(nsess == 1 and "\n\n|wConnected session:|n" or "\n\n|wConnected sessions (%i):|n" % nsess) result.append(nsess == 1 and
"\n\n|wConnected session:|n" or
"\n\n|wConnected sessions (%i):|n" % nsess)
for isess, sess in enumerate(sessions): for isess, sess in enumerate(sessions):
csessid = sess.sessid csessid = sess.sessid
addr = "%s (%s)" % (sess.protocol_key, isinstance(sess.address, tuple) and addr = "%s (%s)" % (sess.protocol_key, isinstance(sess.address, tuple) and
str(sess.address[0]) or str(sess.address)) str(sess.address[0]) or
result.append("\n %s %s" % (session.sessid == csessid and "|w* %s|n" % (isess + 1) or str(sess.address))
result.append("\n %s %s" % (
session and
session.sessid == csessid and
"|w* %s|n" % (isess + 1) or
" %s" % (isess + 1), addr)) " %s" % (isess + 1), addr))
result.append("\n\n |whelp|n - more commands") result.append("\n\n |whelp|n - more commands")
result.append("\n |wooc <Text>|n - talk on public channel") result.append("\n |wooc <Text>|n - talk on public channel")

View file

@ -62,19 +62,19 @@ class TestAccountSessionHandler(TestCase):
self.assertEqual(self.handler.count(), len(self.handler.get())) self.assertEqual(self.handler.count(), len(self.handler.get()))
@override_settings(GUEST_ENABLED=True, GUEST_LIST=["bruce_wayne"])
class TestDefaultGuest(EvenniaTest): class TestDefaultGuest(EvenniaTest):
"Check DefaultGuest class" "Check DefaultGuest class"
ip = '212.216.134.22' ip = '212.216.134.22'
def test_authenticate(self): @override_settings(GUEST_ENABLED=False)
def test_create_not_enabled(self):
# Guest account should not be permitted # Guest account should not be permitted
account, errors = DefaultGuest.authenticate(ip=self.ip) account, errors = DefaultGuest.authenticate(ip=self.ip)
self.assertFalse(account, 'Guest account was created despite being disabled.') self.assertFalse(account, 'Guest account was created despite being disabled.')
settings.GUEST_ENABLED = True def test_authenticate(self):
settings.GUEST_LIST = ['bruce_wayne']
# Create a guest account # Create a guest account
account, errors = DefaultGuest.authenticate(ip=self.ip) account, errors = DefaultGuest.authenticate(ip=self.ip)
self.assertTrue(account, 'Guest account should have been created.') self.assertTrue(account, 'Guest account should have been created.')
@ -83,7 +83,32 @@ class TestDefaultGuest(EvenniaTest):
account, errors = DefaultGuest.authenticate(ip=self.ip) account, errors = DefaultGuest.authenticate(ip=self.ip)
self.assertFalse(account, 'Two guest accounts were created with a single entry on the guest list!') self.assertFalse(account, 'Two guest accounts were created with a single entry on the guest list!')
settings.GUEST_ENABLED = False @patch("evennia.accounts.accounts.ChannelDB.objects.get_channel")
def test_create(self, get_channel):
get_channel.connect = MagicMock(return_value=True)
account, errors = DefaultGuest.create()
self.assertTrue(account, "Guest account should have been created.")
self.assertFalse(errors)
def test_at_post_login(self):
self.account.db._last_puppet = self.char1
self.account.at_post_login(self.session)
self.account.at_post_login()
def test_at_server_shutdown(self):
account, errors = DefaultGuest.create(ip=self.ip)
self.char1.delete = MagicMock()
account.db._playable_characters = [self.char1]
account.at_server_shutdown()
self.char1.delete.assert_called()
def test_at_post_disconnect(self):
account, errors = DefaultGuest.create(ip=self.ip)
self.char1.delete = MagicMock()
account.db._playable_characters = [self.char1]
account.at_post_disconnect()
self.char1.delete.assert_called()
class TestDefaultAccountAuth(EvenniaTest): class TestDefaultAccountAuth(EvenniaTest):
@ -315,3 +340,50 @@ class TestDefaultAccountEv(EvenniaTest):
with patch("evennia.accounts.accounts._MULTISESSION_MODE", 2): with patch("evennia.accounts.accounts._MULTISESSION_MODE", 2):
self.account.puppet_object(self.session, self.char1) self.account.puppet_object(self.session, self.char1)
self.account.msg.assert_called_with("You are already puppeting this object.") self.account.msg.assert_called_with("You are already puppeting this object.")
@patch("evennia.accounts.accounts.time.time", return_value=10000)
def test_idle_time(self, mock_time):
self.session.cmd_last_visible = 10000 - 10
idle = self.account.idle_time
self.assertEqual(idle, 10)
# test no sessions
unload_module("evennia.accounts.accounts")
with patch("evennia.accounts.accounts._SESSIONS.sessions_from_account", return_value=[]) as mock_sessh:
idle = self.account.idle_time
self.assertEqual(idle, None)
@patch("evennia.accounts.accounts.time.time", return_value=10000)
def test_connection_time(self, mock_time):
self.session.conn_time = 10000 - 10
conn = self.account.connection_time
self.assertEqual(conn, 10)
# test no sessions
unload_module("evennia.accounts.accounts")
with patch("evennia.accounts.accounts._SESSIONS.sessions_from_account", return_value=[]) as mock_sessh:
idle = self.account.connection_time
self.assertEqual(idle, None)
def test_create_account(self):
acct = create.account(
"TestAccount3", "test@test.com", "testpassword123",
locks="test:all()",
tags=[("tag1", "category1"), ("tag2", "category2", "data1"), ("tag3", None)],
attributes=[("key1", "value1", "category1",
"edit:false()", True),
("key2", "value2")])
acct.save()
self.assertTrue(acct.pk)
def test_at_look(self):
ret = self.account.at_look()
self.assertTrue("Out-of-Character" in ret)
ret = self.account.at_look(target=self.obj1)
self.assertTrue("Obj" in ret)
ret = self.account.at_look(session=self.session)
self.assertTrue("*" in ret) # * marks session is active in list
ret = self.account.at_look(target=self.obj1, session=self.session)
self.assertTrue("Obj" in ret)
ret = self.account.at_look(target="Invalid", session=self.session)
self.assertEqual(ret, 'Invalid has no in-game appearance.')

View file

@ -402,6 +402,7 @@ def create_account(key, email, password,
typeclass=None, typeclass=None,
is_superuser=False, is_superuser=False,
locks=None, permissions=None, locks=None, permissions=None,
tags=None, attributes=None,
report_to=None): report_to=None):
""" """
This creates a new account. This creates a new account.
@ -414,15 +415,20 @@ def create_account(key, email, password,
password (str): Password in cleartext. password (str): Password in cleartext.
Kwargs: Kwargs:
typeclass (str): The typeclass to use for the account.
is_superuser (bool): Wether or not this account is to be a superuser is_superuser (bool): Wether or not this account is to be a superuser
locks (str): Lockstring. locks (str): Lockstring.
permission (list): List of permission strings. permission (list): List of permission strings.
tags (list): List of Tags on form `(key, category[, data])`
attributes (list): List of Attributes on form
`(key, value [, category, [,lockstring [, default_pass]]])`
report_to (Object): An object with a msg() method to report report_to (Object): An object with a msg() method to report
errors to. If not given, errors will be logged. errors to. If not given, errors will be logged.
Raises: Raises:
ValueError: If `key` already exists in database. ValueError: If `key` already exists in database.
Notes: Notes:
Usually only the server admin should need to be superuser, all Usually only the server admin should need to be superuser, all
other access levels can be handled with more fine-grained other access levels can be handled with more fine-grained
@ -437,6 +443,8 @@ def create_account(key, email, password,
typeclass = typeclass if typeclass else settings.BASE_ACCOUNT_TYPECLASS typeclass = typeclass if typeclass else settings.BASE_ACCOUNT_TYPECLASS
locks = make_iter(locks) if locks is not None else None locks = make_iter(locks) if locks is not None else None
permissions = make_iter(permissions) if permissions is not None else None permissions = make_iter(permissions) if permissions is not None else None
tags = make_iter(tags) if tags is not None else None
attributes = make_iter(attributes) if attributes is not None else None
if isinstance(typeclass, basestring): if isinstance(typeclass, basestring):
# a path is given. Load the actual typeclass. # a path is given. Load the actual typeclass.
@ -462,7 +470,8 @@ def create_account(key, email, password,
is_staff=is_superuser, is_superuser=is_superuser, is_staff=is_superuser, is_superuser=is_superuser,
last_login=now, date_joined=now) last_login=now, date_joined=now)
new_account.set_password(password) new_account.set_password(password)
new_account._createdict = dict(locks=locks, permissions=permissions, report_to=report_to) new_account._createdict = dict(locks=locks, permissions=permissions, report_to=report_to,
tags=tags, attributes=attributes)
# saving will trigger the signal that calls the # saving will trigger the signal that calls the
# at_first_save hook on the typeclass, where the _createdict # at_first_save hook on the typeclass, where the _createdict
# can be used. # can be used.

View file

@ -19,13 +19,15 @@ SESSIONS.data_out = Mock()
SESSIONS.disconnect = Mock() SESSIONS.disconnect = Mock()
def unload_module(module_or_object): def unload_module(module):
""" """
Reset import so one can mock global constants. Reset import so one can mock global constants.
Args: Args:
module_or_object (module or object): The module will module (module, object or str): The module will
be removed so it will have to be imported again. be removed so it will have to be imported again. If given
an object, the module in which that object sits will be unloaded. A string
should directly give the module pathname to unload.
Example: Example:
# (in a test method) # (in a test method)
@ -40,10 +42,13 @@ def unload_module(module_or_object):
loaded once). loaded once).
""" """
if hasattr(module_or_object, "__module__"): if isinstance(module, basestring):
modulename = module_or_object.__module__ modulename = module
elif hasattr(module, "__module__"):
modulename = module.__module__
else: else:
modulename = module_or_object.__name__ modulename = module.__name__
if modulename in sys.modules: if modulename in sys.modules:
del sys.modules[modulename] del sys.modules[modulename]