Skip to content

Commit

Permalink
fix fetch_esb_public_key command (TencentBlueKing#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-smile authored and piglei committed Mar 21, 2024
1 parent cab64c3 commit 910de73
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(_gateway_name, *args, **kwargs)
copied_kwargs["gateway_name"] = _gateway_name
super(Command, self).handle(*args, **copied_kwargs)
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 910de73

Please sign in to comment.