Skip to content

Commit

Permalink
修复单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-smile committed Dec 8, 2023
1 parent 5804a6c commit 43647c0
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 260 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def post(self, request, *args, **kwargs):
data = slz.validated_data

manager = PermissionDimensionManager.get_manager(data["grant_dimension"])
instance = manager.create_apply_record(
record = manager.create_apply_record(
data["target_app_code"],
request.gateway,
data.get("resource_ids", []),
Expand All @@ -134,14 +134,14 @@ def post(self, request, *args, **kwargs):
)

try:
apply_async_on_commit(send_mail_for_perm_apply, args=[instance.id])
apply_async_on_commit(send_mail_for_perm_apply, args=[record.id])
except Exception:
logger.exception("send mail to api manager fail. apply_record_id=%s", instance.apply_record_id)
logger.exception("send mail to api manager fail. apply_record_id=%s", record.id)

return OKJsonResponse(
"OK",
data={
"record_id": instance.apply_record_id,
"record_id": record.id,
},
)

Expand Down
3 changes: 1 addition & 2 deletions src/dashboard/apigateway/apigateway/apps/permission/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
)
from apigateway.apps.permission.models import (
AppAPIPermission,
AppPermissionApply,
AppPermissionRecord,
AppResourcePermission,
)
Expand All @@ -61,7 +60,7 @@ def send_mail_for_perm_apply(record_id):
"""
申请权限,发送邮件通知管理员审批
"""
record = AppPermissionApply.objects.get(id=record_id)
record = AppPermissionRecord.objects.get(id=record_id)

apigw_domain = getattr(settings, "DASHBOARD_FE_URL", "").rstrip("/")
manager = PermissionDimensionManager.get_manager(record.grant_dimension)
Expand Down
8 changes: 4 additions & 4 deletions src/dashboard/apigateway/apigateway/biz/esb/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def create_apply_record(
username: str,
) -> AppPermissionApplyRecord:
"""创建权限申请单"""
instance = AppPermissionApplyRecord.objects.create_record(
record = AppPermissionApplyRecord.objects.create_record(
board=system.board,
bk_app_code=bk_app_code,
applied_by=username,
Expand All @@ -91,14 +91,14 @@ def create_apply_record(
component_id__in=component_ids,
).delete()
AppPermissionApplyStatus.objects.batch_create(
record=instance,
record=record,
bk_app_code=bk_app_code,
system=system,
component_ids=component_ids,
status=ApplyStatusEnum.PENDING.value,
)

return instance
return record

def renew_permission(self, bk_app_code: str, component_ids: List[int], expire_days: int):
"""权限续期"""
Expand Down Expand Up @@ -299,7 +299,7 @@ def patch_permission_apply_records(self, records: List[AppPermissionApplyRecord]


class AppComponentPermissionData(BaseModel):
expires_in: int
expires_in: Optional[int]


class ComponentPermission(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/apigateway/apigateway/biz/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def create_apply_record(
resources=Resource.objects.filter_by_ids(gateway, ids=resource_ids),
)

return instance
return record


class APIPermissionDimensionManager(PermissionDimensionManager):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# TencentBlueKing is pleased to support the open source community by making
# 蓝鲸智云 - API 网关(BlueKing - APIGateway) available.
# Copyright (C) 2017 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.
#
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.
#
from ddf import G

from apigateway.apps.esb.bkcore.models import AppComponentPermission
from apigateway.utils.time import to_datetime_from_now


class TestAppComponentPermissionManager:
def test_renew_permissions(self, unique_id):
perm = G(
AppComponentPermission,
bk_app_code=unique_id,
component_id=1,
expires=to_datetime_from_now(days=100),
)

AppComponentPermission.objects.renew_permissions(unique_id, [1], 70)
perm.refresh_from_db()
to_datetime_from_now(days=99) < perm.expires < to_datetime_from_now(days=101)

AppComponentPermission.objects.renew_permissions(unique_id, [1], 170)
perm.refresh_from_db()
to_datetime_from_now(days=160) < perm.expires < to_datetime_from_now(days=180)
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,26 @@ def test_list(self, mocker, request_factory):
return_value="my-test",
)
mocker.patch(
"apigateway.apis.open.esb.permission.views.ComponentPermissionBuilder.build",
return_value=[
{
"id": 1,
"board": "test",
"name": "test",
"system_name": "test",
"description": "test",
"description_en": "test_en",
"expires_in": math.inf,
"permission_level": "normal",
"permission_status": "owned",
"doc_link": "",
"tag": "",
"apigateway.apis.open.esb.permission.views.ComponentPermissionManager.get_manager",
return_value=mocker.MagicMock(
**{
"list_permissions.return_value": [
{
"id": 1,
"board": "test",
"name": "test",
"system_name": "test",
"description": "test",
"description_en": "test_en",
"expires_in": math.inf,
"permission_level": "normal",
"permission_status": "owned",
"doc_link": "",
"tag": "",
}
]
}
],
),
)

params = {
Expand Down Expand Up @@ -82,15 +86,13 @@ def test_list(self, mocker, request_factory):


class TestAppPermissionApplyV1APIView:
def test_apply(self, mocker, request_factory, unique_id):
def test_apply(self, settings, mocker, request_factory, unique_id):
settings.USE_GATEWAY_BK_ESB_MANAGE_COMPONENT_PERMISSIONS = False

mocker.patch(
"apigateway.apis.open.esb.permission.serializers.BKAppCodeValidator.__call__",
return_value=None,
)
mocker.patch(
"apigateway.apis.open.esb.permission.views.send_mail_for_perm_apply.apply_async",
return_value=None,
)

system = G(ComponentSystem)
channel = G(ESBChannel, system=system)
Expand Down Expand Up @@ -123,22 +125,26 @@ def test_list(self, mocker, request_factory):
return_value=[1],
)
mocker.patch(
"apigateway.apis.open.esb.permission.views.ComponentPermissionBuilder.build",
return_value=[
{
"board": "test",
"id": 1,
"name": "test",
"system_name": "test",
"description": "desc",
"description_en": "desc_en",
"expires_in": 10,
"permission_level": "nomal",
"permission_status": "owned",
"permission_action": "",
"doc_link": "",
},
],
"apigateway.apis.open.esb.permission.views.ComponentPermissionManager.get_manager",
return_value=mocker.MagicMock(
**{
"list_permissions.return_value": [
{
"board": "test",
"id": 1,
"name": "test",
"system_name": "test",
"description": "desc",
"description_en": "desc_en",
"expires_in": 10,
"permission_level": "nomal",
"permission_status": "owned",
"permission_action": "",
"doc_link": "",
},
]
}
),
)
mocker.patch(
"apigateway.apis.open.esb.permission.serializers.BoardConfigManager.get_optional_display_label",
Expand Down
Loading

0 comments on commit 43647c0

Please sign in to comment.