def test_raise_exception_for_invalid_tab_ids(tabbed_dashboard: Dashboard) -> None: with pytest.raises(ReportScheduleInvalidError) as exc_info: CreateReportScheduleCommand( { **DASHBOARD_REPORT_SCHEDULE_DEFAULTS, "name": "tabbed dashboard report (invalid tab ids)", "dashboard": tabbed_dashboard.id, "extra": {"dashboard": {"activeTabs": ["TAB-INVALID_ID"]}}, } ).run() assert "Invalid tab ids" in str(exc_info.value.normalized_messages()) with pytest.raises(ReportScheduleInvalidError) as exc_info: CreateReportScheduleCommand( { **DASHBOARD_REPORT_SCHEDULE_DEFAULTS, "name": "tabbed dashboard report (invalid tab ids in anchor)", "dashboard": tabbed_dashboard.id, "extra": { "dashboard": { "activeTabs": ["TAB-L1AA"], "anchor": "TAB-INVALID_ID", } }, } ).run() assert "Invalid tab ids" in str(exc_info.value.normalized_messages())
def post(self) -> Response: """Creates a new Report Schedule --- post: description: >- Create a new Report Schedule requestBody: description: Report Schedule schema required: true content: application/json: schema: $ref: '#/components/schemas/{{self.__class__.__name__}}.post' responses: 201: description: Report schedule added content: application/json: schema: type: object properties: id: type: number result: $ref: '#/components/schemas/{{self.__class__.__name__}}.post' 400: $ref: '#/components/responses/400' 401: $ref: '#/components/responses/401' 404: $ref: '#/components/responses/404' 500: $ref: '#/components/responses/500' """ if not request.is_json: return self.response_400(message="Request is not JSON") try: item = self.add_model_schema.load(request.json) # This validates custom Schema with custom validations except ValidationError as error: return self.response_400(message=error.messages) try: new_model = CreateReportScheduleCommand(g.user, item).run() return self.response(201, id=new_model.id, result=item) except ReportScheduleNotFoundError as ex: return self.response_400(message=str(ex)) except ReportScheduleInvalidError as ex: return self.response_422(message=ex.normalized_messages()) except ReportScheduleCreateFailedError as ex: logger.error( "Error creating report schedule %s: %s", self.__class__.__name__, str(ex), exc_info=True, ) return self.response_422(message=str(ex))
def test_accept_valid_tab_ids(tabbed_dashboard: Dashboard) -> None: report_schedule = CreateReportScheduleCommand( { **DASHBOARD_REPORT_SCHEDULE_DEFAULTS, "name": "tabbed dashboard report (valid tabs id)", "dashboard": tabbed_dashboard.id, "extra": {"dashboard": {"activeTabs": ["TAB-L1AA", "TAB-L2AB"]}}, } ).run() assert report_schedule.extra == { "dashboard": {"activeTabs": ["TAB-L1AA", "TAB-L2AB"]} } db.session.delete(report_schedule) db.session.commit()
def post(self, ) -> Response: """Creates a new Report Schedule --- post: description: >- Create a new Report Schedule requestBody: description: Report Schedule schema required: true content: application/json: schema: $ref: '#/components/schemas/{{self.__class__.__name__}}.post' responses: 201: description: Report schedule added content: application/json: schema: type: object properties: id: type: number result: $ref: '#/components/schemas/{{self.__class__.__name__}}.post' 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.add_model_schema.load(request.json) # normally this would be covered by a decorator, however # due to this model being formatted incorrectly the data # needed some manipulation. event_logger.log_with_context( action="ReportScheduleRestApi.post", dashboard_id=request.json.get("dashboard", None), chart_id=request.json.get("chart", None), report_format=request.json.get("report_format", None), active=request.json.get("active", None), ) # This validates custom Schema with custom validations except ValidationError as error: return self.response_400(message=error.messages) try: new_model = CreateReportScheduleCommand(g.user, item).run() return self.response(201, id=new_model.id, result=item) except ReportScheduleNotFoundError as ex: return self.response_400(message=str(ex)) except ReportScheduleInvalidError as ex: return self.response_422(message=ex.normalized_messages()) except ReportScheduleCreateFailedError as ex: logger.error( "Error creating report schedule %s: %s", self.__class__.__name__, str(ex), exc_info=True, ) return self.response_422(message=str(ex))