rusackas commented on code in PR #41299: URL: https://github.com/apache/superset/pull/41299#discussion_r3456944822
########## tests/unit_tests/test_check_secret_key.py: ########## @@ -0,0 +1,64 @@ +# 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. +"""Tests for SupersetAppInitializer.check_secret_key.""" + +from unittest.mock import MagicMock, patch + +import pytest + +from superset.constants import CHANGE_ME_SECRET_KEY +from superset.initialization import SupersetAppInitializer + + +def _make_initializer(secret_key, *, debug=False, testing=False): Review Comment: Good catch — annotated. ########## tests/unit_tests/test_check_secret_key.py: ########## @@ -0,0 +1,64 @@ +# 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. +"""Tests for SupersetAppInitializer.check_secret_key.""" + +from unittest.mock import MagicMock, patch + +import pytest + +from superset.constants import CHANGE_ME_SECRET_KEY +from superset.initialization import SupersetAppInitializer + + +def _make_initializer(secret_key, *, debug=False, testing=False): + """Build a bare initializer with just the attributes check_secret_key needs.""" + init = object.__new__(SupersetAppInitializer) + init.config = {"SECRET_KEY": secret_key} + app = MagicMock() + app.debug = debug + app.config = {"TESTING": testing} + init.superset_app = app + return init + + [email protected]("secret_key", ["", None, CHANGE_ME_SECRET_KEY]) +def test_check_secret_key_refuses_to_start_when_insecure(secret_key): Review Comment: Good catch — annotated. ########## tests/unit_tests/test_check_secret_key.py: ########## @@ -0,0 +1,64 @@ +# 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. +"""Tests for SupersetAppInitializer.check_secret_key.""" + +from unittest.mock import MagicMock, patch + +import pytest + +from superset.constants import CHANGE_ME_SECRET_KEY +from superset.initialization import SupersetAppInitializer + + +def _make_initializer(secret_key, *, debug=False, testing=False): + """Build a bare initializer with just the attributes check_secret_key needs.""" + init = object.__new__(SupersetAppInitializer) + init.config = {"SECRET_KEY": secret_key} + app = MagicMock() + app.debug = debug + app.config = {"TESTING": testing} + init.superset_app = app + return init + + [email protected]("secret_key", ["", None, CHANGE_ME_SECRET_KEY]) +def test_check_secret_key_refuses_to_start_when_insecure(secret_key): + """An empty/missing or placeholder key fails closed in non-debug mode.""" + initializer = _make_initializer(secret_key) + with patch("superset.initialization.is_test", return_value=False): + with pytest.raises(SystemExit): + initializer.check_secret_key() + + [email protected]("secret_key", ["", None, CHANGE_ME_SECRET_KEY]) +def test_check_secret_key_warns_but_starts_in_debug(secret_key): Review Comment: Good catch — annotated. ########## tests/unit_tests/test_check_secret_key.py: ########## @@ -0,0 +1,64 @@ +# 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. +"""Tests for SupersetAppInitializer.check_secret_key.""" + +from unittest.mock import MagicMock, patch + +import pytest + +from superset.constants import CHANGE_ME_SECRET_KEY +from superset.initialization import SupersetAppInitializer + + +def _make_initializer(secret_key, *, debug=False, testing=False): + """Build a bare initializer with just the attributes check_secret_key needs.""" + init = object.__new__(SupersetAppInitializer) + init.config = {"SECRET_KEY": secret_key} + app = MagicMock() + app.debug = debug + app.config = {"TESTING": testing} + init.superset_app = app + return init + + [email protected]("secret_key", ["", None, CHANGE_ME_SECRET_KEY]) +def test_check_secret_key_refuses_to_start_when_insecure(secret_key): + """An empty/missing or placeholder key fails closed in non-debug mode.""" + initializer = _make_initializer(secret_key) + with patch("superset.initialization.is_test", return_value=False): + with pytest.raises(SystemExit): + initializer.check_secret_key() + + [email protected]("secret_key", ["", None, CHANGE_ME_SECRET_KEY]) +def test_check_secret_key_warns_but_starts_in_debug(secret_key): + """In debug/testing mode an insecure key warns but does not exit.""" + initializer = _make_initializer(secret_key, debug=True) + with patch("superset.initialization.is_test", return_value=False): + # Should not raise SystemExit. + initializer.check_secret_key() + + +def test_check_secret_key_accepts_strong_key(): + """A non-empty, non-placeholder key starts without warning or exit.""" Review Comment: Good catch — annotated. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
