def get_by_name(cls, name, session): try: return session.query(cls).filter(Probe.name == name).one() except NoResultFound as e: raise IrmaDatabaseResultNotFound(e) except MultipleResultsFound as e: raise IrmaDatabaseError(e)
def get_scan(cls, scan_id, user_id, session): try: return session.query(cls).filter(cls.scan_id == scan_id and cls.user_id == user_id).one() except NoResultFound as e: raise IrmaDatabaseResultNotFound(e) except MultipleResultsFound as e: raise IrmaDatabaseError(e)
def test_process_notfound_error(self): error = IrmaDatabaseResultNotFound("message_error") with self.assertRaises(api_errors.HTTPError) as context: try: raise error except Exception as e: api_errors.IrmaExceptionHandler.process_error(e) self.assertEqual(context.exception.title, "NoResultFound")
def test002_new_file_not_existing(self, m_scan): m_scan.get_scan.side_effect = IrmaDatabaseResultNotFound() old_nb_files = self.scan.nb_files new_nb_files = randint(150, 200) scan = module.new(self.frontend_scanid, self.user, new_nb_files, self.session) self.session.query().filter().update.assert_not_called() m_scan().save.assert_called() self.assertNotEqual(self.nb_files, new_nb_files) self.assertEqual(scan.nb_files, m_scan().nb_files)
def get_by_rmqvhost(session, rmqvhost=None): # FIXME: get rmq_vhost dynamically if rmqvhost is None: rmqvhost = config.brain_config['broker_frontend'].vhost try: return session.query(User).filter(User.rmqvhost == rmqvhost).one() except NoResultFound as e: raise IrmaDatabaseResultNotFound(e) except MultipleResultsFound as e: raise IrmaDatabaseError(e)
def test003_process_notfound_error(self, m_sys, m_abort): m_sys.exc_info.return_value = (MagicMock(), None, MagicMock()) error = IrmaDatabaseResultNotFound("message_error") exp_code = 404 exp_error = api_errors.ApiError("request_error", "Object not Found") api_errors.process_error(error) m_abort.assert_called_once() call_args = m_abort.call_args[0] self.assertEqual(call_args[0], exp_code) self.assertEqual(str(call_args[1]), str(exp_error))
def _download(sha256, db): """Retrieve a file based on its sha256""" log.debug("h_value %s", sha256) fobj = File.load_from_sha256(sha256, db) # check if file is still present if fobj.path is None: raise IrmaDatabaseResultNotFound("downloading a removed file") # Force download ctype = 'application/octet-stream; charset=UTF-8' # Suggest Filename to sha256 cdisposition = "attachment; filename={}".format(sha256) response.headers["Content-Type"] = ctype response.headers["Content-Disposition"] = cdisposition return open(fobj.path).read()
def load_by_scanid_fileid(cls, scanid, fileid, session): """Find the list of filewebs in a given scan with same file in the database :param scanid: the scan external id :param fileid: the file id :param session: the session to use :rtype: list of FileWeb :return: list of matching objects :raise: IrmaDatabaseResultNotFound """ try: return session.query(cls).filter(cls.id_scan == scanid, cls.id_file == fileid).all() except NoResultFound as e: raise IrmaDatabaseResultNotFound(e)
def load_from_ext_id(cls, external_id, session): """Find the object in the database :param external_id: the id to look for :param session: the session to use :rtype: cls :return: the object that corresponds to the external_id :raise: IrmaDatabaseResultNotFound, IrmaDatabaseError """ try: return session.query(cls).filter( cls.external_id == external_id).one() except NoResultFound as e: raise IrmaDatabaseResultNotFound(e) except MultipleResultsFound as e: raise IrmaDatabaseError(e)
def load_from_sha256(cls, sha256, session): """Find the object in the database, update data if file was previously deleted :param sha256: the sha256 to look for :param session: the session to use :rtype: cls :return: the object that corresponds to the sha256 :raise: IrmaDatabaseResultNotFound, IrmaDatabaseError, IrmaFileSystemError """ try: asked_file = session.query(cls).filter(cls.sha256 == sha256).one() except NoResultFound as e: raise IrmaDatabaseResultNotFound(e) except MultipleResultsFound as e: raise IrmaDatabaseError(e) # Check if file is still present if asked_file.path is not None and not os.path.exists(asked_file.path): asked_file.path = None return asked_file
def test_new_file_not_existing(self, m_scan): m_scan.get_scan.side_effect = IrmaDatabaseResultNotFound() module.new(self.frontend_scanid, self.user, self.session) self.session.add.assert_called()