From 5a8b7f9e3acc04767ac8e5881b8095224db25182 Mon Sep 17 00:00:00 2001 From: nero <827392902@qq.com> Date: Tue, 5 Sep 2023 10:53:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20cr=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bk-user/bkuser/biz/data_source.py | 4 +- src/bk-user/bkuser/biz/tenant.py | 6 +- src/bk-user/tests/apis/conftest.py | 4 +- .../web/organization/test_organization.py | 123 ++++++++++++------ src/bk-user/tests/biz/test_data_source.py | 27 ++-- src/bk-user/tests/biz/test_tenant.py | 36 ++--- src/bk-user/tests/conftest.py | 24 ++-- src/bk-user/tests/test_utils/data_source.py | 6 + src/bk-user/tests/test_utils/tenant.py | 11 +- 9 files changed, 155 insertions(+), 86 deletions(-) diff --git a/src/bk-user/bkuser/biz/data_source.py b/src/bk-user/bkuser/biz/data_source.py index 00618edfd..9710ff799 100644 --- a/src/bk-user/bkuser/biz/data_source.py +++ b/src/bk-user/bkuser/biz/data_source.py @@ -25,7 +25,7 @@ class DataSourceDepartmentInfoWithChildren(BaseModel): id: int name: str - children_ids: List[int] + child_ids: List[int] class DataSourceSimpleInfo(BaseModel): @@ -64,7 +64,7 @@ def get_department_info_map_by_ids(department_ids: List[int]) -> Dict[int, DataS departments_map[item.id] = DataSourceDepartmentInfoWithChildren( id=item.id, name=item.name, - children_ids=list( + child_ids=list( DataSourceDepartmentRelation.objects.get(department=item) .get_children() .values_list("department_id", flat=True) diff --git a/src/bk-user/bkuser/biz/tenant.py b/src/bk-user/bkuser/biz/tenant.py index 8e704dfbc..4c92799c8 100644 --- a/src/bk-user/bkuser/biz/tenant.py +++ b/src/bk-user/bkuser/biz/tenant.py @@ -353,7 +353,7 @@ def convert_data_source_department_to_tenant_department( # data_source_departments中包含了父子部门的ID,协同数据源需要查询绑定了该租户 department_ids = list(data_source_departments.keys()) for department in data_source_departments.values(): - department_ids += department.children_ids + department_ids += department.child_ids # NOTE: 协同数据源,可能存在未授权全部子部门 # 提前拉取所有映射, 过滤绑定的租户部门 @@ -374,9 +374,7 @@ def convert_data_source_department_to_tenant_department( # 部门基础信息 data_source_department_info = data_source_departments[data_source_department_id] # 只要一个子部门被授权,都是存在子部门 - children_flag = [ - True for child in data_source_department_info.children_ids if child in bound_departments_ids - ] + children_flag = [True for child in data_source_department_info.child_ids if child in bound_departments_ids] data.append( TenantDepartmentBaseInfo( id=tenant_department.id, diff --git a/src/bk-user/tests/apis/conftest.py b/src/bk-user/tests/apis/conftest.py index 28ab398aa..e390dffb0 100644 --- a/src/bk-user/tests/apis/conftest.py +++ b/src/bk-user/tests/apis/conftest.py @@ -14,10 +14,10 @@ @pytest.fixture() -def bk_user(fake_tenant) -> User: +def bk_user(default_tenant) -> User: """创建测试用用户""" user, _ = User.objects.get_or_create(username="fake_user") - user.set_property("tenant_id", fake_tenant.id) + user.set_property("tenant_id", default_tenant.id) return user diff --git a/src/bk-user/tests/apis/web/organization/test_organization.py b/src/bk-user/tests/apis/web/organization/test_organization.py index 14fbd10d4..5fb3b4411 100644 --- a/src/bk-user/tests/apis/web/organization/test_organization.py +++ b/src/bk-user/tests/apis/web/organization/test_organization.py @@ -8,6 +8,8 @@ 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 ( DataSourceDepartmentRelation, @@ -15,8 +17,10 @@ DataSourceUserLeaderRelation, ) from bkuser.apps.tenant.models import Tenant, TenantDepartment, TenantManager, TenantUser +from bkuser.auth.models import User from django.urls import reverse from rest_framework import status +from rest_framework.test import APIClient from tests.test_utils.tenant import create_tenant @@ -25,39 +29,52 @@ class TestTenantListApi: @pytest.fixture() - def other_tenant(self): - return Tenant.objects.create(id="other-tenant", name="other-tenant") - - def test_list_tenants(self, api_client, bk_user): + def other_tenant(self) -> Tenant: + return create_tenant("other-tenant") + + def test_list_tenants( + self, + api_client: APIClient, + bk_user: User, + default_tenant: Tenant, + other_tenant: Tenant, + default_tenant_departments, + ): resp = api_client.get(reverse("organization.tenant.list")) # 至少会有一个当前用户所属的租户 assert len(resp.data) >= 1 resp_data = resp.data for tenant_info in resp_data: - if tenant_info["id"] != bk_user.id: + if tenant_info["id"] != bk_user.get_property("tenant_id"): assert not tenant_info["departments"] else: assert len(tenant_info["departments"]) >= 1 class TestTenantRetrieveUpdateApi: + NOT_EXIST_TENANT_ID = 7334 + @pytest.fixture() - def other_tenant(self): + def other_tenant(self) -> Tenant: return create_tenant("other_tenant") - def test_retrieve_tenant(self, api_client, bk_user): + def test_retrieve_tenant(self, api_client: APIClient, bk_user: User): tenant_id = bk_user.get_property("tenant_id") resp = api_client.get(reverse("organization.tenant.retrieve_update", kwargs={"id": tenant_id})) - tenant = Tenant.objects.get(id=tenant_id) resp_data = resp.data + + 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"]) + 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, other_tenant): + def test_retrieve_other_tenant(self, api_client: APIClient, other_tenant: Tenant): resp = api_client.get(reverse("organization.tenant.retrieve_update", kwargs={"id": other_tenant.id})) resp_data = resp.data assert other_tenant.id == resp_data["id"] @@ -65,27 +82,29 @@ def test_retrieve_other_tenant(self, api_client, other_tenant): assert other_tenant.updated_at_display == resp_data["updated_at"] assert other_tenant.logo == resp_data["logo"] assert other_tenant.feature_flags == resp_data["feature_flags"] + # 非当前用户所在租户,不返回管理员 assert not resp_data["managers"] - def test_retrieve_not_exist_tenant(self, api_client): - resp = api_client.get(reverse("organization.tenant.retrieve_update", kwargs={"id": 7334})) + 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})) assert resp.status_code == status.HTTP_404_NOT_FOUND - def test_update_tenant(self, api_client, fake_tenant, fake_tenant_users): - new_manager_ids = [user.id for user in fake_tenant_users] + def test_update_tenant( + self, api_client: APIClient, default_tenant: Tenant, default_tenant_users: List[TenantUser] + ): + new_manager_ids = [user.id for user in default_tenant_users] update_data = { "id": "fake-tenant-updated", "name": "fake-tenant-updated", "logo": "aabb", - "feature_flags": {"user_number_visible": True}, + "feature_flags": {"user_number_visible": False}, "manager_ids": new_manager_ids, } - resp = api_client.put( - reverse("organization.tenant.retrieve_update", kwargs={"id": fake_tenant.id}), data=update_data + api_client.put( + reverse("organization.tenant.retrieve_update", kwargs={"id": default_tenant.id}), data=update_data ) - assert resp.status_code == status.HTTP_200_OK - tenant = Tenant.objects.get(id=fake_tenant.id) + tenant = Tenant.objects.get(id=default_tenant.id) assert tenant.id != update_data["id"] assert tenant.name == update_data["name"] assert tenant.feature_flags == update_data["feature_flags"] @@ -95,15 +114,17 @@ def test_update_tenant(self, api_client, fake_tenant, fake_tenant_users): TenantManager.objects.filter(tenant=tenant).values_list("tenant_user_id", flat=True) ) - def test_update_other_tenant(self, api_client, other_tenant, fake_tenant_users): + def test_update_other_tenant( + self, api_client: APIClient, other_tenant: Tenant, default_tenant_users: List[TenantUser] + ): resp = api_client.put( reverse("organization.tenant.retrieve_update", kwargs={"id": other_tenant.id}), data={ "id": "fake-tenant-updated", "name": "fake-tenant-updated", - "feature_flags": {}, + "feature_flags": {"user_number_visible": False}, "logo": "aabb", - "manager_ids": [user.id for user in fake_tenant_users], + "manager_ids": [user.id for user in default_tenant_users], }, ) # 进行更新非当前用户的租户,异常返回 @@ -111,43 +132,66 @@ def test_update_other_tenant(self, api_client, other_tenant, fake_tenant_users): class TestTenantUserListApi: - def test_list_tenant_users(self, api_client, fake_tenant, fake_tenant_users): - resp = api_client.get(reverse("organization.tenant.users.list", kwargs={"id": fake_tenant.id})) - assert TenantUser.objects.filter(tenant=fake_tenant).count() == resp.data["count"] + def test_list_tenant_users( + self, api_client: APIClient, default_tenant: Tenant, default_tenant_users: List[TenantUser] + ): + resp = api_client.get(reverse("organization.tenant.users.list", kwargs={"id": default_tenant.id})) + assert TenantUser.objects.filter(tenant=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.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"] + assert tenant_user.data_source_user.phone == item["phone"] + assert tenant_user.data_source_user.phone_country_code == item["phone_country_code"] + assert tenant_user.data_source_user.logo == item["logo"] class TestTenantDepartmentChildrenListApi: - def test_retrieve_children(self, api_client, fake_tenant_departments): - for item in fake_tenant_departments: + def test_retrieve_children(self, api_client: APIClient, default_tenant_departments: List[TenantDepartment]): + for item in default_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() tenant_children = TenantDepartment.objects.filter( data_source_department_id__in=children.values_list("department_id", flat=True) ) assert tenant_children.count() == len(resp.data) + for department in resp.data: + tenant_department = tenant_children.filter(id=department["id"]).first() + assert tenant_department + assert tenant_department.data_source_department.name == department["name"] class TestTenantDepartmentUserListApi: - def test_list_department_users(self, api_client, fake_tenant_departments, fake_tenant_users): - for tenant_department in fake_tenant_departments: + def test_list_department_users( + self, + api_client: APIClient, + default_tenant_departments: List[TenantDepartment], + default_tenant_users: List[TenantUser], + ): + for tenant_department in default_tenant_departments: resp = api_client.get(reverse("departments.users.list", kwargs={"id": tenant_department.id})) + resp_data = resp.data - department_user_relationship = DataSourceDepartmentUserRelation.objects.filter( - department=tenant_department.data_source_department - ).values_list("user_id", flat=True) tenant_users = TenantUser.objects.filter( - tenant_id=tenant_department.tenant_id, data_source_user_id__in=department_user_relationship + tenant_id=tenant_department.tenant_id, + data_source_user_id__in=DataSourceDepartmentUserRelation.objects.filter( + department=tenant_department.data_source_department + ).values_list("user_id", flat=True), ) - resp_data = resp.data assert tenant_users.count() == resp_data["count"] + result_data_ids = [user["id"] for user in resp_data["results"]] for item in tenant_users: assert item.id in result_data_ids class TestTenantUserRetrieveApi: - def test_retrieve_user(self, api_client, fake_tenant_users): - for tenant_user in fake_tenant_users: + def test_retrieve_user(self, api_client: APIClient, default_tenant_users: List[TenantUser]): + for tenant_user in default_tenant_users: resp = api_client.get(reverse("department.users.retrieve", kwargs={"id": tenant_user.id})) resp_data = resp.data @@ -163,12 +207,13 @@ def test_retrieve_user(self, api_client, fake_tenant_users): assert data_source_user.phone == resp_data["phone"] assert data_source_user.phone_country_code == resp_data["phone_country_code"] - data_source_department_ids = DataSourceDepartmentUserRelation.objects.filter( - user_id=data_source_user.id - ).values_list("department_id", flat=True) tenant_departments = TenantDepartment.objects.filter( - data_source_department_id=data_source_department_ids, tenant_id=tenant_user.id + data_source_department_id=DataSourceDepartmentUserRelation.objects.filter( + user_id=data_source_user.id + ).values_list("department_id", flat=True), + tenant_id=tenant_user.id, ) + # 返回结果和数据库数据做比对 department_flags = [ True for department in resp_data["departments"] if department["id"] in tenant_departments ] diff --git a/src/bk-user/tests/biz/test_data_source.py b/src/bk-user/tests/biz/test_data_source.py index e981d21cd..8c12e3ed5 100644 --- a/src/bk-user/tests/biz/test_data_source.py +++ b/src/bk-user/tests/biz/test_data_source.py @@ -8,12 +8,14 @@ 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 ( DataSourceDepartment, DataSourceDepartmentRelation, DataSourceDepartmentUserRelation, + DataSourceUser, DataSourceUserLeaderRelation, ) from bkuser.biz.data_source import DataSourceDepartmentHandler, DataSourceUserHandler @@ -22,20 +24,21 @@ class TestDataSourceDepartmentHandler: - def test_get_department_info_map_by_ids(self, fake_data_source_departments): - fake_department_ids = [department.id for department in fake_data_source_departments] + def test_get_department_info_map_by_ids(self, local_data_source_departments: List[DataSourceDepartment]): + fake_department_ids = [department.id for department in local_data_source_departments] departments_map = DataSourceDepartmentHandler.get_department_info_map_by_ids(fake_department_ids) for item in DataSourceDepartment.objects.filter(id__in=fake_department_ids): departments_info = departments_map.get(item.id) assert departments_info assert item.name == departments_info.name - children_ids = ( + child_ids = ( DataSourceDepartmentRelation.objects.get(department=item) .get_children() .values_list("department_id", flat=True) ) - assert list(children_ids) == departments_info.children_ids + for child_id in departments_info.child_ids: + assert child_id in child_ids @pytest.mark.parametrize( "not_exist_data_source_department_ids", @@ -46,14 +49,16 @@ def test_get_department_info_map_by_ids(self, fake_data_source_departments): [14, 24, 34], ], ) - def test_not_exist_get_department_info_map_by_ids(self, not_exist_data_source_department_ids): + def test_not_exist_get_department_info_map_by_ids(self, not_exist_data_source_department_ids: List[int]): departments_map = DataSourceDepartmentHandler.get_department_info_map_by_ids( not_exist_data_source_department_ids ) assert departments_map == {} - def test_get_user_department_ids_map(self, fake_data_source_departments, fake_data_source_users): - user_ids = [user.id for user in fake_data_source_users] + def test_get_user_department_ids_map( + self, local_data_source_departments: List[DataSourceDepartment], local_data_source_users: List[DataSourceUser] + ): + user_ids = [user.id for user in local_data_source_users] user_department_relationship_map = DataSourceDepartmentHandler.get_user_department_ids_map(user_ids) for user in user_ids: @@ -72,14 +77,14 @@ def test_get_user_department_ids_map(self, fake_data_source_departments, fake_da [14, 24, 34], ], ) - def test_not_exist_get_user_department_ids_map(self, not_exist_data_source_user_ids): + def test_not_exist_get_user_department_ids_map(self, not_exist_data_source_user_ids: List[int]): department_ids_map = DataSourceDepartmentHandler.get_user_department_ids_map(not_exist_data_source_user_ids) assert department_ids_map == {} class TestDataSourceUserHandler: - def test_get_user_leader_ids_map(self, fake_data_source_users): - data_source_user_ids = [item.id for item in fake_data_source_users] + def test_get_user_leader_ids_map(self, local_data_source_users: List[DataSourceUser]): + 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) for user_id in data_source_user_ids: @@ -99,6 +104,6 @@ def test_get_user_leader_ids_map(self, fake_data_source_users): [14, 24, 34], ], ) - def test_not_exist_get_user_leader_ids_map(self, not_exist_data_source_user_ids): + def test_not_exist_get_user_leader_ids_map(self, not_exist_data_source_user_ids: List[int]): department_ids_map = DataSourceDepartmentHandler.get_user_department_ids_map(not_exist_data_source_user_ids) assert department_ids_map == {} diff --git a/src/bk-user/tests/biz/test_tenant.py b/src/bk-user/tests/biz/test_tenant.py index 9450866ef..e558ce7ec 100644 --- a/src/bk-user/tests/biz/test_tenant.py +++ b/src/bk-user/tests/biz/test_tenant.py @@ -22,11 +22,11 @@ class TestTenantUserHandler: - def test_get_tenant_user_leaders_map_by_id(self, fake_tenant_users): - tenant_user_ids = [item.id for item in fake_tenant_users] + def test_get_tenant_user_leaders_map_by_id(self, default_tenant_users): + tenant_user_ids = [item.id for item in default_tenant_users] tenant_user_leader_map = TenantUserHandler.get_tenant_user_leaders_map_by_id(tenant_user_ids) - for tenant_user in fake_tenant_users: + for tenant_user in default_tenant_users: tenant_user_leader_ids = [leader_info.id for leader_info in tenant_user_leader_map.get(tenant_user.id, [])] data_source_leader_ids = DataSourceUserLeaderRelation.objects.filter( user=tenant_user.data_source_user @@ -35,15 +35,19 @@ def test_get_tenant_user_leaders_map_by_id(self, fake_tenant_users): assert len(tenant_user_leader_ids) == tenant_users.count() assert not set(tenant_user_leader_ids) - set(tenant_users.values_list("id", flat=True)) - def test_get_tenant_user_departments_map_by_id(self, fake_tenant, fake_tenant_users, fake_tenant_departments): + def test_get_tenant_user_departments_map_by_id( + self, default_tenant, default_tenant_users, default_tenant_departments + ): tenant_user_departments_map = TenantUserHandler.get_tenant_user_departments_map_by_id( - [user.id for user in fake_tenant_users] + [user.id for user in default_tenant_users] ) - for tenant_user in fake_tenant_users: + for tenant_user in default_tenant_users: tenant_departments = tenant_user_departments_map.get(tenant_user.id, []) tenant_departments_ids = [tenant_department.id for tenant_department in tenant_departments] - data_source_department = TenantDepartment.objects.filter(id__in=tenant_departments_ids, tenant=fake_tenant) + data_source_department = TenantDepartment.objects.filter( + id__in=tenant_departments_ids, tenant=default_tenant + ) user_related_department = DataSourceDepartmentUserRelation.objects.filter( user_id=tenant_user.data_source_user_id ) @@ -51,21 +55,21 @@ def test_get_tenant_user_departments_map_by_id(self, fake_tenant, fake_tenant_us data_source_department.values_list("data_source_department_id", flat=True) ) - def test_get_tenant_user_ids_by_tenant(self, fake_tenant: Tenant): - tenant_user_ids = TenantUserHandler.get_tenant_user_ids_by_tenant(fake_tenant.id) - assert len(tenant_user_ids) == TenantUser.objects.filter(tenant=fake_tenant).count() + def test_get_tenant_user_ids_by_tenant(self, default_tenant: Tenant): + tenant_user_ids = TenantUserHandler.get_tenant_user_ids_by_tenant(default_tenant.id) + assert len(tenant_user_ids) == TenantUser.objects.filter(tenant=default_tenant).count() assert not set(tenant_user_ids) - set( - TenantUser.objects.filter(tenant=fake_tenant).values_list("id", flat=True) + TenantUser.objects.filter(tenant=default_tenant).values_list("id", flat=True) ) class TestTenantDepartmentHandler: def test_convert_data_source_department_to_tenant_department( - self, fake_tenant, fake_data_source_departments, fake_tenant_departments + self, default_tenant, local_data_source_departments, default_tenant_departments ): - data_source_department_ids = [department.id for department in fake_data_source_departments] + data_source_department_ids = [department.id for department in local_data_source_departments] tenant_departments = TenantDepartmentHandler.convert_data_source_department_to_tenant_department( - fake_tenant.id, data_source_department_ids + default_tenant.id, data_source_department_ids ) assert len(data_source_department_ids) == len(tenant_departments) @@ -93,9 +97,9 @@ def test_convert_data_source_department_to_tenant_department( ], ) def test_not_exist_convert_data_source_department_to_tenant_department( - self, fake_tenant, not_exist_data_source_department_ids + self, default_tenant, not_exist_data_source_department_ids ): tenant_departments = TenantDepartmentHandler.convert_data_source_department_to_tenant_department( - fake_tenant.id, not_exist_data_source_department_ids + default_tenant.id, not_exist_data_source_department_ids ) assert not tenant_departments diff --git a/src/bk-user/tests/conftest.py b/src/bk-user/tests/conftest.py index 467fbad6c..e2597a5e7 100644 --- a/src/bk-user/tests/conftest.py +++ b/src/bk-user/tests/conftest.py @@ -26,7 +26,7 @@ @pytest.fixture() -def fake_tenant() -> Tenant: +def default_tenant() -> Tenant: """ 创建测试租户 """ @@ -34,44 +34,46 @@ def fake_tenant() -> Tenant: @pytest.fixture() -def fake_data_source(fake_tenant) -> DataSource: +def local_data_source(default_tenant) -> DataSource: """ 创建测试数据源 """ - return DataSource.objects.create(name="fake_data_source", owner_tenant_id=fake_tenant.id, plugin_id="local") + return DataSource.objects.create(name="local_data_source", owner_tenant_id=default_tenant.id, plugin_id="local") @pytest.fixture() -def fake_data_source_departments(fake_data_source) -> List[DataSourceDepartment]: +def local_data_source_departments(local_data_source) -> List[DataSourceDepartment]: """ 创建测试数据源部门,并以首个对象作为父部门 """ - return create_data_source_departments_with_relationship(fake_data_source.id) + return create_data_source_departments_with_relationship(local_data_source.id) @pytest.fixture() -def fake_data_source_users(fake_data_source, fake_data_source_departments) -> List[DataSourceUser]: +def local_data_source_users(local_data_source, local_data_source_departments) -> List[DataSourceUser]: """ 创建测试数据源用户, 并以首个对象作为leader """ return create_data_source_users_with_relationship( - fake_data_source.id, [department.id for department in fake_data_source_departments] + local_data_source.id, [department.id for department in local_data_source_departments] ) @pytest.fixture() -def fake_tenant_users(fake_tenant, fake_data_source, fake_data_source_users) -> List[TenantUser]: +def default_tenant_users(default_tenant, local_data_source, local_data_source_users) -> List[TenantUser]: """ 根据测试数据源用户,创建租户用户 """ - return create_tenant_users(fake_tenant.id, fake_data_source.id, [user.id for user in fake_data_source_users]) + return create_tenant_users(default_tenant.id, local_data_source.id, [user.id for user in local_data_source_users]) @pytest.fixture() -def fake_tenant_departments(fake_tenant, fake_data_source, fake_data_source_departments) -> List[TenantDepartment]: +def default_tenant_departments( + default_tenant, local_data_source, local_data_source_departments +) -> List[TenantDepartment]: """ 根据测试数据源部门,创建租户部门 """ return create_tenant_departments( - fake_tenant.id, fake_data_source.id, [department.id for department in fake_data_source_departments] + default_tenant.id, local_data_source.id, [department.id for department in local_data_source_departments] ) diff --git a/src/bk-user/tests/test_utils/data_source.py b/src/bk-user/tests/test_utils/data_source.py index c450f61ae..4cdfca721 100644 --- a/src/bk-user/tests/test_utils/data_source.py +++ b/src/bk-user/tests/test_utils/data_source.py @@ -21,6 +21,9 @@ def create_data_source_departments_with_relationship(data_source_id: int): + """ + 创建数据源部门,并以首个对象为其余对象的父部门 + """ data_source_departments: List[DataSourceDepartment] = [] for i in range(10): department = DataSourceDepartment.objects.create(data_source_id=data_source_id, name=f"fake_dept_{i}") @@ -36,6 +39,9 @@ def create_data_source_departments_with_relationship(data_source_id: int): def create_data_source_users_with_relationship(data_source_id: int, department_ids: List[int]): + """ + 创建数据源用户,并以首个对象为其余对象的上级 + """ data_source_users: List[DataSourceUser] = [] for i in range(10): fake_user = DataSourceUser.objects.create( diff --git a/src/bk-user/tests/test_utils/tenant.py b/src/bk-user/tests/test_utils/tenant.py index 8abd0cc7e..f311023c2 100644 --- a/src/bk-user/tests/test_utils/tenant.py +++ b/src/bk-user/tests/test_utils/tenant.py @@ -17,10 +17,16 @@ def create_tenant(tenant_id: Optional[str] = DEFAULT_TENANT) -> Tenant: + """ + 创建租户 + """ return Tenant.objects.create(id=tenant_id, name=tenant_id, feature_flags={"user_number_visible": True}) def create_tenant_users(tenant_id: str, data_source_id: int, data_source_user_ids: List[int]) -> List[TenantUser]: + """ + 创建租户用户 + """ tenant_users: List[TenantUser] = [] for user_id in data_source_user_ids: tenant_user = TenantUser.objects.create( @@ -34,8 +40,11 @@ def create_tenant_users(tenant_id: str, data_source_id: int, data_source_user_id def create_tenant_departments( - tenant_id: str, data_source_id, data_source_department_ids: List[int] + tenant_id: str, data_source_id: int, data_source_department_ids: List[int] ) -> List[TenantDepartment]: + """ + 创建租户部门 + """ tenant_departments: List[TenantDepartment] = [] for department in data_source_department_ids: tenant_department = TenantDepartment.objects.create(