Exemple #1
0
    def test_delete_form_data_command(self, mock_g):
        mock_g.user = security_manager.find_user("admin")
        app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
            "REFRESH_TIMEOUT_ON_RETRIEVAL": True
        }

        dataset = (db.session.query(SqlaTable).filter_by(
            table_name="dummy_sql_table").first())
        slice = db.session.query(Slice).filter_by(
            slice_name="slice_name").first()

        datasource = f"{dataset.id}__{DatasourceType.TABLE}"
        create_args = CommandParameters(
            datasource_id=dataset.id,
            datasource_type=DatasourceType.TABLE,
            chart_id=slice.id,
            tab_id=1,
            form_data=json.dumps({"datasource": datasource}),
        )
        key = CreateFormDataCommand(create_args).run()

        delete_args = CommandParameters(key=key, )

        delete_command = DeleteFormDataCommand(delete_args)
        response = delete_command.run()

        assert response == True
Exemple #2
0
    def test_update_form_data_command(self, mock_g):
        mock_g.user = security_manager.find_user("admin")
        app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
            "REFRESH_TIMEOUT_ON_RETRIEVAL": True
        }

        dataset = (db.session.query(SqlaTable).filter_by(
            table_name="dummy_sql_table").first())
        slice = db.session.query(Slice).filter_by(
            slice_name="slice_name").first()

        query = db.session.query(Query).filter_by(
            sql="select 1 as foo;").first()

        datasource = f"{dataset.id}__{DatasourceType.TABLE}"
        create_args = CommandParameters(
            actor=mock_g.user,
            datasource_id=dataset.id,
            datasource_type=DatasourceType.TABLE,
            chart_id=slice.id,
            tab_id=1,
            form_data=json.dumps({"datasource": datasource}),
        )
        key = CreateFormDataCommand(create_args).run()

        query_datasource = f"{dataset.id}__{DatasourceType.TABLE}"
        update_args = CommandParameters(
            actor=mock_g.user,
            datasource_id=query.id,
            datasource_type=DatasourceType.QUERY,
            chart_id=slice.id,
            tab_id=1,
            form_data=json.dumps({"datasource": query_datasource}),
            key=key,
        )

        update_command = UpdateFormDataCommand(update_args)
        new_key = update_command.run()

        # it should return a key
        assert isinstance(new_key, str)
        # the updated key returned should be different from the old one
        assert new_key != key

        key_args = CommandParameters(actor=mock_g.user, key=key)
        get_command = GetFormDataCommand(key_args)

        cache_data = json.loads(get_command.run())

        assert cache_data.get("datasource") == query_datasource
Exemple #3
0
 def post(self) -> Response:
     """Stores a new form_data.
     ---
     post:
       description: >-
         Stores a new form_data.
       parameters:
       - in: query
         schema:
           type: integer
         name: tab_id
       requestBody:
         required: true
         content:
           application/json:
             schema:
               $ref: '#/components/schemas/FormDataPostSchema'
       responses:
         201:
           description: The form_data was stored successfully.
           content:
             application/json:
               schema:
                 type: object
                 properties:
                   key:
                     type: string
                     description: The key to retrieve the form_data.
         400:
           $ref: '#/components/responses/400'
         401:
           $ref: '#/components/responses/401'
         422:
           $ref: '#/components/responses/422'
         500:
           $ref: '#/components/responses/500'
     """
     try:
         item = self.add_model_schema.load(request.json)
         tab_id = request.args.get("tab_id")
         args = CommandParameters(
             actor=g.user,
             dataset_id=item["dataset_id"],
             chart_id=item.get("chart_id"),
             tab_id=tab_id,
             form_data=item["form_data"],
         )
         key = CreateFormDataCommand(args).run()
         return self.response(201, key=key)
     except ValidationError as ex:
         return self.response(400, message=ex.messages)
     except (
             ChartAccessDeniedError,
             DatasetAccessDeniedError,
             TemporaryCacheAccessDeniedError,
     ) as ex:
         return self.response(403, message=str(ex))
     except (ChartNotFoundError, DatasetNotFoundError) as ex:
         return self.response(404, message=str(ex))
Exemple #4
0
    def test_delete_form_data_command_key_expired(self, mock_g):
        mock_g.user = security_manager.find_user("admin")
        app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
            "REFRESH_TIMEOUT_ON_RETRIEVAL": True
        }

        delete_args = CommandParameters(key="some_expired_key", )

        delete_command = DeleteFormDataCommand(delete_args)
        response = delete_command.run()

        assert response == False
Exemple #5
0
 def delete(self, key: str) -> Response:
     """Deletes a form_data.
     ---
     delete:
       description: >-
         Deletes a form_data.
       parameters:
       - in: path
         schema:
           type: string
         name: key
         description: The form_data key.
       responses:
         200:
           description: Deleted the stored form_data.
           content:
             application/json:
               schema:
                 type: object
                 properties:
                   message:
                     type: string
                     description: The result of the operation
         400:
           $ref: '#/components/responses/400'
         401:
           $ref: '#/components/responses/401'
         404:
           $ref: '#/components/responses/404'
         422:
           $ref: '#/components/responses/422'
         500:
           $ref: '#/components/responses/500'
     """
     try:
         args = CommandParameters(actor=g.user, key=key)
         result = DeleteFormDataCommand(args).run()
         if not result:
             return self.response_404()
         return self.response(200, message="Deleted successfully")
     except (
             ChartAccessDeniedError,
             DatasetAccessDeniedError,
             TemporaryCacheAccessDeniedError,
     ) as ex:
         return self.response(403, message=str(ex))
     except (ChartNotFoundError, DatasetNotFoundError) as ex:
         return self.response(404, message=str(ex))
Exemple #6
0
 def get(self, key: str) -> Response:
     """Retrives a form_data.
     ---
     get:
       description: >-
         Retrives a form_data.
       parameters:
       - in: path
         schema:
           type: string
         name: key
       responses:
         200:
           description: Returns the stored form_data.
           content:
             application/json:
               schema:
                 type: object
                 properties:
                   form_data:
                     type: string
                     description: The stored form_data
         400:
           $ref: '#/components/responses/400'
         401:
           $ref: '#/components/responses/401'
         404:
           $ref: '#/components/responses/404'
         422:
           $ref: '#/components/responses/422'
         500:
           $ref: '#/components/responses/500'
     """
     try:
         args = CommandParameters(actor=g.user, key=key)
         form_data = GetFormDataCommand(args).run()
         if not form_data:
             return self.response_404()
         return self.response(200, form_data=form_data)
     except (
             ChartAccessDeniedError,
             DatasetAccessDeniedError,
             TemporaryCacheAccessDeniedError,
     ) as ex:
         return self.response(403, message=str(ex))
     except (ChartNotFoundError, DatasetNotFoundError) as ex:
         return self.response(404, message=str(ex))
Exemple #7
0
    def test_create_form_data_command(self, mock_g):
        mock_g.user = security_manager.find_user("admin")

        dataset = (db.session.query(SqlaTable).filter_by(
            table_name="dummy_sql_table").first())
        slice = db.session.query(Slice).filter_by(
            slice_name="slice_name").first()

        datasource = f"{dataset.id}__{DatasourceType.TABLE}"
        args = CommandParameters(
            datasource_id=dataset.id,
            datasource_type=DatasourceType.TABLE,
            chart_id=slice.id,
            tab_id=1,
            form_data=json.dumps({"datasource": datasource}),
        )
        command = CreateFormDataCommand(args)

        assert isinstance(command.run(), str)
Exemple #8
0
    def test_create_form_data_command_invalid_type(self, mock_g):
        mock_g.user = security_manager.find_user("admin")
        app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
            "REFRESH_TIMEOUT_ON_RETRIEVAL": True
        }

        dataset = (db.session.query(SqlaTable).filter_by(
            table_name="dummy_sql_table").first())
        slice = db.session.query(Slice).filter_by(
            slice_name="slice_name").first()

        datasource = f"{dataset.id}__{DatasourceType.TABLE}"
        create_args = CommandParameters(
            datasource_id=dataset.id,
            datasource_type="InvalidType",
            chart_id=slice.id,
            tab_id=1,
            form_data=json.dumps({"datasource": datasource}),
        )
        with pytest.raises(DatasourceTypeInvalidError) as exc:
            CreateFormDataCommand(create_args).run()

        assert "Datasource type is invalid" in str(exc.value)
Exemple #9
0
    def test_create_form_data_command_type_as_string(self, mock_g):
        mock_g.user = security_manager.find_user("admin")
        app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
            "REFRESH_TIMEOUT_ON_RETRIEVAL": True
        }

        dataset = (db.session.query(SqlaTable).filter_by(
            table_name="dummy_sql_table").first())
        slice = db.session.query(Slice).filter_by(
            slice_name="slice_name").first()

        datasource = f"{dataset.id}__{DatasourceType.TABLE}"
        create_args = CommandParameters(
            actor=mock_g.user,
            datasource_id=dataset.id,
            datasource_type="table",
            chart_id=slice.id,
            tab_id=1,
            form_data=json.dumps({"datasource": datasource}),
        )
        command = CreateFormDataCommand(create_args)

        assert isinstance(command.run(), str)
Exemple #10
0
 def put(self, key: str) -> Response:
     """Updates an existing form_data.
     ---
     put:
       description: >-
         Updates an existing form_data.
       parameters:
       - in: path
         schema:
           type: string
         name: key
       requestBody:
         required: true
         content:
           application/json:
             schema:
               $ref: '#/components/schemas/FormDataPutSchema'
       responses:
         200:
           description: The form_data was stored successfully.
           content:
             application/json:
               schema:
                 type: object
                 properties:
                   message:
                     type: string
                     description: The result of the operation
         400:
           $ref: '#/components/responses/400'
         401:
           $ref: '#/components/responses/401'
         404:
           $ref: '#/components/responses/404'
         422:
           $ref: '#/components/responses/422'
         500:
           $ref: '#/components/responses/500'
     """
     try:
         item = self.edit_model_schema.load(request.json)
         args = CommandParameters(
             actor=g.user,
             dataset_id=item["dataset_id"],
             chart_id=item.get("chart_id"),
             key=key,
             form_data=item["form_data"],
         )
         result = UpdateFormDataCommand(args).run()
         if not result:
             return self.response_404()
         return self.response(200, message="Value updated successfully.")
     except ValidationError as ex:
         return self.response(400, message=ex.messages)
     except (
         ChartAccessDeniedError,
         DatasetAccessDeniedError,
         KeyValueAccessDeniedError,
     ) as ex:
         return self.response(403, message=str(ex))
     except (ChartNotFoundError, DatasetNotFoundError) as ex:
         return self.response(404, message=str(ex))
Exemple #11
0
 def put(self, key: str) -> Response:
     """Updates an existing form_data.
     ---
     put:
       description: >-
         Updates an existing form_data.
       parameters:
       - in: path
         schema:
           type: string
         name: key
       - in: query
         schema:
           type: integer
         name: tab_id
       requestBody:
         required: true
         content:
           application/json:
             schema:
               $ref: '#/components/schemas/FormDataPutSchema'
       responses:
         200:
           description: The form_data was stored successfully.
           content:
             application/json:
               schema:
                 type: object
                 properties:
                   key:
                     type: string
                     description: The key to retrieve the form_data.
         400:
           $ref: '#/components/responses/400'
         401:
           $ref: '#/components/responses/401'
         404:
           $ref: '#/components/responses/404'
         422:
           $ref: '#/components/responses/422'
         500:
           $ref: '#/components/responses/500'
     """
     try:
         item = self.edit_model_schema.load(request.json)
         tab_id = request.args.get("tab_id")
         args = CommandParameters(
             actor=g.user,
             datasource_id=item["datasource_id"],
             datasource_type=item["datasource_type"],
             chart_id=item.get("chart_id"),
             tab_id=tab_id,
             key=key,
             form_data=item["form_data"],
         )
         result = UpdateFormDataCommand(args).run()
         if not result:
             return self.response_404()
         return self.response(200, key=result)
     except ValidationError as ex:
         return self.response(400, message=ex.messages)
     except TemporaryCacheAccessDeniedError as ex:
         return self.response(403, message=str(ex))
     except TemporaryCacheResourceNotFoundError as ex:
         return self.response(404, message=str(ex))