2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2010-09-12 16:37:00 +00:00
|
|
|
This defines some test commands for use while testing the MUD and its components.
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
|
2010-09-12 16:37:00 +00:00
|
|
|
from django.conf import settings
|
2010-08-29 18:46:58 +00:00
|
|
|
from django.db import IntegrityError
|
|
|
|
|
from src.comms.models import Msg
|
|
|
|
|
from src.permissions import permissions
|
2010-10-21 20:15:11 +00:00
|
|
|
from src.utils import create, debug, utils
|
2010-11-23 01:24:56 +00:00
|
|
|
from src.commands.default.muxcommand import MuxCommand
|
2010-09-12 16:37:00 +00:00
|
|
|
|
2010-09-19 06:57:08 +00:00
|
|
|
from src.commands import cmdsethandler
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
# Test permissions
|
|
|
|
|
|
|
|
|
|
class CmdTest(MuxCommand):
|
|
|
|
|
"""
|
|
|
|
|
test the command system
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
@test <any argument or switch>
|
|
|
|
|
|
|
|
|
|
This command will echo back all argument or switches
|
|
|
|
|
given to it, showcasing the muxcommand style.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
key = "@test"
|
|
|
|
|
aliases = ["@te", "@test all"]
|
2010-10-31 08:10:02 +00:00
|
|
|
help_category = "Testing"
|
|
|
|
|
permissions = "cmd:Immortals" #Wizards
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
# the muxcommand class itself handles the display
|
|
|
|
|
# so we just defer to it by not adding any function.
|
2010-09-19 06:57:08 +00:00
|
|
|
|
|
|
|
|
def func(self):
|
2010-10-21 20:15:11 +00:00
|
|
|
|
|
|
|
|
def test():
|
|
|
|
|
li = []
|
|
|
|
|
for l in range(10000):
|
|
|
|
|
li.append(l)
|
|
|
|
|
self.caller.msg(li[-1])
|
|
|
|
|
return "This is the return text"
|
|
|
|
|
#print 1/0
|
|
|
|
|
def succ(f):
|
|
|
|
|
self.caller.msg("This is called after successful completion. Return value: %s" % f)
|
|
|
|
|
def err(e):
|
|
|
|
|
self.caller.msg("An error was encountered... %s" % e)
|
|
|
|
|
|
|
|
|
|
#self.caller.msg("printed before call to sync run ...")
|
|
|
|
|
#test()
|
|
|
|
|
#self.caller.msg("after after call to sync run...")
|
|
|
|
|
|
|
|
|
|
self.caller.msg("printed before call to async run ...")
|
|
|
|
|
utils.run_async(test, at_return=succ, at_err=err)
|
|
|
|
|
self.caller.msg("printed after call to async run ...")
|
|
|
|
|
|
|
|
|
|
#cmdsetname = "game.gamesrc.commands.default.cmdset_default.DefaultCmdSet"
|
|
|
|
|
#self.caller.msg(cmdsethandler.CACHED_CMDSETS)
|
|
|
|
|
#cmdsethandler.import_cmdset(cmdsetname, self, self)
|
|
|
|
|
#self.caller.msg("Imported %s" % cmdsetname)
|
|
|
|
|
#self.caller.msg(cmdsethandler.CACHED_CMDSETS)
|
2010-09-19 06:57:08 +00:00
|
|
|
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class CmdTestPerms(MuxCommand):
|
|
|
|
|
"""
|
|
|
|
|
Test command - test permissions
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
@testperm [[lockstring] [=permstring]]
|
|
|
|
|
|
|
|
|
|
With no arguments, runs a sequence of tests for the
|
|
|
|
|
permission system using the calling player's permissions.
|
|
|
|
|
|
|
|
|
|
If <lockstring> is given, match caller's permissions
|
|
|
|
|
against these locks. If also <permstring> is given,
|
|
|
|
|
match this against the given locks instead.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
key = "@testperm"
|
|
|
|
|
permissions = "cmd:Immortals Wizards"
|
2010-10-31 08:10:02 +00:00
|
|
|
help_category = "Testing"
|
2010-09-19 06:57:08 +00:00
|
|
|
def func(self):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
Run tests
|
|
|
|
|
"""
|
2010-09-19 06:57:08 +00:00
|
|
|
caller = self.caller
|
|
|
|
|
|
|
|
|
|
if caller.user.is_superuser:
|
|
|
|
|
caller.msg("You are a superuser. Permission tests are pointless.")
|
2010-08-29 18:46:58 +00:00
|
|
|
return
|
|
|
|
|
# create a test object
|
|
|
|
|
obj = create.create_object(None, "accessed_object") # this will use default typeclass
|
|
|
|
|
obj_id = obj.id
|
2010-09-19 06:57:08 +00:00
|
|
|
caller.msg("obj_attr: %s" % obj.attr("testattr"))
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
# perms = ["has_permission", "has permission", "skey:has_permission",
|
|
|
|
|
# "has_id(%s)" % obj_id, "has_attr(testattr)",
|
|
|
|
|
# "has_attr(testattr, testattr_value)"]
|
|
|
|
|
|
|
|
|
|
# test setting permissions
|
2010-09-19 06:57:08 +00:00
|
|
|
uprofile = caller.user.get_profile()
|
2010-08-29 18:46:58 +00:00
|
|
|
# do testing
|
2010-09-19 06:57:08 +00:00
|
|
|
caller.msg("----------------")
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
permissions.set_perm(obj, "has_permission")
|
|
|
|
|
permissions.add_perm(obj, "skey:has_permission")
|
2010-09-19 06:57:08 +00:00
|
|
|
caller.msg(" keys:[%s] locks:[%s]" % (uprofile.permissions, obj.permissions))
|
|
|
|
|
caller.msg("normal permtest: %s" % permissions.has_perm(uprofile, obj))
|
|
|
|
|
caller.msg("skey permtest: %s" % permissions.has_perm(uprofile, obj, 'skey'))
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
permissions.set_perm(uprofile, "has_permission")
|
2010-09-19 06:57:08 +00:00
|
|
|
caller.msg(" keys:[%s] locks:[%s]" % (uprofile.permissions, obj.permissions))
|
|
|
|
|
caller.msg("normal permtest: %s" % permissions.has_perm(uprofile, obj))
|
|
|
|
|
caller.msg("skey permtest: %s" % permissions.has_perm(uprofile, obj, 'skey'))
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
# function tests
|
|
|
|
|
permissions.set_perm(obj, "has_id(%s)" % (uprofile.id))
|
2010-09-19 06:57:08 +00:00
|
|
|
caller.msg(" keys:[%s] locks:[%s]" % (uprofile.permissions, obj.permissions))
|
|
|
|
|
caller.msg("functest: %s" % permissions.has_perm(uprofile, obj))
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
uprofile.attr("testattr", "testattr_value")
|
|
|
|
|
permissions.set_perm(obj, "has_attr(testattr, testattr_value)")
|
2010-09-19 06:57:08 +00:00
|
|
|
caller.msg(" keys:[%s] locks:[%s]" % (uprofile.permissions, obj.permissions))
|
|
|
|
|
caller.msg("functest: %s" % permissions.has_perm(uprofile, obj))
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
# cleanup of test permissions
|
|
|
|
|
permissions.del_perm(uprofile, "has_permission")
|
2010-09-19 06:57:08 +00:00
|
|
|
caller.msg(" cleanup: keys:[%s] locks:[%s]" % (uprofile.permissions, obj.permissions))
|
2010-08-29 18:46:58 +00:00
|
|
|
obj.delete()
|
|
|
|
|
uprofile.attr("testattr", delete=True)
|
|
|
|
|
|
|
|
|
|
|
2010-09-19 06:57:08 +00:00
|
|
|
# # Add/remove states (removed; not valid.)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2010-09-19 06:57:08 +00:00
|
|
|
# EXAMPLE_STATE="game.gamesrc.commands.examples.example.EXAMPLESTATE"
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2010-09-19 06:57:08 +00:00
|
|
|
# class CmdTestState(MuxCommand):
|
|
|
|
|
# """
|
|
|
|
|
# Test command - add a state.
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2010-09-19 06:57:08 +00:00
|
|
|
# Usage:
|
|
|
|
|
# @teststate[/switch] [<python path to state instance>]
|
|
|
|
|
# Switches:
|
|
|
|
|
# add - add a state
|
|
|
|
|
# clear - remove all added states.
|
|
|
|
|
# list - view current state stack
|
|
|
|
|
# reload - reload current state stack
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2010-09-19 06:57:08 +00:00
|
|
|
# If no python path is given, an example state will be added.
|
|
|
|
|
# You will know it worked if you can use the commands '@testcommand'
|
|
|
|
|
# and 'smile'.
|
|
|
|
|
# """
|
|
|
|
|
|
|
|
|
|
# key = "@teststate"
|
|
|
|
|
# alias = "@testingstate"
|
|
|
|
|
# permissions = "cmd:Immortals Wizards"
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2010-09-19 06:57:08 +00:00
|
|
|
# def func(self):
|
|
|
|
|
# """
|
|
|
|
|
# inp is the dict returned from MuxCommand's parser.
|
|
|
|
|
# """
|
|
|
|
|
# caller = self.caller
|
|
|
|
|
# switches = self.switches
|
|
|
|
|
|
|
|
|
|
# if not switches or switches[0] not in ["add", "clear", "list", "reload"]:
|
|
|
|
|
# string = "Usage: @teststate[/add|clear|list|reload] [<python path>]"
|
|
|
|
|
# caller.msg(string)
|
|
|
|
|
# elif "clear" in switches:
|
|
|
|
|
# caller.cmdset.clear()
|
|
|
|
|
# caller.msg("All cmdset cleared.")
|
|
|
|
|
# return
|
|
|
|
|
# elif "list" in switches:
|
|
|
|
|
# string = "%s" % caller.cmdset
|
|
|
|
|
# caller.msg(string)
|
|
|
|
|
# elif "reload" in switches:
|
|
|
|
|
# caller.cmdset.load()
|
|
|
|
|
# caller.msg("Cmdset reloaded.")
|
|
|
|
|
# else: #add
|
|
|
|
|
# arg = inp["raw"]
|
|
|
|
|
# if not arg:
|
|
|
|
|
# arg = EXAMPLE_STATE
|
|
|
|
|
# caller.cmdset.add(arg)
|
|
|
|
|
# string = "Added state '%s'." % caller.cmdset.state.key
|
|
|
|
|
# caller.msg(string)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
class TestCom(MuxCommand):
|
|
|
|
|
"""
|
|
|
|
|
Test the command system
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
@testcom/create/list [channel]
|
|
|
|
|
"""
|
|
|
|
|
key = "@testcom"
|
|
|
|
|
permissions = "cmd:Immortals Wizards"
|
2010-10-31 08:10:02 +00:00
|
|
|
help_category = "Testing"
|
2010-08-29 18:46:58 +00:00
|
|
|
def func(self):
|
|
|
|
|
"Run the test program"
|
|
|
|
|
caller = self.caller
|
|
|
|
|
|
|
|
|
|
if 'create' in self.switches:
|
|
|
|
|
if self.args:
|
|
|
|
|
chankey = self.args
|
|
|
|
|
try:
|
|
|
|
|
channel = create.create_channel(chankey)
|
|
|
|
|
except IntegrityError:
|
|
|
|
|
caller.msg("Channel '%s' already exists." % chankey)
|
|
|
|
|
return
|
|
|
|
|
channel.connect_to(caller)
|
|
|
|
|
caller.msg("Created new channel %s" % chankey)
|
|
|
|
|
msgobj = create.create_message(caller.player,
|
|
|
|
|
"First post to new channel!")
|
|
|
|
|
channel.msg(msgobj)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
elif 'list' in self.switches:
|
|
|
|
|
msgresults = Msg.objects.get_messages_by_sender(caller)
|
|
|
|
|
string = "\n".join(["%s %s: %s" % (msg.date_sent,
|
|
|
|
|
[str(chan.key) for chan in msg.channels.all()],
|
|
|
|
|
msg.message)
|
|
|
|
|
for msg in msgresults])
|
|
|
|
|
caller.msg(string)
|
|
|
|
|
return
|
|
|
|
|
caller.msg("Usage: @testcom/create channel")
|