Пример #1
0
 def setUp(self):
     self._config = get_config_for_testing(TMP_DIR)
     self.mongo_server = MongoMgr(config=self._config)
     self.db_frontend_editing = FrontendEditingDbInterface(
         config=self._config)
     self.db_frontend_interface = FrontEndDbInterface(config=self._config)
     self.db_backend_interface = BackEndDbInterface(config=self._config)
Пример #2
0
def main(command_line_options=sys.argv):
    _, config = program_setup(PROGRAM_NAME,
                              PROGRAM_DESCRIPTION,
                              command_line_options=command_line_options)

    logging.info('Trying to start Mongo Server and initializing users...')
    mongo_manger = MongoMgr(config=config, auth=False)

    db_service_frontend_editing = FrontendEditingDbInterface(config)
    convert_comments_to_new_format(db_service_frontend_editing)
    convert_release_dates_to_date_object_format(db_service_frontend_editing)
    add_parent_firmware_list_to_file_object(db_service_frontend_editing)

    mongo_manger.shutdown()

    return 0
Пример #3
0
def convert_comments_to_new_format(db_service):
    for collection in [db_service.firmwares, db_service.file_objects]:
        comment_query = collection.find({"comments": {"$type": "object"}}, {"_id": 1, "comments": 1})
        for entry in comment_query:
            firmware_id = entry["_id"]
            comment_field = entry["comments"]
            if type(comment_field) == dict:
                logging.debug("converting comments of {}".format(firmware_id))
                try:
                    updated_comment_field = [
                        {"time": time, "author": comment_field[time][0], "comment": comment_field[time][1]}
                        for time in comment_field
                    ]
                    db_service.update_object_field(firmware_id, "comments", updated_comment_field)
                except Exception as e:
                    logging.error("could not convert comment entry: {} {}".format(sys.exc_info()[0].__name__, e))


if __name__ == '__main__':
    args = _setup_argparser()
    config = _load_config(args)
    _setup_logging()

    logging.info("Trying to start Mongo Server and initializing users...")
    mongo_manger = MongoMgr(config=config, auth=False)
    db_service_frontend_editing = FrontendEditingDbInterface(config)

    convert_comments_to_new_format(db_service_frontend_editing)
    convert_release_dates_to_date_object_format(db_service_frontend_editing)
    add_parent_firmware_list_to_file_object(db_service_frontend_editing)
Пример #4
0
class TestStorageDbInterfaceFrontendEditing(unittest.TestCase):
    def setUp(self):
        self._config = get_config_for_testing(TMP_DIR)
        self.mongo_server = MongoMgr(config=self._config)
        self.db_frontend_editing = FrontendEditingDbInterface(
            config=self._config)
        self.db_frontend_interface = FrontEndDbInterface(config=self._config)
        self.db_backend_interface = BackEndDbInterface(config=self._config)

    def tearDown(self):
        self.db_frontend_editing.shutdown()
        self.db_frontend_interface.shutdown()
        self.db_backend_interface.client.drop_database(
            self._config.get('data_storage', 'main_database'))
        self.db_backend_interface.shutdown()
        self.mongo_server.shutdown()
        TMP_DIR.cleanup()
        gc.collect()

    def test_add_comment(self):
        test_fw = create_test_firmware()
        self.db_backend_interface.add_object(test_fw)
        comment, author, uid, time = 'this is a test comment!', 'author', test_fw.get_uid(
        ), 1234567890
        self.db_frontend_editing.add_comment_to_object(uid, comment, author,
                                                       time)
        test_fw = self.db_backend_interface.get_object(uid)
        self.assertEqual(test_fw.comments[0], {
            'time': str(time),
            'author': author,
            'comment': comment
        })

    def test_get_latest_comments(self):
        comments = [{
            'time': '1234567890',
            'author': 'author1',
            'comment': 'test comment'
        }, {
            'time': '1234567899',
            'author': 'author2',
            'comment': 'test comment2'
        }]
        test_fw = self._add_test_fw_with_comments_to_db()
        latest_comments = self.db_frontend_interface.get_latest_comments()
        comments.sort(key=lambda x: x['time'], reverse=True)
        for i in range(len(comments)):
            time, author, comment, uid = comments[i]['time'], comments[i][
                'author'], comments[i]['comment'], test_fw.get_uid()
            self.assertEqual(latest_comments[i]['time'], time)
            self.assertEqual(latest_comments[i]['author'], author)
            self.assertEqual(latest_comments[i]['comment'], comment)
            self.assertEqual(latest_comments[i]['uid'], uid)

    def test_remove_element_from_array_in_field(self):
        test_fw = self._add_test_fw_with_comments_to_db()
        retrieved_fw = self.db_backend_interface.get_object(test_fw.get_uid())
        self.assertEqual(len(retrieved_fw.comments), 2,
                         'comments were not saved correctly')

        self.db_frontend_editing.remove_element_from_array_in_field(
            test_fw.get_uid(), 'comments', {'time': '1234567899'})
        retrieved_fw = self.db_backend_interface.get_object(test_fw.get_uid())
        self.assertEqual(len(retrieved_fw.comments), 1,
                         'comment was not deleted')

    def test_delete_comment(self):
        test_fw = self._add_test_fw_with_comments_to_db()
        retrieved_fw = self.db_backend_interface.get_object(test_fw.get_uid())
        self.assertEqual(len(retrieved_fw.comments), 2,
                         'comments were not saved correctly')

        self.db_frontend_editing.delete_comment(test_fw.get_uid(),
                                                '1234567899')
        retrieved_fw = self.db_backend_interface.get_object(test_fw.get_uid())
        self.assertEqual(len(retrieved_fw.comments), 1,
                         'comment was not deleted')

    def _add_test_fw_with_comments_to_db(self):
        test_fw = create_test_firmware()
        comments = [{
            'time': '1234567890',
            'author': 'author1',
            'comment': 'test comment'
        }, {
            'time': '1234567899',
            'author': 'author2',
            'comment': 'test comment2'
        }]
        test_fw.comments.extend(comments)
        self.db_backend_interface.add_object(test_fw)
        return test_fw
 def setUp(self):
     self.db_frontend_editing = FrontendEditingDbInterface(
         config=self._config)
     self.db_frontend_interface = FrontEndDbInterface(config=self._config)
     self.db_backend_interface = BackEndDbInterface(config=self._config)
class TestStorageDbInterfaceFrontendEditing(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls._config = get_config_for_testing(TMP_DIR)
        cls.mongo_server = MongoMgr(config=cls._config)

    def setUp(self):
        self.db_frontend_editing = FrontendEditingDbInterface(
            config=self._config)
        self.db_frontend_interface = FrontEndDbInterface(config=self._config)
        self.db_backend_interface = BackEndDbInterface(config=self._config)

    def tearDown(self):
        self.db_frontend_editing.shutdown()
        self.db_frontend_interface.shutdown()
        self.db_backend_interface.client.drop_database(
            self._config.get('data_storage', 'main_database'))
        self.db_backend_interface.shutdown()
        gc.collect()

    @classmethod
    def tearDownClass(cls):
        cls.mongo_server.shutdown()
        TMP_DIR.cleanup()

    def test_add_comment(self):
        test_fw = create_test_firmware()
        self.db_backend_interface.add_object(test_fw)
        comment, author, uid, time = 'this is a test comment!', 'author', test_fw.uid, 1234567890
        self.db_frontend_editing.add_comment_to_object(uid, comment, author,
                                                       time)
        test_fw = self.db_backend_interface.get_object(uid)
        self.assertEqual(test_fw.comments[0], {
            'time': str(time),
            'author': author,
            'comment': comment
        })

    def test_get_latest_comments(self):
        comments = [{
            'time': '1234567890',
            'author': 'author1',
            'comment': 'test comment'
        }, {
            'time': '1234567899',
            'author': 'author2',
            'comment': 'test comment2'
        }]
        test_fw = self._add_test_fw_with_comments_to_db()
        latest_comments = self.db_frontend_interface.get_latest_comments()
        comments.sort(key=lambda x: x['time'], reverse=True)
        for i, comment in enumerate(comments):
            assert latest_comments[i]['time'] == comment['time']
            assert latest_comments[i]['author'] == comment['author']
            assert latest_comments[i]['comment'] == comment['comment']
            assert latest_comments[i]['uid'] == test_fw.uid

    def test_remove_element_from_array_in_field(self):
        test_fw = self._add_test_fw_with_comments_to_db()
        retrieved_fw = self.db_backend_interface.get_object(test_fw.uid)
        self.assertEqual(len(retrieved_fw.comments), 2,
                         'comments were not saved correctly')

        self.db_frontend_editing.remove_element_from_array_in_field(
            test_fw.uid, 'comments', {'time': '1234567899'})
        retrieved_fw = self.db_backend_interface.get_object(test_fw.uid)
        self.assertEqual(len(retrieved_fw.comments), 1,
                         'comment was not deleted')

    def test_delete_comment(self):
        test_fw = self._add_test_fw_with_comments_to_db()
        retrieved_fw = self.db_backend_interface.get_object(test_fw.uid)
        self.assertEqual(len(retrieved_fw.comments), 2,
                         'comments were not saved correctly')

        self.db_frontend_editing.delete_comment(test_fw.uid, '1234567899')
        retrieved_fw = self.db_backend_interface.get_object(test_fw.uid)
        self.assertEqual(len(retrieved_fw.comments), 1,
                         'comment was not deleted')

    def _add_test_fw_with_comments_to_db(self):
        test_fw = create_test_firmware()
        comments = [{
            'time': '1234567890',
            'author': 'author1',
            'comment': 'test comment'
        }, {
            'time': '1234567899',
            'author': 'author2',
            'comment': 'test comment2'
        }]
        test_fw.comments.extend(comments)
        self.db_backend_interface.add_object(test_fw)
        return test_fw

    def test_update_object_field(self):
        test_fw = create_test_firmware(vendor='foo')
        self.db_backend_interface.add_object(test_fw)

        result = self.db_frontend_editing.get_object(test_fw.uid)
        assert result.vendor == 'foo'

        self.db_frontend_editing.update_object_field(test_fw.uid, 'vendor',
                                                     'bar')
        result = self.db_frontend_editing.get_object(test_fw.uid)
        assert result.vendor == 'bar'

    def test_add_to_search_query_cache(self):
        query = '{"device_class": "Router"}'
        uid = create_uid(query)
        assert self.db_frontend_editing.add_to_search_query_cache(query) == uid
        assert self.db_frontend_editing.search_query_cache.find_one(
            {'_id': uid})['search_query'] == query
        # check what happens if search is added again
        assert self.db_frontend_editing.add_to_search_query_cache(query) == uid
        assert self.db_frontend_editing.search_query_cache.count_documents(
            {'_id': uid}) == 1