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:approval bug #793

Merged
merged 6 commits into from
Aug 9, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
from django.utils.functional import cached_property
from pydantic import BaseModel, parse_obj_as

from apigateway.apps.permission.constants import GrantDimensionEnum, PermissionLevelEnum, PermissionStatusEnum
from apigateway.apps.permission.constants import (
GrantDimensionEnum,
PermissionLevelEnum,
PermissionStatusEnum,
)
from apigateway.apps.permission.models import AppGatewayPermission, AppPermissionApplyStatus, AppResourcePermission
from apigateway.biz.released_resource import ReleasedResourceHandler
from apigateway.biz.resource_version import ResourceVersionHandler
Expand Down Expand Up @@ -222,6 +226,7 @@ def build(self) -> list:
}

doc_links = ReleasedResourceHandler.get_latest_doc_link(list(resource_map.keys()))
# resource_map由 已有的资源
for resource_id, resource in resource_map.items():
resource_fields = resource_id_to_fields.get(resource_id, {})
resource["api_name"] = resource_fields.get("gateway__name", "")
Expand All @@ -230,7 +235,14 @@ def build(self) -> list:
resource["api_permission_apply_status"] = gateway_id_to_permission_apply_status.get(
resource_fields.get("gateway_id"), ""
)
resource["resource_permission_apply_status"] = resource_id_to_permission_apply_status.get(resource_id, "")
# 判断这个资源是否有网关资源的权限,而不是直接通过
# 如果应用已经有网关权限,则不展示单个资源申请的状态
if api_permission_map.get(resource["gateway_id"]):
resource["resource_permission_apply_status"] = ""
else:
resource["resource_permission_apply_status"] = resource_id_to_permission_apply_status.get(
resource_id, ""
)
Lawrence-lkq marked this conversation as resolved.
Show resolved Hide resolved

resource_permissions = parse_obj_as(List[ResourcePermission], list(resource_map.values()))
return [perm.as_dict() for perm in resource_permissions]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,128 @@ def test_build(self, mocker, fake_gateway, unique_id):
}
]

# 网关权限存在,资源审批状态权限为拒绝 BUG
def test_build_with_approval_bug_owned(self, mocker, fake_gateway, unique_id):
r = G(Resource, gateway=fake_gateway)
# 网关权限
G(AppGatewayPermission, gateway=fake_gateway, bk_app_code=unique_id, expires=None)
# 资源审批状态权限
G(
AppPermissionApplyStatus,
gateway=fake_gateway,
bk_app_code=unique_id,
grant_dimension=GrantDimensionEnum.RESOURCE.value,
resource_id=r.id,
status=ApplyStatusEnum.REJECTED.value,
)
mocker.patch(
"apigateway.apis.open.permission.helpers.ResourceVersionHandler.get_released_public_resources",
return_value=[
{
"id": r.id,
"name": "test1-1",
"description": "desc",
"description_en": "desc_en",
"resource_perm_required": True,
},
],
)
mocker.patch(
"apigateway.apis.open.permission.helpers.ReleasedResource.objects.filter_latest_released_resources",
return_value=[
{
"id": r.id,
"name": "test1-2",
"description": "desc",
"description_en": "desc_en",
"resource_perm_required": True,
},
],
)
mocker.patch(
"apigateway.apis.open.permission.helpers.ReleasedResourceHandler.get_latest_doc_link",
return_value={
r.id: "test",
},
)

result = AppPermissionBuilder(unique_id).build()
assert result == [
{
"id": r.id,
"name": "test1-2",
"api_name": fake_gateway.name,
"gateway_id": fake_gateway.id,
"description": "desc",
"description_en": "desc_en",
"permission_status": "owned", # 拥有权限
"permission_level": "normal",
"expires_in": math.inf,
"doc_link": "test",
}
]

# 没有网关权限的情况
def test_build_with_approval_bug_rejected(self, mocker, fake_gateway, unique_id):
r = G(Resource, gateway=fake_gateway)
# 资源审批状态权限
G(
AppPermissionApplyStatus,
gateway=fake_gateway,
bk_app_code=unique_id,
grant_dimension=GrantDimensionEnum.RESOURCE.value,
resource=r,
status=ApplyStatusEnum.REJECTED.value,
)
# 创建资源权限
G(AppResourcePermission, gateway=fake_gateway, bk_app_code=unique_id, resource_id=r.id)
mocker.patch(
"apigateway.apis.open.permission.helpers.ResourceVersionHandler.get_released_public_resources",
return_value=[
{
"id": r.id,
"name": "test1-1",
"description": "desc",
"description_en": "desc_en",
"resource_perm_required": True,
},
],
)
mocker.patch(
"apigateway.apis.open.permission.helpers.ReleasedResource.objects.filter_latest_released_resources",
return_value=[
{
"id": r.id,
"name": "test1-2",
"description": "desc",
"description_en": "desc_en",
"resource_perm_required": True,
},
],
)
mocker.patch(
"apigateway.apis.open.permission.helpers.ReleasedResourceHandler.get_latest_doc_link",
return_value={
r.id: "test",
},
)

result = AppPermissionBuilder(unique_id).build()
assert result == [
{
"id": r.id,
"name": "test1-2",
"api_name": fake_gateway.name,
"gateway_id": fake_gateway.id,
"description": "desc",
"description_en": "desc_en",
"permission_status": "rejected", # 拒绝状态
"permission_level": "normal",
"expires_in": 15551999,
"doc_link": "test",
}
]

def test_build_with_apply_status(self, mocker, fake_gateway, fake_resource, unique_id):
G(
AppResourcePermission,
Expand Down
Loading