def test_get_user(self):
     with self.__app.test_request_context('/test',
                                          headers={'X-User-Name': 'Test'}):
         self.assertEqual(UserAuthentication.get_user(), 'Test')
     with self.__app.test_request_context('/test', headers={}):
         self.assertEqual(UserAuthentication.get_user(), 'default-owner')
     with self.__app.test_request_context('/test',
                                          headers={
                                              'Authorization':
                                              'bearer my_incorrect_token'
                                          }):
         self.assertEqual(UserAuthentication.get_user(), 'default-owner')
     with patch(
             "hbp_nrp_backend.simulation_control.__BackendSimulationLifecycle.UserAuthentication.client"
     ) as client:
         client.get_user = MagicMock(return_value={'id': 'myid'})
         with self.__app.test_request_context(
                 '/test', headers={'Authorization': 'bearer my_token'}):
             self.assertEqual(UserAuthentication.get_user(), 'myid')
Exemple #2
0
    def post(self):
        """
        Creates a new simulation which is neither 'initialized' nor 'started'.
        :< json int brainProcesses: Number of brain processes to use (overrides ExD Configuration)
        :< json string experimentID: The experiment ID of the experiment
        :> json string gzserverHost: The host where gzserver will be run: local for using the same
                                     machine of the backend, lugano to use a dedicated instance on
                                     the Lugano viz cluster
        :< json string reservation: the name of the cluster reservation subsequently used to
                                    allocate a job
        :< json string state: The initial state of the simulation
        :< json boolean private: Defines whether the simulation is based on a private experiment
        :< json string playbackPath: Path to simulation recording to play (optional)
        :< json string ctx-id: The context id of the collab if we are running a collab based
                               simulation

        :> json string owner: The simulation owner (Unified Portal user name or 'hbp-default')
        :> json integer simulationID: The id of the simulation (needed for further REST calls)
        :> json string creationDate: Date of creation of this simulation
        :> json string creationUniqueID: The simulation unique creation ID that is used by the
                                         Frontend to identify this simulation

        :status 400: Experiment configuration is not valid
        :status 401: gzserverHost is not valid
        :status 402: Another simulation is already running on the server
        :status 201: Simulation created successfully
        """
        # Use context manager to lock access to simulations while a new simulation is created
        with SimulationService.comm_lock:
            body = request.get_json(force=True)
            sim_id = len(simulations)
            if 'experimentID' not in body:
                raise NRPServicesClientErrorException(
                    'Experiment ID not given.')

            if ('gzserverHost' in body) and (body.get('gzserverHost')
                                             not in ['local', 'lugano']):
                raise NRPServicesClientErrorException(
                    'Invalid gazebo server host.', error_code=401)

            if True in [
                    s.state not in ['stopped', 'failed'] for s in simulations
            ]:
                raise NRPServicesClientErrorException(
                    'Another simulation is already running on the server.',
                    error_code=409)

            if 'brainProcesses' in body and \
                    (not isinstance(body.get('brainProcesses'), int)
                     or body.get('brainProcesses') < 1):
                raise NRPServicesClientErrorException(
                    'Invalid number of brain processes.')

            sim_gzserver_host = body.get('gzserverHost', 'local')
            sim_reservation = body.get('reservation', None)
            sim_experiment_id = body.get('experimentID', None)
            sim_state = body.get('state', 'created')
            playback_path = body.get('playbackPath', None)
            sim_owner = UserAuthentication.get_user()
            sim_brain_processes = body.get('brainProcesses', 1)
            private = body.get('private', False)
            ctx_id = body.get('ctxId', None)
            token = UserAuthentication.get_header_token()

            sim = Simulation(sim_id,
                             sim_experiment_id,
                             sim_owner,
                             sim_gzserver_host,
                             sim_reservation,
                             sim_brain_processes,
                             sim_state,
                             playback_path=playback_path,
                             private=private,
                             ctx_id=ctx_id,
                             token=token)

            # TODO: remove me. I probably am not used anywhere
            sim.creationUniqueID = body.get('creationUniqueID',
                                            str(time.time() + random.random()))

            simulations.append(sim)

        sim.state = "initialized"

        return marshal(simulations[sim_id], Simulation.resource_fields), 201, {
            'location': api.url_for(SimulationControl, sim_id=sim_id),
            'gzserverHost': sim_gzserver_host
        }