Skip to content

Commit

Permalink
refactor: cr调整
Browse files Browse the repository at this point in the history
  • Loading branch information
neronkl committed Sep 6, 2023
1 parent 389d773 commit a083dc9
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 188 deletions.
5 changes: 2 additions & 3 deletions src/bk-user/bkuser/biz/tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,12 @@ def convert_data_source_department_to_tenant_department(
continue
# 部门基础信息
data_source_department_info = data_source_departments[data_source_department_id]
# 只要一个子部门被授权,都是存在子部门
children_flag = [True for child in data_source_department_info.child_ids if child in bound_departments_ids]
# 协同数据源:只要一个子部门被授权给该租户,has_children=True
data.append(
TenantDepartmentBaseInfo(
id=tenant_department.id,
name=data_source_department_info.name,
has_children=any(children_flag),
has_children=bool(set(data_source_department_info.child_ids) & set(bound_departments_ids)),
)
)
return data
Expand Down
69 changes: 69 additions & 0 deletions src/bk-user/tests/apis/web/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- 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.
"""
from typing import List

import pytest
from bkuser.apps.data_source.constants import DataSourcePluginEnum
from bkuser.apps.data_source.models import (
DataSource,
DataSourceDepartment,
DataSourceUser,
)
from bkuser.apps.tenant.models import TenantDepartment, TenantUser

from tests.test_utils.data_source import (
create_data_source_departments_with_relations,
create_data_source_users_with_relations,
)
from tests.test_utils.helpers import generate_random_string
from tests.test_utils.tenant import create_tenant_departments, create_tenant_users


@pytest.fixture()
def local_data_source(default_tenant) -> DataSource:
"""
创建测试数据源
"""
return DataSource.objects.create(
name=generate_random_string(), owner_tenant_id=default_tenant, plugin_id=DataSourcePluginEnum.LOCAL
)


@pytest.fixture()
def local_data_source_departments(local_data_source) -> List[DataSourceDepartment]:
"""
创建测试数据源部门,并以首个对象作为父部门
"""
return create_data_source_departments_with_relations(local_data_source)


@pytest.fixture()
def local_data_source_users(local_data_source, local_data_source_departments) -> List[DataSourceUser]:
"""
创建测试数据源用户, 并以首个对象作为leader, 随机关联部门
"""
return create_data_source_users_with_relations(local_data_source, local_data_source_departments)


@pytest.fixture()
def tenant_users(default_tenant, local_data_source_users) -> List[TenantUser]:
"""
根据测试数据源用户,创建租户用户
"""
return create_tenant_users(default_tenant, local_data_source_users)


@pytest.fixture()
def tenant_departments(default_tenant, local_data_source, local_data_source_departments) -> List[TenantDepartment]:
"""
根据测试数据源部门,创建租户部门
"""
return create_tenant_departments(default_tenant, local_data_source_departments)
86 changes: 42 additions & 44 deletions src/bk-user/tests/apis/web/organization/test_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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.
"""
from typing import List

import pytest
from bkuser.apps.data_source.models import (
Expand All @@ -17,56 +16,58 @@
DataSourceUserLeaderRelation,
)
from bkuser.apps.tenant.models import Tenant, TenantDepartment, TenantManager, TenantUser
from bkuser.auth.models import User
from django.urls import reverse
from django.utils import timezone
from rest_framework import status
from rest_framework.test import APIClient

from tests.test_utils.helpers import generate_random_string

pytestmark = pytest.mark.django_db


class TestTenantListApi:
def test_list_tenants(
self,
api_client: APIClient,
bk_user: User,
default_tenant: str,
random_tenant: str,
default_tenant_departments: List[TenantDepartment],
api_client,
bk_user,
default_tenant,
tenant_departments,
):
resp = api_client.get(reverse("organization.tenant.list"))

# 至少会有一个当前用户所属的租户
current_tenant_id = bk_user.get_property("tenant_id")
assert len(resp.data) >= 1
resp_data = resp.data
for tenant_info in resp_data:
if tenant_info["id"] != bk_user.get_property("tenant_id"):
assert current_tenant_id == resp.data[0]["id"]

for tenant_info in resp.data:
if tenant_info["id"] != current_tenant_id:
assert not tenant_info["departments"]
else:
assert len(tenant_info["departments"]) >= 1


class TestTenantRetrieveUpdateApi:
NOT_EXIST_TENANT_ID = 9999

def test_retrieve_tenant(self, api_client: APIClient, bk_user: User):
def test_retrieve_tenant(self, api_client, bk_user, default_tenant):
tenant_id = bk_user.get_property("tenant_id")
resp = api_client.get(reverse("organization.tenant.retrieve_update", kwargs={"id": tenant_id}))
resp_data = resp.data
resp = api_client.get(
reverse("organization.tenant.retrieve_update", kwargs={"id": bk_user.get_property("tenant_id")})
)

tenant = Tenant.objects.get(id=tenant_id)

assert tenant_id == resp_data["id"]
assert tenant.id == resp_data["id"]
assert tenant.name == resp_data["name"]
assert tenant.updated_at_display == resp_data["updated_at"]
assert tenant.logo == resp_data["logo"]
assert tenant.feature_flags == resp_data["feature_flags"]
assert TenantManager.objects.filter(tenant=tenant).count() == len(resp_data["managers"])
assert tenant.id == resp.data["id"]
assert tenant.name == resp.data["name"]
assert tenant.updated_at_display == resp.data["updated_at"]
assert tenant.logo == resp.data["logo"]
assert tenant.feature_flags == resp.data["feature_flags"]
assert TenantManager.objects.filter(tenant=tenant).count() == len(resp.data["managers"])

for item in resp_data["managers"]:
for item in resp.data["managers"]:
assert TenantManager.objects.filter(tenant=tenant, tenant_user_id=item["id"]).exists()

def test_retrieve_other_tenant(self, api_client: APIClient, random_tenant: str):
def test_retrieve_other_tenant(self, api_client, random_tenant):
resp = api_client.get(reverse("organization.tenant.retrieve_update", kwargs={"id": random_tenant}))
resp_data = resp.data
tenant = Tenant.objects.get(id=random_tenant)
Expand All @@ -79,12 +80,12 @@ def test_retrieve_other_tenant(self, api_client: APIClient, random_tenant: str):
# 非当前用户所在租户,不返回管理员
assert not resp_data["managers"]

def test_retrieve_not_exist_tenant(self, api_client: APIClient):
resp = api_client.get(reverse("organization.tenant.retrieve_update", kwargs={"id": self.NOT_EXIST_TENANT_ID}))
def test_retrieve_not_exist_tenant(self, api_client):
resp = api_client.get(reverse("organization.tenant.retrieve_update", kwargs={"id": generate_random_string()}))
assert resp.status_code == status.HTTP_404_NOT_FOUND

def test_update_tenant(self, api_client: APIClient, default_tenant: str, default_tenant_users: List[TenantUser]):
new_manager_ids = [user.id for user in default_tenant_users]
def test_update_tenant(self, api_client, default_tenant, tenant_users):
new_manager_ids = [user.id for user in tenant_users]
update_data = {
"id": "fake-tenant-updated",
"name": "fake-tenant-updated",
Expand All @@ -105,33 +106,29 @@ def test_update_tenant(self, api_client: APIClient, default_tenant: str, default
TenantManager.objects.filter(tenant=tenant).values_list("tenant_user_id", flat=True)
)

def test_update_other_tenant(
self, api_client: APIClient, random_tenant: str, default_tenant_users: List[TenantUser]
):
def test_update_other_tenant(self, api_client, random_tenant, tenant_users):
resp = api_client.put(
reverse("organization.tenant.retrieve_update", kwargs={"id": random_tenant}),
data={
"id": "fake-tenant-updated",
"name": "fake-tenant-updated",
"feature_flags": {"user_number_visible": False},
"logo": "aabb",
"manager_ids": [user.id for user in default_tenant_users],
"manager_ids": [user.id for user in tenant_users],
},
)
# 进行更新非当前用户的租户,异常返回
assert resp.status_code == status.HTTP_403_FORBIDDEN


class TestTenantUserListApi:
def test_list_tenant_users(
self, api_client: APIClient, default_tenant: str, default_tenant_users: List[TenantUser]
):
def test_list_tenant_users(self, api_client, default_tenant, tenant_users):
resp = api_client.get(reverse("organization.tenant.users.list", kwargs={"id": default_tenant}))

assert TenantUser.objects.filter(tenant_id=default_tenant).count() == resp.data["count"]
for item in resp.data["results"]:
tenant_user = TenantUser.objects.filter(id=item["id"]).first()
assert tenant_user
assert tenant_user is not None
assert tenant_user.data_source_user.username == item["username"]
assert tenant_user.data_source_user.full_name == item["full_name"]
assert tenant_user.data_source_user.email == item["email"]
Expand All @@ -141,8 +138,8 @@ def test_list_tenant_users(


class TestTenantDepartmentChildrenListApi:
def test_retrieve_children(self, api_client: APIClient, default_tenant_departments: List[TenantDepartment]):
for item in default_tenant_departments:
def test_retrieve_children(self, api_client, tenant_departments):
for item in tenant_departments:
resp = api_client.get(reverse("organization.children.list", kwargs={"id": item.id}))

children = DataSourceDepartmentRelation.objects.get(department=item.data_source_department).get_children()
Expand All @@ -161,10 +158,10 @@ class TestTenantDepartmentUserListApi:
def test_list_department_users(
self,
api_client: APIClient,
default_tenant_departments: List[TenantDepartment],
default_tenant_users: List[TenantUser],
tenant_departments,
tenant_users,
):
for tenant_department in default_tenant_departments:
for tenant_department in tenant_departments:
resp = api_client.get(reverse("departments.users.list", kwargs={"id": tenant_department.id}))
resp_data = resp.data

Expand All @@ -182,15 +179,16 @@ def test_list_department_users(


class TestTenantUserRetrieveApi:
def test_retrieve_user(self, api_client: APIClient, default_tenant_users: List[TenantUser]):
for tenant_user in default_tenant_users:
def test_retrieve_user(self, api_client, tenant_users):
for tenant_user in tenant_users:
resp = api_client.get(reverse("department.users.retrieve", kwargs={"id": tenant_user.id}))

resp_data = resp.data
data_source_user = tenant_user.data_source_user

assert tenant_user.id == resp_data["id"]
assert tenant_user.account_expired_at.strftime("%Y-%m-%d %H:%M:%S") == resp_data["account_expired_at"]
real_account_expired_at = timezone.localtime(tenant_user.account_expired_at)
assert real_account_expired_at.strftime("%Y-%m-%d %H:%M:%S") == resp_data["account_expired_at"]

assert data_source_user.username == resp_data["username"]
assert data_source_user.full_name == resp_data["full_name"]
Expand Down
69 changes: 69 additions & 0 deletions src/bk-user/tests/biz/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- 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.
"""
from typing import List

import pytest
from bkuser.apps.data_source.constants import DataSourcePluginEnum
from bkuser.apps.data_source.models import (
DataSource,
DataSourceDepartment,
DataSourceUser,
)
from bkuser.apps.tenant.models import TenantDepartment, TenantUser

from tests.test_utils.data_source import (
create_data_source_departments_with_relations,
create_data_source_users_with_relations,
)
from tests.test_utils.helpers import generate_random_string
from tests.test_utils.tenant import create_tenant_departments, create_tenant_users


@pytest.fixture()
def local_data_source(default_tenant) -> DataSource:
"""
创建测试数据源
"""
return DataSource.objects.create(
name=generate_random_string(), owner_tenant_id=default_tenant, plugin_id=DataSourcePluginEnum.LOCAL
)


@pytest.fixture()
def local_data_source_departments(local_data_source) -> List[DataSourceDepartment]:
"""
创建测试数据源部门,并以首个对象作为父部门
"""
return create_data_source_departments_with_relations(local_data_source)


@pytest.fixture()
def local_data_source_users(local_data_source, local_data_source_departments) -> List[DataSourceUser]:
"""
创建测试数据源用户, 并以首个对象作为leader, 随机关联部门
"""
return create_data_source_users_with_relations(local_data_source, local_data_source_departments)


@pytest.fixture()
def tenant_users(default_tenant, local_data_source_users) -> List[TenantUser]:
"""
根据测试数据源用户,创建租户用户
"""
return create_tenant_users(default_tenant, local_data_source_users)


@pytest.fixture()
def tenant_departments(default_tenant, local_data_source, local_data_source_departments) -> List[TenantDepartment]:
"""
根据测试数据源部门,创建租户部门
"""
return create_tenant_departments(default_tenant, local_data_source_departments)
13 changes: 6 additions & 7 deletions src/bk-user/tests/biz/test_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
DataSourceDepartment,
DataSourceDepartmentRelation,
DataSourceDepartmentUserRelation,
DataSourceUser,
DataSourceUserLeaderRelation,
)
from bkuser.biz.data_source import DataSourceDepartmentHandler, DataSourceUserHandler
Expand All @@ -24,7 +23,7 @@


class TestDataSourceDepartmentHandler:
def test_get_department_info_map_by_ids(self, local_data_source_departments: List[DataSourceDepartment]):
def test_get_department_info_map_by_ids(self, local_data_source_departments):
departments_map = DataSourceDepartmentHandler.get_department_info_map_by_ids(
[department.id for department in local_data_source_departments]
)
Expand All @@ -51,15 +50,15 @@ def test_get_department_info_map_by_ids(self, local_data_source_departments: Lis
[14, 24, 34],
],
)
def test_not_exist_get_department_info_map_by_ids(self, not_exist_data_source_department_ids: List[int]):
def test_not_exist_get_department_info_map_by_ids(self, not_exist_data_source_department_ids):
departments_map = DataSourceDepartmentHandler.get_department_info_map_by_ids(
not_exist_data_source_department_ids
)
data_source_departments = DataSourceDepartment.objects.filter(id__in=not_exist_data_source_department_ids)
assert data_source_departments.count() == len(departments_map.keys())
assert not departments_map

def test_get_user_department_ids_map(
self, local_data_source_departments: List[DataSourceDepartment], local_data_source_users: List[DataSourceUser]
):
def test_get_user_department_ids_map(self, local_data_source_departments, local_data_source_users):
user_ids = [user.id for user in local_data_source_users]

user_department_relationship_map = DataSourceDepartmentHandler.get_user_department_ids_map(user_ids)
Expand Down Expand Up @@ -87,7 +86,7 @@ def test_not_exist_get_user_department_ids_map(self, not_exist_data_source_user_


class TestDataSourceUserHandler:
def test_get_user_leader_ids_map(self, local_data_source_users: List[DataSourceUser]):
def test_get_user_leader_ids_map(self, local_data_source_users):
data_source_user_ids = [item.id for item in local_data_source_users]
leader_ids_map = DataSourceUserHandler.get_user_leader_ids_map(data_source_user_ids)

Expand Down
Loading

0 comments on commit a083dc9

Please sign in to comment.