Skip to content

Commit

Permalink
fix: sync data source to tenant only effect current related (#1278)
Browse files Browse the repository at this point in the history
  • Loading branch information
narasux authored Oct 8, 2023
1 parent 64d78bb commit b105963
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/bk-user/bkuser/apps/sync/syncers.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def __init__(self, task: TenantSyncTask, data_source: DataSource, tenant: Tenant

def sync(self):
"""TODO (su) 支持协同后,同步到租户的数据有范围限制"""
exists_tenant_departments = TenantDepartment.objects.filter(tenant=self.tenant)
exists_tenant_departments = TenantDepartment.objects.filter(tenant=self.tenant, data_source=self.data_source)
data_source_departments = DataSourceDepartment.objects.filter(data_source=self.data_source)

# 删除掉租户中存在的,但是数据源中不存在的
Expand Down Expand Up @@ -360,7 +360,7 @@ def __init__(self, task: TenantSyncTask, data_source: DataSource, tenant: Tenant

def sync(self):
"""TODO (su) 支持协同后,同步到租户的数据有范围限制"""
exists_tenant_users = TenantUser.objects.filter(tenant=self.tenant)
exists_tenant_users = TenantUser.objects.filter(tenant=self.tenant, data_source=self.data_source)
data_source_users = DataSourceUser.objects.filter(data_source=self.data_source)

# 删除掉租户中存在的,但是数据源中不存在的
Expand Down
59 changes: 47 additions & 12 deletions src/bk-user/tests/apps/sync/test_syncers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
TenantUserSyncer,
)
from bkuser.apps.tenant.models import Tenant, TenantDepartment, TenantUser
from bkuser.common.constants import PERMANENT_TIME
from bkuser.plugins.models import RawDataSourceDepartment, RawDataSourceUser
from bkuser.utils.uuid import generate_uuid

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -257,12 +259,20 @@ def _gen_user_depts_from_db(data_source_users: List[DataSourceUser]) -> Dict[str


class TestTenantDepartmentSyncer:
def test_cud(self, tenant_sync_task, full_local_data_source, default_tenant):
def test_cud(self, tenant_sync_task, full_local_data_source, bare_general_data_source, default_tenant):
# 另外的数据源同步到租户的数据
other_ds_dept = DataSourceDepartment.objects.create(
data_source=bare_general_data_source, code="company_x", name="公司X"
)
TenantDepartment.objects.create(
tenant=default_tenant, data_source=bare_general_data_source, data_source_department=other_ds_dept
)

# 初始化场景
TenantDepartmentSyncer(tenant_sync_task, full_local_data_source, default_tenant).sync()
assert self._gen_ds_dept_ids_with_data_source(
data_source=full_local_data_source
) == self._gen_ds_dept_ids_with_tenant(default_tenant)
) == self._gen_ds_dept_ids_with_tenant(default_tenant, full_local_data_source)

# 更新场景
DataSourceDepartment.objects.filter(
Expand All @@ -273,17 +283,21 @@ def test_cud(self, tenant_sync_task, full_local_data_source, default_tenant):
TenantDepartmentSyncer(tenant_sync_task, full_local_data_source, default_tenant).sync()
assert self._gen_ds_dept_ids_with_data_source(
data_source=full_local_data_source
) == self._gen_ds_dept_ids_with_tenant(default_tenant)
) == self._gen_ds_dept_ids_with_tenant(default_tenant, full_local_data_source)

# 删除场景
# 删除场景,只会删除当前数据源关联的租户部门
DataSourceDepartment.objects.filter(data_source=full_local_data_source).delete()
TenantDepartmentSyncer(tenant_sync_task, full_local_data_source, default_tenant).sync()
assert not TenantDepartment.objects.filter(tenant=default_tenant).exists()

def _gen_ds_dept_ids_with_tenant(self, tenant: Tenant) -> Set[int]:
tenant_depts = TenantDepartment.objects.filter(tenant=default_tenant)
assert tenant_depts.exists()
assert not tenant_depts.filter(data_source=full_local_data_source).exists()

def _gen_ds_dept_ids_with_tenant(self, tenant: Tenant, data_source: DataSource) -> Set[int]:
return set(
TenantDepartment.objects.filter(
tenant=tenant,
data_source=data_source,
).values_list("data_source_department_id", flat=True)
)

Expand All @@ -296,12 +310,29 @@ def _gen_ds_dept_ids_with_data_source(self, data_source: DataSource) -> Set[int]


class TestTenantUserSyncer:
def test_cud(self, tenant_sync_task, full_local_data_source, default_tenant):
def test_cud(self, tenant_sync_task, full_local_data_source, bare_general_data_source, default_tenant):
# 另外的数据源同步到租户的数据
other_ds_user = DataSourceUser.objects.create(
data_source=bare_general_data_source,
code="Employee-201",
username="libai",
full_name="李白",
email="libai@m.com",
phone="13512345991",
)
TenantUser.objects.create(
id=generate_uuid(),
tenant=default_tenant,
data_source_user=other_ds_user,
data_source=bare_general_data_source,
account_expired_at=PERMANENT_TIME,
)

# 初始化场景
TenantUserSyncer(tenant_sync_task, full_local_data_source, default_tenant).sync()
assert self._gen_ds_user_ids_with_data_source(
data_source=full_local_data_source
) == self._gen_ds_user_ids_with_tenant(default_tenant)
) == self._gen_ds_user_ids_with_tenant(default_tenant, full_local_data_source)

# 更新场景
DataSourceUser.objects.filter(
Expand All @@ -319,17 +350,21 @@ def test_cud(self, tenant_sync_task, full_local_data_source, default_tenant):
TenantUserSyncer(tenant_sync_task, full_local_data_source, default_tenant).sync()
assert self._gen_ds_user_ids_with_data_source(
data_source=full_local_data_source
) == self._gen_ds_user_ids_with_tenant(default_tenant)
) == self._gen_ds_user_ids_with_tenant(default_tenant, full_local_data_source)

# 删除场景
# 删除场景,只会删除当前数据源关联的租户用户
DataSourceUser.objects.filter(data_source=full_local_data_source).delete()
TenantUserSyncer(tenant_sync_task, full_local_data_source, default_tenant).sync()
assert not TenantUser.objects.filter(tenant=default_tenant).exists()

def _gen_ds_user_ids_with_tenant(self, tenant: Tenant) -> Set[int]:
tenant_users = TenantUser.objects.filter(tenant=default_tenant)
assert tenant_users.exists()
assert not tenant_users.filter(data_source=full_local_data_source).exists()

def _gen_ds_user_ids_with_tenant(self, tenant: Tenant, data_source: DataSource) -> Set[int]:
return set(
TenantUser.objects.filter(
tenant=tenant,
data_source=data_source,
).values_list("data_source_user_id", flat=True)
)

Expand Down

0 comments on commit b105963

Please sign in to comment.