Skip to content

Commit

Permalink
fix: cr
Browse files Browse the repository at this point in the history
  • Loading branch information
nannan00 committed Nov 24, 2023
1 parent e6968d1 commit b0d7fde
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/bk-login/bklogin/component/bk_user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class GlobalInfo(BaseModel):

tenant_visible: bool
enabled_auth_tenant_number: int
# 当且仅当只有一个租户认证可用时候才有值,即 enabled_auth_tenant_number = 1 时才有值
only_enabled_auth_tenant: OnlyEnabledAuthTenant | None


Expand Down
4 changes: 2 additions & 2 deletions src/bk-user/bkuser/apis/login/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ def get(self, request, *args, **kwargs):
# 唯一启用认证的租户信息
only_enabled_auth_tenant: Dict[str, Any] | None = None
if enabled_auth_tenant_number == 1:
owner_tenant_id, enabled_ids = next(iter(enabled_idp_map.items()))
owner_tenant_id, enabled_idps = next(iter(enabled_idp_map.items()))
tenant = Tenant.objects.get(id=owner_tenant_id)
only_enabled_auth_tenant = {
"id": tenant.id,
"name": tenant.name,
"logo": tenant.logo,
"enabled_idps": enabled_ids,
"enabled_idps": enabled_idps,
}

return Response(
Expand Down
10 changes: 10 additions & 0 deletions src/bk-user/tests/apis/login/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) 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.
"""
44 changes: 44 additions & 0 deletions src/bk-user/tests/apis/login/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) 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 bkuser.apps.idp.models import Idp, IdpPlugin
from bkuser.idp_plugins.constants import BuiltinIdpPluginEnum
from django.urls import reverse

from tests.test_utils.helpers import generate_random_string

pytestmark = pytest.mark.django_db


class TestGlobalInfoRetrieveApi:
def test_retrieve_with_only_tenant(self, api_client, default_tenant):
resp = api_client.get(reverse("login.global_info.retrieve"))
assert not resp.data["tenant_visible"]
assert resp.data["enabled_auth_tenant_number"] == 1
assert resp.data["only_enabled_auth_tenant"] is not None
assert resp.data["only_enabled_auth_tenant"]["id"] == default_tenant.id
assert len(resp.data["only_enabled_auth_tenant"]["enabled_idps"]) == 1
assert resp.data["only_enabled_auth_tenant"]["enabled_idps"][0]["plugin_id"] == BuiltinIdpPluginEnum.LOCAL

def test_retrieve_with_mult_tenant_but_not_mult_idp(self, api_client, default_tenant, random_tenant):
self.test_retrieve_with_only_tenant(api_client, default_tenant)

def test_retrieve_with_mult_tenant_and_mult_idp(self, api_client, default_tenant, random_tenant):
Idp.objects.create(
name=generate_random_string(),
owner_tenant_id=random_tenant.id,
plugin=IdpPlugin.objects.get(id=BuiltinIdpPluginEnum.LOCAL),
)
resp = api_client.get(reverse("login.global_info.retrieve"))

assert not resp.data["tenant_visible"]
assert resp.data["enabled_auth_tenant_number"] > 1
assert resp.data["only_enabled_auth_tenant"] is None

0 comments on commit b0d7fde

Please sign in to comment.