Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(report): edit without custom width #24612

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions superset/reports/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from typing import Any, Union
from typing import Any, Optional, Union

from croniter import croniter
from flask import current_app
Expand Down Expand Up @@ -220,7 +220,13 @@
)

@validates("custom_width")
def validate_custom_width(self, value: int) -> None: # pylint: disable=no-self-use
def validate_custom_width( # pylint: disable=no-self-use
self,
value: Optional[int],
) -> None:
if value is None:
return

Check warning on line 228 in superset/reports/schemas.py

View check run for this annotation

Codecov / codecov/patch

superset/reports/schemas.py#L228

Added line #L228 was not covered by tests

min_width = current_app.config["ALERT_REPORTS_MIN_CUSTOM_SCREENSHOT_WIDTH"]
max_width = current_app.config["ALERT_REPORTS_MAX_CUSTOM_SCREENSHOT_WIDTH"]
if not min_width <= value <= max_width:
Expand Down Expand Up @@ -344,7 +350,13 @@
)

@validates("custom_width")
def validate_custom_width(self, value: int) -> None: # pylint: disable=no-self-use
def validate_custom_width( # pylint: disable=no-self-use
self,
value: Optional[int],
) -> None:
if value is None:
return

Check warning on line 358 in superset/reports/schemas.py

View check run for this annotation

Codecov / codecov/patch

superset/reports/schemas.py#L357-L358

Added lines #L357 - L358 were not covered by tests

min_width = current_app.config["ALERT_REPORTS_MIN_CUSTOM_SCREENSHOT_WIDTH"]
max_width = current_app.config["ALERT_REPORTS_MAX_CUSTOM_SCREENSHOT_WIDTH"]
if not min_width <= value <= max_width:
Expand Down
16 changes: 16 additions & 0 deletions tests/unit_tests/reports/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
75 changes: 75 additions & 0 deletions tests/unit_tests/reports/schemas_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import pytest
from marshmallow import ValidationError
from pytest_mock import MockFixture

from superset.reports.schemas import ReportSchedulePostSchema, ReportSchedulePutSchema


def test_report_post_schema_custom_width_validation(mocker: MockFixture) -> None:
"""
Test the custom width validation.
"""
current_app = mocker.patch("superset.reports.schemas.current_app")
current_app.config = {
"ALERT_REPORTS_MIN_CUSTOM_SCREENSHOT_WIDTH": 100,
"ALERT_REPORTS_MAX_CUSTOM_SCREENSHOT_WIDTH": 200,
}

schema = ReportSchedulePostSchema()

schema.load(
{
"type": "Report",
"name": "A report",
"description": "My report",
"active": True,
"crontab": "* * * * *",
"timezone": "America/Los_Angeles",
"custom_width": 100,
}
)

# not required
schema.load(
{
"type": "Report",
"name": "A report",
"description": "My report",
"active": True,
"crontab": "* * * * *",
"timezone": "America/Los_Angeles",
}
)

with pytest.raises(ValidationError) as excinfo:
schema.load(
{
"type": "Report",
"name": "A report",
"description": "My report",
"active": True,
"crontab": "* * * * *",
"timezone": "America/Los_Angeles",
"custom_width": 1000,
}
)
assert excinfo.value.messages == {
"custom_width": ["Screenshot width must be between 100px and 200px"]
}
Loading