Exemple #1
0
    def setUp(self):
        unittest.TestCase.setUp(self)
        cfgfile = open("/etc/skarphed/skarphed.conf", "r").read().split("\n")
        cfg = {}
        for line in cfgfile:
            if line.startswith("#") or line.find("=") == -1:
                continue
            key, value = line.split("=")
            cfg[key] = value

        del (cfgfile)

        #HERE BE DRAGONS:
        #Currently only supports one testinstance with the instanceID 0
        p = cfg["SCV_WEBPATH"] + "0"
        sys.path.append(p)

        from instanceconf import SCV_INSTANCE_SCOPE_ID
        cfg["SCV_INSTANCE_SCOPE_ID"] = SCV_INSTANCE_SCOPE_ID

        sys.path.append(cfg["SCV_LIBPATH"])

        from scv import Core

        self._core = Core(cfg)
Exemple #2
0
    def setUp(self):
        unittest.TestCase.setUp(self)
        cfgfile = open("/etc/skarphed/skarphed.conf", "r").read().split("\n")
        cfg = {}
        for line in cfgfile:
            if line.startswith("#") or line.find("=") == -1:
                continue
            key, value = line.split("=")
            cfg[key] = value

        del (cfgfile)

        # HERE BE DRAGONS:
        # Currently only supports one testinstance with the instanceID 0
        p = cfg["SCV_WEBPATH"] + "0"
        sys.path.append(p)

        from instanceconf import SCV_INSTANCE_SCOPE_ID

        cfg["SCV_INSTANCE_SCOPE_ID"] = SCV_INSTANCE_SCOPE_ID

        sys.path.append(cfg["SCV_LIBPATH"])

        from scv import Core

        self._core = Core(cfg)
Exemple #3
0
def application(environ, start_response):
    response_body = []
    response_headers = []

    session_id = ""

    core = Core(cfg)

    if environ['PATH_INFO'].startswith("/static/"):
        path = environ['PATH_INFO'].replace("/static/","",1)

        binary_manager = core.get_binary_manager()
        binary = binary_manager.get_by_filename(path)
        data = binary.get_data()
        
        response_body=[data]
        response_headers.append(('Content-Type', binary.get_mime()))

    elif environ['PATH_INFO'].startswith("/rpc/"):
        ret = core.rpc_call(environ)
        response_body.extend(ret["body"])
        response_headers.extend(ret["header"])
        response_headers.append(('Content-Type', 'application/json'))

    elif environ['PATH_INFO'].startswith("/css/"):
        configuration = core.get_configuration()
        css_folder = configuration.get_entry("core.css_folder")
        wpath = configuration.get_entry("global.webpath")
        iid = configuration.get_entry("core.instance_id")
        css = open(wpath+str(iid)+css_folder+"/"+environ["PATH_INFO"].replace("/css/","",1)).read()
        response_body = [css]
        response_headers.append(('Content-Type', 'text/css'))

    elif environ['PATH_INFO'].startswith("/web/"):
        ret = core.web_call(environ)
        response_body.extend(ret["body"])
        response_headers.extend(ret["header"])
        response_headers.append(('Content-Type', 'text/html'))

    elif environ['PATH_INFO'].startswith("/debug/"):
        response_body = ['%s: %s' % (key, value)
                    for key, value in sorted(environ.items())]
        response_body = ['\n'.join(response_body)]
        response_headers.append(('Content-Type', 'text/plain'))

    else: #call default view
        ret = core.web_call(environ)
        response_body.extend(ret["body"])
        response_headers.extend(ret["header"])
        response_headers.append(('Content-Type', 'text/html; charset=utf-8'))


    # So the content-lenght is the sum of all string's lengths
    content_length = 0
    for s in response_body:
        content_length += len(s)

    response_headers.append(('Content-Length', str(content_length)))

    status = '200 OK'

    start_response(status, response_headers)

    return response_body
Exemple #4
0
class CoreTestCase(unittest.TestCase):
    def setUp(self):
        unittest.TestCase.setUp(self)
        cfgfile = open("/etc/skarphed/skarphed.conf", "r").read().split("\n")
        cfg = {}
        for line in cfgfile:
            if line.startswith("#") or line.find("=") == -1:
                continue
            key, value = line.split("=")
            cfg[key] = value

        del (cfgfile)

        # HERE BE DRAGONS:
        # Currently only supports one testinstance with the instanceID 0
        p = cfg["SCV_WEBPATH"] + "0"
        sys.path.append(p)

        from instanceconf import SCV_INSTANCE_SCOPE_ID

        cfg["SCV_INSTANCE_SCOPE_ID"] = SCV_INSTANCE_SCOPE_ID

        sys.path.append(cfg["SCV_LIBPATH"])

        from scv import Core

        self._core = Core(cfg)

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    def setSessionUser(self, permissions=[]):
        """
        Set a sessionuser and give him a list of permissions
        """
        if type(permissions) != list:
            permissions = [permissions]

        user_manager = self._core.get_user_manager()
        session_user = user_manager.create_user("session_user", "password")
        session_manager = self._core.get_session_manager()
        session = session_manager.create_session(session_user)
        session_manager.set_current_session(session)
        for permission in permissions:
            session_user.grant_permission(permission, ignore_check=True)
        self._session_user = session_user
        self._session = session

    def unsetSessionUser(self):
        session_manager = self._core.get_session_manager()
        session_manager.set_current_session(None)
        self._session.delete()
        self._session_user.delete()

    def assertFail(self, msg=""):
        """
        Execute assertFail() to let a test fail instantly
        """
        self.assertTrue(False, msg)

    def setUpTestModule(self):
        """
        Set up a module in the database for testing purposes.
        Returns the generated module.
        """
        db = self._core.get_db()
        nr = db.get_seq_next("MOD_GEN")
        stmnt = "INSERT INTO MODULES (MOD_ID, MOD_NAME, MOD_DISPLAYNAME, MOD_VERSIONMAJOR, MOD_VERSIONMINOR, MOD_VERSIONREV, MOD_JSMANDATORY) \
                      VALUES (?,?,?,?,?,?,?) ;"
        db.query(
            self._core,
            stmnt,
            (nr, "testprogrammer_testmodule", "TestModule", 10, 11, 1337, common.enums.JSMandatory.NO),
            commit=True,
        )
        module_manager = self._core.get_module_manager()
        return module_manager.get_module(nr)

    def tearDownTestModule(self, module=None):
        """
        Tear down a testmodule and erase it from the database.
        Removes every module if called without parameters.
        if parameter module is given, removes only that module
        """
        db = self._core.get_db()
        if module is None:
            db.query(self._core, "DELETE FROM MODULES;", commit=True)
        else:
            stmnt = "DELETE FROM MODULES WHERE MOD_ID = ? ;"
            db.query(self._core, stmnt, (module.get_id(),), commit=True)
        return

    def setUpTestTemplate(self):
        """
        Sets up a test template
        """
        testpermissions = ["skarphed.sites.create", "skarphed.sites.delete", "skarphed.sites.modify"]
        self.setSessionUser(testpermissions)

        templatefile = open("testdata/default_template.tgz", "r")
        templatedata = templatefile.read()
        templatefile.close()

        template_manager = self._core.get_template_manager()
        template_manager.install_from_data(templatedata)

        self.unsetSessionUser()

    def tearDownTestTemplate(self):
        """
        Removes the test template
        """
        testpermissions = ["skarphed.sites.create", "skarphed.sites.delete", "skarphed.sites.modify"]
        self.setSessionUser(testpermissions)

        template_manager = self._core.get_template_manager()
        template = template_manager.get_current_template()
        template.uninstall()
        self.unsetSessionUser()
    if line.startswith("#") or line.find("=") == -1:
        continue
    key, value = line.split("=")
    cfg[key]=value

sys.path.append(os.path.dirname(__file__))

from instanceconf import SCV_INSTANCE_SCOPE_ID
cfg["SCV_INSTANCE_SCOPE_ID"] = SCV_INSTANCE_SCOPE_ID

sys.path.append(cfg["SCV_LIBPATH"])

from scv import Core
from scv import OperationDaemon

core = Core(cfg)
configuration = core.get_configuration()
pidfile = configuration.get_entry("core.webpath")+"/opd.pid"
opd = OperationDaemon(core, pidfile)

# This script accepts a dummy argument (sys.argv[2])
# This argument is supposed to be the instance id, so
# one can distinguish the daemon-processes from each 
# other in e.g. htop

success = False
if len(sys.argv) == 2 or len(sys.argv) == 3:
    if sys.argv[1] == 'start':
        opd.start()
        success = True
    elif sys.argv[1] == 'stop':
Exemple #6
0
def application(environ, start_response):
    response_body = []
    response_headers = []

    session_id = ""

    core = Core(cfg)

    if environ['PATH_INFO'].startswith("/static/"):
        path = environ['PATH_INFO'].replace("/static/", "", 1)

        binary_manager = core.get_binary_manager()
        binary = binary_manager.get_by_filename(path)
        data = binary.get_data()

        response_body = [data]
        response_headers.append(('Content-Type', binary.get_mime()))

    elif environ['PATH_INFO'].startswith("/rpc/"):
        ret = core.rpc_call(environ)
        response_body.extend(ret["body"])
        response_headers.extend(ret["header"])
        response_headers.append(('Content-Type', 'application/json'))

    elif environ['PATH_INFO'].startswith("/css/"):
        configuration = core.get_configuration()
        css_folder = configuration.get_entry("core.css_folder")
        wpath = configuration.get_entry("global.webpath")
        iid = configuration.get_entry("core.instance_id")
        css = open(wpath + str(iid) + css_folder + "/" +
                   environ["PATH_INFO"].replace("/css/", "", 1)).read()
        response_body = [css]
        response_headers.append(('Content-Type', 'text/css'))

    elif environ['PATH_INFO'].startswith("/web/"):
        ret = core.web_call(environ)
        response_body.extend(ret["body"])
        response_headers.extend(ret["header"])
        response_headers.append(('Content-Type', 'text/html'))

    elif environ['PATH_INFO'].startswith("/debug/"):
        response_body = [
            '%s: %s' % (key, value) for key, value in sorted(environ.items())
        ]
        response_body = ['\n'.join(response_body)]
        response_headers.append(('Content-Type', 'text/plain'))

    else:  #call default view
        ret = core.web_call(environ)
        response_body.extend(ret["body"])
        response_headers.extend(ret["header"])
        response_headers.append(('Content-Type', 'text/html; charset=utf-8'))

    # So the content-lenght is the sum of all string's lengths
    content_length = 0
    for s in response_body:
        content_length += len(s)

    response_headers.append(('Content-Length', str(content_length)))

    status = '200 OK'

    start_response(status, response_headers)

    return response_body
Exemple #7
0
class CoreTestCase(unittest.TestCase):
    def setUp(self):
        unittest.TestCase.setUp(self)
        cfgfile = open("/etc/skarphed/skarphed.conf", "r").read().split("\n")
        cfg = {}
        for line in cfgfile:
            if line.startswith("#") or line.find("=") == -1:
                continue
            key, value = line.split("=")
            cfg[key] = value

        del (cfgfile)

        #HERE BE DRAGONS:
        #Currently only supports one testinstance with the instanceID 0
        p = cfg["SCV_WEBPATH"] + "0"
        sys.path.append(p)

        from instanceconf import SCV_INSTANCE_SCOPE_ID
        cfg["SCV_INSTANCE_SCOPE_ID"] = SCV_INSTANCE_SCOPE_ID

        sys.path.append(cfg["SCV_LIBPATH"])

        from scv import Core

        self._core = Core(cfg)

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    def setSessionUser(self, permissions=[]):
        """
        Set a sessionuser and give him a list of permissions
        """
        if type(permissions) != list:
            permissions = [permissions]

        user_manager = self._core.get_user_manager()
        session_user = user_manager.create_user("session_user", "password")
        session_manager = self._core.get_session_manager()
        session = session_manager.create_session(session_user)
        session_manager.set_current_session(session)
        for permission in permissions:
            session_user.grant_permission(permission, ignore_check=True)
        self._session_user = session_user
        self._session = session

    def unsetSessionUser(self):
        session_manager = self._core.get_session_manager()
        session_manager.set_current_session(None)
        self._session.delete()
        self._session_user.delete()

    def assertFail(self, msg=""):
        """
        Execute assertFail() to let a test fail instantly
        """
        self.assertTrue(False, msg)

    def setUpTestModule(self):
        """
        Set up a module in the database for testing purposes.
        Returns the generated module.
        """
        db = self._core.get_db()
        nr = db.get_seq_next("MOD_GEN")
        stmnt = "INSERT INTO MODULES (MOD_ID, MOD_NAME, MOD_DISPLAYNAME, MOD_VERSIONMAJOR, MOD_VERSIONMINOR, MOD_VERSIONREV, MOD_JSMANDATORY) \
                      VALUES (?,?,?,?,?,?,?) ;"

        db.query(self._core,
                 stmnt, (nr, "testprogrammer_testmodule", "TestModule", 10, 11,
                         1337, common.enums.JSMandatory.NO),
                 commit=True)
        module_manager = self._core.get_module_manager()
        return module_manager.get_module(nr)

    def tearDownTestModule(self, module=None):
        """
        Tear down a testmodule and erase it from the database.
        Removes every module if called without parameters.
        if parameter module is given, removes only that module
        """
        db = self._core.get_db()
        if module is None:
            db.query(self._core, "DELETE FROM MODULES;", commit=True)
        else:
            stmnt = "DELETE FROM MODULES WHERE MOD_ID = ? ;"
            db.query(self._core, stmnt, (module.get_id(), ), commit=True)
        return

    def setUpTestTemplate(self):
        """
        Sets up a test template
        """
        testpermissions = [
            "skarphed.sites.create", "skarphed.sites.delete",
            "skarphed.sites.modify"
        ]
        self.setSessionUser(testpermissions)

        templatefile = open("testdata/default_template.tgz", "r")
        templatedata = templatefile.read()
        templatefile.close()

        template_manager = self._core.get_template_manager()
        template_manager.install_from_data(templatedata)

        self.unsetSessionUser()

    def tearDownTestTemplate(self):
        """
        Removes the test template
        """
        testpermissions = [
            "skarphed.sites.create", "skarphed.sites.delete",
            "skarphed.sites.modify"
        ]
        self.setSessionUser(testpermissions)

        template_manager = self._core.get_template_manager()
        template = template_manager.get_current_template()
        template.uninstall()
        self.unsetSessionUser()
Exemple #8
0
    if line.startswith("#") or line.find("=") == -1:
        continue
    key, value = line.split("=")
    cfg[key] = value

sys.path.append(os.path.dirname(__file__))

from instanceconf import SCV_INSTANCE_SCOPE_ID
cfg["SCV_INSTANCE_SCOPE_ID"] = SCV_INSTANCE_SCOPE_ID

sys.path.append(cfg["SCV_LIBPATH"])

from scv import Core
from scv import OperationDaemon

core = Core(cfg)
configuration = core.get_configuration()
pidfile = configuration.get_entry("core.webpath") + "/opd.pid"
opd = OperationDaemon(core, pidfile)

# This script accepts a dummy argument (sys.argv[2])
# This argument is supposed to be the instance id, so
# one can distinguish the daemon-processes from each
# other in e.g. htop

success = False
if len(sys.argv) == 2 or len(sys.argv) == 3:
    if sys.argv[1] == 'start':
        opd.start()
        success = True
    elif sys.argv[1] == 'stop':