示例#1
0
def init_cherrypy(arguments=None):
    #### Mount static folders from modules marked for introspection
    arguments = arguments or []
    CONFIGUER = TvbProfile.current.web.CHERRYPY_CONFIGURATION
    for module in arguments:
        module_inst = importlib.import_module(str(module))
        module_path = os.path.dirname(os.path.abspath(module_inst.__file__))
        CONFIGUER["/static_" + str(module)] = {'tools.staticdir.on': True,
                                               'tools.staticdir.dir': '.',
                                               'tools.staticdir.root': module_path}

    #### Mount controllers, and specify the root URL for them.
    cherrypy.tree.mount(BaseController(), "/", config=CONFIGUER)
    cherrypy.tree.mount(UserController(), "/user/", config=CONFIGUER)
    cherrypy.tree.mount(ProjectController(), "/project/", config=CONFIGUER)
    cherrypy.tree.mount(FigureController(), "/project/figure/", config=CONFIGUER)
    cherrypy.tree.mount(FlowController(), "/flow/", config=CONFIGUER)
    cherrypy.tree.mount(SettingsController(), "/settings/", config=CONFIGUER)
    cherrypy.tree.mount(HelpController(), "/help/", config=CONFIGUER)
    cherrypy.tree.mount(SimulatorController(), "/burst/", config=CONFIGUER)
    cherrypy.tree.mount(ParameterExplorationController(), "/burst/explore/", config=CONFIGUER)
    cherrypy.tree.mount(DynamicModelController(), "/burst/dynamic/", config=CONFIGUER)
    cherrypy.tree.mount(SpatioTemporalController(), "/spatial/", config=CONFIGUER)
    cherrypy.tree.mount(RegionsModelParametersController(), "/burst/modelparameters/regions/", config=CONFIGUER)
    cherrypy.tree.mount(SurfaceModelParametersController(), "/spatial/modelparameters/surface/", config=CONFIGUER)
    cherrypy.tree.mount(RegionStimulusController(), "/spatial/stimulus/region/", config=CONFIGUER)
    cherrypy.tree.mount(SurfaceStimulusController(), "/spatial/stimulus/surface/", config=CONFIGUER)
    cherrypy.tree.mount(LocalConnectivityController(), "/spatial/localconnectivity/", config=CONFIGUER)
    cherrypy.tree.mount(NoiseConfigurationController(), "/burst/noise/", config=CONFIGUER)
    cherrypy.tree.mount(HPCController(), "/hpc/", config=CONFIGUER)

    cherrypy.config.update(CONFIGUER)

    # ----------------- Register additional request handlers -----------------
    # This tool checks for MAX upload size
    cherrypy.tools.upload = Tool('on_start_resource', RequestHandler.check_upload_size)
    # This tools clean up files on disk (mainly after export)
    cherrypy.tools.cleanup = Tool('on_end_request', RequestHandler.clean_files_on_disk)
    # ----------------- End register additional request handlers ----------------

    # Register housekeeping job
    if TvbProfile.current.hpc.IS_HPC_RUN and TvbProfile.current.hpc.CAN_RUN_HPC:
        cherrypy.engine.housekeeper = cherrypy.process.plugins.BackgroundTask(
            TvbProfile.current.hpc.BACKGROUND_JOB_INTERVAL, HPCOperationService.check_operations_job)
        cherrypy.engine.housekeeper.start()

    # HTTP Server is fired now ######
    cherrypy.engine.start()
    def setUp(self):

        self.init(with_data=False)
        self.settings_c = SettingsController()
        self.assertTrue(TvbProfile.is_first_run())
class SettingsControllerTest(BaseTransactionalControllerTest):
    """
    Unit tests for SettingsController class
    """

    was_reset = False
    accepted_db_url = ('sqlite:///TestFolder' + os.path.sep + 'tvb-database.db'
                       if TvbProfile.current.db.SELECTED_DB == 'sqlite'
                       else TvbProfile.current.db.DB_URL)

    VALID_SETTINGS = {'TVB_STORAGE': "TestFolder",
                      'USR_DISK_SPACE': 1,
                      'MAXIMUM_NR_OF_THREADS': 6,
                      'MAXIMUM_NR_OF_VERTICES_ON_SURFACE': 142,
                      'MAXIMUM_NR_OF_OPS_IN_RANGE': 16,
                      'MATLAB_EXECUTABLE': '',

                      'DEPLOY_CLUSTER': 'True',
                      'SELECTED_DB': TvbProfile.current.db.SELECTED_DB,  # Not changeable,due to test profile overwrites
                      'URL_VALUE': accepted_db_url,

                      'URL_WEB': "http://*****:*****@test.test'}


    def setUp(self):

        self.init(with_data=False)
        self.settings_c = SettingsController()
        self.assertTrue(TvbProfile.is_first_run())


    def tearDown(self):
        """ Cleans the testing environment """
        self.cleanup()

        if os.path.exists(self.VALID_SETTINGS['TVB_STORAGE']):
            shutil.rmtree(self.VALID_SETTINGS['TVB_STORAGE'])


    def test_with_invalid_admin_settings(self):

        self._assert_invalid_parameters({'ADMINISTRATOR_NAME': '',
                                         'ADMINISTRATOR_PASSWORD': '',
                                         'ADMINISTRATOR_EMAIL': ''})

        self._assert_invalid_parameters({'ADMINISTRATOR_EMAIL': "bla.com"})


    def test_with_invalid_web_settings(self):

        self._assert_invalid_parameters({'URL_WEB': '',
                                         'WEB_SERVER_PORT': 'a'})

        self._assert_invalid_parameters({'URL_WEB': 'off://bla',
                                         'WEB_SERVER_PORT': '70000'})


    def test_with_invalid_settings(self):

        self._assert_invalid_parameters({'TVB_STORAGE': '',
                                         'SELECTED_DB': '',
                                         'URL_VALUE': ''})

        self._assert_invalid_parameters({'USR_DISK_SPACE': '',
                                         'MAXIMUM_NR_OF_THREADS': '0',
                                         'MAXIMUM_NR_OF_VERTICES_ON_SURFACE': '-1',
                                         'MAXIMUM_NR_OF_OPS_IN_RANGE': '2'})

        self._assert_invalid_parameters({'USR_DISK_SPACE': 'a',
                                         'MAXIMUM_NR_OF_THREADS': '20',
                                         'MAXIMUM_NR_OF_VERTICES_ON_SURFACE': str(256 * 256 * 256 + 1),
                                         'MAXIMUM_NR_OF_OPS_IN_RANGE': '10000'})

        self._assert_invalid_parameters({'MAXIMUM_NR_OF_THREADS': 'c',
                                         'MAXIMUM_NR_OF_VERTICES_ON_SURFACE': 'b',
                                         'MAXIMUM_NR_OF_OPS_IN_RANGE': 'a'})


    def _assert_invalid_parameters(self, params_dictionary):
        """
        Simulate submit of a given params (key:value) and check that they are found in the error response
        """
        submit_data = copy.copy(self.VALID_SETTINGS)
        for key, value in params_dictionary.iteritems():
            submit_data[key] = value

        response = self.settings_c.settings(save_settings=True, **submit_data)

        self.assertTrue(common.KEY_ERRORS in response)
        for key in params_dictionary:
            self.assertTrue(key in response[common.KEY_ERRORS], "Not found in errors %s" % key)


    def test_with_valid_settings(self):

        submit_data = self.VALID_SETTINGS
        self.settings_c._restart_services = self._fake_restart_services

        self.assertRaises(cherrypy.HTTPRedirect, self.settings_c.settings, save_settings=True, **self.VALID_SETTINGS)

        # wait until 'restart' is done
        sleep(1)
        self.assertTrue(self.was_reset)
        self.assertEqual(16, len(TvbProfile.current.manager.stored_settings))

        self.assertEqual(submit_data['TVB_STORAGE'], TvbProfile.current.TVB_STORAGE)
        self.assertEqual(submit_data['USR_DISK_SPACE'] * 2 ** 10, TvbProfile.current.MAX_DISK_SPACE)
        self.assertEqual(submit_data['MAXIMUM_NR_OF_THREADS'], TvbProfile.current.MAX_THREADS_NUMBER)
        self.assertEqual(submit_data['MAXIMUM_NR_OF_OPS_IN_RANGE'], TvbProfile.current.MAX_RANGE_NUMBER)
        self.assertEqual(submit_data['MAXIMUM_NR_OF_VERTICES_ON_SURFACE'],
                         TvbProfile.current.MAX_SURFACE_VERTICES_NUMBER)

        self.assertEqual(submit_data['DEPLOY_CLUSTER'], str(TvbProfile.current.cluster.IS_DEPLOY))
        self.assertEqual(submit_data['SELECTED_DB'], TvbProfile.current.db.SELECTED_DB)
        self.assertEqual(submit_data['URL_VALUE'], TvbProfile.current.db.DB_URL)

        self.assertEqual(submit_data['URL_WEB'], TvbProfile.current.web.BASE_URL)
        self.assertEqual(submit_data['WEB_SERVER_PORT'], TvbProfile.current.web.SERVER_PORT)

        self.assertEqual(submit_data['ADMINISTRATOR_NAME'], TvbProfile.current.web.admin.ADMINISTRATOR_NAME)
        self.assertEqual(submit_data['ADMINISTRATOR_EMAIL'], TvbProfile.current.web.admin.ADMINISTRATOR_EMAIL)
        self.assertEqual(md5(submit_data['ADMINISTRATOR_PASSWORD']).hexdigest(),
                         TvbProfile.current.web.admin.ADMINISTRATOR_PASSWORD)


    def _fake_restart_services(self, should_reset):
        """
        This function will replace the SettingsController._restart_service method,
        to avoid problems in tests due to restart.
        """
        self.was_reset = should_reset
        TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME)


    def test_check_db_url(self):
        """
        Test that for a various DB URLs, the correct check response is returned.
        """
        submit_data = {stored.KEY_STORAGE: TvbProfile.current.TVB_STORAGE,
                       stored.KEY_DB_URL: TvbProfile.current.db.DB_URL}
        result = json.loads(self.settings_c.check_db_url(**submit_data))
        self.assertEqual(result['status'], 'ok')

        submit_data[stored.KEY_DB_URL] = "this URL should be invalid"
        result = json.loads(self.settings_c.check_db_url(**submit_data))
        self.assertEqual(result['status'], 'not ok')


    @unittest.skipIf(get_matlab_executable() is None, "Matlab or Octave not installed!")
    def test_check_matlab_path(self):
        """
        Test that for a various Matlab paths, the correct check response is returned.
        """
        submit_data = {stored.KEY_MATLAB_EXECUTABLE: get_matlab_executable()}
        result = json.loads(self.settings_c.validate_matlab_path(**submit_data))
        self.assertEqual(result['status'], 'ok')

        submit_data[stored.KEY_MATLAB_EXECUTABLE] = "/this/path/should/be/invalid"
        result = json.loads(self.settings_c.validate_matlab_path(**submit_data))
        self.assertEqual(result['status'], 'not ok')
    def transactional_setup_method(self):

        self.init(with_data=False)
        self.settings_c = SettingsController()
        assert TvbProfile.is_first_run()
示例#5
0
    def transactional_setup_method(self):

        self.init(with_data=False)
        self.settings_c = SettingsController()
        assert TvbProfile.is_first_run()
示例#6
0
class TestSettingsController(BaseTransactionalControllerTest):
    """
    Unit tests for SettingsController class
    """

    was_reset = False
    storage = str(Path.home()) + os.path.sep + "TestFolder"
    accepted_db_url = ('sqlite:///' + storage + os.path.sep +
                       'tvb-database.db' if TvbProfile.current.db.SELECTED_DB
                       == 'sqlite' else TvbProfile.current.db.DB_URL)

    VALID_SETTINGS = {
        'KEYCLOAK_CONFIGURATION':
        storage + os.path.sep + 'keycloak-config.json',
        'KEYCLOAK_WEB_CONFIGURATION':
        storage + os.path.sep + 'keycloak-web-config.json',
        'TVB_STORAGE': storage,
        'USR_DISK_SPACE': 1,
        'MAXIMUM_NR_OF_THREADS': 6,
        'MAXIMUM_NR_OF_VERTICES_ON_SURFACE': 142,
        'MAXIMUM_NR_OF_OPS_IN_RANGE': 16,
        'MATLAB_EXECUTABLE': '',
        'DEPLOY_CLUSTER': 'True',
        'CLUSTER_SCHEDULER': TvbProfile.current.cluster.SCHEDULER_OAR,
        'SELECTED_DB': TvbProfile.current.db.
        SELECTED_DB,  # Not changeable,due to test profile overwrites
        'URL_VALUE': accepted_db_url,
        'WEB_SERVER_PORT': 9999,
        'ADMINISTRATOR_NAME': 'test_admin',
        'ADMINISTRATOR_DISPLAY_NAME': 'test_Administrator',
        'ADMINISTRATOR_PASSWORD': "******",
        'ADMINISTRATOR_EMAIL': '*****@*****.**'
    }

    def transactional_setup_method(self):

        self.init(with_data=False)
        self.settings_c = SettingsController()
        assert TvbProfile.is_first_run()

    def transactional_teardown_method(self):
        """ Cleans the testing environment """
        self.cleanup()
        self.clean_database()

        if os.path.exists(self.VALID_SETTINGS['TVB_STORAGE']):
            shutil.rmtree(self.VALID_SETTINGS['TVB_STORAGE'])

    def test_with_invalid_admin_settings(self):

        self._assert_invalid_parameters({
            'ADMINISTRATOR_NAME': '',
            'ADMINISTRATOR_PASSWORD': '',
            'ADMINISTRATOR_EMAIL': ''
        })

        self._assert_invalid_parameters({'ADMINISTRATOR_EMAIL': "bla.com"})

    def test_with_invalid_web_settings(self):

        self._assert_invalid_parameters({'WEB_SERVER_PORT': 'a'})

        self._assert_invalid_parameters({'WEB_SERVER_PORT': '70000'})

    def test_with_invalid_settings(self):

        self._assert_invalid_parameters({
            'TVB_STORAGE': '',
            'SELECTED_DB': '',
            'URL_VALUE': ''
        })

        self._assert_invalid_parameters({
            'USR_DISK_SPACE': '',
            'MAXIMUM_NR_OF_THREADS': '0',
            'MAXIMUM_NR_OF_VERTICES_ON_SURFACE': '-1',
            'MAXIMUM_NR_OF_OPS_IN_RANGE': '2'
        })

        self._assert_invalid_parameters({
            'USR_DISK_SPACE':
            'a',
            'MAXIMUM_NR_OF_THREADS':
            '20',
            'MAXIMUM_NR_OF_VERTICES_ON_SURFACE':
            str(256 * 256 * 256 + 1),
            'MAXIMUM_NR_OF_OPS_IN_RANGE':
            '10000'
        })

        self._assert_invalid_parameters({
            'MAXIMUM_NR_OF_THREADS': 'c',
            'MAXIMUM_NR_OF_VERTICES_ON_SURFACE': 'b',
            'MAXIMUM_NR_OF_OPS_IN_RANGE': 'a'
        })

    def _assert_invalid_parameters(self, params_dictionary):
        """
        Simulate submit of a given params (key:value) and check that they are found in the error response
        """
        submit_data = copy.copy(self.VALID_SETTINGS)
        for key, value in params_dictionary.items():
            submit_data[key] = value

        response = self.settings_c.settings(save_settings=True, **submit_data)

        assert common.KEY_ERRORS in response
        for key in params_dictionary:
            assert key in response[
                common.KEY_ERRORS], "Not found in errors %s" % key

    def test_with_valid_settings(self):
        # Ensure we submit acceptable values, depending on the current profile (set-up after fixtures execution)
        submit_data = copy.copy(self.VALID_SETTINGS)
        accepted_db_url = ('sqlite:///' + self.storage + os.path.sep +
                           'tvb-database.db'
                           if TvbProfile.current.db.SELECTED_DB == 'sqlite'
                           else TvbProfile.current.db.DB_URL)
        submit_data['SELECTED_DB'] = TvbProfile.current.db.SELECTED_DB
        submit_data['URL_VALUE'] = accepted_db_url

        self.settings_c._restart_services = self._fake_restart_services

        with pytest.raises(cherrypy.HTTPRedirect):
            self.settings_c.settings(save_settings=True, **self.VALID_SETTINGS)

        # wait until 'restart' is done
        sleep(1)
        assert self.was_reset
        assert len(TvbProfile.current.manager.stored_settings) == 21

        assert submit_data['TVB_STORAGE'] == TvbProfile.current.TVB_STORAGE
        assert submit_data[
            'USR_DISK_SPACE'] * 2**10 == TvbProfile.current.MAX_DISK_SPACE
        assert submit_data[
            'MAXIMUM_NR_OF_THREADS'] == TvbProfile.current.MAX_THREADS_NUMBER
        assert submit_data[
            'MAXIMUM_NR_OF_OPS_IN_RANGE'] == TvbProfile.current.MAX_RANGE_NUMBER
        assert submit_data[
            'MAXIMUM_NR_OF_VERTICES_ON_SURFACE'] == TvbProfile.current.MAX_SURFACE_VERTICES_NUMBER

        assert submit_data['DEPLOY_CLUSTER'] == str(
            TvbProfile.current.cluster.IS_DEPLOY)
        assert submit_data['SELECTED_DB'] == TvbProfile.current.db.SELECTED_DB
        assert submit_data['URL_VALUE'] == TvbProfile.current.db.DB_URL

        assert submit_data[
            'WEB_SERVER_PORT'] == TvbProfile.current.web.SERVER_PORT

        assert submit_data[
            'ADMINISTRATOR_NAME'] == TvbProfile.current.web.admin.ADMINISTRATOR_NAME
        assert submit_data[
            'ADMINISTRATOR_EMAIL'] == TvbProfile.current.web.admin.ADMINISTRATOR_EMAIL
        assert hash_password(
            submit_data['ADMINISTRATOR_PASSWORD']
        ) == TvbProfile.current.web.admin.ADMINISTRATOR_PASSWORD

    def _fake_restart_services(self, should_reset):
        """
        This function will replace the SettingsController._restart_service method,
        to avoid problems in tests due to restart.
        """
        self.was_reset = should_reset
        TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME)

    def test_check_db_url(self):
        """
        Test that for a various DB URLs, the correct check response is returned.
        """
        submit_data = {
            stored.KEY_STORAGE: TvbProfile.current.TVB_STORAGE,
            stored.KEY_DB_URL: TvbProfile.current.db.DB_URL
        }
        result = json.loads(self.settings_c.check_db_url(**submit_data))
        assert result['status'] == 'ok'

        submit_data[stored.KEY_DB_URL] = "this URL should be invalid"
        result = json.loads(self.settings_c.check_db_url(**submit_data))
        assert result['status'] == 'not ok'

    @pytest.mark.skipif(get_matlab_executable() is None,
                        reason="Matlab or Octave not installed!")
    def test_check_matlab_path(self):
        """
        Test that for a various Matlab paths, the correct check response is returned.
        """
        submit_data = {stored.KEY_MATLAB_EXECUTABLE: get_matlab_executable()}
        result = json.loads(
            self.settings_c.validate_matlab_path(**submit_data))
        assert result['status'] == 'ok'

        submit_data[
            stored.KEY_MATLAB_EXECUTABLE] = "/this/path/should/be/invalid"
        result = json.loads(
            self.settings_c.validate_matlab_path(**submit_data))
        assert result['status'] == 'not ok'