Skip to content

Commit

Permalink
refactor: cr调整
Browse files Browse the repository at this point in the history
  • Loading branch information
neronkl committed Sep 5, 2023
1 parent 00e29e6 commit 5a8b7f9
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 86 deletions.
4 changes: 2 additions & 2 deletions src/bk-user/bkuser/biz/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class DataSourceDepartmentInfoWithChildren(BaseModel):
id: int
name: str
children_ids: List[int]
child_ids: List[int]


class DataSourceSimpleInfo(BaseModel):
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions src/bk-user/bkuser/biz/tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: 协同数据源,可能存在未授权全部子部门
# 提前拉取所有映射, 过滤绑定的租户部门
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/bk-user/tests/apis/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
123 changes: 84 additions & 39 deletions src/bk-user/tests/apis/web/organization/test_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
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,
DataSourceDepartmentUserRelation,
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

Expand All @@ -25,67 +29,82 @@

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"]
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}))
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"]
Expand All @@ -95,59 +114,84 @@ 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],
},
)
# 进行更新非当前用户的租户,异常返回
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"]
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
Expand All @@ -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
]
Expand Down
27 changes: 16 additions & 11 deletions src/bk-user/tests/biz/test_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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 == {}
Loading

0 comments on commit 5a8b7f9

Please sign in to comment.