diff --git a/src/bk-user/tests/apis/__init__.py b/src/bk-user/tests/apis/__init__.py new file mode 100644 index 000000000..1060b7bf4 --- /dev/null +++ b/src/bk-user/tests/apis/__init__.py @@ -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. +""" diff --git a/src/bk-user/tests/apis/conftest.py b/src/bk-user/tests/apis/conftest.py new file mode 100644 index 000000000..28ab398aa --- /dev/null +++ b/src/bk-user/tests/apis/conftest.py @@ -0,0 +1,29 @@ +# -*- 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.auth.models import User +from rest_framework.test import APIClient + + +@pytest.fixture() +def bk_user(fake_tenant) -> User: + """创建测试用用户""" + user, _ = User.objects.get_or_create(username="fake_user") + user.set_property("tenant_id", fake_tenant.id) + return user + + +@pytest.fixture() +def api_client(bk_user) -> APIClient: + """Return an authenticated client""" + client = APIClient() + client.force_authenticate(user=bk_user) + return client diff --git a/src/bk-user/tests/apis/web/__init__.py b/src/bk-user/tests/apis/web/__init__.py new file mode 100644 index 000000000..1060b7bf4 --- /dev/null +++ b/src/bk-user/tests/apis/web/__init__.py @@ -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. +""" diff --git a/src/bk-user/tests/apis/web/organization/__init__.py b/src/bk-user/tests/apis/web/organization/__init__.py new file mode 100644 index 000000000..1060b7bf4 --- /dev/null +++ b/src/bk-user/tests/apis/web/organization/__init__.py @@ -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. +""" diff --git a/src/bk-user/tests/apis/web/organization/test_organization.py b/src/bk-user/tests/apis/web/organization/test_organization.py new file mode 100644 index 000000000..14fbd10d4 --- /dev/null +++ b/src/bk-user/tests/apis/web/organization/test_organization.py @@ -0,0 +1,184 @@ +# -*- 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.data_source.models import ( + DataSourceDepartmentRelation, + DataSourceDepartmentUserRelation, + DataSourceUserLeaderRelation, +) +from bkuser.apps.tenant.models import Tenant, TenantDepartment, TenantManager, TenantUser +from django.urls import reverse +from rest_framework import status + +from tests.test_utils.tenant import create_tenant + +pytestmark = pytest.mark.django_db + + +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): + 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: + assert not tenant_info["departments"] + else: + assert len(tenant_info["departments"]) >= 1 + + +class TestTenantRetrieveUpdateApi: + @pytest.fixture() + def other_tenant(self): + return create_tenant("other_tenant") + + def test_retrieve_tenant(self, api_client, bk_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 + 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"]) + + def test_retrieve_other_tenant(self, api_client, other_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"] + assert other_tenant.name == resp_data["name"] + 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})) + 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] + update_data = { + "id": "fake-tenant-updated", + "name": "fake-tenant-updated", + "logo": "aabb", + "feature_flags": {"user_number_visible": True}, + "manager_ids": new_manager_ids, + } + resp = api_client.put( + reverse("organization.tenant.retrieve_update", kwargs={"id": fake_tenant.id}), data=update_data + ) + assert resp.status_code == status.HTTP_200_OK + + tenant = Tenant.objects.get(id=fake_tenant.id) + assert tenant.id != update_data["id"] + assert tenant.name == update_data["name"] + assert tenant.feature_flags == update_data["feature_flags"] + assert tenant.logo == update_data["logo"] + for new_manager in new_manager_ids: + assert new_manager in list( + 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): + 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": {}, + "logo": "aabb", + "manager_ids": [user.id for user in fake_tenant_users], + }, + ) + # 进行更新非当前用户的租户,异常返回 + assert resp.status_code == status.HTTP_403_FORBIDDEN + + +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"] + + +class TestTenantDepartmentChildrenListApi: + def test_retrieve_children(self, api_client, fake_tenant_departments): + for item in fake_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) + + +class TestTenantDepartmentUserListApi: + def test_list_department_users(self, api_client, fake_tenant_departments, fake_tenant_users): + for tenant_department in fake_tenant_departments: + resp = api_client.get(reverse("departments.users.list", kwargs={"id": tenant_department.id})) + + 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 + ) + 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: + 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"] + + assert data_source_user.username == resp_data["username"] + assert data_source_user.full_name == resp_data["full_name"] + + assert data_source_user.email == resp_data["email"] + 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 + ) + department_flags = [ + True for department in resp_data["departments"] if department["id"] in tenant_departments + ] + assert all(department_flags) + + data_source_leader_ids = DataSourceUserLeaderRelation.objects.filter(user=data_source_user).values_list( + "leader_id", flat=True + ) + tenant_leaders = TenantUser.objects.filter( + data_source_user_id__in=data_source_leader_ids, tenant_id=tenant_user.id + ) + leader_flags = [True for user in resp_data["leaders"] if user["id"] in tenant_leaders] + assert all(leader_flags) diff --git a/src/bk-user/tests/biz/__init__.py b/src/bk-user/tests/biz/__init__.py new file mode 100644 index 000000000..1060b7bf4 --- /dev/null +++ b/src/bk-user/tests/biz/__init__.py @@ -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. +""" diff --git a/src/bk-user/tests/biz/test_data_source.py b/src/bk-user/tests/biz/test_data_source.py new file mode 100644 index 000000000..e981d21cd --- /dev/null +++ b/src/bk-user/tests/biz/test_data_source.py @@ -0,0 +1,104 @@ +# -*- 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.data_source.models import ( + DataSourceDepartment, + DataSourceDepartmentRelation, + DataSourceDepartmentUserRelation, + DataSourceUserLeaderRelation, +) +from bkuser.biz.data_source import DataSourceDepartmentHandler, DataSourceUserHandler + +pytestmark = pytest.mark.django_db + + +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] + 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 = ( + DataSourceDepartmentRelation.objects.get(department=item) + .get_children() + .values_list("department_id", flat=True) + ) + assert list(children_ids) == departments_info.children_ids + + @pytest.mark.parametrize( + "not_exist_data_source_department_ids", + [ + [], + [1, 2, 3], + [11, 22, 33], + [14, 24, 34], + ], + ) + 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 + ) + 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] + + user_department_relationship_map = DataSourceDepartmentHandler.get_user_department_ids_map(user_ids) + for user in user_ids: + department_ids = user_department_relationship_map.get(user) + assert department_ids + assert list(department_ids) == list( + DataSourceDepartmentUserRelation.objects.filter(user_id=user).values_list("department_id", flat=True) + ) + + @pytest.mark.parametrize( + "not_exist_data_source_user_ids", + [ + [], + [1, 2, 3], + [11, 22, 33], + [14, 24, 34], + ], + ) + def test_not_exist_get_user_department_ids_map(self, not_exist_data_source_user_ids): + 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] + leader_ids_map = DataSourceUserHandler.get_user_leader_ids_map(data_source_user_ids) + + for user_id in data_source_user_ids: + leader_ids = leader_ids_map.get(user_id) + if not DataSourceUserLeaderRelation.objects.filter(user_id=user_id): + assert not leader_ids + else: + assert leader_ids == list( + DataSourceUserLeaderRelation.objects.filter(user_id=user_id).values_list("leader_id", flat=True) + ) + + @pytest.mark.parametrize( + "not_exist_data_source_user_ids", + [ + [], + [11, 22, 33], + [14, 24, 34], + ], + ) + def test_not_exist_get_user_leader_ids_map(self, not_exist_data_source_user_ids): + 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 new file mode 100644 index 000000000..9450866ef --- /dev/null +++ b/src/bk-user/tests/biz/test_tenant.py @@ -0,0 +1,101 @@ +# -*- 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.data_source.models import ( + DataSourceDepartmentRelation, + DataSourceDepartmentUserRelation, + DataSourceUserLeaderRelation, +) +from bkuser.apps.tenant.models import Tenant, TenantDepartment, TenantUser +from bkuser.biz.tenant import TenantDepartmentHandler, TenantUserHandler + +pytestmark = pytest.mark.django_db + + +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] + + tenant_user_leader_map = TenantUserHandler.get_tenant_user_leaders_map_by_id(tenant_user_ids) + for tenant_user in fake_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 + ).values_list("leader_id", flat=True) + tenant_users = TenantUser.objects.filter(data_source_user_id__in=data_source_leader_ids) + 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): + tenant_user_departments_map = TenantUserHandler.get_tenant_user_departments_map_by_id( + [user.id for user in fake_tenant_users] + ) + + for tenant_user in fake_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) + user_related_department = DataSourceDepartmentUserRelation.objects.filter( + user_id=tenant_user.data_source_user_id + ) + assert not set(user_related_department.values_list("department_id", flat=True)) - set( + 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() + assert not set(tenant_user_ids) - set( + TenantUser.objects.filter(tenant=fake_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 + ): + data_source_department_ids = [department.id for department in fake_data_source_departments] + tenant_departments = TenantDepartmentHandler.convert_data_source_department_to_tenant_department( + fake_tenant.id, data_source_department_ids + ) + assert len(data_source_department_ids) == len(tenant_departments) + + for department in tenant_departments: + tenant_department = TenantDepartment.objects.get(id=department.id) + assert tenant_department.data_source_department_id in data_source_department_ids + assert department.name == tenant_department.data_source_department.name + + children_id = ( + DataSourceDepartmentRelation.objects.get(department=tenant_department.data_source_department) + .get_children() + .values_list("department_id", flat=True) + ) + + tenant_children_departments = TenantDepartment.objects.filter(data_source_department_id__in=children_id) + assert department.has_children == tenant_children_departments.exists() + + @pytest.mark.parametrize( + "not_exist_data_source_department_ids", + [ + [], + [1, 2, 3], + [11, 22, 33], + [14, 24, 34], + ], + ) + def test_not_exist_convert_data_source_department_to_tenant_department( + self, fake_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 + ) + assert not tenant_departments diff --git a/src/bk-user/tests/conftest.py b/src/bk-user/tests/conftest.py index 1060b7bf4..467fbad6c 100644 --- a/src/bk-user/tests/conftest.py +++ b/src/bk-user/tests/conftest.py @@ -8,3 +8,70 @@ 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 ( + DataSource, + DataSourceDepartment, + DataSourceUser, +) +from bkuser.apps.tenant.models import Tenant, TenantDepartment, TenantUser + +from tests.test_utils.data_source import ( + create_data_source_departments_with_relationship, + create_data_source_users_with_relationship, +) +from tests.test_utils.tenant import create_tenant, create_tenant_departments, create_tenant_users + + +@pytest.fixture() +def fake_tenant() -> Tenant: + """ + 创建测试租户 + """ + return create_tenant() + + +@pytest.fixture() +def fake_data_source(fake_tenant) -> DataSource: + """ + 创建测试数据源 + """ + return DataSource.objects.create(name="fake_data_source", owner_tenant_id=fake_tenant.id, plugin_id="local") + + +@pytest.fixture() +def fake_data_source_departments(fake_data_source) -> List[DataSourceDepartment]: + """ + 创建测试数据源部门,并以首个对象作为父部门 + """ + return create_data_source_departments_with_relationship(fake_data_source.id) + + +@pytest.fixture() +def fake_data_source_users(fake_data_source, fake_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] + ) + + +@pytest.fixture() +def fake_tenant_users(fake_tenant, fake_data_source, fake_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]) + + +@pytest.fixture() +def fake_tenant_departments(fake_tenant, fake_data_source, fake_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] + ) diff --git a/src/bk-user/tests/test_utils/data_source.py b/src/bk-user/tests/test_utils/data_source.py new file mode 100644 index 000000000..c450f61ae --- /dev/null +++ b/src/bk-user/tests/test_utils/data_source.py @@ -0,0 +1,53 @@ +# -*- 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 random +from typing import List + +from bkuser.apps.data_source.models import ( + DataSourceDepartment, + DataSourceDepartmentRelation, + DataSourceDepartmentUserRelation, + DataSourceUser, + DataSourceUserLeaderRelation, +) + + +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}") + data_source_departments.append(department) + + # 添加部门关系 + root = DataSourceDepartmentRelation.objects.create( + department=data_source_departments[0], data_source_id=data_source_id + ) + for item in data_source_departments[1:]: + DataSourceDepartmentRelation.objects.create(department=item, data_source_id=data_source_id, parent=root) + return data_source_departments + + +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( + full_name=f"fake-user-{i}", username=f"fake-user-{i}", phone="1312345678", data_source_id=data_source_id + ) + data_source_users.append(fake_user) + + # 添加上下级关系 + leader = data_source_users[0] + for user in data_source_users[1:]: + DataSourceUserLeaderRelation.objects.get_or_create(user=user, leader=leader) + + for item in data_source_users: + DataSourceDepartmentUserRelation.objects.get_or_create(user=item, department_id=random.choice(department_ids)) + return data_source_users diff --git a/src/bk-user/tests/test_utils/tenant.py b/src/bk-user/tests/test_utils/tenant.py new file mode 100644 index 000000000..8abd0cc7e --- /dev/null +++ b/src/bk-user/tests/test_utils/tenant.py @@ -0,0 +1,47 @@ +# -*- 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, Optional + +from bkuser.apps.tenant.models import Tenant, TenantDepartment, TenantUser +from bkuser.utils.uuid import generate_uuid + +DEFAULT_TENANT = "default" + + +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( + data_source_user_id=user_id, + data_source_id=data_source_id, + tenant_id=tenant_id, + id=generate_uuid(), + ) + tenant_users.append(tenant_user) + return tenant_users + + +def create_tenant_departments( + tenant_id: str, data_source_id, data_source_department_ids: List[int] +) -> List[TenantDepartment]: + tenant_departments: List[TenantDepartment] = [] + for department in data_source_department_ids: + tenant_department = TenantDepartment.objects.create( + data_source_department_id=department, + data_source_id=data_source_id, + tenant_id=tenant_id, + ) + tenant_departments.append(tenant_department) + return tenant_departments