From dc62b0a7f15d0b637fa1e6a5daaa3dff4d749159 Mon Sep 17 00:00:00 2001 From: alex-smile <443677891@qq.com> Date: Wed, 27 Dec 2023 10:58:43 +0800 Subject: [PATCH 1/2] fix fetch_esb_public_key command --- .../commands/fetch_esb_public_key.py | 2 +- .../commands/test_fetch_esb_public_key.py | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 sdks/apigw-manager/tests/apigw_manager/apigw/management/commands/test_fetch_esb_public_key.py diff --git a/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/fetch_esb_public_key.py b/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/fetch_esb_public_key.py index adb66c62..d409b896 100644 --- a/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/fetch_esb_public_key.py +++ b/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/fetch_esb_public_key.py @@ -17,4 +17,4 @@ class Command(BaseCommand): def handle(self, gateway_name, *args, **kwargs): for _gateway_name in ["bk-esb", "apigw"]: - super(Command, self).handle(_gateway_name, *args, **kwargs) + super(Command, self).handle(*args, gateway_name=_gateway_name, **kwargs) diff --git a/sdks/apigw-manager/tests/apigw_manager/apigw/management/commands/test_fetch_esb_public_key.py b/sdks/apigw-manager/tests/apigw_manager/apigw/management/commands/test_fetch_esb_public_key.py new file mode 100644 index 00000000..457c3fc0 --- /dev/null +++ b/sdks/apigw-manager/tests/apigw_manager/apigw/management/commands/test_fetch_esb_public_key.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +""" + * TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-蓝鲸 PaaS 平台(BlueKing-PaaS) available. + * Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * 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 apigw_manager.apigw.management.commands.fetch_esb_public_key import Command + + +@pytest.fixture() +def manager(mocker): + return mocker.MagicMock() + + +@pytest.fixture() +def public_key_manager(mocker): + return mocker.MagicMock() + + +@pytest.fixture() +def command(mocker, manager, public_key_manager): + mock_fun = mocker.patch("apigw_manager.apigw.helper.make_default_public_key_manager") + mock_fun.return_value = public_key_manager + + command = Command() + command.manager_class = mocker.MagicMock(return_value=manager) + + return command + + +def test_handle_no_save(command, manager, public_key_manager): + command.handle(gateway_name="foo", print_=True, no_save=True) + manager.public_key.assert_called() + public_key_manager.set.assert_not_called() + + +def test_handle(command, manager, public_key_manager): + command.handle(gateway_name="foo", print_=True, no_save=False) + manager.public_key.assert_called() + public_key_manager.set.assert_called() From da92db19532a05738e94873251f3f8e89080df4e Mon Sep 17 00:00:00 2001 From: alex-smile <443677891@qq.com> Date: Wed, 27 Dec 2023 11:16:49 +0800 Subject: [PATCH 2/2] optimize Command fetch_esb_public_key/fetch_apigw_public_key params --- .../apigw/management/commands/fetch_apigw_public_key.py | 6 +++--- .../apigw/management/commands/fetch_esb_public_key.py | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/fetch_apigw_public_key.py b/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/fetch_apigw_public_key.py index 13abdb87..01932681 100644 --- a/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/fetch_apigw_public_key.py +++ b/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/fetch_apigw_public_key.py @@ -26,13 +26,13 @@ def add_arguments(self, parser): parser.add_argument("--bk-app-secret", dest="bk_app_secret", help="app secret") parser.add_argument("--no-save", default=False, action="store_true", help="do not save the public key") - def do(self, manager, configuration, print_, no_save, *args, **kwargs): + def do(self, manager, configuration, *args, **kwargs): result = manager.public_key() - if print_: + if kwargs.get("print_"): print(result["public_key"]) - if no_save: + if kwargs.get("no_save"): return public_key_manager = helper.make_default_public_key_manager() diff --git a/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/fetch_esb_public_key.py b/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/fetch_esb_public_key.py index d409b896..e9e93bfd 100644 --- a/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/fetch_esb_public_key.py +++ b/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/fetch_esb_public_key.py @@ -8,6 +8,7 @@ * 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 copy from apigw_manager.apigw.management.commands.fetch_apigw_public_key import Command as BaseCommand @@ -16,5 +17,8 @@ class Command(BaseCommand): """Get the esb public key and store it into the database""" def handle(self, gateway_name, *args, **kwargs): + copied_kwargs = copy.deepcopy(kwargs) + for _gateway_name in ["bk-esb", "apigw"]: - super(Command, self).handle(*args, gateway_name=_gateway_name, **kwargs) + copied_kwargs["gateway_name"] = _gateway_name + super(Command, self).handle(*args, **copied_kwargs)