def update_object(self, new_object=None, old_object=None): update_dictionary = { 'processed_analysis': self._update_processed_analysis(new_object, old_object), 'files_included': update_included_files(new_object, old_object), 'virtual_file_path': update_virtual_file_path(new_object, old_object), 'analysis_tags': update_analysis_tags(new_object, old_object), } if type(new_object) == Firmware: update_dictionary.update({ 'version': new_object.version, 'device_name': new_object.device_name, 'device_class': new_object.device_class, 'vendor': new_object.vendor, 'release_date': convert_str_to_time(new_object.release_date), 'tags': new_object.tags, }) collection = self.firmwares else: update_dictionary.update({ 'parent_firmware_uids': list(set.union(set(old_object['parent_firmware_uids']), new_object.parent_firmware_uids)) }) collection = self.file_objects collection.update_one({'_id': new_object.get_uid()}, {'$set': update_dictionary})
def _update_analysis(self, file_object: FileObject, analysis_system: str, result: dict): try: collection = self.firmwares if isinstance(file_object, Firmware) else self.file_objects entry_with_tags = collection.find_one({'_id': file_object.uid}, {'analysis_tags': 1}) collection.update_one( {'_id': file_object.uid}, {'$set': { 'processed_analysis.{}'.format(analysis_system): result, 'analysis_tags': update_analysis_tags(file_object, entry_with_tags) }} ) except Exception as exception: logging.error('Update of analysis failed badly ({})'.format(exception)) raise exception
def test_update_analysis_tags_overwrite(mutable_test_file, mongo_entry): mutable_test_file.analysis_tags = {'new_tag': 'hurray', 'existing_tag': 'overwrite'} analysis_tags = update_analysis_tags(mutable_test_file, mongo_entry) assert len(analysis_tags.keys()) == 2, 'unaccounted tag' assert analysis_tags['existing_tag'] == 'overwrite'
def test_update_analysis_tags_normal(mutable_test_file, mongo_entry): mutable_test_file.analysis_tags = {'new_tag': 'hurray'} analysis_tags = update_analysis_tags(mutable_test_file, mongo_entry) assert all(key in analysis_tags for key in ['existing_tag', 'new_tag']), 'not both tags found' assert len(analysis_tags.keys()) == 2, 'unaccounted tag'